This repository has been archived by the owner on Feb 16, 2019. It is now read-only.
/
dnspodd.go
413 lines (371 loc) · 9.3 KB
/
dnspodd.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
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path"
"sort"
"strings"
"sync"
"time"
"github.com/caiguanhao/dnspodd/vendor/diffmatchpatch"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
const (
GIST_DNS_FILENAME = "DNS"
LINE_FORMAT = "%-8s %-20s %-5s %-6s %-20s %-15s %s\n"
DNSPODD_MAX_RETRIES = 2
GIST_MAX_RETRIES = 2
)
type (
Status struct {
Code string `json:"code"`
CreatedAt string `json:"created_at"`
Message string `json:"message"`
}
DomainList struct {
Domains []struct {
CnameSpeedup string `json:"cname_speedup"`
CreatedOn string `json:"created_on"`
ExtStatus string `json:"ext_status"`
Grade string `json:"grade"`
GradeTitle string `json:"grade_title"`
GroupID string `json:"group_id"`
ID int `json:"id"`
IsMark string `json:"is_mark"`
IsVip string `json:"is_vip"`
Name string `json:"name"`
Owner string `json:"owner"`
Punycode string `json:"punycode"`
Records string `json:"records"`
Remark string `json:"remark"`
SearchenginePush string `json:"searchengine_push"`
Status string `json:"status"`
TTL string `json:"ttl"`
UpdatedOn string `json:"updated_on"`
} `json:"domains"`
Info struct {
AllTotal int `json:"all_total"`
DomainTotal int `json:"domain_total"`
ErrorTotal int `json:"error_total"`
IsmarkTotal int `json:"ismark_total"`
LockTotal int `json:"lock_total"`
MineTotal int `json:"mine_total"`
PauseTotal int `json:"pause_total"`
ShareOutTotal int `json:"share_out_total"`
ShareTotal int `json:"share_total"`
SpamTotal int `json:"spam_total"`
VipExpire int `json:"vip_expire"`
VipTotal int `json:"vip_total"`
} `json:"info"`
Status Status `json:"status"`
}
RecordList struct {
Domain struct {
Grade string `json:"grade"`
ID int `json:"id"`
Name string `json:"name"`
Owner string `json:"owner"`
Punycode string `json:"punycode"`
} `json:"domain"`
Info struct {
RecordTotal string `json:"record_total"`
SubDomains string `json:"sub_domains"`
} `json:"info"`
Records []Record `json:"records"`
Status Status `json:"status"`
}
Record struct {
DomainName string
Enabled string `json:"enabled"`
ID string `json:"id"`
Line string `json:"line"`
MonitorStatus string `json:"monitor_status"`
Mx string `json:"mx"`
Name string `json:"name"`
Remark string `json:"remark"`
Status string `json:"status"`
TTL string `json:"ttl"`
Type string `json:"type"`
UpdatedOn string `json:"updated_on"`
UseAqb string `json:"use_aqb"`
Value string `json:"value"`
}
ByNormal []Record
)
func (t ByNormal) Len() int { return len(t) }
func (t ByNormal) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t ByNormal) Less(i, j int) bool {
if t[i].Type == t[j].Type {
if t[i].DomainName == t[j].DomainName {
if t[i].Name == t[j].Name {
if t[i].Value == t[j].Value {
return t[i].UpdatedOn < t[j].UpdatedOn
}
return t[i].Value < t[j].Value
}
return t[i].Name < t[j].Name
}
return t[i].DomainName < t[j].DomainName
}
return t[i].Type < t[j].Type
}
func getListOfDomains() (*DomainList, error) {
data := url.Values{
"login_email": {DNSPOD_EMAIL},
"login_password": {DNSPOD_PASSWORD},
"format": {"json"},
}
resp, err := http.PostForm("https://dnsapi.cn/Domain.List", data)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
list := DomainList{}
err = json.Unmarshal(body, &list)
if err != nil {
return nil, err
}
if list.Status.Code != "1" {
return nil, errors.New(list.Status.Message)
}
return &list, nil
}
func getDomainRecordInfoById(id int) (*RecordList, error) {
data := url.Values{
"login_email": {DNSPOD_EMAIL},
"login_password": {DNSPOD_PASSWORD},
"format": {"json"},
"domain_id": {fmt.Sprintf("%d", id)},
}
resp, err := http.PostForm("https://dnsapi.cn/Record.List", data)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
list := RecordList{}
err = json.Unmarshal(body, &list)
if err != nil {
return nil, err
}
if list.Status.Code != "1" {
return nil, errors.New(list.Status.Message)
}
return &list, nil
}
func isEnabled(input string) string {
if input == "1" {
return "Yes"
}
return "No"
}
func generateDNSTable() (*string, error) {
list, err := getListOfDomains()
if err != nil {
return nil, err
}
var allRecords []Record
var wg sync.WaitGroup
for _, domain := range list.Domains {
wg.Add(1)
go func(id int, name string) {
defer wg.Done()
records, _ := getDomainRecordInfoById(id)
for _, record := range records.Records {
record.DomainName = name
allRecords = append(allRecords, record)
}
if isVerbose {
log.Printf("fetched %d records from %s", len(records.Records), name)
}
}(domain.ID, domain.Name)
}
wg.Wait()
sort.Sort(ByNormal(allRecords))
var ret string
ret += fmt.Sprintf(LINE_FORMAT,
"Enabled",
"Updated At",
"TTL",
"Type",
"Domain",
"Name",
"Value",
)
for _, record := range allRecords {
ret += fmt.Sprintf(LINE_FORMAT,
isEnabled(record.Enabled),
record.UpdatedOn,
record.TTL,
record.Type,
record.DomainName,
record.Name,
record.Value,
)
}
return &ret, nil
}
func getOldDNSTable() (*string, error) {
if isVerbose {
log.Print("fetching list from github")
}
var gist *github.Gist
var err error
for tried := 0; tried < GIST_MAX_RETRIES; tried++ {
gist, _, err = githubClient.Gists.Get(GIST_ID)
if err == nil {
break
}
if isVerbose {
log.Print("retrying fetching list")
}
}
if err != nil {
return nil, err
}
if isVerbose {
log.Printf("fetched list from github (%s)", *gist.HTMLURL)
}
return gist.Files[GIST_DNS_FILENAME].Content, nil
}
func makeDNSTableDiff(oldTable, newTable string) (ret string, count int) {
dmp := diffmatchpatch.New()
oldTableLines, newTableLines, lines := dmp.DiffLinesToChars(oldTable, newTable)
lineBasedDiffs := dmp.DiffCleanupSemantic(dmp.DiffMain(oldTableLines, newTableLines, false))
lineBasedDiffs = dmp.DiffCharsToLines(lineBasedDiffs, lines)
line := 1
for _, diff := range lineBasedDiffs {
switch diff.Type {
case diffmatchpatch.DiffInsert:
ret += fmt.Sprintf("line %d added:\n", line)
ret += diff.Text + "\n"
count++
case diffmatchpatch.DiffDelete:
ret += fmt.Sprintf("line %d deleted:\n", line)
ret += diff.Text + "\n"
count++
default:
line += strings.Count(diff.Text, "\n")
}
}
return
}
var githubClient *github.Client
var isVerbose bool
func init() {
flag.BoolVar(&isVerbose, "v", false, "")
flag.BoolVar(&isVerbose, "verbose", false, "")
flag.Usage = func() {
fmt.Println(path.Base(os.Args[0]), "- Print DNSPOD table changes")
fmt.Println()
fmt.Println("Options:")
fmt.Println(" -v, --verbose Show more output")
fmt.Println()
fmt.Println("Source: https://github.com/caiguanhao/dnspodd")
}
flag.Parse()
githubClient = github.NewClient(oauth2.NewClient(oauth2.NoContext, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: string(GITHUB_TOKEN)},
)))
if PROXY_URL != "" {
proxy, err := url.Parse(PROXY_URL)
if err == nil {
http.DefaultTransport = &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
if req.URL.Host == "api.github.com" {
return proxy, nil
}
return nil, nil
},
}
}
}
}
func main() {
var oldTable *string
var newTable *string
var oldTableErr error
var newTableErr error
for tried := 0; tried < DNSPODD_MAX_RETRIES; tried++ {
if isVerbose {
log.Print("fetching dns table")
}
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
oldTable, oldTableErr = getOldDNSTable()
}()
go func() {
defer wg.Done()
newTable, newTableErr = generateDNSTable()
}()
wg.Wait()
if oldTableErr != nil {
log.Fatalln(oldTableErr)
}
if newTableErr != nil {
log.Fatalln(newTableErr)
}
if strings.Compare(*oldTable, *newTable) == 0 {
if isVerbose {
log.Printf("found no changes to the DNS table")
}
return
}
if isVerbose {
log.Print("retrying fetching dns table")
}
time.Sleep(time.Second * 5)
}
diff, diffCount := makeDNSTableDiff(*oldTable, *newTable)
fmt.Printf("found %d changes to the DNS table:\n", diffCount)
fmt.Println()
fmt.Println(strings.TrimSpace(diff))
if isVerbose {
log.Printf("updating table")
}
var gist *github.Gist
var err error
updated := github.Gist{
Files: map[github.GistFilename]github.GistFile{
GIST_DNS_FILENAME: github.GistFile{
Content: newTable,
},
},
}
for tried := 0; tried < GIST_MAX_RETRIES; tried++ {
gist, _, err = githubClient.Gists.Edit(GIST_ID, &updated)
if err == nil {
break
}
if isVerbose {
log.Print("retrying updating table")
}
}
if err != nil {
log.Fatalln(err)
}
if isVerbose {
log.Printf("table updated (%s)", *gist.HTMLURL)
} else {
fmt.Println()
fmt.Printf("For more info, visit %s\n", *gist.HTMLURL)
}
os.Exit(1)
}