2021-04-23 00:39:54 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2021-07-11 16:16:36 +08:00
|
|
|
"os"
|
2021-04-23 00:39:54 +08:00
|
|
|
|
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Record struct {
|
2021-04-23 23:08:06 +08:00
|
|
|
Type string `json:"type"`
|
2021-04-23 00:39:54 +08:00
|
|
|
RR string `json:"RR"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2021-04-23 23:08:06 +08:00
|
|
|
RegionId string `json:"regionId"`
|
|
|
|
AccessKeyId string `json:"accessKeyId"`
|
|
|
|
AccessSecret string `json:"accessSecret"`
|
|
|
|
DomainName string `json:"domainName"`
|
|
|
|
Records []Record `json:"records"`
|
2021-04-23 00:39:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type IpJson struct {
|
2021-09-19 21:13:13 +08:00
|
|
|
Status string `json:"status"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Query string `json:"query"`
|
2021-04-23 00:39:54 +08:00
|
|
|
}
|
|
|
|
|
2022-04-03 10:46:51 +08:00
|
|
|
func getPubIp() (ip string) {
|
|
|
|
res, err := http.Get("https://ifconfig.me")
|
|
|
|
defer res.Body.Close()
|
|
|
|
if err != nil || res.StatusCode != 200 {
|
|
|
|
log.Fatal("http get error:", err, " http response:", res.StatusCode)
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("read http body error:", err)
|
|
|
|
}
|
|
|
|
ip = string(data)
|
|
|
|
fmt.Println("ip:", ip)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-23 00:39:54 +08:00
|
|
|
func main() {
|
|
|
|
|
|
|
|
configPath := flag.String("config", "./config.json", "path to config file")
|
|
|
|
flag.Parse()
|
2021-04-23 23:08:06 +08:00
|
|
|
fmt.Println("config file path:", *configPath)
|
2021-04-23 00:39:54 +08:00
|
|
|
|
|
|
|
configJson, err := ioutil.ReadFile(*configPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
var config Config
|
|
|
|
json.Unmarshal(configJson, &config)
|
|
|
|
|
2022-04-03 10:46:51 +08:00
|
|
|
ip := getPubIp()
|
2021-04-23 00:39:54 +08:00
|
|
|
|
|
|
|
client, err := alidns.NewClientWithAccessKey(config.RegionId, config.AccessKeyId, config.AccessSecret)
|
2021-07-11 16:16:36 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2021-04-23 00:39:54 +08:00
|
|
|
|
|
|
|
describeDomainRequest := alidns.CreateDescribeDomainRecordsRequest()
|
|
|
|
describeDomainRequest.Scheme = "https"
|
|
|
|
|
|
|
|
describeDomainRequest.DomainName = config.DomainName
|
|
|
|
|
|
|
|
domainRecords, err := client.DescribeDomainRecords(describeDomainRequest)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
2021-07-11 16:16:36 +08:00
|
|
|
os.Exit(-1)
|
2021-04-23 00:39:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var found bool = false
|
2021-04-23 23:08:06 +08:00
|
|
|
|
2021-04-23 00:39:54 +08:00
|
|
|
// find subdomain
|
2021-04-23 23:08:06 +08:00
|
|
|
for _, record := range config.Records {
|
2021-05-15 01:56:20 +08:00
|
|
|
found = false
|
2021-04-23 23:08:06 +08:00
|
|
|
for _, record_ := range domainRecords.DomainRecords.Record {
|
|
|
|
|
|
|
|
if record.Type == record_.Type &&
|
|
|
|
record.RR == record_.RR {
|
|
|
|
found = true //found subdomain record
|
|
|
|
if ip == record_.Value { // ip not changed
|
|
|
|
fmt.Println(record.RR+"."+config.DomainName, "unchanged:", ip)
|
2021-04-23 00:39:54 +08:00
|
|
|
} else {
|
2021-04-23 23:08:06 +08:00
|
|
|
fmt.Println(record.RR+"."+config.DomainName, "changed:", record_.Value, "->", ip)
|
|
|
|
fmt.Println("updating record...")
|
|
|
|
|
|
|
|
updateDomainRecordRequest := alidns.CreateUpdateDomainRecordRequest()
|
|
|
|
updateDomainRecordRequest.Scheme = "https"
|
|
|
|
updateDomainRecordRequest.RecordId = record_.RecordId
|
|
|
|
updateDomainRecordRequest.RR = record.RR
|
|
|
|
updateDomainRecordRequest.Type = record.Type
|
|
|
|
updateDomainRecordRequest.Value = ip
|
|
|
|
response, err := client.UpdateDomainRecord(updateDomainRecordRequest)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
if response.IsSuccess() {
|
|
|
|
fmt.Println("success")
|
|
|
|
} else {
|
|
|
|
fmt.Println("failed.", response.GetHttpContentString())
|
|
|
|
}
|
2021-04-23 00:39:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 23:08:06 +08:00
|
|
|
if !found {
|
|
|
|
fmt.Println(record.RR+"."+config.DomainName, "not found in records.")
|
|
|
|
fmt.Println("adding record:", record.RR+"."+config.DomainName, "->", ip)
|
2021-04-23 00:39:54 +08:00
|
|
|
|
2021-04-23 23:08:06 +08:00
|
|
|
addDomainRequest := alidns.CreateAddDomainRecordRequest()
|
|
|
|
addDomainRequest.Scheme = "https"
|
2021-04-23 00:39:54 +08:00
|
|
|
|
2021-04-23 23:08:06 +08:00
|
|
|
addDomainRequest.DomainName = config.DomainName
|
|
|
|
addDomainRequest.RR = record.RR
|
|
|
|
addDomainRequest.Type = record.Type
|
|
|
|
addDomainRequest.Value = ip
|
2021-04-23 00:39:54 +08:00
|
|
|
|
2021-04-23 23:08:06 +08:00
|
|
|
response, err := client.AddDomainRecord(addDomainRequest)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
if response.IsSuccess() {
|
|
|
|
fmt.Println("success")
|
|
|
|
} else {
|
|
|
|
fmt.Println("failed.", response.GetHttpContentString())
|
|
|
|
}
|
2021-04-23 00:39:54 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-23 23:08:06 +08:00
|
|
|
|
2021-04-23 00:39:54 +08:00
|
|
|
fmt.Println()
|
|
|
|
}
|