forked from ma6174/myhttpdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnspod.go
67 lines (63 loc) · 1.19 KB
/
dnspod.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
package main
import (
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
func NewDnspodCli(timeout time.Duration) *DnspodCli {
return &DnspodCli{
cli: &http.Client{Timeout: timeout},
}
}
type DnspodCli struct {
cli *http.Client
}
func (d *DnspodCli) Query(domain string) (info *TTLInfo) {
info = &TTLInfo{
Domain: domain,
TTLTo: time.Now().Add(time.Second * 3),
}
if strings.HasSuffix(domain, ".") {
domain = domain[:len(domain)-1]
}
query := url.Values{}
query.Set("dn", domain)
query.Set("ttl", "1")
u := "http://119.29.29.29/d?" + query.Encode()
resp, err := d.cli.Get(u)
if err != nil {
info.Err = err
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
info.Err = err
return
}
sp := strings.Split(string(body), ",")
info.Records = strings.Split(sp[0], ";")
if len(sp) < 2 {
return
}
if len(info.Records) > 20 {
info.Records = info.Records[:20]
}
ttl, err := strconv.Atoi(sp[1])
if err != nil {
info.Err = err
return
}
if ttl > 3600 {
ttl = 3600
}
if ttl <= 0 || len(info.Records) == 0 {
ttl = 3
}
info.TTLTo = time.Now().Add(time.Duration(ttl) * time.Second)
info.TTL = uint32(ttl)
return
}