-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddns.go
337 lines (281 loc) · 8.33 KB
/
ddns.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package main
import (
"flag"
"fmt"
"os"
"github.com/Jackarain/ddns/alidns"
"github.com/Jackarain/ddns/cloudflare"
"github.com/Jackarain/ddns/dnspod"
"github.com/Jackarain/ddns/dnsutils"
"github.com/Jackarain/ddns/f3322"
"github.com/Jackarain/ddns/godaddy"
"github.com/Jackarain/ddns/henet"
"github.com/Jackarain/ddns/namesilo"
"github.com/Jackarain/ddns/oray"
)
var (
help bool
useGodaddy bool
useDnspod bool
useF3322 bool
useOray bool
useNamesilo bool
useHenet bool
useAlidns bool
useCloudFlare bool
token string
user string
passwd string
domain string
subdomain string
dnsType string
command string
)
func init() {
flag.BoolVar(&help, "help", false, "help message")
flag.BoolVar(&useGodaddy, "godaddy", false, "Use godaddy api")
flag.BoolVar(&useDnspod, "dnspod", false, "Use dnspod api")
flag.BoolVar(&useF3322, "f3322", false, "Use f3322 api")
flag.BoolVar(&useOray, "oray", false, "Use oray api")
flag.BoolVar(&useNamesilo, "namesilo", false, "Use namesilo api")
flag.BoolVar(&useHenet, "henet", false, "Use henet api")
flag.BoolVar(&useAlidns, "ali", false, "Use alidns api")
flag.BoolVar(&useCloudFlare, "cloudflare", false, "Use cloudflare api")
flag.StringVar(&dnsutils.FetchIPv4AddrUrl, "externalIPv4", "", "Provide a URL to get the external IPv4 address")
flag.StringVar(&dnsutils.FetchIPv6AddrUrl, "externalIPv6", "", "Provide a URL to get the external IPv6 address")
// token 用于 dnspod, godaddy, namesilo, cloudflare, henet api
flag.StringVar(&token, "token", "", "godaddy api-key:secret-key, dnspod token, namesilo api-key:secret-key, cloudflare api-token, henet password")
// user, passwd 用于 f3322/oray/ali api
flag.StringVar(&user, "user", "", "f3322/oray/ali username only")
flag.StringVar(&passwd, "passwd", "", "f3322/oray/ali password only")
flag.StringVar(&domain, "domain", "", "Main domain")
flag.StringVar(&subdomain, "subdomain", "", "Sub domain")
flag.StringVar(&dnsType, "dnstype", "A", "dns type, AAAA/A")
flag.StringVar(&command, "command", "", "Use command's output as IP address")
}
func doDnspod() {
if len(domain) == 0 || len(subdomain) == 0 || len(token) == 0 || len(dnsType) == 0 {
fmt.Println("dnspod domain/subdomain/token/dnstype required")
return
}
ridFileName := subdomain + dnsType
rid, err := dnsutils.FileReadString(ridFileName)
if err != nil || rid == "" {
rid, err = dnspod.FetchRecordID(token, domain, subdomain, dnsType)
if err != nil {
fmt.Println("FetchRecordID: " + err.Error())
return
}
dnsutils.FileWriteString(ridFileName, rid)
}
var extIP string
if command != "" {
extIP = dnsutils.DoCommand(command)
fmt.Println(extIP)
}
fmt.Println(subdomain, "dnspod record id:", rid)
if dnsType == "A" {
dnspod.DoDNSPODv4(domain, subdomain, token, rid, extIP)
} else if dnsType == "AAAA" {
dnspod.DoDNSPODv6(domain, subdomain, token, rid, extIP)
}
}
func doGodaddy() {
if len(domain) == 0 || len(subdomain) == 0 || len(token) == 0 || len(dnsType) == 0 {
fmt.Println("godaddy domain/subdomain/token/dnstype required")
return
}
var extIP string
if command != "" {
extIP = dnsutils.DoCommand(command)
fmt.Println(extIP)
}
if dnsType == "A" {
godaddy.DoGodaddyv4(domain, subdomain, token, extIP)
} else if dnsType == "AAAA" {
godaddy.DoGodaddyv6(domain, subdomain, token, extIP)
}
}
func doF3322() {
if len(domain) == 0 || len(user) == 0 || len(passwd) == 0 || len(dnsType) == 0 {
fmt.Println("f3322 domain/user/passwd/dnstype required")
return
}
var extIP string
if command != "" {
extIP = dnsutils.DoCommand(command)
fmt.Println(extIP)
}
f3322.User = user
f3322.Passwd = passwd
if dnsType == "A" {
if len(subdomain) > 0 && len(domain) > 0 {
domain = subdomain + "." + domain
}
f3322.DoF3322v4(domain, extIP)
} else if dnsType == "AAAA" {
fmt.Println("f3322 doesn’t work with ipv6")
}
}
func doNamesilo() {
if len(domain) == 0 || len(subdomain) == 0 || len(token) == 0 || len(dnsType) == 0 {
fmt.Println("namesilo domain/subdomain/token/dnstype required")
return
}
ridFileName := subdomain + dnsType
rid, err := dnsutils.FileReadString(ridFileName)
if err != nil || rid == "" {
rid, err = namesilo.FetchRecordID(token, domain, subdomain)
if err != nil {
fmt.Println("FetchRecordID: " + err.Error())
return
}
dnsutils.FileWriteString(ridFileName, rid)
}
var extIP string
if command != "" {
extIP = dnsutils.DoCommand(command)
fmt.Println(extIP)
}
fmt.Println(subdomain, "namesilo record id:", rid)
if dnsType == "A" {
namesilo.DoNamesiloV4(domain, subdomain, token, rid, extIP)
} else if dnsType == "AAAA" {
namesilo.DoNamesiloV6(domain, subdomain, token, rid, extIP)
}
}
func doHenet() {
if len(domain) == 0 || len(subdomain) == 0 || len(token) == 0 || len(dnsType) == 0 {
fmt.Println("henet domain/subdomain/token/dnstype required")
return
}
var extIP string
if command != "" {
extIP = dnsutils.DoCommand(command)
fmt.Println(extIP)
}
if dnsType == "A" {
henet.DoHenetv4(domain, subdomain, token, extIP)
} else if dnsType == "AAAA" {
henet.DoHenetv6(domain, subdomain, token, extIP)
}
}
func doOray() {
if len(domain) == 0 || len(subdomain) == 0 || len(user) == 0 || len(passwd) == 0 || len(dnsType) == 0 {
fmt.Println("oray domain/subdomain/user/passwd/dnstype required")
return
}
var extIP string
if command != "" {
extIP = dnsutils.DoCommand(command)
fmt.Println(extIP)
}
oray.User = user
oray.Passwd = passwd
if dnsType == "A" {
if len(subdomain) > 0 && len(domain) > 0 {
domain = subdomain + "." + domain
}
oray.DoOrayv4(domain, extIP)
} else if dnsType == "AAAA" {
fmt.Println("oray doesn’t work with ipv6")
}
}
func doAlidns() {
if len(domain) == 0 || len(subdomain) == 0 || len(user) == 0 || len(passwd) == 0 || len(dnsType) == 0 {
fmt.Println("alidns domain/subdomain/user/passwd/dnstype required")
return
}
var extIP string
if command != "" {
extIP = dnsutils.DoCommand(command)
fmt.Println(extIP)
}
alidns.User = user
alidns.Passwd = passwd
// 从文件中读取record id
ridFileName := subdomain + dnsType
rid, err := dnsutils.FileReadString(ridFileName)
if err != nil || rid == "" {
rid, err = alidns.FetchRecordID(domain)
if err != nil {
fmt.Println("FetchRecordID: " + err.Error())
return
}
dnsutils.FileWriteString(ridFileName, rid)
}
if dnsType == "A" {
alidns.DoAlidnsV4(domain, subdomain, rid, extIP)
} else if dnsType == "AAAA" {
alidns.DoAlidnsV6(domain, subdomain, rid, extIP)
}
}
func doCloudFlare() {
if len(domain) == 0 || len(subdomain) == 0 || len(token) == 0 || len(dnsType) == 0 {
fmt.Println("cloudflare domain/subdomain/token/dnstype required")
return
}
// 首先从文件中读取 zone_id, 并缓存到本地文件.
zoneIDFileName := subdomain + "zone_id"
zone_id, err := dnsutils.FileReadString(zoneIDFileName)
if err != nil || zone_id == "" {
zone_id, err = cloudflare.FetchZoneID(domain, token)
if err != nil {
fmt.Println("FetchZoneID: " + err.Error())
return
}
dnsutils.FileWriteString(zoneIDFileName, zone_id)
}
// 如果指定了command, 则使用command的输出内容作为公网ip
var extIP string
if command != "" {
extIP = dnsutils.DoCommand(command)
fmt.Println(extIP)
}
if len(subdomain) > 0 && len(domain) > 0 {
domain = subdomain + "." + domain
}
// 从文件中读取record id
ridFileName := subdomain + dnsType
rid, err := dnsutils.FileReadString(ridFileName)
if err != nil || rid == "" {
rid, err = cloudflare.FetchRecordID(zone_id, token, domain)
if err != nil {
fmt.Println("FetchRecordID: " + err.Error())
return
}
dnsutils.FileWriteString(ridFileName, rid)
}
// 根据dnsType选择更新A记录或AAAA记录
if dnsType == "A" {
cloudflare.DoCFv4(domain, token, zone_id, rid, extIP)
} else if dnsType == "AAAA" {
cloudflare.DoCFv6(domain, token, zone_id, rid, extIP)
}
}
func main() {
flag.Parse()
if help || len(os.Args) == 1 {
flag.Usage()
return
}
if useDnspod { // dnspod api
doDnspod()
} else if useGodaddy { // godaddy api
doGodaddy()
} else if useF3322 { // f3322 api
doF3322()
} else if useOray { // oray api
doOray()
} else if useNamesilo { // namesilo api
doNamesilo()
} else if useHenet { // henet api
doHenet()
} else if useAlidns { // alidns api
doAlidns()
} else if useCloudFlare { // cloudflare api
doCloudFlare()
} else {
fmt.Println("No api selected")
}
}