-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
335 lines (299 loc) · 8.42 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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/go-ping/ping"
log "github.com/sirupsen/logrus"
)
var tls string
var port string
var globalIP string
var resultList []LatencyResult
var blockList = make(map[string]bool)
type DomainInfo struct {
Subdomain string `json:"subdomain"`
Domain string `json:"domain"`
Ports []string `json:"ports"`
}
type Config struct {
Email string `json:"email"`
Key string `json:"key"`
DomainInfos [][]string `json:"domainInfos"`
}
type LatencyResult struct {
Latency int `json:"latency"`
IP string `json:"ip"`
LossRate float64 `json:"lossRate"`
}
func getLatency(ip string, count int) LatencyResult {
var latency int = 9999
var lossRate float64 = 1.00
var lossRateStr string
pinger, err := ping.NewPinger(ip)
if err != nil {
logStr := fmt.Sprintf("ping 错误: %s", err)
log.Info(logStr)
return LatencyResult{Latency: 9999, IP: ip, LossRate: 1.00}
}
pinger.SetPrivileged(true)
pinger.Count = count
pinger.Timeout = 1000 * 1000 * 1000 // 1秒
pinger.Run()
stats := pinger.Statistics()
if stats.PacketLoss < 35 {
latency = int(stats.AvgRtt.Seconds() * 1000)
lossRateStr = fmt.Sprintf("%.2f", stats.PacketLoss)
lossRate, _ = strconv.ParseFloat(lossRateStr, 64)
return LatencyResult{Latency: latency, IP: ip, LossRate: lossRate}
} else {
return LatencyResult{Latency: latency, IP: ip, LossRate: lossRate}
}
}
func getResultList(content string, blockArea string) []LatencyResult {
var wg sync.WaitGroup
ipList := strings.Split(strings.Trim(content, "\n"), "\n")
resultChan := make(chan LatencyResult, len(ipList))
for _, ip := range ipList {
if strings.Contains(ip, "#") {
parts := strings.Split(ip, "#")
ip = parts[0]
if blockArea != "" && strings.Contains(parts[1], blockArea) {
continue
}
}
wg.Add(1)
go func(ip string) {
defer wg.Done()
resultChan <- getLatency(ip, 4)
}(ip)
}
wg.Wait()
close(resultChan)
for result := range resultChan {
if result.Latency > 999 {
continue
}
resultList = append(resultList, result)
}
return resultList
}
func getIP(resultList []LatencyResult) string {
sort.Slice(resultList, func(i, j int) bool {
return resultList[i].Latency < resultList[j].Latency
})
for _, item := range resultList {
if _, exists := blockList[item.IP]; !exists && item.LossRate <= 0.35 {
lossRateStr := fmt.Sprintf("%.2f", item.LossRate)
logStr := fmt.Sprintf("所选ip-%s的丢包率为: %s, 延时为:%dms", item.IP, lossRateStr, item.Latency)
log.Info(logStr)
return item.IP
}
}
return ""
}
func uploadIP(ip, name string, domain string, email string, key string) {
retry := 0
url := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones?name=%s", domain)
header := map[string]string{
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36",
"Content-Type": "application/json",
"X-Auth-Email": email,
"X-Auth-Key": key,
}
client := &http.Client{Timeout: 15 * time.Second}
for retry < 5 {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
logStr := fmt.Sprintf("第%d次访问 %s 失败: %s", retry, url, err)
log.Info(logStr)
retry++
continue
}
for key, value := range header {
req.Header.Set(key, value)
}
resp, err := client.Do(req)
if err != nil {
logStr := fmt.Sprintf("第%d次创建 %s 访问出错: %s", retry, url, err)
log.Info(logStr)
retry++
continue
}
body, err := io.ReadAll(resp.Body)
if err != nil {
logStr := fmt.Sprintf("第%d次读取 %s 访问结果出错: %s", retry, url, err)
resp.Body.Close()
log.Info(logStr)
retry++
continue
}
resp.Body.Close()
var result map[string]interface{}
json.Unmarshal(body, &result)
zid := result["result"].([]interface{})[0].(map[string]interface{})["id"].(string)
url = fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records?name=%s.%s", zid, name, domain)
req, err = http.NewRequest("GET", url, nil)
if err != nil {
logStr := fmt.Sprintf("第%d次访问 %s 失败: %s", retry, url, err)
log.Info(logStr)
retry++
continue
}
for key, value := range header {
req.Header.Set(key, value)
}
resp, err = client.Do(req)
if err != nil {
logStr := fmt.Sprintf("第%d次创建 %s 访问出错: %s", retry, url, err)
log.Info(logStr)
retry++
continue
}
body, err = io.ReadAll(resp.Body)
if err != nil {
logStr := fmt.Sprintf("第%d次读取 %s 访问结果出错: %s", retry, url, err)
resp.Body.Close()
log.Info(logStr)
retry++
continue
}
resp.Body.Close()
var dnsRecords map[string]interface{}
json.Unmarshal(body, &dnsRecords)
resultList := dnsRecords["result"].([]interface{})
rid := ""
proxied := false
for _, record := range resultList {
if record.(map[string]interface{})["type"].(string) == "A" {
rid = record.(map[string]interface{})["id"].(string)
proxied = record.(map[string]interface{})["proxied"].(bool)
break
}
}
params := map[string]interface{}{
"id": zid,
"type": "A",
"name": fmt.Sprintf("%s.%s", name, domain),
"content": ip,
"proxied": proxied,
}
if rid == "" {
continue
}
url = fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records/%s", zid, rid)
data, _ := json.Marshal(params)
req, err = http.NewRequest("PUT", url, bytes.NewBuffer(data))
if err != nil {
logStr := fmt.Sprintf("第%d次访问 %s 失败: %s", retry, url, err)
log.Info(logStr)
retry++
continue
}
for key, value := range header {
req.Header.Set(key, value)
}
resp, err = client.Do(req)
if err != nil {
logStr := fmt.Sprintf("第%d次创建 %s 访问出错: %s", retry, url, err)
log.Info(logStr)
retry++
continue
}
if resp.StatusCode == 200 {
logStr := fmt.Sprintf("成功更新%s.%s的ip为%s", name, domain, ip)
resp.Body.Close()
log.Info(logStr)
break
} else {
resp.Body.Close()
retry++
}
}
if retry >= 5 {
logStr := fmt.Sprintf("%s.%s的ip更新失败", name, domain)
log.Info(logStr)
}
}
func handleMain(config Config, domainInfo []string) {
url := fmt.Sprintf("%s.%s", domainInfo[0], domainInfo[1])
result := getLatency(url, 10)
if result.Latency > 200 {
if len(resultList) == 0 {
resp, err := http.Get("https://ipdb.api.030101.xyz/?type=bestcf&country=true")
if err != nil {
logStr := fmt.Sprintf("下载优选IP列表错误: %s", err)
log.Info(logStr)
return
}
defer resp.Body.Close()
buf, err := io.ReadAll(resp.Body)
if err != nil {
logStr := fmt.Sprintf("读取优选IP列表错误: %s", err)
log.Info(logStr)
return
}
var content string
content = string(buf)
if len(content) > 0 {
if len(domainInfo) == 3 {
resultList = getResultList(string(content), domainInfo[2])
} else {
resultList = getResultList(string(content), "")
}
}
}
if globalIP == "" && len(resultList) > 0 {
globalIP = getIP(resultList)
}
if globalIP != "" {
uploadIP(globalIP, domainInfo[0], domainInfo[1], config.Email, config.Key)
}
} else {
logStr := fmt.Sprintf("域名%s的ip延时为%dms小于200ms, 未更新", url, result.Latency)
log.Info(logStr)
}
}
func main() {
// 定义命令行参数
filePath := flag.String("file", "config.json", "文件路径和名称")
// 解析命令行参数
flag.Parse()
// 设置日志输出
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
// 打开文件
file, err := os.Open(*filePath)
if err != nil {
logStr := fmt.Sprintf("无法打开配置文件: %s", err)
log.Info(logStr)
return
}
defer file.Close()
// 读取文件内容
bytes, err := io.ReadAll(file)
if err != nil {
logStr := fmt.Sprintf("无法读取配置文件: %s", err)
log.Info(logStr)
return
}
// 解析 JSON 文件内容
var config Config
if err := json.Unmarshal(bytes, &config); err != nil {
logStr := fmt.Sprintf("无法解析配置文件: %s", err)
log.Info(logStr)
return
}
for _, domainInfo := range config.DomainInfos {
handleMain(config, domainInfo)
}
}