set precise subdomain instead of top level domain
This commit is contained in:
parent
ddddb4bf66
commit
7d2c4eab59
61
alidns.go
61
alidns.go
@ -16,6 +16,7 @@ type Config struct {
|
|||||||
AccessKeyId string `json:"accessKeyId"`
|
AccessKeyId string `json:"accessKeyId"`
|
||||||
AccessSecret string `json:"accessSecret"`
|
AccessSecret string `json:"accessSecret"`
|
||||||
DomainName string `json:"domainName"`
|
DomainName string `json:"domainName"`
|
||||||
|
RR string `json:"RR"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type IpJson struct {
|
type IpJson struct {
|
||||||
@ -77,15 +78,46 @@ func main() {
|
|||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if domainRecords.TotalCount == 0 { // add domain record if none exists
|
var i int64 = 0
|
||||||
|
var found bool = false
|
||||||
|
// find subdomain
|
||||||
|
for i = 0; i < domainRecords.TotalCount; i++ {
|
||||||
|
if config.RR == domainRecords.DomainRecords.Record[i].RR {
|
||||||
|
found = true //found subdomain record
|
||||||
|
if ip == domainRecords.DomainRecords.Record[i].Value { // ip not changed
|
||||||
|
fmt.Println("ip", ip, "not changed!")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("ip changed from %s to %s\n", domainRecords.DomainRecords.Record[0].Value, ip)
|
||||||
|
fmt.Printf("---> update %s record to %s ...\n", config.RR+"."+config.DomainName, ip)
|
||||||
|
|
||||||
|
updateDomainRecordRequest := alidns.CreateUpdateDomainRecordRequest()
|
||||||
|
updateDomainRecordRequest.Scheme = "https"
|
||||||
|
updateDomainRecordRequest.RecordId = domainRecords.DomainRecords.Record[0].RecordId
|
||||||
|
updateDomainRecordRequest.RR = config.RR
|
||||||
|
updateDomainRecordRequest.Type = "A"
|
||||||
|
updateDomainRecordRequest.Value = ip
|
||||||
|
response, err := client.UpdateDomainRecord(updateDomainRecordRequest)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
if response.IsSuccess() {
|
||||||
|
fmt.Println("success")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("failed.\n%s\n", response.GetHttpContentString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
fmt.Printf("ip is %s, but no domain record found.\n", ip)
|
fmt.Printf("ip is %s, but no domain record found.\n", ip)
|
||||||
fmt.Printf("---> update %s record to %s ...\n", config.DomainName, ip)
|
fmt.Printf("---> update %s record to %s ...\n", config.RR+"."+config.DomainName, ip)
|
||||||
|
|
||||||
addDomainRequest := alidns.CreateAddDomainRecordRequest()
|
addDomainRequest := alidns.CreateAddDomainRecordRequest()
|
||||||
addDomainRequest.Scheme = "https"
|
addDomainRequest.Scheme = "https"
|
||||||
|
|
||||||
addDomainRequest.DomainName = config.DomainName
|
addDomainRequest.DomainName = config.DomainName
|
||||||
addDomainRequest.RR = "@"
|
addDomainRequest.RR = config.RR
|
||||||
addDomainRequest.Type = "A"
|
addDomainRequest.Type = "A"
|
||||||
addDomainRequest.Value = ip
|
addDomainRequest.Value = ip
|
||||||
|
|
||||||
@ -98,29 +130,6 @@ func main() {
|
|||||||
} else {
|
} else {
|
||||||
fmt.Printf("failed.\n%s\n", response.GetHttpContentString())
|
fmt.Printf("failed.\n%s\n", response.GetHttpContentString())
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if ip == domainRecords.DomainRecords.Record[0].Value { //ip unchanged
|
|
||||||
fmt.Printf("ip unchanged: %s\n", ip)
|
|
||||||
|
|
||||||
} else { //update record[0] by default
|
|
||||||
fmt.Printf("ip changed from %s to %s\n", domainRecords.DomainRecords.Record[0].Value, ip)
|
|
||||||
fmt.Printf("---> update %s record to %s ...\n", config.DomainName, ip)
|
|
||||||
|
|
||||||
updateDomainRecordRequest := alidns.CreateUpdateDomainRecordRequest()
|
|
||||||
updateDomainRecordRequest.Scheme = "https"
|
|
||||||
updateDomainRecordRequest.RecordId = domainRecords.DomainRecords.Record[0].RecordId
|
|
||||||
updateDomainRecordRequest.RR = "@"
|
|
||||||
updateDomainRecordRequest.Type = "A"
|
|
||||||
updateDomainRecordRequest.Value = ip
|
|
||||||
response, err := client.UpdateDomainRecord(updateDomainRecordRequest)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
}
|
|
||||||
if response.IsSuccess() {
|
|
||||||
fmt.Println("success")
|
|
||||||
} else {
|
|
||||||
fmt.Printf("failed.\n%s\n", response.GetHttpContentString())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
@ -2,5 +2,6 @@
|
|||||||
"regionId": "cn-hangzhou",
|
"regionId": "cn-hangzhou",
|
||||||
"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",
|
||||||
|
"RR": "@"
|
||||||
}
|
}
|
Reference in New Issue
Block a user