You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package main
import (
"ddns/global"
"ddns/util"
"fmt"
"git.sre.ink/go/gtool"
"github.com/denverdino/aliyungo/dns"
)
func main() {
// 获取公网IP信息
publicIP := gtool.NET.PublicIP()
if publicIP == "" {
return
}
// 获取阿里DNS信息
client := dns.NewClient(global.AccessKeyID, global.AccessKeySecret)
client.SetDebug(false)
domainInfo := new(dns.DescribeDomainRecordsArgs)
domainInfo.DomainName = global.DomainName
domainInfo.PageNumber = 1
domainInfo.PageSize = 500 //最大是500
oldRecord, err := client.DescribeDomainRecords(domainInfo)
if err != nil {
fmt.Println("链接服务器错误", err)
msg := "ddns解析链接服务器错误\n"
msg += "域名信息: " + global.RR + global.DomainName + "\n"
msg += "链接服务器错误: " + err.Error() + "\n"
util.SendWechat(msg)
return
}
// 匹配公网IP地址
var exsitRecordID string
for _, record := range oldRecord.DomainRecords.Record {
if record.DomainName == global.DomainName && record.RR == global.RR {
if record.Value == publicIP {
fmt.Println("当前配置解析地址与公网IP相同不需要修改。")
return
}
exsitRecordID = record.RecordId
}
}
if 0 < len(exsitRecordID) {
// 有配置记录,更新配置
updateRecord := new(dns.UpdateDomainRecordArgs)
updateRecord.RecordId = exsitRecordID
updateRecord.RR = global.RR
updateRecord.Value = publicIP
updateRecord.Type = dns.ARecord
rsp := new(dns.UpdateDomainRecordResponse)
rsp, err := client.UpdateDomainRecord(updateRecord)
if nil != err {
fmt.Println("修改解析失败", err)
} else {
fmt.Println("修改解析成功", rsp)
}
} else {
// 没有配置记录,新增配置
newRecord := new(dns.AddDomainRecordArgs)
newRecord.DomainName = global.DomainName
newRecord.RR = global.RR
newRecord.Value = publicIP
newRecord.Type = dns.ARecord
rsp := new(dns.AddDomainRecordResponse)
rsp, err = client.AddDomainRecord(newRecord)
if nil != err {
fmt.Println("添加DNS解析失败", err)
} else {
fmt.Println("添加DNS解析成功", rsp)
}
}
}