Create & Init Project...
This commit is contained in:
commit
f1dece2573
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module yumc.pw/cloud/ali-ddns-cli
|
||||
|
||||
go 1.12
|
||||
|
||||
require github.com/denverdino/aliyungo v0.0.0-20190410085603-611ead8a6fed
|
2
go.sum
Normal file
2
go.sum
Normal file
@ -0,0 +1,2 @@
|
||||
github.com/denverdino/aliyungo v0.0.0-20190410085603-611ead8a6fed h1:WtFFp2kd7j/ATD3dT5tdjyoXuynxHu6D0AJVG9Be1q4=
|
||||
github.com/denverdino/aliyungo v0.0.0-20190410085603-611ead8a6fed/go.mod h1:dV8lFg6daOBZbT6/BDGIz6Y3WFGn8juu6G+CQ6LHtl0=
|
107
main.go
Normal file
107
main.go
Normal file
@ -0,0 +1,107 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/denverdino/aliyungo/dns"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
var client *dns.Client
|
||||
var domain string
|
||||
var sub string
|
||||
var recordType string
|
||||
var recordValue string
|
||||
|
||||
var domainRecord *dns.AddDomainRecordArgs
|
||||
|
||||
func main() {
|
||||
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()
|
||||
|
||||
resp, err := http.Get("http://ip.3322.org")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
str, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
_ = resp.Body.Close()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
recordValue = string(str)
|
||||
|
||||
domainRecord = &dns.AddDomainRecordArgs{
|
||||
DomainName: domain,
|
||||
RR: sub,
|
||||
Type: recordType,
|
||||
Value: recordValue,
|
||||
}
|
||||
|
||||
record, err := listRecord(domainRecord)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var re interface{}
|
||||
|
||||
if record != nil {
|
||||
re = updateRecord(record.RecordId, domainRecord)
|
||||
} else {
|
||||
re = addRecord(domainRecord)
|
||||
}
|
||||
reByte, err := json.Marshal(re)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(reByte))
|
||||
}
|
||||
|
||||
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 {
|
||||
addResult, _ := client.AddDomainRecord(domainRecord)
|
||||
return addResult
|
||||
}
|
||||
|
||||
func updateRecord(id string, domainRecord *dns.AddDomainRecordArgs) *dns.UpdateDomainRecordResponse {
|
||||
result, _ := client.UpdateDomainRecord(&dns.UpdateDomainRecordArgs{
|
||||
RecordId: id,
|
||||
RR: domainRecord.RR,
|
||||
Type: domainRecord.Type,
|
||||
Value: domainRecord.Value,
|
||||
})
|
||||
return result
|
||||
}
|
Loading…
Reference in New Issue
Block a user