@@ -93,9 +93,9 @@ func (c *Client) GetCityByIp(input net.IP) string {
93
93
}
94
94
95
95
// 调用火山云接口,判断IP是否归属火山云
96
- func (c * Client ) Checkvolcengine (input net.IP ) (cdn string , isp string ) {
96
+ func (c * Client ) Checkvolcengine (input net.IP ) (cdn string , isp string , iscdn bool ) {
97
97
if c .config .VolcengineKey == "" {
98
- return "" , ""
98
+ return "" , "" , false
99
99
}
100
100
ip := input .String ()
101
101
region := "cn-beijing"
@@ -115,28 +115,30 @@ func (c *Client) Checkvolcengine(input net.IP) (cdn string, isp string) {
115
115
response , err := svc .DescribeCdnIP (describeCdnIPInput )
116
116
if err != nil {
117
117
slog .Error ("火山云接口查询异常:" , err )
118
- return "" , ""
118
+ return "" , "" , false
119
119
}
120
120
patternStr := `CdnIp: (.*?),`
121
121
Platform , err := gregex .MatchString (patternStr , response .String ())
122
122
if err != nil {
123
- return "" , ""
123
+ slog .Error ("火山云json解析出错" , err )
124
+ return "" , "" , false
124
125
}
125
126
if Platform [1 ] == "True" {
126
127
patternStr = `Location: "(.*?)"`
127
128
result , reerr := gregex .MatchString (patternStr , response .String ())
128
129
if reerr != nil {
129
- return "火山云" , ""
130
+ slog .Error ("火山云json解析错误" , reerr )
131
+ return "" , "" , false
130
132
}
131
- return "火山云" , result [1 ]
133
+ return "火山云" , result [1 ], true
132
134
}
133
- return "" , ""
135
+ return "" , "" , false
134
136
}
135
137
136
138
// 调用腾讯云DescribeCdnIp接口,判断ip是否属于腾讯云
137
- func (c * Client ) CheckTencent (input net.IP ) (cdn string , isp string ) {
139
+ func (c * Client ) CheckTencent (input net.IP ) (cdn string , isp string , iscdn bool ) {
138
140
if c .config .TencentId == "" {
139
- return "" , ""
141
+ return "" , "" , false
140
142
}
141
143
ip := input .String ()
142
144
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
@@ -156,28 +158,29 @@ func (c *Client) CheckTencent(input net.IP) (cdn string, isp string) {
156
158
//fmt.Print(response.ToJsonString())
157
159
if err != nil {
158
160
slog .Error ("腾讯接口查询异常:" , err )
159
- return "" , ""
161
+ return "" , "" , false
160
162
}
161
163
patternStr := `"Platform":"(.*?)"`
162
164
Platform , err := gregex .MatchString (patternStr , response .ToJsonString ())
163
165
if err != nil {
164
- return "" , ""
166
+ return "" , "" , false
165
167
}
166
168
if Platform [1 ] == "yes" {
167
169
patternStr = `"Location":"(.*?)"`
168
170
result , reerr := gregex .MatchString (patternStr , response .ToJsonString ())
169
171
if reerr != nil {
170
- return "腾讯云" , ""
172
+ slog .Error ("腾讯云json解析错误" , reerr )
173
+ return "" , "" , false
171
174
}
172
- return "腾讯云" , result [1 ]
175
+ return "腾讯云" , result [1 ], true
173
176
}
174
- return "" , ""
177
+ return "" , "" , false
175
178
}
176
179
177
180
// 调用阿里云DescribeIpInfo接口,判断ip是否属于阿里云
178
- func (c * Client ) CheckAliyun (input net.IP ) (cdn string , isp string ) {
181
+ func (c * Client ) CheckAliyun (input net.IP ) (cdn string , isp string , iscdn bool ) {
179
182
if c .config .AlibabaId == "" {
180
- return "" , ""
183
+ return "" , "" , false
181
184
}
182
185
ip := input .String ()
183
186
config := & ali_openapi.Config {
@@ -191,30 +194,32 @@ func (c *Client) CheckAliyun(input net.IP) (cdn string, isp string) {
191
194
client , err := ali_cdn20180510 .NewClient (config )
192
195
if err != nil {
193
196
slog .Error ("阿里云接口查询异常:" , err )
194
- return "" , ""
197
+ return "" , "" , false
195
198
}
196
199
describeIpInfoRequest := & ali_cdn20180510.DescribeIpInfoRequest {IP : ali_tea .String (ip )}
197
200
runtime := & ali_util.RuntimeOptions {}
198
201
response , err := client .DescribeIpInfoWithOptions (describeIpInfoRequest , runtime )
199
202
if err != nil {
200
- return "" , ""
203
+ slog .Error ("阿里云接口查询异常:" , err )
204
+ return "" , "" , false
201
205
}
202
206
//fmt.Printf("%s",response.Body.String())
203
207
json , err := gjson .DecodeToJson (response .Body .String ())
204
208
if err != nil {
205
- return "" , ""
209
+ slog .Error ("阿里云json解析出错" , err )
210
+ return "" , "" , false
206
211
}
207
212
if json .Get ("CdnIp" ).String () == "True" {
208
- return "阿里云" , json .Get ("ISP" ).String ()
213
+ return "阿里云" , json .Get ("ISP" ).String (), true
209
214
} else {
210
- return "" , ""
215
+ return "" , "" , false
211
216
}
212
217
}
213
218
214
219
// 调用百度云describeIp接口,判断ip是否属于百度云
215
- func (c * Client ) CheckBaidu (input net.IP ) (cdn string , isp string ) {
220
+ func (c * Client ) CheckBaidu (input net.IP ) (cdn string , isp string , iscdn bool ) {
216
221
if c .config .BaiduId == "" {
217
- return "" , ""
222
+ return "" , "" , false
218
223
}
219
224
ip := input .String ()
220
225
req := & bd_bce.BceRequest {}
@@ -227,34 +232,37 @@ func (c *Client) CheckBaidu(input net.IP) (cdn string, isp string) {
227
232
client , err := bd_bce .NewBceClientWithAkSk (c .config .BaiduId , c .config .BaiduKey , "https://cdn.baidubce.com" )
228
233
if err != nil {
229
234
slog .Error ("百度云接口查询异常:" , err )
230
- return "" , ""
235
+ return "" , "" , false
231
236
}
232
237
resp := & bd_bce.BceResponse {}
233
238
err = client .SendRequest (req , resp )
234
239
if err != nil {
235
- return "" , ""
240
+ slog .Error ("百度云接口查询异常:" , err )
241
+ return "" , "" , false
236
242
}
237
243
respBody := resp .Body ()
238
244
defer respBody .Close ()
239
245
body , err := ioutil .ReadAll (respBody )
240
246
if err != nil {
241
- return "" , ""
247
+ slog .Error ("百度云json解析失败" , err )
248
+ return "" , "" , false
242
249
}
243
250
json , err := gjson .DecodeToJson (string (body ))
244
251
if err != nil {
245
- return "" , ""
252
+ slog .Error ("百度云json解析失败" , err )
253
+ return "" , "" , false
246
254
}
247
255
if json .Get ("cdnIP" ).String () == "true" {
248
- return "百度云" , json .Get ("isp" ).String ()
256
+ return "百度云" , json .Get ("isp" ).String (), true
249
257
} else {
250
- return "" , ""
258
+ return "" , "" , false
251
259
}
252
260
}
253
261
254
262
// 调用华为云接口,判断IP是否属于华为云
255
- func (c * Client ) CheckHuawei (input net.IP ) (cdn string , isp string ) {
263
+ func (c * Client ) CheckHuawei (input net.IP ) (cdn string , isp string , iscdn bool ) {
256
264
if c .config .HuaweiID == "" {
257
- return "" , ""
265
+ return "" , "" , false
258
266
}
259
267
ip := input .String ()
260
268
@@ -274,25 +282,28 @@ func (c *Client) CheckHuawei(input net.IP) (cdn string, isp string) {
274
282
response , err := client .ShowIpInfo (request )
275
283
if err != nil {
276
284
slog .Error ("华为云接口查询异常:" , err )
277
- return "" , ""
285
+ return "" , "" , false
278
286
}
279
287
json , err := gjson .DecodeToJson (response )
280
288
if err != nil {
281
- return "" , ""
289
+ return "" , "" , false
282
290
}
283
291
//归属
284
292
if json .Get ("belongs" ).String () == "true" {
285
- return "华为云" , json .Get ("ip" ).String ()
293
+ return "华为云" , json .Get ("ip" ).String (), true
286
294
}
287
- return "" , ""
295
+ return "" , "" , false
288
296
}
289
297
290
298
// Check checks if ip belongs to one of CDN, WAF and Cloud . It is generic method for Checkxxx methods
291
299
func (c * Client ) Check (inputIp string ) (result config.Result ) {
292
300
result .Ip = inputIp
301
+ result .IsMatch = false
293
302
ip := net .ParseIP (inputIp )
294
303
if ip == nil {
295
- result .IsMatch = false
304
+ result .IsMatch = true
305
+ result .Type = "ip-非法"
306
+ //不是cdn
296
307
return
297
308
}
298
309
//检查ip是否在缓存中
@@ -312,44 +323,44 @@ func (c *Client) Check(inputIp string) (result config.Result) {
312
323
result .Location = location
313
324
314
325
//腾讯
315
- if cdn , isp := c .CheckTencent (ip ); cdn != "" {
326
+ if cdn , isp , iscdn := c .CheckTencent (ip ); iscdn {
316
327
result .Location = location + " " + isp
317
- result .IsMatch = true
328
+ result .IsMatch = iscdn
318
329
result .Type = "tencent cdn-官方接口查询"
319
330
result .Value = cdn
320
331
InsertIPToCache (inputIp , result .Location , cdn )
321
332
return
322
333
}
323
- if cdn , isp := c .Checkvolcengine (ip ); cdn != "" {
334
+ if cdn , isp , iscdn := c .Checkvolcengine (ip ); iscdn {
324
335
result .Location = location + " " + isp
325
- result .IsMatch = true
336
+ result .IsMatch = iscdn
326
337
result .Type = "火山云 cdn-官方接口查询"
327
338
result .Value = cdn
328
339
InsertIPToCache (inputIp , result .Location , cdn )
329
340
return
330
341
}
331
342
//阿里
332
- if cdn , isp := c .CheckAliyun (ip ); cdn != "" {
343
+ if cdn , isp , iscdn := c .CheckAliyun (ip ); iscdn {
333
344
result .Location = location + " " + isp
334
- result .IsMatch = true
345
+ result .IsMatch = iscdn
335
346
result .Type = "Aliyun cdn-官方接口查询"
336
347
result .Value = cdn
337
348
InsertIPToCache (inputIp , result .Location , cdn )
338
349
return
339
350
}
340
351
//百度
341
- if cdn , isp := c .CheckBaidu (ip ); cdn != "" {
352
+ if cdn , isp , iscdn := c .CheckBaidu (ip ); iscdn {
342
353
result .Location = location + " " + isp
343
- result .IsMatch = true
354
+ result .IsMatch = iscdn
344
355
result .Type = "Baidu cdn-官方接口查询"
345
356
result .Value = cdn
346
357
InsertIPToCache (inputIp , result .Location , cdn )
347
358
return
348
359
}
349
360
//华为
350
- if cdn , isp := c .CheckHuawei (ip ); cdn != "" {
361
+ if cdn , isp , iscdn := c .CheckHuawei (ip ); iscdn {
351
362
result .Location = location + " " + isp
352
- result .IsMatch = true
363
+ result .IsMatch = iscdn
353
364
result .Type = "华为云 cdn-官方接口查询"
354
365
result .Value = cdn
355
366
InsertIPToCache (inputIp , result .Location , cdn )
@@ -376,7 +387,6 @@ func (c *Client) Check(inputIp string) (result config.Result) {
376
387
result .Value = value
377
388
return
378
389
}
379
- result .IsMatch = false
380
390
return
381
391
}
382
392
0 commit comments