-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
439 lines (406 loc) · 15.6 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
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
package main
import (
"crypto/tls"
"dnspod-ddns/db"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net"
"net/http"
"os"
"os/exec"
"strings"
"sync"
"time"
tencentErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/regions"
dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323"
)
type Config struct {
SecretId string `json:"secretId"`
SecretKey string `json:"secretKey"`
HttpRecord bool `json:"httpRecord"`
H3Port json.Number `json:"h3Port"`
Domains map[string][]string `json:"domains"`
Ips map[string]string `json:"ips"`
}
func contains(sa []string, i string) bool {
for _, a := range sa {
_, a = parseSubdomain(a)
if a == i {
return true
}
}
return false
}
var confFilePath = flag.String("c", "dns.json", "配置文件路径")
var cachePath = flag.String("d", "/tmp/dns.Cache", "缓存文件路径")
var config Config
var rateLimiter = make(chan struct{}, 80)
func initRateLimiter() {
ticker := time.NewTicker(time.Second)
go func() {
for range ticker.C {
for i := 0; i < 80; i++ {
select {
case rateLimiter <- struct{}{}:
default:
// 如果通道已满,不做任何操作
}
}
}
}()
}
func main() {
initRateLimiter()
flag.Parse()
fmt.Printf("[%s] starting ...\n", time.Now().Format("2006-01-02 15:04:05"))
configFile, err := os.Open(*confFilePath)
if err != nil {
fmt.Println(err)
return
}
defer func() { _ = configFile.Close() }()
byteValue, _ := io.ReadAll(configFile)
file, err := os.Stat(*confFilePath)
if err != nil {
fmt.Println(err)
}
modTime := file.ModTime().Unix()
_ = json.Unmarshal(byteValue, &config)
dbh := db.DB{Root: *cachePath}
credential := common.NewCredential(
config.SecretId,
config.SecretKey,
)
client, _ := dnspod.NewClient(credential, regions.Guangzhou, profile.NewClientProfile())
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client.WithHttpTransport(tr)
remarks := make(map[string]string)
remarkNames := make([]string, len(remarks))
recordCounter := make(map[string]uint)
// 获取本地IP
for remark, cmd := range config.Ips {
out, err := exec.Command("sh", "-c", cmd).Output()
if err != nil {
fmt.Printf("%s", err)
}
remarks[remark] = strings.TrimSuffix(string(out), "\n")
remarkNames = append(remarkNames, remark)
fmt.Printf("[%s] got remark: %s, IP: %s", time.Now().Format("2006-01-02 15:04:05"), remark, out)
}
var cacheTime int64
_ = dbh.Get(&cacheTime, "cacheTime")
// 获取Dnspod已有配置,设置备注并缓存
var success = true
var limit uint64 = 3000
for domain, subDomains := range config.Domains {
s := strings.Split(subDomains[0], "#")
section := dbh.Section(domain, s[0])
if len(section.List()) < 1 || cacheTime < modTime {
fmt.Printf("[%s] cache timeout\n", time.Now().Format("2006-01-02 15:04:05"))
describeDomainRequest := dnspod.NewDescribeDomainRequest()
describeDomainRequest.Domain = &domain
<-rateLimiter
describeDomainResponse, err := client.DescribeDomain(describeDomainRequest)
var tencentCloudSDKError *tencentErrors.TencentCloudSDKError
if errors.As(err, &tencentCloudSDKError) {
fmt.Printf("An API error has returned: %s", err)
continue
}
if describeDomainResponse == nil || describeDomainResponse.Response == nil || describeDomainResponse.Response.DomainInfo == nil {
fmt.Printf("Empty DomainInfo returned: %v", describeDomainResponse)
continue
}
domainInfo := describeDomainResponse.Response.DomainInfo
_ = dbh.Put(domainInfo, domain)
describeRecordListRequest := dnspod.NewDescribeRecordListRequest()
describeRecordListRequest.Domain = &domain
describeRecordListRequest.Limit = &limit
<-rateLimiter
describeRecordListResponse, err := client.DescribeRecordList(describeRecordListRequest)
if errors.As(err, &tencentCloudSDKError) {
fmt.Printf("An API error has returned: %s", err)
continue
}
if describeRecordListResponse == nil || describeRecordListResponse.Response == nil || len(describeRecordListResponse.Response.RecordList) < 1 {
fmt.Printf("Empty RecordList returned: %v", describeRecordListResponse)
continue
}
records := describeRecordListResponse.Response.RecordList
wg := sync.WaitGroup{}
for _, record := range records {
if strings.ToUpper(*record.Status) == `ENABLE` && contains(subDomains, *record.Name) {
recordId := *record.Type + "-" + *record.Remark
recordCounter[recordId+"."+domain]++
section := dbh.Section(domain, *record.Name)
if *record.Remark != "" {
// 更新记录缓存
wg.Add(1)
go makeRecordCache(client, domainInfo, record.RecordId, section, record.Remark, &wg)
} else {
remark := remarkNames[recordCounter[recordId+"."+domain]-1]
fmt.Printf("[%s] Updating %s.%s with remark %s\n", time.Now().Format("2006-01-02 15:04:05"), *record.Name, domain, remark) // 未有此记录,需要更新
modifyRecordRemarkRequest := dnspod.NewModifyRecordRemarkRequest()
modifyRecordRemarkRequest.Domain = &domain
modifyRecordRemarkRequest.Remark = &remark
modifyRecordRemarkRequest.RecordId = record.RecordId
<-rateLimiter
_, err := client.ModifyRecordRemark(modifyRecordRemarkRequest)
if err != nil {
fmt.Printf("update failed: %s\n", err)
} else {
wg.Add(1)
go makeRecordCache(client, domainInfo, record.RecordId, section, &remark, &wg)
}
}
}
}
wg.Wait()
checkDns(subDomains, &dbh, domain, remarks, domainInfo, client)
//domainsInfo, _, _ := client.Domains.List(&dnspod.DomainSearchParam{Keyword: domain})
//if len(domainsInfo) > 0 && domain == domainsInfo[0].Name {
// _ = dbh.Put(domainsInfo[0], domain)
// records, _, _ := client.Records.List(string(domainsInfo[0].ID), "", "A")
// if len(records) > 0 {
//
// }
//
//}
} else {
fmt.Printf("[%s] cache used\n", time.Now().Format("2006-01-02 15:04:05"))
domainInfo := &dnspod.DomainInfo{}
err = dbh.Get(&domainInfo, domain)
if err != nil {
fmt.Printf("[%s] get %s info failed\n", time.Now().Format("2006-01-02 15:04:05"), domain)
success = false
continue
}
checkDns(subDomains, &dbh, domain, remarks, domainInfo, client)
}
}
// success and put cacheTime
if success {
_ = dbh.Put(time.Now().Unix(), "cacheTime")
} else {
_ = dbh.Put(0, "cacheTime")
}
fmt.Printf("[%s] end\n", time.Now().Format("2006-01-02 15:04:05"))
}
func checkDns(subDomains []string, db *db.DB, domain string, remarks map[string]string, domainInfo *dnspod.DomainInfo, client *dnspod.Client) {
fmt.Printf("[%s] check domain %s %v\n", time.Now().Format("2006-01-02 15:04:05"), domain, subDomains)
for _, subDomain := range subDomains {
rmk, subDomain := parseSubdomain(subDomain)
section := db.Section(domain, subDomain)
createWg := sync.WaitGroup{}
updateWg := sync.WaitGroup{}
for remark, ip := range remarks {
if len(rmk) > 0 && rmk != remark {
continue
}
// 本地无缓存
var record dnspod.RecordInfo
lSubDomain := subDomain
lIp := ip
lRemark := remark
dnsType := getDNSType(ip)
if config.HttpRecord && dnsType == "A" {
var record dnspod.RecordInfo
lSubDomain := subDomain
lIp := ip
lRemark := remark
recordId := "HTTPS" + "-" + remark
if err := section.Get(recordId, &record); err != nil {
createWg.Add(1)
go createHttpsRecord(&lSubDomain, domainInfo, &lIp, client, section, &lRemark, &createWg)
} else if !strings.Contains(*record.Value, lIp) && record.Id != nil { // 本地有缓存且IP已改变
updateWg.Add(1)
go updateHttpsRecord(&lSubDomain, domainInfo, &lIp, client, &record, section, &lRemark, &updateWg)
}
}
recordId := dnsType + "-" + remark
if err := section.Get(recordId, &record); err != nil {
createWg.Add(1)
go createRecord(&lSubDomain, domainInfo, &lIp, client, section, &lRemark, &createWg)
} else if lIp != *record.Value && record.Id != nil { // 本地有缓存且IP已改变
updateWg.Add(1)
go updateRecord(&lSubDomain, domainInfo, &lIp, client, &record, section, &lRemark, &updateWg)
} else {
fmt.Printf("[%s] %s.%s[%s] IP无变化\n", time.Now().Format("2006-01-02 15:04:05"), lSubDomain, domain, lRemark)
}
}
createWg.Wait()
updateWg.Wait()
}
}
func updateRecord(subDomain *string, domainInfo *dnspod.DomainInfo, ip *string, client *dnspod.Client, record *dnspod.RecordInfo, section *db.Section, remark *string, wg *sync.WaitGroup) {
defer wg.Done()
fmt.Printf("[%s] Updating %s.%s[%s], IP:%s\n", time.Now().Format("2006-01-02 15:04:05"), *subDomain, *domainInfo.Domain, *remark, *ip)
modifyRecordRequest := dnspod.NewModifyRecordRequest()
modifyRecordRequest.Domain = domainInfo.Domain
modifyRecordRequest.RecordType = record.RecordType
modifyRecordRequest.RecordLine = record.RecordLine
modifyRecordRequest.Value = ip
modifyRecordRequest.RecordId = record.Id
modifyRecordRequest.SubDomain = subDomain
modifyRecordRequest.RecordLineId = record.RecordLineId
modifyRecordRequest.Remark = record.Remark
<-rateLimiter
_, err := client.ModifyRecord(modifyRecordRequest)
if err != nil {
fmt.Printf("update failed: %s\n", err)
req, _ := json.Marshal(modifyRecordRequest)
fmt.Printf("modifyRecordRequest: %s\n", req)
} else {
makeRecordCache(client, domainInfo, record.Id, section, remark, nil)
}
}
func updateHttpsRecord(subDomain *string, domainInfo *dnspod.DomainInfo, ip *string, client *dnspod.Client, record *dnspod.RecordInfo, section *db.Section, remark *string, wg *sync.WaitGroup) {
defer wg.Done()
value := fmt.Sprintf(`%s.%s. alpn="h3" ipv4hint="%s" port="%s"`, *subDomain, *domainInfo.Domain, *ip, config.H3Port)
if *subDomain == "@" {
value = fmt.Sprintf(`%s. alpn="h3" ipv4hint="%s" port="%s"`, *domainInfo.Domain, *ip, config.H3Port)
}
fmt.Printf("[%s] Updating %s.%s[%s], value:%s\n", time.Now().Format("2006-01-02 15:04:05"), *subDomain, *domainInfo.Domain, *remark, value)
modifyRecordRequest := dnspod.NewModifyRecordRequest()
modifyRecordRequest.Domain = domainInfo.Domain
modifyRecordRequest.RecordType = record.RecordType
modifyRecordRequest.RecordLine = record.RecordLine
modifyRecordRequest.MX = record.MX
modifyRecordRequest.Value = &value
modifyRecordRequest.RecordId = record.Id
modifyRecordRequest.SubDomain = subDomain
modifyRecordRequest.RecordLineId = record.RecordLineId
modifyRecordRequest.Remark = record.Remark
<-rateLimiter
_, err := client.ModifyRecord(modifyRecordRequest)
if err != nil {
fmt.Printf("update failed: %s\n", err)
req, _ := json.Marshal(modifyRecordRequest)
fmt.Printf("modifyRecordRequest: %s\n", req)
} else {
makeRecordCache(client, domainInfo, record.Id, section, remark, nil)
}
}
func createRecord(subDomain *string, domainInfo *dnspod.DomainInfo, ip *string, client *dnspod.Client, section *db.Section, remark *string, wg *sync.WaitGroup) {
defer wg.Done()
fmt.Printf("[%s] creating %s.%s[%s] , IP:%s\n", time.Now().Format("2006-01-02 15:04:05"), *subDomain, *domainInfo.Domain, *remark, *ip) // 未有此记录,需要创建
recordType := getDNSType(*ip)
recordLine := "默认"
createRecordRequest := dnspod.NewCreateRecordRequest()
createRecordRequest.SubDomain = subDomain
createRecordRequest.Domain = domainInfo.Domain
createRecordRequest.RecordType = &recordType
createRecordRequest.RecordLine = &recordLine
createRecordRequest.Value = ip
createRecordRequest.Remark = remark
<-rateLimiter
createRecordResponse, err := client.CreateRecord(createRecordRequest)
var tencentCloudSDKError *tencentErrors.TencentCloudSDKError
if errors.As(err, &tencentCloudSDKError) {
fmt.Printf("An API error has returned: %s\n", err)
req, _ := json.Marshal(createRecordRequest)
fmt.Printf("createRecordRequest: %s\n", req)
return
}
if createRecordResponse == nil || createRecordResponse.Response == nil || createRecordResponse.Response.RecordId == nil {
fmt.Printf("Empty RecordId returned: %v", createRecordResponse)
return
}
if err == nil {
makeRecordCache(client, domainInfo, createRecordResponse.Response.RecordId, section, remark, nil)
}
}
func createHttpsRecord(subDomain *string, domainInfo *dnspod.DomainInfo, ip *string, client *dnspod.Client, section *db.Section, remark *string, wg *sync.WaitGroup) {
defer wg.Done()
value := fmt.Sprintf(`%s.%s. alpn="h3" ipv4hint="%s" port="%s"`, *subDomain, *domainInfo.Domain, *ip, config.H3Port)
if *subDomain == "@" {
value = fmt.Sprintf(`%s. alpn="h3" ipv4hint="%s" port="%s"`, *domainInfo.Domain, *ip, config.H3Port)
}
fmt.Printf("[%s] creating %s.%s[%s] , value:%s\n", time.Now().Format("2006-01-02 15:04:05"), *subDomain, *domainInfo.Domain, *remark, value) // 未有此记录,需要创建
recordType := "HTTPS"
recordLine := "默认"
var mx uint64 = 1
createRecordRequest := dnspod.NewCreateRecordRequest()
createRecordRequest.SubDomain = subDomain
createRecordRequest.Domain = domainInfo.Domain
createRecordRequest.RecordType = &recordType
createRecordRequest.RecordLine = &recordLine
createRecordRequest.Value = &value
createRecordRequest.Remark = remark
createRecordRequest.MX = &mx
<-rateLimiter
createRecordResponse, err := client.CreateRecord(createRecordRequest)
var tencentCloudSDKError *tencentErrors.TencentCloudSDKError
if errors.As(err, &tencentCloudSDKError) {
fmt.Printf("An API error has returned: %s\n", err)
req, _ := json.Marshal(createRecordRequest)
fmt.Printf("createHttpsRecord: %s\n", req)
return
}
if createRecordResponse == nil || createRecordResponse.Response == nil || createRecordResponse.Response.RecordId == nil {
fmt.Printf("Empty RecordId returned: %v", createRecordResponse)
return
}
if err == nil {
makeRecordCache(client, domainInfo, createRecordResponse.Response.RecordId, section, remark, nil)
}
}
func makeRecordCache(client *dnspod.Client, domainInfo *dnspod.DomainInfo, recordId *uint64, section *db.Section, remark *string, wg *sync.WaitGroup) {
if wg != nil {
//fmt.Printf("[%s] defer wg.Done()\n", time.Now().Format("2006-01-02 15:04:05"))
defer wg.Done()
}
record, err := getRecordInfo(domainInfo.Domain, recordId, client)
if err != nil {
return
}
fmt.Printf("[%s] Cached [%s] %s.%s[%s]\n", time.Now().Format("2006-01-02 15:04:05"), *record.RecordType, *record.SubDomain, *domainInfo.Domain, *remark)
_recordId := *record.RecordType + "-" + *remark
_ = section.Put(_recordId, record)
}
func parseSubdomain(subDomain string) (remark, domain string) {
remark = ""
domain = ""
s := strings.Split(subDomain, "#")
if len(s) > 1 {
domain = s[0]
remark = s[1]
} else {
domain = s[0]
}
return
}
func getRecordInfo(domain *string, recordId *uint64, client *dnspod.Client) (*dnspod.RecordInfo, error) {
var tencentCloudSDKError *tencentErrors.TencentCloudSDKError
describeRecordRequest := dnspod.NewDescribeRecordRequest()
describeRecordRequest.Domain = domain
describeRecordRequest.RecordId = recordId
<-rateLimiter
describeRecordResponse, err := client.DescribeRecord(describeRecordRequest)
if errors.As(err, &tencentCloudSDKError) {
fmt.Printf("An API error has returned: %s", err)
return nil, err
}
if describeRecordResponse == nil || describeRecordResponse.Response == nil || describeRecordResponse.Response.RecordInfo == nil {
fmt.Printf("Empty RecordInfo returned: %v", describeRecordResponse)
return nil, err
}
return describeRecordResponse.Response.RecordInfo, nil
}
func getDNSType(value string) string {
value = strings.TrimSpace(value)
if ip := net.ParseIP(value); ip != nil {
if strings.Contains(value, ":") {
return "AAAA"
}
return "A"
}
return ""
}