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) } } }