2023-11-24 06:47:47 +00:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"log"
|
|
|
|
|
|
"net"
|
2023-11-24 09:03:18 +00:00
|
|
|
|
"os"
|
|
|
|
|
|
"strings"
|
2023-11-24 06:47:47 +00:00
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
2023-11-24 09:03:18 +00:00
|
|
|
|
domain := os.Getenv("DOMAIN")
|
|
|
|
|
|
rr := os.Getenv("RR")
|
2024-02-05 15:20:58 +00:00
|
|
|
|
regionID := os.Getenv("REGIONID")
|
2023-11-25 07:29:55 +00:00
|
|
|
|
if regionID == "" {
|
|
|
|
|
|
regionID = "cn-hangzhou"
|
|
|
|
|
|
}
|
2024-02-05 15:20:58 +00:00
|
|
|
|
accessKeyID := os.Getenv("ACCESSKEYID")
|
|
|
|
|
|
accessSecret := os.Getenv("ACCESSSECRET")
|
2023-11-24 06:47:47 +00:00
|
|
|
|
|
2024-02-05 15:20:58 +00:00
|
|
|
|
ip6 := ""
|
2023-11-24 06:47:47 +00:00
|
|
|
|
for {
|
2024-02-05 15:20:58 +00:00
|
|
|
|
time.Sleep(3 * time.Second)
|
2023-11-24 06:47:47 +00:00
|
|
|
|
|
|
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Println(err)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-05 15:20:58 +00:00
|
|
|
|
currentIPV6 := ""
|
2023-11-24 06:47:47 +00:00
|
|
|
|
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 {
|
2024-02-05 15:20:58 +00:00
|
|
|
|
if currentIPV6 != ipnet.IP.String() {
|
|
|
|
|
|
currentIPV6 = ipnet.IP.String()
|
2023-11-24 06:47:47 +00:00
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-05 15:20:58 +00:00
|
|
|
|
if currentIPV6 == "" || currentIPV6 == ip6 {
|
2023-11-24 06:47:47 +00:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-05 15:20:58 +00:00
|
|
|
|
client, err := alidns.NewClientWithAccessKey(regionID, accessKeyID, accessSecret)
|
2023-11-24 06:47:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Println(err.Error())
|
2024-02-05 15:20:58 +00:00
|
|
|
|
continue
|
2023-11-24 09:03:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-05 15:20:58 +00:00
|
|
|
|
drRequest := alidns.CreateDescribeDomainRecordsRequest()
|
|
|
|
|
|
drRequest.KeyWord = rr
|
|
|
|
|
|
drRequest.DomainName = domain
|
|
|
|
|
|
drRequest.TypeKeyWord = "AAAA"
|
|
|
|
|
|
|
|
|
|
|
|
drResponse, err := client.DescribeDomainRecords(drRequest)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Println(err)
|
|
|
|
|
|
continue
|
2023-11-24 09:03:18 +00:00
|
|
|
|
}
|
2024-02-05 15:20:58 +00:00
|
|
|
|
if !drResponse.IsSuccess() {
|
2023-11-24 06:47:47 +00:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-05 15:20:58 +00:00
|
|
|
|
rid := ""
|
|
|
|
|
|
for _, r := range drResponse.DomainRecords.Record {
|
|
|
|
|
|
rid = r.RecordId
|
|
|
|
|
|
if r.Value == currentIPV6 {
|
|
|
|
|
|
ip6 = currentIPV6
|
|
|
|
|
|
rid = ""
|
|
|
|
|
|
}
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if rid == "" {
|
2023-11-24 09:03:18 +00:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
2023-11-24 06:47:47 +00:00
|
|
|
|
|
|
|
|
|
|
//修改域名对应的IP,修改成功后continue
|
|
|
|
|
|
req := alidns.CreateUpdateDomainRecordRequest()
|
|
|
|
|
|
req.Scheme = "https"
|
2023-11-24 09:03:18 +00:00
|
|
|
|
req.RR = rr
|
2024-02-05 15:20:58 +00:00
|
|
|
|
req.RecordId = rid
|
|
|
|
|
|
req.Value = currentIPV6
|
2023-11-24 06:47:47 +00:00
|
|
|
|
req.Type = "AAAA"
|
2024-02-05 15:20:58 +00:00
|
|
|
|
|
2023-11-24 06:47:47 +00:00
|
|
|
|
resp, err := client.UpdateDomainRecord(req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Println(err.Error())
|
2023-11-25 07:29:55 +00:00
|
|
|
|
if !strings.Contains(err.Error(), fmt.Sprintf("not %s", domain)) {
|
2023-11-24 09:03:18 +00:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
2023-11-24 06:47:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if resp.IsSuccess() {
|
2024-02-05 15:20:58 +00:00
|
|
|
|
ip6 = currentIPV6
|
|
|
|
|
|
log.Println("update success: ", ip6)
|
2023-11-24 06:47:47 +00:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//当域名修改失败时判断是否是没有对应的记录,不存在时创建新的域名记录
|
|
|
|
|
|
request := alidns.CreateAddDomainRecordRequest()
|
|
|
|
|
|
request.Scheme = "https"
|
2024-02-05 15:20:58 +00:00
|
|
|
|
request.Value = currentIPV6
|
2023-11-24 06:47:47 +00:00
|
|
|
|
request.Type = "AAAA"
|
2023-11-24 09:03:18 +00:00
|
|
|
|
request.RR = rr
|
|
|
|
|
|
request.DomainName = domain
|
2023-11-24 06:47:47 +00:00
|
|
|
|
response, err := client.AddDomainRecord(request)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fmt.Print(err.Error())
|
2024-02-05 15:20:58 +00:00
|
|
|
|
continue
|
2023-11-24 06:47:47 +00:00
|
|
|
|
}
|
2023-11-24 09:03:18 +00:00
|
|
|
|
if !response.IsSuccess() {
|
|
|
|
|
|
fmt.Print(response.GetHttpContentString())
|
2024-02-05 15:20:58 +00:00
|
|
|
|
ip6 = currentIPV6
|
2023-11-24 09:03:18 +00:00
|
|
|
|
}
|
2023-11-24 06:47:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|