-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliddns.go
127 lines (106 loc) · 2.72 KB
/
aliddns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
log "github.com/sirupsen/logrus"
)
type CONFIG struct {
AccessKeyId string `json:"accessKeyId"`
AccessSecret string `json:"accessSecret"`
RecordID string `json:"RecordID"`
}
var Config CONFIG
func main() {
file, _ := os.Open("./config.json")
defer file.Close()
decoder := json.NewDecoder(file)
err := decoder.Decode(&Config)
if err != nil {
panic(err)
}
go SetDDNSService()
for {
time.Sleep(time.Duration(60) * time.Second)
}
}
func SetDDNSService() {
var WanIP string
var RecordIP string
var RecordRR string
RecordIP, RecordRR = GetAliRecordIP() // 服务器启动时,从阿里云获取一次
for {
WanIP = GetWanIPStr()
log.Info("Get WAN IP: ", WanIP)
if WanIP != "" && WanIP != RecordIP {
log.Info("Wan IP changed. Will change the record IP.")
err := SetDDNS(RecordRR, WanIP)
if err == nil {
RecordIP = WanIP
}
} else {
//log.Info("Wan IP hold.")
}
time.Sleep(time.Duration(60) * time.Second)
}
}
func SetDDNS(RecordRR string, wanIP string) (err error) {
client, err := alidns.NewClientWithAccessKey("cn-hangzhou", Config.AccessKeyId, Config.AccessSecret)
request := alidns.CreateUpdateDomainRecordRequest()
request.Scheme = "https"
request.RecordId = Config.RecordID
request.RR = RecordRR
request.Type = "A"
request.Value = wanIP //GetWanIPStr() //"118.123.37.212"
request.Lang = "en"
request.UserClientIp = wanIP // "118.123.37.211"
request.TTL = "600"
request.Priority = "1"
request.Line = "default"
response, err := client.UpdateDomainRecord(request)
if err != nil {
fmt.Print(err.Error(), response)
return err
}
fmt.Printf("response is %#v\n", response)
return nil
}
func GetAliRecordIP() (recordIP string, recordRR string) {
client, err := alidns.NewClientWithAccessKey("cn-hangzhou", Config.AccessKeyId, Config.AccessSecret)
request := alidns.CreateDescribeDomainRecordInfoRequest()
request.Scheme = "https"
request.RecordId = Config.RecordID
request.Lang = "en"
request.UserClientIp = ""
response, err := client.DescribeDomainRecordInfo(request)
if err != nil {
fmt.Print(err.Error())
return "", ""
}
log.Info("Record IP: ", response.Value)
return response.Value, response.RR
}
func GetWanIPStr() (wanip string) {
cmd := exec.Command("curl", "ident.me")
cmd.Stdin = strings.NewReader("some input")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Error("error: ", err)
return ""
}
//fmt.Printf("in all caps: %q\n", out.String())
wanip = out.String()
if wanip != "" {
//log.Info("Get WAN IP ok: ", wanip)
} else {
log.Warn("Get WAN IP failed")
}
return wanip
}