-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
215 lines (183 loc) · 4.63 KB
/
api.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
package main
import (
"encoding/xml"
"fmt"
"io"
"log"
"net/http"
"strings"
)
// 获取 DNS 记录列表
func GetDNSRecords(result *DNSRecordsResult) {
url := GlobalConfig.Url.Base + GlobalConfig.Url.Get
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
q := req.URL.Query()
q.Add("version", GlobalConfig.Version)
q.Add("type", GlobalConfig.Type)
q.Add("key", GlobalConfig.Key)
q.Add("domain", GlobalConfig.Domain)
req.URL.RawQuery = q.Encode()
log.Println("【查询】", req.URL.String())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
err = xml.Unmarshal(body, result)
if err != nil {
log.Fatal(err)
}
result.Reply.RecordListLog()
}
// 获取更新记录值时需要传的 host, 保持原 host 不变
func (rr *ResourceRecord) GetUpdateHost() {
if rr.Host == GlobalConfig.Domain {
rr.Host = ""
} else {
rr.Host = strings.Split(rr.Host, "."+GlobalConfig.Domain)[0]
}
}
// 更新 DNS 记录
func (rr *ResourceRecord) UpdateDNSRecord() {
url := GlobalConfig.Url.Base + GlobalConfig.Url.Update
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
q := req.URL.Query()
q.Add("version", GlobalConfig.Version)
q.Add("type", GlobalConfig.Type)
q.Add("key", GlobalConfig.Key)
q.Add("domain", GlobalConfig.Domain)
q.Add("rrid", rr.RecordId)
q.Add("rrhost", rr.Host)
q.Add("rrvalue", rr.Value)
q.Add("rrttl", rr.Ttl)
q.Add("rrdistance", rr.Distance)
req.URL.RawQuery = q.Encode()
log.Println("【更新】", req.URL.String())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
result := &DNSRecordsOperateResult{}
err = xml.Unmarshal(body, result)
if err != nil {
log.Fatal(err)
}
result.RecordOperateLog(rr, "Update")
}
// 添加 DNS 记录
func (rr *ResourceRecord) AddDNSRecord() {
url := GlobalConfig.Url.Base + GlobalConfig.Url.Add
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
q := req.URL.Query()
q.Add("version", GlobalConfig.Version)
q.Add("type", GlobalConfig.Type)
q.Add("key", GlobalConfig.Key)
q.Add("domain", GlobalConfig.Domain)
q.Add("rrtype", rr.Type)
q.Add("rrhost", rr.Host)
q.Add("rrvalue", rr.Value)
q.Add("rrttl", rr.Ttl)
q.Add("rrdistance", rr.Distance)
req.URL.RawQuery = q.Encode()
log.Println("【新增】", req.URL.String())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
result := &DNSRecordsOperateResult{}
err = xml.Unmarshal(body, result)
if err != nil {
log.Fatal(err)
}
result.RecordOperateLog(rr, "Add")
}
// 删除 DNS 记录
func (rr *ResourceRecord) DeleteDNSRecord() {
url := GlobalConfig.Url.Base + GlobalConfig.Url.Delete
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
q := req.URL.Query()
q.Add("version", GlobalConfig.Version)
q.Add("type", GlobalConfig.Type)
q.Add("key", GlobalConfig.Key)
q.Add("domain", GlobalConfig.Domain)
q.Add("rrid", rr.RecordId)
req.URL.RawQuery = q.Encode()
log.Println("【删除】", req.URL.String())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
result := &DNSRecordsOperateResult{}
err = xml.Unmarshal(body, result)
if err != nil {
log.Fatal(err)
}
result.RecordOperateLog(rr, "Delete")
}
// 动态解析本地 IPv6 地址到 namesilo
func (rr *ResourceRecord) LocalPublicIPv6DDNS() {
// ip 变化则更新记录值
if isLatest := InList(IPs, rr.Value); isLatest {
// 记录值已是最新 ip
rr.RecordIsLatestLog()
} else {
fmt.Println(rr)
// 更新记录值
rr.Value = TargetIP
rr.UpdateDNSRecord()
}
}
// 配置中需要初始化添加的记录值
func InitAddDNSRecord() {
for _, record := range GlobalConfig.Records {
if InList(ResourceRecordHosts, record.Host) {
log.Printf(`【配置记录值初始化】Host: "%s" 已存在于记录值列表,将自动忽略`, record.Host)
continue
}
if len(record.Type) == 0 || len(record.Value) == 0 || len(record.Distance) == 0 {
log.Fatalln("ConfigError: record 中 type, value, ttl 为必填项")
}
r := ResourceRecord{}
r.Type = record.Type
r.Host = record.Host
r.Value = record.Value
r.Ttl = record.Ttl
r.Distance = record.Distance
r.AddDNSRecord()
}
}