62
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										62
									
								
								main.go
									
									
									
									
									
								
							@@ -7,6 +7,7 @@ import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/denverdino/aliyungo/dns"
 | 
			
		||||
)
 | 
			
		||||
@@ -18,51 +19,52 @@ var recordType string
 | 
			
		||||
var recordValue string
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	fmt.Println("Check  Time: " + time.Now().Format("2006-01-02 15:04:05"))
 | 
			
		||||
	initClient()
 | 
			
		||||
	domainRecord := parseArgs()
 | 
			
		||||
	flag.Parse()
 | 
			
		||||
	updateDNSRecord(domainRecord)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func initClient() {
 | 
			
		||||
	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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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,
 | 
			
		||||
		DomainName: *flag.String("domain", os.Getenv("ALI_DNS_DOMAIN"), "Domain"),
 | 
			
		||||
		RR:         *flag.String("sub", os.Getenv("ALI_DNS_SUB_DOMAIN"), "RR"),
 | 
			
		||||
		Type:       *flag.String("type", "A", "RR"),
 | 
			
		||||
		Value:      *flag.String("value", getLocalIP(), "Value"),
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func updateDNSRecord(domainRecord *dns.AddDomainRecordArgs) {
 | 
			
		||||
	fmt.Println("Local  IP: " + domainRecord.Value)
 | 
			
		||||
	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")
 | 
			
		||||
	if record != nil && record.Value == domainRecord.Value {
 | 
			
		||||
		fmt.Println("Record RR: " + domainRecord.RR + " Not Change")
 | 
			
		||||
		fmt.Println("Record ID: " + record.RecordId)
 | 
			
		||||
		fmt.Println("Record Value: " + domainRecord.Value)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	} else {
 | 
			
		||||
 | 
			
		||||
	if record == nil {
 | 
			
		||||
		_, err = addRecord(domainRecord)
 | 
			
		||||
		fmt.Println("Add Record " + domainRecord.RR + " Value " + domainRecord.Value)
 | 
			
		||||
		fmt.Println("Record RR: " + domainRecord.RR + " Added!")
 | 
			
		||||
	} else {
 | 
			
		||||
		_, err = updateRecord(record.RecordId, domainRecord)
 | 
			
		||||
		fmt.Println("Record RR: " + domainRecord.RR + " Updated!")
 | 
			
		||||
		fmt.Println("Record ID: " + record.RecordId)
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println("Record Value: " + domainRecord.Value)
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("Update Or Add Record ERR: " + err.Error())
 | 
			
		||||
@@ -91,9 +93,9 @@ func listRecord(domainRecord *dns.AddDomainRecordArgs) (*dns.RecordTypeNew, erro
 | 
			
		||||
	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
 | 
			
		||||
	for _, r := range resp.DomainRecords.Record {
 | 
			
		||||
		if r.RR == domainRecord.RR {
 | 
			
		||||
			return &r, nil
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil, err
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user