优化完善并测试通过
This commit is contained in:
parent
229c58095f
commit
6c7f01b7e8
|
|
@ -2,4 +2,5 @@
|
|||
.vscode/
|
||||
go.sum
|
||||
ddns6
|
||||
*.cmd
|
||||
*.cmd
|
||||
.env
|
||||
|
|
|
|||
|
|
@ -2,10 +2,10 @@ FROM harbor.ks.easyj.top/zt/alpine:0.1
|
|||
|
||||
ENV APP_DIR=/app \
|
||||
DOMAIN=myschools.me \
|
||||
RR=PI1 \
|
||||
AL_REGIONID=cn-hangzhou \
|
||||
AL_ACCESSKEYID=LTAI5tJV828nqSqGpkouh1FD \
|
||||
AL_ACCESSSECRET=aiMEp37bHLOjtoJFTDTfpq1o37cGtA
|
||||
RR=pi \
|
||||
REGIONID=cn-hangzhou \
|
||||
ACCESSKEYID=LTAI5tJV828nqSqGpkouh1FD \
|
||||
ACCESSSECRET=aiMEp37bHLOjtoJFTDTfpq1o37cGtA
|
||||
|
||||
COPY ddns6 ${APP_DIR}/ddns6
|
||||
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -2,7 +2,7 @@ module myschools.me/suguo/ddns6
|
|||
|
||||
go 1.19
|
||||
|
||||
require github.com/aliyun/alibaba-cloud-sdk-go v1.62.617
|
||||
require github.com/aliyun/alibaba-cloud-sdk-go v1.62.676
|
||||
|
||||
require (
|
||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
|
||||
|
|
|
|||
79
main.go
79
main.go
|
|
@ -14,17 +14,16 @@ import (
|
|||
func main() {
|
||||
domain := os.Getenv("DOMAIN")
|
||||
rr := os.Getenv("RR")
|
||||
regionID := os.Getenv("AL_REGIONID")
|
||||
regionID := os.Getenv("REGIONID")
|
||||
if regionID == "" {
|
||||
regionID = "cn-hangzhou"
|
||||
}
|
||||
accessKeyID := os.Getenv("AL_ACCESSKEYID")
|
||||
accessSecret := os.Getenv("AL_ACCESSSECRET")
|
||||
|
||||
fullDNS := fmt.Sprintf("%s.%s", rr, domain)
|
||||
accessKeyID := os.Getenv("ACCESSKEYID")
|
||||
accessSecret := os.Getenv("ACCESSSECRET")
|
||||
|
||||
ip6 := ""
|
||||
for {
|
||||
time.Sleep(time.Minute)
|
||||
time.Sleep(3 * time.Second)
|
||||
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
|
|
@ -32,41 +31,20 @@ func main() {
|
|||
continue
|
||||
}
|
||||
|
||||
ip6 := ""
|
||||
currentIPV6 := ""
|
||||
for _, address := range addrs {
|
||||
// 检查 ip 地址判断是否回环地址
|
||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && !ipnet.IP.IsPrivate() && ipnet.IP.IsGlobalUnicast() {
|
||||
if ipnet.IP.To16() != nil {
|
||||
if ip6 != ipnet.IP.String() {
|
||||
ip6 = ipnet.IP.String()
|
||||
if currentIPV6 != ipnet.IP.String() {
|
||||
currentIPV6 = ipnet.IP.String()
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ip6 == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
//查询域名对应的IP是否相同,相同时跳过。
|
||||
ns, err := net.LookupHost(fullDNS)
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
|
||||
if !strings.Contains(err.Error(), "no such host") {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, n := range ns {
|
||||
if n == ip6 {
|
||||
found = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
if found {
|
||||
if currentIPV6 == "" || currentIPV6 == ip6 {
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -76,13 +54,42 @@ func main() {
|
|||
continue
|
||||
}
|
||||
|
||||
drRequest := alidns.CreateDescribeDomainRecordsRequest()
|
||||
drRequest.KeyWord = rr
|
||||
drRequest.DomainName = domain
|
||||
drRequest.TypeKeyWord = "AAAA"
|
||||
|
||||
drResponse, err := client.DescribeDomainRecords(drRequest)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
if !drResponse.IsSuccess() {
|
||||
continue
|
||||
}
|
||||
|
||||
rid := ""
|
||||
for _, r := range drResponse.DomainRecords.Record {
|
||||
rid = r.RecordId
|
||||
if r.Value == currentIPV6 {
|
||||
ip6 = currentIPV6
|
||||
rid = ""
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if rid == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
//修改域名对应的IP,修改成功后continue
|
||||
req := alidns.CreateUpdateDomainRecordRequest()
|
||||
req.Scheme = "https"
|
||||
req.RR = rr
|
||||
req.Value = ip6
|
||||
req.RecordId = rid
|
||||
req.Value = currentIPV6
|
||||
req.Type = "AAAA"
|
||||
req.Domain = domain
|
||||
|
||||
resp, err := client.UpdateDomainRecord(req)
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
|
|
@ -92,22 +99,26 @@ func main() {
|
|||
}
|
||||
|
||||
if resp.IsSuccess() {
|
||||
ip6 = currentIPV6
|
||||
log.Println("update success: ", ip6)
|
||||
continue
|
||||
}
|
||||
|
||||
//当域名修改失败时判断是否是没有对应的记录,不存在时创建新的域名记录
|
||||
request := alidns.CreateAddDomainRecordRequest()
|
||||
request.Scheme = "https"
|
||||
request.Value = ip6
|
||||
request.Value = currentIPV6
|
||||
request.Type = "AAAA"
|
||||
request.RR = rr
|
||||
request.DomainName = domain
|
||||
response, err := client.AddDomainRecord(request)
|
||||
if err != nil {
|
||||
fmt.Print(err.Error())
|
||||
continue
|
||||
}
|
||||
if !response.IsSuccess() {
|
||||
fmt.Print(response.GetHttpContentString())
|
||||
ip6 = currentIPV6
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue