This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
385 lines (331 loc) · 10.4 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"regexp"
)
// 结构体定义用于解析GraphQL查询结果的JSON
type GraphQLResponse struct {
Data struct {
Viewer struct {
Zones []struct {
HTTPRequests1DGroups []struct {
Date struct {
Date string `json:"date"`
} `json:"dimensions"`
Sum struct {
Bytes int `json:"bytes"`
Requests int `json:"requests"`
} `json:"sum"`
} `json:"httpRequests1dGroups"`
} `json:"zones"`
} `json:"viewer"`
} `json:"data"`
}
// User 结构体用于保存用户信息
type User struct {
Email string `json:"email"`
Key string `json:"key"`
}
// ResultData 结构体用于保存每次的数据
type ResultData struct {
Pattern string `json:"pattern"`
Bytes int `json:"bytes"`
Requests int `json:"requests"`
}
func readConfig(filename string) ([]User, error) {
fileContent, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var users []User
err = json.Unmarshal(fileContent, &users)
if err != nil {
return nil, err
}
return users, nil
}
// 获取账户信息
func listAccounts(email, key string) (*http.Response, error) {
url := "https://api.cloudflare.com/client/v4/accounts"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("X-Auth-Email", email)
req.Header.Set("X-Auth-Key", key)
client := &http.Client{Timeout: 10 * time.Second}
return client.Do(req)
}
// 获取域名信息
func listZones(email, key string) (*http.Response, error) {
url := "https://api.cloudflare.com/client/v4/zones"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("X-Auth-Email", email)
req.Header.Set("X-Auth-Key", key)
client := &http.Client{Timeout: 10 * time.Second}
return client.Do(req)
}
// 获取Workers路由(获取节点域名)
func listFilters(email, key, zoneID string) (*http.Response, error) {
url := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/workers/filters", zoneID)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("X-Auth-Email", email)
req.Header.Set("X-Auth-Key", key)
client := &http.Client{Timeout: 10 * time.Second}
return client.Do(req)
}
// GraphQL API调用
func graphqlAPI(email, key, zoneTag, start, end string) (*http.Response, error) {
url := "https://api.cloudflare.com/client/v4/graphql"
query := `
{
viewer {
zones(filter: { zoneTag: $tag }) {
httpRequests1dGroups(
orderBy: [date_ASC]
limit: 1000
filter: { date_gt: $start, date_lt: $end }
) {
date: dimensions {
date
}
sum {
bytes
requests
}
}
}
}
}`
variables := map[string]string{"tag": zoneTag, "start": start, "end": end}
requestBody, err := json.Marshal(map[string]interface{}{"query": query, "variables": variables})
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Auth-Email", email)
req.Header.Set("X-Auth-Key", key)
client := &http.Client{Timeout: 10 * time.Second}
return client.Do(req)
}
// 将当前日期移位n天,并返回移位日期和移位日期的前一个和下一个日期。
func dateShift(n int) (string, string, string) {
today := time.Now().Local().AddDate(0, 0, n)
shiftedDate := today.AddDate(0, 0, n)
previousDate := shiftedDate.AddDate(0, 0, -1)
nextDate := shiftedDate.AddDate(0, 0, 1)
return shiftedDate.Format("2006-01-02"), previousDate.Format("2006-01-02"), nextDate.Format("2006-01-02")
}
var allUserData []ResultData // 添加一个全局数组,用于保存所有用户的数据
func processAndSaveData(pattern, graphqlBody string, bytes, requests int) {
// 提取pattern网址
fmt.Println("pattern", pattern)
// 创建 ResultData 结构体
resultData := ResultData{
Pattern: pattern,
Bytes: bytes,
Requests: requests,
}
// 将数据添加到全局数组中
allUserData = append(allUserData, resultData)
}
func runForUser(user User) {
email := user.Email
key := user.Key
fmt.Println("Processing for User:", email)
var zoneTag string
var domain string
// 获取账户信息
accountsResponse, err := listAccounts(email, key)
if err != nil {
fmt.Println("Error fetching accounts:", err)
return
}
defer accountsResponse.Body.Close()
// 输出账户信息
accountsBody, _ := ioutil.ReadAll(accountsResponse.Body)
fmt.Println("Accounts Response:", string(accountsBody))
// 获取域名信息
zonesResponse, err := listZones(email, key)
if err != nil {
fmt.Println("Error fetching zones:", err)
return
}
defer zonesResponse.Body.Close()
// 输出域名信息
zonesBody, _ := ioutil.ReadAll(zonesResponse.Body)
fmt.Println("Zones Response:", string(zonesBody))
// 解析域名信息
var zonesData map[string]interface{}
if err := json.Unmarshal(zonesBody, &zonesData); err != nil {
fmt.Println("Error parsing Zones response:", err)
return
}
// 获取域名列表
resultInfo, ok := zonesData["result_info"].(map[string]interface{})
if !ok {
fmt.Println("Error getting result_info from response")
return
}
totalCount, ok := resultInfo["total_count"].(float64)
if !ok {
fmt.Println("Error getting total_count from result_info")
return
}
zonesList, ok := zonesData["result"].([]interface{})
if !ok {
fmt.Println("Error getting zones list from response")
return
}
// 创建一个保存所有zoneID的数组
var allZoneIDs []string
// 遍历所有域名,并提取ID
for i := 0; i < int(totalCount); i++ {
zoneInfo, ok := zonesList[i].(map[string]interface{})
if !ok {
fmt.Println("Error getting zone info")
continue
}
// 获取当前域名的ID
zoneID, ok := zoneInfo["id"].(string)
if !ok {
fmt.Println("Error getting zone ID")
continue
}
// 使用当前域名的ID进行后续操作,例如打印或存储
fmt.Println("Zone ID:", zoneID)
// 将当前域名的ID添加到数组中
allZoneIDs = append(allZoneIDs, zoneID)
fmt.Println("allZoneIDs:", allZoneIDs)
}
// 循环遍历每个域名的ID
for _, zoneID := range allZoneIDs {
// 使用当前域名的ID调用listFilters函数
filtersResponse, err := listFilters(email, key, zoneID)
if err != nil {
fmt.Println("Error fetching filters for zone ID", zoneID, ":", err)
continue
}
defer filtersResponse.Body.Close()
// 解析Filters响应
var filtersData map[string]interface{}
filtersBody, _ := ioutil.ReadAll(filtersResponse.Body)
if err := json.Unmarshal(filtersBody, &filtersData); err != nil {
fmt.Println("Error parsing Filters response for zone ID", zoneID, ":", err)
continue
}
// 检查是否存在非空的result字段
if result, ok := filtersData["result"].([]interface{}); ok && len(result) > 0 {
// 获取第一个非空result字段的zoneID,并赋值给zoneTag
if matchedZoneID, ok := result[0].(map[string]interface{})["id"].(string); ok {
zoneTag = zoneID
pattern := fmt.Sprintf("%v", result)
// 输出符合条件的zoneID
fmt.Println("Matching Zone ID:", matchedZoneID)
fmt.Println("pattern:", pattern)
re := regexp.MustCompile(`pattern:([^/\s]+)/*`)
match := re.FindStringSubmatch(pattern)
if len(match) > 1 {
domain = match[1]
fmt.Println(domain)
} else {
fmt.Println("未找到匹配项")
}
break
}
}
}
// 输出最终选择的zoneTag
fmt.Println("Final Zone Tag:", zoneTag)
fmt.Println("Final domain:", domain)
// 获取当前日期和前后两天的日期
_, start, end := dateShift(0)
// 获取域名数据统计
graphqlResponse, err := graphqlAPI(email, key, zoneTag, start, end)
fmt.Println("Time:", start, end)
if err != nil {
fmt.Println("Error fetching GraphQL data:", err)
return
}
defer graphqlResponse.Body.Close()
var responseData GraphQLResponse
graphqlBody, _ := ioutil.ReadAll(graphqlResponse.Body)
err = json.Unmarshal(graphqlBody, &responseData)
if err != nil {
fmt.Println("Error parsing GraphQL response:", err)
return
}
fmt.Println("GraphQL Response:", responseData)
// 提取 pattern、bytes 和 requests
for _, zone := range responseData.Data.Viewer.Zones {
// 检查是否存在 HTTPRequests1DGroups
if len(zone.HTTPRequests1DGroups) > 0 {
for _, group := range zone.HTTPRequests1DGroups {
// 直接引用 group 中的字段
pattern := domain
bytes := group.Sum.Bytes
requests := group.Sum.Requests
// 处理数据并保存到 JSON 文件
processAndSaveData(pattern, string(graphqlBody), bytes, requests)
}
} else {
// 如果数据为空,将 bytes 和 requests 的值设置为 0
pattern := domain
bytes := 0
requests := 0
// 处理数据并保存到 JSON 文件
processAndSaveData(pattern, string(graphqlBody), bytes, requests)
}
}
}
// 处理所有用户的数据并保存到文件
func processAndSaveAllUserData() error {
// 获取当前日期并用作文件名
fileName := fmt.Sprintf("data.json")
// 将所有用户的数据转换为 JSON 格式
jsonData, err := json.Marshal(allUserData)
if err != nil {
return err
}
// 将数据保存到文件
err = ioutil.WriteFile(fileName, jsonData, 0644)
if err != nil {
return err
}
fmt.Printf("All user data saved to %s\n", fileName)
return nil
}
func main() {
// Read users from the config file
users, err := readConfig("config.json")
if err != nil {
fmt.Println("Error reading config file:", err)
return
}
// Loop through each user
for _, user := range users {
// Perform operations for each user
runForUser(user)
}
// 处理所有用户的数据并保存到文件
err = processAndSaveAllUserData()
if err != nil {
fmt.Println("Error processing and saving all user data:", err)
return
}
}