mirror of
https://github.com/jeessy2/ddns-go.git
synced 2024-11-25 16:46:24 +08:00
new
This commit is contained in:
parent
38d6aa7baf
commit
3006d8887d
17
.vscode/launch.json
vendored
Normal file
17
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${workspaceFolder}/main.go",
|
||||
"env": {},
|
||||
"args": []
|
||||
}
|
||||
]
|
||||
}
|
@ -1,9 +1,13 @@
|
||||
ipv4:
|
||||
enable: true
|
||||
url: https://api-ipv4.ip.sb/ip
|
||||
domains:
|
||||
- cloud.cty.cool
|
||||
ipv6:
|
||||
enable: true
|
||||
url: https://api-ipv6.ip.sb/ip
|
||||
domains:
|
||||
- cloud.cty.cool
|
||||
|
||||
dns:
|
||||
name: alidns
|
||||
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package config
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
@ -11,12 +11,14 @@ import (
|
||||
// Config 配置
|
||||
type Config struct {
|
||||
Ipv4 struct {
|
||||
Enable bool
|
||||
URL string
|
||||
Enable bool
|
||||
URL string
|
||||
Domains []string
|
||||
}
|
||||
Ipv6 struct {
|
||||
Enable bool
|
||||
URL string
|
||||
Enable bool
|
||||
URL string
|
||||
Domains []string
|
||||
}
|
||||
DNS struct {
|
||||
Name string
|
||||
@ -25,7 +27,8 @@ type Config struct {
|
||||
}
|
||||
}
|
||||
|
||||
func (conf *Config) getConfigFromFile() {
|
||||
// GetConfigFromFile 获得配置
|
||||
func (conf *Config) GetConfigFromFile() {
|
||||
byt, err := ioutil.ReadFile("config.yaml")
|
||||
if err != nil {
|
||||
log.Println("config.yaml读取失败")
|
||||
@ -33,35 +36,34 @@ func (conf *Config) getConfigFromFile() {
|
||||
yaml.Unmarshal(byt, conf)
|
||||
}
|
||||
|
||||
func (conf *Config) getIpv4Addr() (result string, err error) {
|
||||
// GetIpv4Addr 获得IPV4地址
|
||||
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)
|
||||
result = "8.8.8.8"
|
||||
return
|
||||
}
|
||||
|
||||
func (conf *Config) getIpv6Addr() (result string, err error) {
|
||||
// GetIpv6Addr 获得IPV6地址
|
||||
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
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package config
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
@ -9,7 +9,7 @@ import (
|
||||
|
||||
func TestConfig(t *testing.T) {
|
||||
conf := &Config{}
|
||||
byt, err := ioutil.ReadFile("config.yaml")
|
||||
byt, err := ioutil.ReadFile("../config.yaml")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
@ -1,27 +1,94 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
|
||||
"ddns-go/config"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"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)
|
||||
// AddRecord 添加记录
|
||||
func (ali *Alidns) AddRecord(conf *config.Config) (ipv4 bool, ipv6 bool) {
|
||||
client, err := alidns.NewClientWithAccessKey("cn-hangzhou", conf.DNS.ID, conf.DNS.Secret)
|
||||
if err != nil {
|
||||
fmt.Print(err.Error())
|
||||
log.Println("Alidns链接失败")
|
||||
return false, false
|
||||
}
|
||||
fmt.Printf("response is %#v\n", response)
|
||||
ipv4Success := addIpv4Record(client, conf)
|
||||
ipv6Success := addIpv6Record(client, conf)
|
||||
|
||||
return ipv4Success, ipv6Success
|
||||
}
|
||||
|
||||
func addIpv4Record(client *alidns.Client, conf *config.Config) bool {
|
||||
ipv4Addr, err := conf.GetIpv4Addr()
|
||||
if err != nil {
|
||||
log.Println("获得IPV4失败")
|
||||
}
|
||||
for _, domain := range conf.Ipv4.Domains {
|
||||
subDomain := strings.Split(domain, ".")
|
||||
if len(subDomain) >= 2 {
|
||||
reqExist := alidns.CreateDescribeDomainsRequest()
|
||||
reqExist.Domain = domain[len(subDomain[0])+1:]
|
||||
reqExist.PageSize = "500"
|
||||
reqExist.PageNumber = "1"
|
||||
|
||||
rep, err := client.DescribeDomains(reqExist)
|
||||
fmt.Println(rep.Domains)
|
||||
|
||||
request := alidns.CreateAddDomainRecordRequest()
|
||||
request.Scheme = "https"
|
||||
request.Value = ipv4Addr
|
||||
request.Type = "A"
|
||||
request.RR = subDomain[0]
|
||||
request.DomainName = domain[len(subDomain[0])+1:]
|
||||
|
||||
_, err = client.AddDomainRecord(request)
|
||||
if err != nil {
|
||||
fmt.Print(err.Error())
|
||||
return false
|
||||
}
|
||||
log.Println("成功添加域名:", domain)
|
||||
} else {
|
||||
log.Println(domain, "不是一个域名")
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func addIpv6Record(client *alidns.Client, conf *config.Config) bool {
|
||||
ipv6Addr, err := conf.GetIpv6Addr()
|
||||
if err != nil {
|
||||
log.Println("获得IPV6失败")
|
||||
}
|
||||
for _, domain := range conf.Ipv4.Domains {
|
||||
subDomain := strings.Split(domain, ".")
|
||||
if len(subDomain) >= 2 {
|
||||
request := alidns.CreateAddDomainRecordRequest()
|
||||
request.Scheme = "https"
|
||||
request.Value = ipv6Addr
|
||||
request.Type = "AAAA"
|
||||
request.RR = subDomain[0]
|
||||
request.DomainName = domain[len(subDomain[0])+1:]
|
||||
|
||||
_, err := client.AddDomainRecord(request)
|
||||
if err != nil {
|
||||
fmt.Print(err.Error())
|
||||
return false
|
||||
}
|
||||
log.Println("成功添加域名:", domain)
|
||||
} else {
|
||||
log.Println(domain, "不是一个域名")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package dns
|
||||
|
||||
import "ddns-go/config"
|
||||
|
||||
// DNS interface
|
||||
type DNS interface {
|
||||
addRecord() (ipv4 bool, ipv6 bool)
|
||||
AddRecord(conf *config.Config) (ipv4 bool, ipv6 bool)
|
||||
}
|
||||
|
4
go.mod
4
go.mod
@ -4,5 +4,9 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/aliyun/alibaba-cloud-sdk-go v1.61.439
|
||||
github.com/jmespath/go-jmespath v0.3.0 // indirect
|
||||
github.com/json-iterator/go v1.1.10 // indirect
|
||||
github.com/modern-go/reflect2 v1.0.1 // indirect
|
||||
gopkg.in/ini.v1 v1.60.1 // indirect
|
||||
gopkg.in/yaml.v2 v2.3.0
|
||||
)
|
||||
|
12
go.sum
12
go.sum
@ -4,21 +4,30 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
||||
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/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
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/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
|
||||
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
|
||||
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/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
|
||||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
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/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||
github.com/modern-go/reflect2 v1.0.1/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=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
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=
|
||||
@ -27,5 +36,8 @@ golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3
|
||||
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/ini.v1 v1.60.1 h1:P5y5shSkb0CFe44qEeMBgn8JLow09MP17jlJHanke5g=
|
||||
gopkg.in/ini.v1 v1.60.1/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
10
main.go
10
main.go
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"ddns-go/config"
|
||||
"ddns-go/dns"
|
||||
)
|
||||
|
||||
@ -10,17 +11,14 @@ const (
|
||||
)
|
||||
|
||||
func main() {
|
||||
conf := &Config{}
|
||||
conf.getConfigFromFile()
|
||||
|
||||
ipv4, errIpv4 := conf.getIpv4Addr()
|
||||
ipv6, errIpv6 := conf.getIpv4Addr()
|
||||
conf := &config.Config{}
|
||||
conf.GetConfigFromFile()
|
||||
|
||||
var dnsSelected dns.DNS
|
||||
switch conf.DNS.Name {
|
||||
case "alidns":
|
||||
dnsSelected = &dns.Alidns{}
|
||||
}
|
||||
dnsSelected.addRecord()
|
||||
dnsSelected.AddRecord(conf)
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user