Merge pull request 'add support for multiple records.' (#2) from wangjiacai/alidns:dev01 into master
Reviewed-on: https://gitea.wq520.cloud:4435/jiacai_wang/alidns/pulls/2
This commit is contained in:
commit
28178f54b9
282
alidns.go
282
alidns.go
@ -1,137 +1,145 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Record struct {
|
||||||
RegionId string `json:"regionId"`
|
Type string `json:"type"`
|
||||||
AccessKeyId string `json:"accessKeyId"`
|
RR string `json:"RR"`
|
||||||
AccessSecret string `json:"accessSecret"`
|
}
|
||||||
DomainName string `json:"domainName"`
|
|
||||||
Type string `json:"Type"`
|
type Config struct {
|
||||||
RR string `json:"RR"`
|
RegionId string `json:"regionId"`
|
||||||
}
|
AccessKeyId string `json:"accessKeyId"`
|
||||||
|
AccessSecret string `json:"accessSecret"`
|
||||||
type IpJson struct {
|
DomainName string `json:"domainName"`
|
||||||
Status string `json:"status"`
|
Records []Record `json:"records"`
|
||||||
Country string `json:"country"`
|
}
|
||||||
CountryCode string `json:"countryCode"`
|
|
||||||
Region string `json:"region"`
|
type IpJson struct {
|
||||||
RegionName string `json:"regionName"`
|
Status string `json:"status"`
|
||||||
City string `json:"city"`
|
Country string `json:"country"`
|
||||||
Zip string `json:"zip"`
|
CountryCode string `json:"countryCode"`
|
||||||
Lat float64 `json:"lat"`
|
Region string `json:"region"`
|
||||||
Lon float64 `json:"lon"`
|
RegionName string `json:"regionName"`
|
||||||
Timezone string `json:"timezone"`
|
City string `json:"city"`
|
||||||
Isp string `json:"isp"`
|
Zip string `json:"zip"`
|
||||||
Org string `json:"org"`
|
Lat float64 `json:"lat"`
|
||||||
As string `json:"as"`
|
Lon float64 `json:"lon"`
|
||||||
Query string `json:"query"`
|
Timezone string `json:"timezone"`
|
||||||
}
|
Isp string `json:"isp"`
|
||||||
|
Org string `json:"org"`
|
||||||
func main() {
|
As string `json:"as"`
|
||||||
|
Query string `json:"query"`
|
||||||
configPath := flag.String("config", "./config.json", "path to config file")
|
}
|
||||||
flag.Parse()
|
|
||||||
fmt.Println("config file path: ", *configPath)
|
func main() {
|
||||||
|
|
||||||
configJson, err := ioutil.ReadFile(*configPath)
|
configPath := flag.String("config", "./config.json", "path to config file")
|
||||||
if err != nil {
|
flag.Parse()
|
||||||
log.Fatal(err.Error())
|
fmt.Println("config file path:", *configPath)
|
||||||
}
|
|
||||||
var config Config
|
configJson, err := ioutil.ReadFile(*configPath)
|
||||||
json.Unmarshal(configJson, &config)
|
if err != nil {
|
||||||
|
log.Fatal(err.Error())
|
||||||
res, err := http.Get("http://ip-api.com/json/")
|
}
|
||||||
if err != nil {
|
var config Config
|
||||||
log.Fatal(err)
|
json.Unmarshal(configJson, &config)
|
||||||
}
|
|
||||||
|
res, err := http.Get("http://ip-api.com/json/")
|
||||||
data, err := ioutil.ReadAll(res.Body)
|
if err != nil {
|
||||||
res.Body.Close()
|
log.Fatal(err)
|
||||||
if err != nil {
|
}
|
||||||
log.Fatal(err)
|
|
||||||
}
|
data, err := ioutil.ReadAll(res.Body)
|
||||||
var ipJson IpJson
|
res.Body.Close()
|
||||||
json.Unmarshal(data, &ipJson)
|
if err != nil {
|
||||||
ip := ipJson.Query
|
log.Fatal(err)
|
||||||
if ipJson.Status != "success" {
|
}
|
||||||
log.Fatal("get ip failed")
|
var ipJson IpJson
|
||||||
}
|
json.Unmarshal(data, &ipJson)
|
||||||
|
ip := ipJson.Query
|
||||||
client, err := alidns.NewClientWithAccessKey(config.RegionId, config.AccessKeyId, config.AccessSecret)
|
if ipJson.Status != "success" {
|
||||||
|
log.Fatal("get ip failed")
|
||||||
describeDomainRequest := alidns.CreateDescribeDomainRecordsRequest()
|
}
|
||||||
describeDomainRequest.Scheme = "https"
|
|
||||||
|
client, err := alidns.NewClientWithAccessKey(config.RegionId, config.AccessKeyId, config.AccessSecret)
|
||||||
describeDomainRequest.DomainName = config.DomainName
|
|
||||||
|
describeDomainRequest := alidns.CreateDescribeDomainRecordsRequest()
|
||||||
domainRecords, err := client.DescribeDomainRecords(describeDomainRequest)
|
describeDomainRequest.Scheme = "https"
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err.Error())
|
describeDomainRequest.DomainName = config.DomainName
|
||||||
}
|
|
||||||
|
domainRecords, err := client.DescribeDomainRecords(describeDomainRequest)
|
||||||
var i int64 = 0
|
if err != nil {
|
||||||
var found bool = false
|
fmt.Println(err.Error())
|
||||||
// find subdomain
|
}
|
||||||
for i = 0; i < domainRecords.TotalCount; i++ {
|
|
||||||
if config.RR == domainRecords.DomainRecords.Record[i].RR && config.Type == domainRecords.DomainRecords.Record[i].Type {
|
var found bool = false
|
||||||
found = true //found subdomain record
|
|
||||||
if ip == domainRecords.DomainRecords.Record[i].Value { // ip not changed
|
// find subdomain
|
||||||
fmt.Println("ip", ip, "not changed!")
|
for _, record := range config.Records {
|
||||||
} else {
|
for _, record_ := range domainRecords.DomainRecords.Record {
|
||||||
fmt.Printf("ip changed from %s to %s\n", domainRecords.DomainRecords.Record[i].Value, ip)
|
|
||||||
fmt.Printf("---> update %s record to %s ...\n", config.RR+"."+config.DomainName, ip)
|
if record.Type == record_.Type &&
|
||||||
|
record.RR == record_.RR {
|
||||||
updateDomainRecordRequest := alidns.CreateUpdateDomainRecordRequest()
|
found = true //found subdomain record
|
||||||
updateDomainRecordRequest.Scheme = "https"
|
if ip == record_.Value { // ip not changed
|
||||||
updateDomainRecordRequest.RecordId = domainRecords.DomainRecords.Record[i].RecordId
|
fmt.Println(record.RR+"."+config.DomainName, "unchanged:", ip)
|
||||||
updateDomainRecordRequest.RR = config.RR
|
} else {
|
||||||
updateDomainRecordRequest.Type = config.Type
|
fmt.Println(record.RR+"."+config.DomainName, "changed:", record_.Value, "->", ip)
|
||||||
updateDomainRecordRequest.Value = ip
|
fmt.Println("updating record...")
|
||||||
response, err := client.UpdateDomainRecord(updateDomainRecordRequest)
|
|
||||||
if err != nil {
|
updateDomainRecordRequest := alidns.CreateUpdateDomainRecordRequest()
|
||||||
fmt.Println(err.Error())
|
updateDomainRecordRequest.Scheme = "https"
|
||||||
}
|
updateDomainRecordRequest.RecordId = record_.RecordId
|
||||||
if response.IsSuccess() {
|
updateDomainRecordRequest.RR = record.RR
|
||||||
fmt.Println("success")
|
updateDomainRecordRequest.Type = record.Type
|
||||||
} else {
|
updateDomainRecordRequest.Value = ip
|
||||||
fmt.Printf("failed.\n%s\n", response.GetHttpContentString())
|
response, err := client.UpdateDomainRecord(updateDomainRecordRequest)
|
||||||
}
|
if err != nil {
|
||||||
}
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
|
if response.IsSuccess() {
|
||||||
}
|
fmt.Println("success")
|
||||||
|
} else {
|
||||||
if !found {
|
fmt.Println("failed.", response.GetHttpContentString())
|
||||||
fmt.Printf("ip is %s, but no domain record found.\n", ip)
|
}
|
||||||
fmt.Printf("---> add %s record to %s ...\n", config.RR+"."+config.DomainName, ip)
|
}
|
||||||
|
}
|
||||||
addDomainRequest := alidns.CreateAddDomainRecordRequest()
|
}
|
||||||
addDomainRequest.Scheme = "https"
|
|
||||||
|
if !found {
|
||||||
addDomainRequest.DomainName = config.DomainName
|
fmt.Println(record.RR+"."+config.DomainName, "not found in records.")
|
||||||
addDomainRequest.RR = config.RR
|
fmt.Println("adding record:", record.RR+"."+config.DomainName, "->", ip)
|
||||||
addDomainRequest.Type = config.Type
|
|
||||||
addDomainRequest.Value = ip
|
addDomainRequest := alidns.CreateAddDomainRecordRequest()
|
||||||
|
addDomainRequest.Scheme = "https"
|
||||||
response, err := client.AddDomainRecord(addDomainRequest)
|
|
||||||
if err != nil {
|
addDomainRequest.DomainName = config.DomainName
|
||||||
fmt.Println(err.Error())
|
addDomainRequest.RR = record.RR
|
||||||
}
|
addDomainRequest.Type = record.Type
|
||||||
if response.IsSuccess() {
|
addDomainRequest.Value = ip
|
||||||
fmt.Println("success")
|
|
||||||
} else {
|
response, err := client.AddDomainRecord(addDomainRequest)
|
||||||
fmt.Printf("failed.\n%s\n", response.GetHttpContentString())
|
if err != nil {
|
||||||
}
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
fmt.Println()
|
if response.IsSuccess() {
|
||||||
}
|
fmt.Println("success")
|
||||||
|
} else {
|
||||||
|
fmt.Println("failed.", response.GetHttpContentString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
"accessKeyId": "YOUR_ACCESS_KEY_ID",
|
"accessKeyId": "YOUR_ACCESS_KEY_ID",
|
||||||
"accessSecret": "YOUR_ACCESS_SECRET",
|
"accessSecret": "YOUR_ACCESS_SECRET",
|
||||||
"domainName": "YOUR_DOMAIN_NAME",
|
"domainName": "YOUR_DOMAIN_NAME",
|
||||||
"Type": "A",
|
"records": [
|
||||||
"RR": "@"
|
{ "type": "A", "RR": "www1"},
|
||||||
|
{ "type": "A", "RR": "www2"}
|
||||||
|
]
|
||||||
}
|
}
|
Reference in New Issue
Block a user