-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathddns.go
More file actions
543 lines (470 loc) · 13.2 KB
/
Copy pathddns.go
File metadata and controls
543 lines (470 loc) · 13.2 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strconv"
"strings"
"time"
"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/freedns"
"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
useFreeDNS bool
token string
user string
passwd string
domain string
subdomain string
dnsType string
command string
configFile string
interval 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.BoolVar(&useFreeDNS, "freedns", false, "Use freedns 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, freedns api
flag.StringVar(&token, "token", "", "godaddy api-key:secret-key, dnspod token, namesilo api-key:secret-key, cloudflare api-token, henet password, freedns token")
// 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")
flag.StringVar(&configFile, "config", "", "Path to config file")
flag.StringVar(&interval, "interval", "", "Interval for timed DDNS updates, e.g. 300 (seconds), 5m, 2h, 1d")
}
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)
switch dnsType {
case "A":
dnspod.DoDNSPODv4(domain, subdomain, token, rid, extIP)
case "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)
}
switch dnsType {
case "A":
godaddy.DoGodaddyv4(domain, subdomain, token, extIP)
case "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
switch dnsType {
case "A":
if len(subdomain) > 0 && len(domain) > 0 {
domain = subdomain + "." + domain
}
f3322.DoF3322v4(domain, extIP)
case "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)
switch dnsType {
case "A":
namesilo.DoNamesiloV4(domain, subdomain, token, rid, extIP)
case "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)
}
switch dnsType {
case "A":
henet.DoHenetv4(domain, subdomain, token, extIP)
case "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
switch dnsType {
case "A":
if len(subdomain) > 0 && len(domain) > 0 {
domain = subdomain + "." + domain
}
oray.DoOrayv4(domain, extIP)
case "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)
}
switch dnsType {
case "A":
alidns.DoAlidnsV4(domain, subdomain, rid, extIP)
case "AAAA":
alidns.DoAlidnsV6(domain, subdomain, rid, extIP)
}
}
func doFreeDNS() {
if len(token) == 0 || len(dnsType) == 0 {
fmt.Println("freedns token/dnstype required")
return
}
var extIP string
if command != "" {
extIP = dnsutils.DoCommand(command)
fmt.Println(extIP)
}
switch dnsType {
case "A":
freedns.DoFreeDNSv4(token, extIP)
case "AAAA":
freedns.DoFreeDNSv6(token, 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记录
switch dnsType {
case "A":
cloudflare.DoCFv4(domain, token, zone_id, rid, extIP)
case "AAAA":
cloudflare.DoCFv6(domain, token, zone_id, rid, extIP)
}
}
// findConfig 从 os.Args 中查找 -config 参数,返回配置文件路径
// 并从 os.Args 中移除 -config 及其值,避免影响后续 flag.Parse()
func findConfig() string {
for i := 0; i < len(os.Args); i++ {
arg := os.Args[i]
if arg == "-config" || arg == "--config" {
if i+1 < len(os.Args) {
cfg := os.Args[i+1]
os.Args = append(os.Args[:i], os.Args[i+2:]...)
return cfg
}
}
if strings.HasPrefix(arg, "-config=") || strings.HasPrefix(arg, "--config=") {
cfg := strings.SplitN(arg, "=", 2)[1]
os.Args = append(os.Args[:i], os.Args[i+1:]...)
return cfg
}
}
return ""
}
// loadConfig 从配置文件中读取参数并设置对应的变量。
// 配置文件每行一个参数,支持 # 开头的注释行,
// 格式为 key=value(如 domain=example.com)或 key(布尔值设为 true)。
// 参数名称与命令行参数名称一致(不需要前面的 - 前缀)。
func loadConfig(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
lineNo := 0
for scanner.Scan() {
lineNo++
line := strings.TrimSpace(scanner.Text())
// 跳过空行和注释行
if line == "" || strings.HasPrefix(line, "#") {
continue
}
var key, value string
if idx := strings.Index(line, "="); idx != -1 {
key = strings.TrimSpace(line[:idx])
value = strings.TrimSpace(line[idx+1:])
} else {
key = line
value = "true"
}
switch key {
case "godaddy":
useGodaddy = value == "true"
case "dnspod":
useDnspod = value == "true"
case "f3322":
useF3322 = value == "true"
case "oray":
useOray = value == "true"
case "namesilo":
useNamesilo = value == "true"
case "henet":
useHenet = value == "true"
case "ali":
useAlidns = value == "true"
case "cloudflare":
useCloudFlare = value == "true"
case "freedns":
useFreeDNS = value == "true"
case "externalIPv4":
dnsutils.FetchIPv4AddrUrl = value
case "externalIPv6":
dnsutils.FetchIPv6AddrUrl = value
case "token":
token = value
case "user":
user = value
case "passwd":
passwd = value
case "domain":
domain = value
case "subdomain":
subdomain = value
case "dnstype":
dnsType = value
case "command":
command = value
case "interval":
interval = value
default:
fmt.Printf("config: unknown key %q at line %d, ignored\n", key, lineNo)
}
}
return scanner.Err()
}
// parseInterval 解析时间间隔字符串,返回对应的 time.Duration。
// 支持格式:纯数字(秒)、数字+m(分钟)、数字+h(小时)、数字+d(天)。
func parseInterval(s string) (time.Duration, error) {
s = strings.TrimSpace(s)
if s == "" {
return 0, fmt.Errorf("empty interval")
}
var multiplier int64 = 1
var numStr string
if strings.HasSuffix(s, "d") {
multiplier = 86400
numStr = s[:len(s)-1]
} else if strings.HasSuffix(s, "h") {
multiplier = 3600
numStr = s[:len(s)-1]
} else if strings.HasSuffix(s, "m") {
multiplier = 60
numStr = s[:len(s)-1]
} else {
numStr = s
}
n, err := strconv.ParseInt(numStr, 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid interval %q: %v", s, err)
}
if n <= 0 {
return 0, fmt.Errorf("interval must be positive, got %d", n)
}
return time.Duration(n*multiplier) * time.Second, nil
}
func doSelectedProvider() {
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 if useFreeDNS { // freedns api
doFreeDNS()
} else {
fmt.Println("No api selected")
}
}
func main() {
// 先查找并加载配置文件(如果指定了 -config)
if cfgPath := findConfig(); cfgPath != "" {
if err := loadConfig(cfgPath); err != nil {
fmt.Fprintf(os.Stderr, "Error loading config file %s: %v\n", cfgPath, err)
os.Exit(1)
}
}
flag.Parse()
if help || len(os.Args) == 1 {
// 如果是通过配置文件设置了参数,len(os.Args)==1 时仍然应该继续执行
if configFile == "" && !useGodaddy && !useDnspod && !useF3322 && !useOray &&
!useNamesilo && !useHenet && !useAlidns && !useCloudFlare && !useFreeDNS {
flag.Usage()
return
}
}
if interval != "" {
// 定时模式:按指定间隔循环调用
d, err := parseInterval(interval)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing interval: %v\n", err)
os.Exit(1)
}
fmt.Printf("Starting timed DDNS updates every %s\n", d)
for {
doSelectedProvider()
time.Sleep(d)
}
} else {
// 单次模式:执行一次后退出
doSelectedProvider()
}
}