From 84c48dae995bf3203cda31c3b090af5bd0d54277 Mon Sep 17 00:00:00 2001 From: sjlleo Date: Fri, 27 May 2022 17:18:31 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E5=AE=8C=E5=96=84=E4=B8=80=E4=B8=8B?= =?UTF-8?q?=20ipapicom?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ipgeo/ipapicom.go | 14 ++++++++------ ipgeo/ipgeo.go | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/ipgeo/ipapicom.go b/ipgeo/ipapicom.go index c4594a2..fd4cbc2 100644 --- a/ipgeo/ipapicom.go +++ b/ipgeo/ipapicom.go @@ -1,11 +1,11 @@ package ipgeo import ( + "errors" "io/ioutil" "log" "net/http" - "os" - "strings" + "regexp" "time" "github.com/tidwall/gjson" @@ -27,12 +27,14 @@ func IPApiCom(ip string) (*IPGeoData, error) { body, _ := ioutil.ReadAll(content.Body) res := gjson.ParseBytes(body) - if res.Get("country").String() == "" { - os.Exit(1) + if res.Get("status").String() != "success" { + return &IPGeoData{}, errors.New("超过API阈值") } - log.Println("ip-api.com 正在使用") + + re := regexp.MustCompile("[0-9]+") + return &IPGeoData{ - Asnumber: strings.Split(res.Get("as").String(), " ")[0], + Asnumber: re.FindString(res.Get("as").String()), Country: res.Get("country").String(), City: res.Get("city").String(), Prov: res.Get("regionName").String(), diff --git a/ipgeo/ipgeo.go b/ipgeo/ipgeo.go index 12c4ca2..c507336 100644 --- a/ipgeo/ipgeo.go +++ b/ipgeo/ipgeo.go @@ -24,7 +24,7 @@ func GetSource(s string) Source { return IPSB case "IPINSIGHT": return IPInSight - case "ip-api.com": + case "IPAPI.COM": return IPApiCom default: return nil