-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
204 lines (178 loc) · 4.84 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"flag"
"fmt"
"net"
"strings"
"time"
_ "github.com/l1b0k/aliyun-ddns/addr/ipify"
_ "github.com/l1b0k/aliyun-ddns/addr/myip"
"github.com/l1b0k/aliyun-ddns/addr/types"
"github.com/l1b0k/aliyun-ddns/version"
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
"github.com/golang/glog"
)
type MatchSet struct {
DomainName string
RR string
Type string
Value string
}
type UpdateSet struct {
RecordId string
RR string
Type string
Value string
}
// upstream host to sync
func upstream(provider types.Interface, domains string) []MatchSet {
realIPs, err := provider.Fetch()
if err != nil {
glog.Warningf("get addr failed %s", err.Error())
return nil
}
if realIPs.IPv4 == nil && realIPs.IPv6 == nil {
glog.Warningf("unable to found addr for %s")
return nil
}
// build matchset
var matchSet []MatchSet
rrs := strings.Split(domains, ",")
for _, rr := range rrs {
if realIPs.IPv6 != nil {
matchSet = append(matchSet, MatchSet{
RR: rr,
Type: "AAAA",
Value: realIPs.IPv6.String(),
})
}
if realIPs.IPv4 != nil {
matchSet = append(matchSet, MatchSet{
RR: rr,
Type: "A",
Value: realIPs.IPv4.String(),
})
}
}
return matchSet
}
func sync(client *alidns.Client, domainName string, matchSet []MatchSet) {
request := alidns.CreateDescribeDomainRecordsRequest()
request.Scheme = "https"
request.AcceptFormat = "json"
request.DomainName = domainName
response, err := client.DescribeDomainRecords(request)
if err != nil {
glog.Errorf("can not get record from aliyun dns %s", err.Error())
return
}
// matchSet is what we expected
// updateSet is what we will do update
// createSet is (matchSet - updateSet) that is we will add
// 1) add all to createSet
createSet := make(map[MatchSet]struct{}, 2)
for _, wantedRecord := range matchSet {
createSet[wantedRecord] = struct{}{}
}
var updateSet []UpdateSet
for _, existed := range response.DomainRecords.Record {
for _, wanted := range matchSet {
if existed.Type != wanted.Type {
continue
}
if existed.RR != wanted.RR {
continue
}
// check semantics
existedIP := net.ParseIP(existed.Value)
if existedIP != nil && existedIP.Equal(net.ParseIP(wanted.Value)) {
delete(createSet, wanted)
continue
}
toUpdate := UpdateSet{
RecordId: existed.RecordId,
RR: existed.RR,
Type: existed.Type,
Value: wanted.Value,
}
updateSet = append(updateSet, toUpdate)
// remove from createSet
delete(createSet, wanted)
}
}
for _, up := range updateSet {
updateRequest := alidns.CreateUpdateDomainRecordRequest()
updateRequest.Scheme = "https"
updateRequest.AcceptFormat = "json"
updateRequest.RecordId = up.RecordId
updateRequest.Type = up.Type
updateRequest.RR = up.RR
updateRequest.Value = up.Value
updateResp, err := client.UpdateDomainRecord(updateRequest)
if err != nil {
glog.Errorf("can not update record for aliyun dns, %s", err.Error())
return
}
glog.Infof("update resolve record (%s %s %s) %s\n", domainName, up.RR, up.Value, updateResp.String())
}
for add := range createSet {
addRequest := alidns.CreateAddDomainRecordRequest()
addRequest.Scheme = "https"
addRequest.AcceptFormat = "json"
addRequest.Type = add.Type
addRequest.RR = add.RR
addRequest.Value = add.Value
addRequest.DomainName = domainName
createResp, err := client.AddDomainRecord(addRequest)
if err != nil {
glog.Errorf("can not add record for aliyun dns, %s", err.Error())
return
}
glog.Infof("create resolve record (%s %s %s) %s\n", domainName, add.RR, add.Value, createResp.String())
}
}
func init() {
flag.Set("logtostderr", "true")
}
func main() {
ak := flag.String("ak", "", "aliyun ak.")
sk := flag.String("sk", "", "aliyun sk.")
regionID := flag.String("region-id", "cn-hangzhou", "aliyun regionId default cn-hangzhou.")
domainName := flag.String("domain-name", "", "domain registered in aliyun.")
domainRR := flag.String("domain-rr", "", "comma separated list eg. @,www .")
provider := flag.String("provider", "ipify", "provider to get public IP. eg. ipify")
versionFlag := flag.Bool("version", false, "print version")
flag.Parse()
if *versionFlag {
fmt.Println(version.Print())
return
}
glog.Infof("version %s", version.Print())
client, err := alidns.NewClientWithAccessKey(*regionID, *ak, *sk)
if err != nil {
glog.Fatal(err)
}
p, ok := types.Plugins[*provider]
if !ok {
glog.Fatal(fmt.Sprintf("can not found provider %s", *provider))
}
// sync immediately
func() {
matchSet := upstream(p(), *domainRR)
if matchSet != nil {
sync(client, *domainName, matchSet)
}
}()
// aliyun has minimum TTL 600 (s)
t := time.NewTicker(11 * time.Minute)
defer t.Stop()
for {
select {
case <-t.C:
matchSet := upstream(p(), *domainRR)
if matchSet != nil {
sync(client, *domainName, matchSet)
}
}
}
}