143 lines
3.8 KiB
Go
143 lines
3.8 KiB
Go
// This file is auto-generated, don't edit it. Thanks.
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
_ "github.com/PuerkitoBio/goquery"
|
|
dns "github.com/alibabacloud-go/alidns-20150109/v2/client"
|
|
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
|
|
"github.com/alibabacloud-go/tea/tea"
|
|
)
|
|
|
|
/**
|
|
* 使用AK&SK初始化账号Client
|
|
* @param accessKeyId
|
|
* @param accessKeySecret
|
|
* @return Client
|
|
* @throws Exception
|
|
*/
|
|
func QueryPublicIP() string {
|
|
type QueryResult struct {
|
|
Code int `json:"code"`
|
|
Msg interface{} `json:"msg"`
|
|
Data struct {
|
|
IP string `json:"ip"`
|
|
IPv6 string `json:"ipv6"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
client := &http.Client{Timeout: 5 * time.Second}
|
|
resp, err := client.Get("https://openapi.lddgo.net/base/gtool/api/v1/GetIp")
|
|
if err != nil {
|
|
fmt.Printf("HTTP request failed: %v\n", err)
|
|
return ""
|
|
}
|
|
defer func() {
|
|
if err := resp.Body.Close(); err != nil {
|
|
fmt.Printf("Failed to close response body: %v\n", err)
|
|
}
|
|
}()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
fmt.Printf("Unexpected status code: %d\n", resp.StatusCode)
|
|
return ""
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
fmt.Printf("Failed to read response body: %v\n", err)
|
|
return ""
|
|
}
|
|
|
|
var query QueryResult
|
|
if err := json.Unmarshal(body, &query); err != nil {
|
|
fmt.Printf("JSON unmarshal failed: %v\n", err)
|
|
return ""
|
|
}
|
|
|
|
if query.Code != 0 { // 假设 0 表示成功状态码
|
|
fmt.Printf("API returned error: %v\n", query.Msg)
|
|
return ""
|
|
}
|
|
|
|
return query.Data.IP
|
|
}
|
|
|
|
func QueryRecordId(client *dns.Client, domainName *string) map[string]interface{} {
|
|
req := &dns.DescribeDomainRecordsRequest{}
|
|
req.DomainName = domainName
|
|
resp, err := client.DescribeDomainRecords(req)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
dict := tea.ToMap(resp)["body"].(map[string]interface{})
|
|
dict = dict["DomainRecords"].(map[string]interface{})
|
|
list := dict["Record"].([]interface{})
|
|
dict = list[0].(map[string]interface{})
|
|
return dict
|
|
}
|
|
|
|
func CreateClient(accessKeyId *string, accessKeySecret *string) (_result *dns.Client, _err error) {
|
|
config := &openapi.Config{
|
|
// 您的AccessKey ID
|
|
AccessKeyId: accessKeyId,
|
|
// 您的AccessKey Secret
|
|
AccessKeySecret: accessKeySecret,
|
|
}
|
|
// 访问的域名
|
|
config.Endpoint = tea.String("alidns.cn-beijing.aliyuncs.com")
|
|
_result = &dns.Client{}
|
|
_result, _err = dns.NewClient(config)
|
|
return _result, _err
|
|
}
|
|
|
|
func UpdateDomainRecord(RecordMap map[string]interface{}, currentHostIP string) (_err error) {
|
|
client, _err := CreateClient(tea.String("LTAI4G6QTMpykP5rBQtYvzR4"), tea.String("0X9WwmXMbIqkuNWjaPg31LhTm5ThKc"))
|
|
if _err != nil {
|
|
return _err
|
|
}
|
|
req := &dns.UpdateDomainRecordRequest{}
|
|
RR := RecordMap["RR"].(string)
|
|
recordType := RecordMap["Type"].(string)
|
|
recordId := RecordMap["RecordId"].(string)
|
|
originHostIP := RecordMap["Value"].(string)
|
|
fmt.Println("Update origin IP address: ", originHostIP, " to new IP address ", currentHostIP)
|
|
req.RecordId = &recordId
|
|
req.RR = &RR
|
|
req.Type = &recordType
|
|
req.Value = ¤tHostIP
|
|
fmt.Println(req.UserClientIp)
|
|
_, err := client.UpdateDomainRecord(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return err
|
|
}
|
|
|
|
func main() {
|
|
ip := ""
|
|
for {
|
|
currentHostIP := QueryPublicIP()
|
|
if currentHostIP != ip {
|
|
client, _ := CreateClient(tea.String("LTAI4G6QTMpykP5rBQtYvzR4"), tea.String("0X9WwmXMbIqkuNWjaPg31LhTm5ThKc"))
|
|
var domainName string
|
|
var RecordMap map[string]interface{}
|
|
domainName = "stayreal.online"
|
|
RecordMap = QueryRecordId(client, &domainName)
|
|
_ = UpdateDomainRecord(RecordMap, currentHostIP)
|
|
domainName = "liubeiting.cn"
|
|
RecordMap = QueryRecordId(client, &domainName)
|
|
_ = UpdateDomainRecord(RecordMap, currentHostIP)
|
|
ip = currentHostIP
|
|
} else {
|
|
fmt.Println("IP address no changes : ", currentHostIP)
|
|
}
|
|
time.Sleep(10 * time.Minute)
|
|
}
|
|
}
|