114 lines
2.9 KiB
Go
114 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/denverdino/aliyungo/dns"
|
|
)
|
|
|
|
var client *dns.Client
|
|
var domain string
|
|
var sub string
|
|
var recordType string
|
|
var recordValue string
|
|
|
|
func main() {
|
|
domainRecord := parseArgs()
|
|
updateDNSRecord(domainRecord)
|
|
}
|
|
|
|
func parseArgs() *dns.AddDomainRecordArgs {
|
|
id := flag.String("id", os.Getenv("ALI_DNS_ID"), "AliId")
|
|
key := flag.String("key", os.Getenv("ALI_DNS_KEY"), "AliKey")
|
|
|
|
client = dns.NewClient(*id, *key)
|
|
|
|
domain = *flag.String("domain", os.Getenv("ALI_DNS_DOMAIN"), "Domain")
|
|
sub = *flag.String("sub", os.Getenv("ALI_DNS_SUB_DOMAIN"), "RR")
|
|
recordType = *flag.String("type", "A", "RR")
|
|
recordValue = *flag.String("value", "127.0.0.1", "Redis Server Host")
|
|
|
|
flag.Parse()
|
|
recordValue = getLocalIP()
|
|
fmt.Println("Local IP: " + recordValue)
|
|
|
|
return &dns.AddDomainRecordArgs{
|
|
DomainName: domain,
|
|
RR: sub,
|
|
Type: recordType,
|
|
Value: recordValue,
|
|
}
|
|
}
|
|
|
|
func updateDNSRecord(domainRecord *dns.AddDomainRecordArgs) {
|
|
record, err := listRecord(domainRecord)
|
|
if err != nil {
|
|
panic("List Record ERR: " + err.Error())
|
|
}
|
|
|
|
if record != nil {
|
|
fmt.Println("Record IP: " + record.Value)
|
|
if record.Value != domainRecord.Value {
|
|
_, err = updateRecord(record.RecordId, domainRecord)
|
|
fmt.Println("Update Record " + domainRecord.RR + " ID " + record.RecordId + " Value " + domainRecord.Value)
|
|
} else {
|
|
fmt.Println("Record " + domainRecord.RR + " ID " + record.RecordId + " Value " + domainRecord.Value + " Not Change")
|
|
}
|
|
} else {
|
|
_, err = addRecord(domainRecord)
|
|
fmt.Println("Add Record " + domainRecord.RR + " Value " + domainRecord.Value)
|
|
}
|
|
|
|
if err != nil {
|
|
fmt.Println("Update Or Add Record ERR: " + err.Error())
|
|
}
|
|
}
|
|
|
|
func getLocalIP() string {
|
|
resp, err := http.Get("http://ip.3322.org")
|
|
if err != nil {
|
|
panic("Get Network IP ERR: " + err.Error())
|
|
}
|
|
str, err := ioutil.ReadAll(resp.Body)
|
|
defer resp.Body.Close()
|
|
if err != nil {
|
|
panic("Read Body Byte ERR: " + err.Error())
|
|
}
|
|
return strings.Replace(string(str), "\n", "", -1)
|
|
}
|
|
|
|
func listRecord(domainRecord *dns.AddDomainRecordArgs) (*dns.RecordTypeNew, error) {
|
|
resp, err := client.DescribeDomainRecordsNew(
|
|
&dns.DescribeDomainRecordsNewArgs{
|
|
DomainName: domainRecord.DomainName,
|
|
RRKeyWord: domainRecord.RR,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for r := range resp.DomainRecords.Record {
|
|
if resp.DomainRecords.Record[r].RR == domainRecord.RR {
|
|
return &resp.DomainRecords.Record[r], nil
|
|
}
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
func addRecord(domainRecord *dns.AddDomainRecordArgs) (*dns.AddDomainRecordResponse, error) {
|
|
return client.AddDomainRecord(domainRecord)
|
|
}
|
|
|
|
func updateRecord(id string, domainRecord *dns.AddDomainRecordArgs) (*dns.UpdateDomainRecordResponse, error) {
|
|
return client.UpdateDomainRecord(&dns.UpdateDomainRecordArgs{
|
|
RecordId: id,
|
|
RR: domainRecord.RR,
|
|
Type: domainRecord.Type,
|
|
Value: domainRecord.Value,
|
|
})
|
|
}
|