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
|
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/cookiejar"
"regexp"
"strings"
"github.com/tidwall/gjson"
)
const (
ZONE_IDENTIFIER = "XXX"
XAuthEmail = "XXX"
XAuthKey = "XXX"
)
// 初始化http请求的client端以复用
func initiateHttpClient() *http.Client {
client := &http.Client{}
return client
}
// 通过HTTP的GET请求获取identifier和ip
func getIdentifierAndIp(name string, dnsType string, httpClient *http.Client) (identifier string, ip string, err error) {
dnsRecordsListUrl := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records?name=%s.littleghost.ml&type=%s", ZONE_IDENTIFIER, name, dnsType)
request, err := http.NewRequest("GET", dnsRecordsListUrl, nil) // 写的时候GET写成了Get,导致网页返回400,排除错误用了我半个小时...
if err != nil {
log.Print(err)
return "", "", err
}
request.Header.Add("X-Auth-Email", XAuthEmail)
request.Header.Add("X-Auth-Key", XAuthKey)
request.Header.Add("Content-Type", "application/json")
response, err := httpClient.Do(request)
if err != nil {
log.Print(err)
return "", "", err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Print(err)
return "", "", err
}
result := gjson.Get(string(body), "result").Array()[0].String() // 结果是gjson.Result
id := gjson.Get(result, "id").String()
ip = gjson.Get(result, "content").String()
return id, ip, nil
}
// 获取本机的IPv4和IPv6地址
func getNativeIp(ipType string) (ip string, err error) {
if ipType == "ipv4" {
jar, _ := cookiejar.New(nil)
httpClient := &http.Client{
Jar: jar,
}
loginUrl := "http://192.168.1.1/"
req, _ := http.NewRequest("POST", loginUrl, strings.NewReader(`{"method":"do","login":{"password":"XXX"}}`))
res, httpError := httpClient.Do(req)
if httpError != nil {
log.Print(httpError)
err = httpError
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
stok := gjson.Get(string(body), "stok").String()
contentUrl := fmt.Sprintf("http://192.168.1.1/stok=%s/ds", stok)
req, _ = http.NewRequest("POST", contentUrl, strings.NewReader(`{"network":{"name":["wan_status"]},"method":"get"}`))
res, httpError = httpClient.Do(req)
if httpError != nil {
log.Print(httpError)
err = httpError
}
defer res.Body.Close()
body, _ = ioutil.ReadAll(res.Body)
ip = gjson.Get(string(body), "network").Get("wan_status").Get("ipaddr").String()
return ip, nil
} else if ipType == "ipv6" {
addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Println(err)
return "", err
}
for _, address := range addrs {
ipnet, ok := address.(*net.IPNet)
if ok && !ipnet.IP.IsLoopback() {
ip = ipnet.IP.String()
matchFlag, err := regexp.MatchString("2001", ip)
if err != nil {
fmt.Println(err)
return "", err
}
if ip != "" && matchFlag {
break
}
}
}
}
return ip, nil
}
// 检查ip是否需要更新
func checkIp(identifier string, ip string, ipType string, httpClient *http.Client) (nativeIp string, updateFlag bool) {
if ipType == "ipv4" {
nativeIpv4, err := getNativeIp("ipv4")
if err != nil {
fmt.Println(err)
updateFlag = false
}
if ip != nativeIpv4 {
nativeIp = nativeIpv4
updateFlag = true
}
} else if ipType == "ipv6" {
nativeIpv6, err := getNativeIp("ipv6")
if err != nil {
fmt.Println(err)
updateFlag = false
}
if ip != nativeIpv6 {
nativeIp = nativeIpv6
updateFlag = true
}
}
return
}
// 通过HTTP的PUT请求更新ip
func updateCloudflareDns(identifier string, name string, dnsType string, ip string, httpClient *http.Client) (err error) {
type updateDnsPostForm struct {
Name string `json:"name"`
DnsType string `json:"type"`
Content string `json:"content"`
Ttl int `json:"ttl"`
Proxied bool `json:"proxied"`
}
updateDnsUrl := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records/%s", ZONE_IDENTIFIER, identifier)
postForm := updateDnsPostForm{
Name: name,
DnsType: dnsType,
Content: ip,
Ttl: 1,
Proxied: false,
}
postBody, err := json.Marshal(postForm)
if err != nil {
log.Print(err)
return err
}
request, err := http.NewRequest("PUT", updateDnsUrl, bytes.NewBuffer(postBody))
if err != nil {
log.Print(err)
return err
}
request.Header.Add("X-Auth-Email", XAuthEmail)
request.Header.Add("X-Auth-Key", XAuthKey)
request.Header.Add("Content-Type", "application/json")
response, err := httpClient.Do(request)
if err != nil {
log.Print(err)
return err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Print(err)
return err
}
fmt.Println(string(body))
return nil
}
// 程序入口
func main() {
httpClient := initiateHttpClient()
identifier, ip, err := getIdentifierAndIp("lab6", "AAAA", httpClient)
if err != nil {
fmt.Println("Can't get identifier")
return
}
nativeIp, updateFlag := checkIp(identifier, ip, "ipv6", httpClient)
if updateFlag {
updateCloudflareDns(identifier, "lab6", "AAAA", nativeIp, httpClient)
}
identifier, ip, err = getIdentifierAndIp("lab", "A", httpClient)
if err != nil {
fmt.Println("Can't get identifier")
return
}
nativeIp, updateFlag = checkIp(identifier, ip, "ipv4", httpClient)
if updateFlag {
updateCloudflareDns(identifier, "lab", "A", nativeIp, httpClient)
}
}
|