-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
336 lines (252 loc) · 6.76 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"main/model"
"main/services"
"main/services/api"
"os"
"path"
"path/filepath"
"strings"
)
var currentPwd, _ = filepath.Abs(filepath.Dir(os.Args[0]))
// 在切片中查找元素
func findInSlice(length int, f func(i int) bool) int {
for i := 0; i < length; i++ {
if f(i) {
return i
}
}
return -1
}
func deleteHostInCloudFlare(config *model.Config, recordlist []model.DNSRecord, hosts []string) {
for _, record := range recordlist {
fmt.Printf("当前域名为: %s , ip: %s , type: %s \n", record.Name, record.Content, record.Type)
findIndex := findInSlice(len(hosts), func(i int) bool {
fmt.Println(hosts[i], record.Name, strings.EqualFold(hosts[i], record.Name))
if strings.EqualFold(hosts[i], record.Name) {
return true
}
return false
})
// 符合条件的清除
if findIndex != -1 {
switch record.Type {
case "A":
fallthrough
case "AAAA":
id, err := api.DeleteDNSRecord(config, record.ID)
if err != nil {
fmt.Println(err)
continue
}
fmt.Printf("删除成功id: %s", id)
default:
}
}
}
}
func createDDNS(config *model.Config, ips services.IPs, zoneID string, ipv6NeighborList []services.Ipv6NeighborInfo) {
tmp := &tmpJSON{}
for _, dns := range config.Ddns {
host := dns.Host
mac := dns.Mac
if dns.IsCurrent { // 当前路由器
// 当前路由器的ddns配置
if len(ips.IPv4) != 0 {
// 有ipv4时创建dns
result, err := api.CreateDNSRecord(config, zoneID, "A", host, ips.IPv4[0], true)
if err != nil {
fmt.Printf("创建失败 %s", err)
continue
}
tmp.Ddns = append(tmp.Ddns, tmpDDNS{
Host: host,
IP: ips.IPv4[0],
})
fmt.Printf("创建成功, %#v", result)
} else if len(ips.IPv6) != 0 {
fmt.Printf("length for ipv6 :%v , %v \n\n", len(ips.IPv6), ips.IPv6)
// 有ipv6时创建dns
result, err := api.CreateDNSRecord(config, zoneID, "AAAA", host, ips.IPv6[0], true)
if err != nil {
fmt.Printf("创建失败 %s", err)
continue
}
tmp.Ddns = append(tmp.Ddns, tmpDDNS{
Host: host,
IP: ips.IPv6[0],
})
fmt.Printf("创建成功, %#v", result)
}
} else if mac != "" { // 路由器下面的子主机并且mac地址存在
findMacIndex := findInSlice(len(ipv6NeighborList), func(i int) bool {
if strings.EqualFold(ipv6NeighborList[i].Mac, mac) {
return true
}
return false
})
if findMacIndex != -1 {
// 创建dns
result, err := api.CreateDNSRecord(config, zoneID, "AAAA", dns.Host, ipv6NeighborList[findMacIndex].Addr, true)
if err != nil {
fmt.Printf("创建失败 %s", err)
continue
}
tmp.Ddns = append(tmp.Ddns, tmpDDNS{
Host: dns.Host,
IP: ipv6NeighborList[findMacIndex].Addr,
})
fmt.Printf("创建成功, %#v", result)
}
}
}
saveTmpFile(tmp)
}
type tmpDDNS struct {
Host string `json:"host"`
IP string `json:"ip"`
}
// 临时文件结构体
type tmpJSON struct {
Ddns []tmpDDNS `json:"ddns"`
}
func readTempFile() (jsonData *tmpJSON, err error) {
tmpFilePath := path.Join(currentPwd, "./cloudflare.ddns.tmp.json")
fileObj, err := os.OpenFile(tmpFilePath, os.O_CREATE|os.O_RDWR, 0x644)
if err != nil {
fmt.Println("文件读取失败", err)
return jsonData, err
}
defer fileObj.Close()
file, err := ioutil.ReadAll(fileObj)
if err != nil {
fmt.Println("文件读取失败", err)
return
}
jsonData = &tmpJSON{}
if string(file) != "" {
err = json.Unmarshal(file, jsonData)
if err != nil {
fmt.Println("临时文件json解析失败", err)
return jsonData, err
}
}
return jsonData, err
}
// 判断是否有记录发生改变
func hasChangedRecordInTempFile(oldData *tmpJSON, config *model.Config, ips services.IPs, ipv6NeighborInfo []services.Ipv6NeighborInfo) bool {
if len(oldData.Ddns) > 0 {
findIndex := findInSlice(len(config.Ddns), func(i int) bool {
if config.Ddns[i].IsCurrent {
ip := ""
if len(ips.IPv4) != 0 {
ip = ips.IPv4[0]
} else if len(ips.IPv6) != 0 {
ip = ips.IPv6[0]
}
hasChanged := findInSlice(len(oldData.Ddns), func(j int) bool {
return oldData.Ddns[j].Host == config.Ddns[i].Host && oldData.Ddns[j].IP != ip
}) != -1
if hasChanged {
return true
}
} else {
findCurrentHostIndex := findInSlice(len(ipv6NeighborInfo), func(i int) bool {
return ipv6NeighborInfo[i].Mac == config.Ddns[i].Mac
})
if findCurrentHostIndex != -1 {
hasChanged := findInSlice(len(oldData.Ddns), func(j int) bool {
return oldData.Ddns[j].Host == config.Ddns[i].Host && oldData.Ddns[j].IP != ipv6NeighborInfo[findCurrentHostIndex].Addr
}) != -1
if hasChanged {
return true
}
}
}
return false
})
// 只要有一个不一样,则
if findIndex != -1 {
return true
}
}
return false
}
func saveTmpFile(tmp *tmpJSON) {
data, err := json.Marshal(tmp)
if err != nil {
return
}
tmpFilePath := path.Join(currentPwd, "./cloudflare.ddns.tmp.json")
err = ioutil.WriteFile(tmpFilePath, data, 0x644)
if err != nil {
return
}
fmt.Println("历史ddns记录已保存", err)
}
func task() {
configFilePath := path.Join(currentPwd, "./config.json")
config, err := services.LoadConfig(configFilePath)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%v", config)
hosts := config.GetConfigAllHost()
// 读取本地ip
ips, err := services.GetCurrentIPs()
if err != nil {
fmt.Println(err)
}
// 读取邻居ip
ipv6NeighborList, err := services.GetIpv6NeighborList()
if err != nil {
fmt.Println(err)
}
fmt.Println(ipv6NeighborList)
// 先判断当前dns是否已经发生变化,发生变化再继续往下执行替换
oldData, err := readTempFile()
if err != nil {
fmt.Println("读取临时文件出错")
return
}
// 如果临时文件,没有数据记录,则直接往下执行,有数据则对比 clouflare 的数据
hasChanged := hasChangedRecordInTempFile(oldData, config, ips, ipv6NeighborList)
if !hasChanged {
return
}
zoneList, err := api.GetZoneRecord(config)
if err != nil {
fmt.Println(err)
return
}
if len(zoneList) == 0 {
fmt.Println("当前用户没有zone列表,请检查cloudflare")
return
}
findIndex := findInSlice(len(zoneList), func(i int) bool {
if strings.EqualFold(zoneList[i].Name, config.DomainName) {
return true
}
return false
})
if findIndex == -1 {
fmt.Println("在cloudflare未找到当前域名,任务结束")
return
}
zoneID := zoneList[findIndex].ID
recordlist, err := api.GetDNSRecord(config, zoneID, "AAAA")
if err != nil {
fmt.Println(err)
return
}
// 先删除包含在内的域名
deleteHostInCloudFlare(config, recordlist, hosts)
createDDNS(config, ips, zoneID, ipv6NeighborList)
}
func main() {
task()
}