github.com/nxtrace/NTrace-core@v1.3.1-0.20240513132635-39169291e8c9/ipgeo/ipsb.go (about) 1 package ipgeo 2 3 import ( 4 "io" 5 "log" 6 "net/http" 7 "os" 8 "time" 9 10 "github.com/tidwall/gjson" 11 ) 12 13 func IPSB(ip string, timeout time.Duration, _ string, _ bool) (*IPGeoData, error) { 14 url := "https://api.ip.sb/geoip/" + ip 15 client := &http.Client{ 16 // 2 秒超时 17 Timeout: timeout, 18 } 19 req, _ := http.NewRequest("GET", url, nil) 20 // 设置 UA,ip.sb 默认禁止 go-client User-Agent 的 api 请求 21 req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0") 22 content, err := client.Do(req) 23 if err != nil { 24 log.Println("api.ip.sb 请求超时(2s),请切换其他API使用") 25 return nil, err 26 } 27 body, _ := io.ReadAll(content.Body) 28 res := gjson.ParseBytes(body) 29 30 if res.Get("country").String() == "" { 31 // 什么都拿不到,证明被Cloudflare风控了 32 os.Exit(1) 33 } 34 35 return &IPGeoData{ 36 Asnumber: res.Get("asn").String(), 37 Country: res.Get("country").String(), 38 City: res.Get("city").String(), 39 Prov: res.Get("region").String(), 40 Owner: res.Get("isp").String(), 41 }, nil 42 }