mirror of
https://github.com/jeessy2/ddns-go.git
synced 2025-10-20 23:43:45 +08:00
init
This commit is contained in:
70
config.go
Normal file
70
config.go
Normal file
@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// Config 配置
|
||||
type Config struct {
|
||||
Ipv4 struct {
|
||||
Enable bool
|
||||
URL string
|
||||
}
|
||||
Ipv6 struct {
|
||||
Enable bool
|
||||
URL string
|
||||
}
|
||||
DNS struct {
|
||||
Name string
|
||||
ID string
|
||||
Secret string
|
||||
}
|
||||
}
|
||||
|
||||
func (conf *Config) getConfigFromFile() {
|
||||
byt, err := ioutil.ReadFile("config.yaml")
|
||||
if err != nil {
|
||||
log.Println("config.yaml读取失败")
|
||||
}
|
||||
yaml.Unmarshal(byt, conf)
|
||||
}
|
||||
|
||||
func (conf *Config) getIpv4Addr() (result string, err error) {
|
||||
resp, err := http.Get(conf.Ipv4.URL)
|
||||
if err != nil {
|
||||
err = err
|
||||
log.Println("获得IPV4失败")
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
err = err
|
||||
log.Println("读取IPV4结果失败")
|
||||
return
|
||||
}
|
||||
result = string(body)
|
||||
return
|
||||
}
|
||||
|
||||
func (conf *Config) getIpv6Addr() (result string, err error) {
|
||||
resp, err := http.Get(conf.Ipv6.URL)
|
||||
if err != nil {
|
||||
err = err
|
||||
log.Println("获得IPV6失败")
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
err = err
|
||||
log.Println("读取IPV6结果失败")
|
||||
return
|
||||
}
|
||||
result = string(body)
|
||||
return
|
||||
}
|
11
config.yaml
Normal file
11
config.yaml
Normal file
@ -0,0 +1,11 @@
|
||||
ipv4:
|
||||
enable: true
|
||||
url: https://api-ipv4.ip.sb/ip
|
||||
ipv6:
|
||||
enable: true
|
||||
url: https://api-ipv6.ip.sb/ip
|
||||
|
||||
dns:
|
||||
name: alidns
|
||||
id: LTAI4FchkR6gejrsAQMyuM3n
|
||||
secret: 6Z2t3rL5LQpLgJ74CMIoXDQCaoXB84
|
21
config_test.go
Normal file
21
config_test.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
func TestConfig(t *testing.T) {
|
||||
conf := &Config{}
|
||||
byt, err := ioutil.ReadFile("config.yaml")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
yaml.Unmarshal(byt, conf)
|
||||
if "alidns" != conf.DNS.Name {
|
||||
t.Error("DNS Name必须为alidns")
|
||||
}
|
||||
}
|
27
dns/alidns.go
Normal file
27
dns/alidns.go
Normal file
@ -0,0 +1,27 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
|
||||
)
|
||||
|
||||
// Alidns 阿里云dns实现
|
||||
type Alidns struct{}
|
||||
|
||||
func (alidns *Alidns) addRecord() (ipv4 bool, ipv6 bool) {
|
||||
client, err := alidns.NewClientWithAccessKey("cn-hangzhou", "<accessKeyId>", "<accessSecret>")
|
||||
|
||||
request := alidns.CreateAddDomainRecordRequest()
|
||||
request.Scheme = "https"
|
||||
|
||||
request.Value = "3.0.3.0"
|
||||
request.Type = "A"
|
||||
request.RR = "apitest1"
|
||||
request.DomainName = "dns-example.com"
|
||||
|
||||
response, err := client.AddDomainRecord(request)
|
||||
if err != nil {
|
||||
fmt.Print(err.Error())
|
||||
}
|
||||
fmt.Printf("response is %#v\n", response)
|
||||
}
|
6
dns/index.go
Normal file
6
dns/index.go
Normal file
@ -0,0 +1,6 @@
|
||||
package dns
|
||||
|
||||
// DNS interface
|
||||
type DNS interface {
|
||||
addRecord() (ipv4 bool, ipv6 bool)
|
||||
}
|
8
go.mod
Normal file
8
go.mod
Normal file
@ -0,0 +1,8 @@
|
||||
module ddns-go
|
||||
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/aliyun/alibaba-cloud-sdk-go v1.61.439
|
||||
gopkg.in/yaml.v2 v2.3.0
|
||||
)
|
31
go.sum
Normal file
31
go.sum
Normal file
@ -0,0 +1,31 @@
|
||||
github.com/aliyun/alibaba-cloud-sdk-go v1.61.439 h1:vRjH9EcYcCkVUwti0q6AKcO8vFqCqe3EQOQl1QK+1zw=
|
||||
github.com/aliyun/alibaba-cloud-sdk-go v1.61.439/go.mod h1:pUKYbK5JQ+1Dfxk80P0qxGqe5dkxDoabbZS7zOcouyA=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptGaCkuDUx6wNykzzlUixGxvkme+H/lnzb+A=
|
||||
github.com/golang/tools v0.0.0-20200826040757-bc8aaaa29e06 h1:o8QN2yZHpPfla7J9ZQpaypzCL6fyEGCsLYv1+FpWdJc=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
|
||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
|
||||
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
|
||||
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/ini.v1 v1.42.0 h1:7N3gPTt50s8GuLortA00n8AqRTk75qOP98+mTPpgzRk=
|
||||
gopkg.in/ini.v1 v1.42.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
26
main.go
Normal file
26
main.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"ddns-go/dns"
|
||||
)
|
||||
|
||||
const (
|
||||
ipv4Addr = "https://api-ipv4.ip.sb/ip"
|
||||
ipv6Addr = "https://api-ipv6.ip.sb/ip"
|
||||
)
|
||||
|
||||
func main() {
|
||||
conf := &Config{}
|
||||
conf.getConfigFromFile()
|
||||
|
||||
ipv4, errIpv4 := conf.getIpv4Addr()
|
||||
ipv6, errIpv6 := conf.getIpv4Addr()
|
||||
|
||||
var dnsSelected dns.DNS
|
||||
switch conf.DNS.Name {
|
||||
case "alidns":
|
||||
dnsSelected = &dns.Alidns{}
|
||||
}
|
||||
dnsSelected.addRecord()
|
||||
|
||||
}
|
Reference in New Issue
Block a user