-
Notifications
You must be signed in to change notification settings - Fork 2
/
dnspod.go
129 lines (122 loc) · 3.39 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
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
package dyndns
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
type dnspodRecordResp struct {
Name string `json:"name,omitempty"`
Line string `json:"line,omitempty"`
Type string `json:"type,omitempty"`
TTL string `json:"ttl,omitempty"`
Value string `json:"value,omitempty"`
Status string `json:"status,omitempty"`
ID string `json:"id,omitempty"`
}
type dnspodResp struct {
Status struct {
Code string `json:"code,omitempty"`
} `json:"status,omitempty"`
Records []dnspodRecordResp `json:"records,omitempty"`
}
func dnspodupd(configs Config) error {
// Switch API endpoint
var dnspodAPIBase, tokenName, defaultRecordLine = "https://dnsapi.cn/", "login_token", "默认"
if configs.Auth.Email == "[email protected]" {
dnspodAPIBase, tokenName, defaultRecordLine = "https://api.dnspod.com/", "user_token", "default"
}
for _, domainName := range configs.Domains {
domainArray := strings.Split(domainName, ".")
domain := domainArray[len(domainArray)-2] + "." + domainArray[len(domainArray)-1]
var subDomain string
if len(domainArray) > 2 {
subDomain = domainName[:len(domainName)-len(domain)-1]
}
// get domain record
v := url.Values{}
v.Set("domain", domain)
v.Set("sub_domain", subDomain)
v.Set("format", "json")
v.Set("record_type", "A")
v.Set(tokenName, configs.Auth.Apikey)
if debug {
log.Println(v)
}
data := ioutil.NopCloser(strings.NewReader(v.Encode()))
// build request
bBody, err := dnspodGetResp(dnspodAPIBase+"Record.List", data)
if err != nil {
return err
}
var resp dnspodResp
err = json.Unmarshal(bBody, &resp)
if err != nil {
return err
}
// create or modify dnspod record
var apiType = "Record.Modify"
v = url.Values{}
v.Set("domain", domain)
v.Set("sub_domain", subDomain)
v.Set("format", "json")
v.Set("record_type", "A")
v.Set(tokenName, configs.Auth.Apikey)
if len(resp.Records) == 0 {
apiType = "Record.Create"
v.Set("record_line", defaultRecordLine)
v.Set("value", configs.Ipupdate)
} else if resp.Records[0].Value != configs.Ipupdate {
v.Set("record_line", defaultRecordLine)
v.Set("value", configs.Ipupdate)
v.Set("status", resp.Records[0].Status)
v.Set("record_id", resp.Records[0].ID)
} else {
return nil
}
if debug {
log.Println(v)
}
data = ioutil.NopCloser(strings.NewReader(v.Encode()))
bBody, err = dnspodGetResp(dnspodAPIBase+apiType, data)
if err != nil {
return err
}
err = json.Unmarshal(bBody, &resp)
if err != nil {
return err
}
if resp.Status.Code != "1" {
return errors.New("Errcode: " + resp.Status.Code)
}
}
return nil
}
func dnspodGetResp(url string, body io.ReadCloser) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("POST", url, body)
// req, err := http.NewRequest("POST", "https://httpbin.org/anything", body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "caddy-ddns/0.0.1 ([email protected])")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if debug {
log.Println(string(bBody))
log.Println(url)
}
return bBody, nil
}