/
ip.go
58 lines (55 loc) · 1.37 KB
/
ip.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
package lib
import (
"fmt"
"github.com/Jeffail/gabs/v2"
"github.com/go-resty/resty/v2"
"github.com/sirupsen/logrus"
"github.com/unionj-cloud/go-doudou/toolkit/stringutils"
)
func QueryIP(ip string, lang string) bool {
client := resty.New().SetRetryCount(3)
if stringutils.IsEmpty(lang) {
lang = "zh-CN"
}
if stringutils.IsEmpty(ip) {
logrus.Error("ip is empty")
return false
}
//http://ip-api.com/json/120.220.220.95?lang=zh-CN
url := fmt.Sprintf("http://ip-api.com/json/%s", ip)
var rs Result
resp, err := client.R().
SetQueryParams(map[string]string{
"lang": lang,
}).
SetResult(&rs).
SetHeader("Accept", "application/json").
Get(url)
if err != nil {
logrus.Error(err)
return false
}
if resp.IsSuccess() {
logrus.Info(gabs.Wrap(rs).StringIndent("", " "))
} else {
logrus.Error(resp.Error())
return false
}
return true
}
type Result struct {
Query string `json:"query"`
Status string `json:"status"`
Country string `json:"country"`
CountryCode string `json:"countryCode"`
Region string `json:"region"`
RegionName string `json:"regionName"`
City string `json:"city"`
Zip string `json:"zip"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
Timezone string `json:"timezone"`
Isp string `json:"isp"`
Org string `json:"org"`
As string `json:"as"`
}