This repository has been archived on 2023-12-12. You can view files and clone it, but cannot push or open issues or pull requests.
alidns/alidns.go

139 lines
3.6 KiB
Go
Raw Normal View History

2021-04-23 00:39:54 +08:00
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"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("read config file error:", err)
2021-04-23 00:39:54 +08:00
}
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)
if err != nil {
log.Fatal("create alidns client error:", err)
}
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 {
log.Fatal("describe domain records error:", err)
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 {
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 {
log.Println("update domain record error:", err)
2021-04-23 23:08:06 +08:00
}
if response.IsSuccess() {
fmt.Println("success")
} else {
log.Println("update domain record error:", response)
2021-04-23 23:08:06 +08:00
}
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 {
log.Println("add domain record error:", err)
2021-04-23 23:08:06 +08:00
}
if response.IsSuccess() {
fmt.Println("success")
} else {
log.Println("add domain record error:", response)
2021-04-23 23:08:06 +08:00
}
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()
}