1
0

feat: optimize function and add Dockerfile

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-04-30 18:31:00 +08:00
parent f1dece2573
commit bb3b879aa5
2 changed files with 48 additions and 37 deletions

5
Dockerfile Normal file
View File

@ -0,0 +1,5 @@
FROM miaowoo/alpine-glibc
ADD ali-ddns-cli /
CMD [ "/ali-ddns-cli" ]

80
main.go
View File

@ -1,13 +1,14 @@
package main package main
import ( import (
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"github.com/denverdino/aliyungo/dns"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"strings"
"github.com/denverdino/aliyungo/dns"
) )
var client *dns.Client var client *dns.Client
@ -16,9 +17,12 @@ var sub string
var recordType string var recordType string
var recordValue string var recordValue string
var domainRecord *dns.AddDomainRecordArgs
func main() { func main() {
domainRecord := parseArgs()
updateDNSRecord(domainRecord)
}
func parseArgs() *dns.AddDomainRecordArgs {
id := flag.String("id", os.Getenv("ALI_DNS_ID"), "AliId") id := flag.String("id", os.Getenv("ALI_DNS_ID"), "AliId")
key := flag.String("key", os.Getenv("ALI_DNS_KEY"), "AliKey") key := flag.String("key", os.Getenv("ALI_DNS_KEY"), "AliKey")
@ -30,48 +34,52 @@ func main() {
recordValue = *flag.String("value", "127.0.0.1", "Redis Server Host") recordValue = *flag.String("value", "127.0.0.1", "Redis Server Host")
flag.Parse() flag.Parse()
recordValue = getLocalIP()
fmt.Println("Local IP: " + recordValue)
resp, err := http.Get("http://ip.3322.org") return &dns.AddDomainRecordArgs{
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, DomainName: domain,
RR: sub, RR: sub,
Type: recordType, Type: recordType,
Value: recordValue, Value: recordValue,
} }
}
func updateDNSRecord(domainRecord *dns.AddDomainRecordArgs) {
record, err := listRecord(domainRecord) record, err := listRecord(domainRecord)
if err != nil { if err != nil {
panic(err) panic("List Record ERR: " + err.Error())
} }
var re interface{}
if record != nil { if record != nil {
re = updateRecord(record.RecordId, domainRecord) 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 { } else {
re = addRecord(domainRecord) _, err = addRecord(domainRecord)
} fmt.Println("Add Record " + domainRecord.RR + " Value " + domainRecord.Value)
reByte, err := json.Marshal(re)
if err != nil {
panic(err)
} }
fmt.Println(string(reByte)) 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) { func listRecord(domainRecord *dns.AddDomainRecordArgs) (*dns.RecordTypeNew, error) {
@ -91,17 +99,15 @@ func listRecord(domainRecord *dns.AddDomainRecordArgs) (*dns.RecordTypeNew, erro
return nil, err return nil, err
} }
func addRecord(domainRecord *dns.AddDomainRecordArgs) *dns.AddDomainRecordResponse { func addRecord(domainRecord *dns.AddDomainRecordArgs) (*dns.AddDomainRecordResponse, error) {
addResult, _ := client.AddDomainRecord(domainRecord) return client.AddDomainRecord(domainRecord)
return addResult
} }
func updateRecord(id string, domainRecord *dns.AddDomainRecordArgs) *dns.UpdateDomainRecordResponse { func updateRecord(id string, domainRecord *dns.AddDomainRecordArgs) (*dns.UpdateDomainRecordResponse, error) {
result, _ := client.UpdateDomainRecord(&dns.UpdateDomainRecordArgs{ return client.UpdateDomainRecord(&dns.UpdateDomainRecordArgs{
RecordId: id, RecordId: id,
RR: domainRecord.RR, RR: domainRecord.RR,
Type: domainRecord.Type, Type: domainRecord.Type,
Value: domainRecord.Value, Value: domainRecord.Value,
}) })
return result
} }