feat: optimize function and add Dockerfile
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
f1dece2573
commit
bb3b879aa5
5
Dockerfile
Normal file
5
Dockerfile
Normal file
@ -0,0 +1,5 @@
|
||||
FROM miaowoo/alpine-glibc
|
||||
|
||||
ADD ali-ddns-cli /
|
||||
|
||||
CMD [ "/ali-ddns-cli" ]
|
80
main.go
80
main.go
@ -1,13 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/denverdino/aliyungo/dns"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/denverdino/aliyungo/dns"
|
||||
)
|
||||
|
||||
var client *dns.Client
|
||||
@ -16,9 +17,12 @@ var sub string
|
||||
var recordType string
|
||||
var recordValue string
|
||||
|
||||
var domainRecord *dns.AddDomainRecordArgs
|
||||
|
||||
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")
|
||||
|
||||
@ -30,48 +34,52 @@ func main() {
|
||||
recordValue = *flag.String("value", "127.0.0.1", "Redis Server Host")
|
||||
|
||||
flag.Parse()
|
||||
recordValue = getLocalIP()
|
||||
fmt.Println("Local IP: " + recordValue)
|
||||
|
||||
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{
|
||||
return &dns.AddDomainRecordArgs{
|
||||
DomainName: domain,
|
||||
RR: sub,
|
||||
Type: recordType,
|
||||
Value: recordValue,
|
||||
}
|
||||
}
|
||||
|
||||
func updateDNSRecord(domainRecord *dns.AddDomainRecordArgs) {
|
||||
record, err := listRecord(domainRecord)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
panic("List Record ERR: " + err.Error())
|
||||
}
|
||||
|
||||
var re interface{}
|
||||
|
||||
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 {
|
||||
re = addRecord(domainRecord)
|
||||
}
|
||||
reByte, err := json.Marshal(re)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
_, err = addRecord(domainRecord)
|
||||
fmt.Println("Add Record " + domainRecord.RR + " Value " + domainRecord.Value)
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -91,17 +99,15 @@ func listRecord(domainRecord *dns.AddDomainRecordArgs) (*dns.RecordTypeNew, erro
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func addRecord(domainRecord *dns.AddDomainRecordArgs) *dns.AddDomainRecordResponse {
|
||||
addResult, _ := client.AddDomainRecord(domainRecord)
|
||||
return addResult
|
||||
func addRecord(domainRecord *dns.AddDomainRecordArgs) (*dns.AddDomainRecordResponse, error) {
|
||||
return client.AddDomainRecord(domainRecord)
|
||||
}
|
||||
|
||||
func updateRecord(id string, domainRecord *dns.AddDomainRecordArgs) *dns.UpdateDomainRecordResponse {
|
||||
result, _ := client.UpdateDomainRecord(&dns.UpdateDomainRecordArgs{
|
||||
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,
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user