diff --git a/ipgeo/ipgeo_test.go b/ipgeo/ipgeo_test.go index 766e1b8..83ae67c 100644 --- a/ipgeo/ipgeo_test.go +++ b/ipgeo/ipgeo_test.go @@ -1,8 +1,9 @@ package ipgeo import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestLeoIP(t *testing.T) { @@ -13,11 +14,29 @@ func TestLeoIP(t *testing.T) { assert.NotEmpty(t, res.Isp) } -func TestIPInSight(t *testing.T) { - res, err := IPInSight("1.1.1.1") +func TestIPSB(t *testing.T) { + res, err := GetIPGeoByIPSB("1.1.1.1") + assert.Nil(t, err) + assert.NotNil(t, res) + assert.NotEmpty(t, res.Asnumber) + assert.NotEmpty(t, res.Isp) +} + +func TestIPInfo(t *testing.T) { + res, err := GetIPGeoByIPInfo("1.1.1.1") assert.Nil(t, err) assert.NotNil(t, res) assert.NotEmpty(t, res.Country) assert.NotEmpty(t, res.City) assert.NotEmpty(t, res.Prov) } + +func TestIPInSight(t *testing.T) { + res, err := IPInSight("1.1.1.1") + assert.Nil(t, err) + assert.NotNil(t, res) + assert.NotEmpty(t, res.Country) + assert.NotEmpty(t, res.Prov) + // 这个库有时候不提供城市信息,返回值为"" + //assert.NotEmpty(t, res.City) +} diff --git a/ipgeo/ipinfo.go b/ipgeo/ipinfo.go index fbcd13b..a973286 100644 --- a/ipgeo/ipinfo.go +++ b/ipgeo/ipinfo.go @@ -1 +1,29 @@ package ipgeo + +import ( + "io/ioutil" + "net/http" + + "github.com/tidwall/gjson" +) + +func GetIPGeoByIPInfo(ip string) (*IPGeoData, error) { + + resp, err := http.Get("https://ipinfo.io/" + ip + "?token=42764a944dabd0") + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + res := gjson.ParseBytes(body) + + return &IPGeoData{ + Country: res.Get("country").String(), + City: res.Get("city").String(), + Prov: res.Get("region").String(), + }, nil +} diff --git a/ipgeo/ipinsight.go b/ipgeo/ipinsight.go index 6dd11f7..df0c233 100644 --- a/ipgeo/ipinsight.go +++ b/ipgeo/ipinsight.go @@ -1,24 +1,11 @@ package ipgeo import ( - "encoding/json" "io/ioutil" "net/http" -) -type ipInSightData struct { - IP string `json:"ip"` - Version string `json:"version"` - IsEuropeanUnion bool `json:"is_european_union"` - ContinentCode string `json:"continent_code"` - IddCode string `json:"idd_code"` - CountryCode string `json:"country_code"` - CountryName string `json:"country_name"` - RegionName string `json:"region_name"` - CityName string `json:"city_name"` - Latitude float64 `json:"latitude"` - Longitude float64 `json:"longitude"` -} + "github.com/tidwall/gjson" +) func IPInSight(ip string) (*IPGeoData, error) { resp, err := http.Get("https://ipinsight.io/query?ip=" + ip) @@ -30,15 +17,11 @@ func IPInSight(ip string) (*IPGeoData, error) { return nil, err } - iPInSightData := &ipInSightData{} - err = json.Unmarshal(body, &iPInSightData) - if err != nil { - return nil, err - } + res := gjson.ParseBytes(body) return &IPGeoData{ - Country: iPInSightData.CountryName, - City: iPInSightData.CityName, - Prov: iPInSightData.RegionName, + Country: res.Get("country_name").String(), + City: res.Get("city_name").String(), + Prov: res.Get("region_name").String(), }, nil } diff --git a/ipgeo/ipsb.go b/ipgeo/ipsb.go index fbcd13b..c7e4e72 100644 --- a/ipgeo/ipsb.go +++ b/ipgeo/ipsb.go @@ -1 +1,30 @@ package ipgeo + +import ( + "io/ioutil" + "net/http" + + "github.com/tidwall/gjson" +) + +func GetIPGeoByIPSB(ip string) (*IPGeoData, error) { + resp, err := http.Get("https://api.ip.sb/geoip/" + ip) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + res := gjson.ParseBytes(body) + + return &IPGeoData{ + Asnumber: res.Get("asn").String(), + Country: res.Get("country").String(), + City: res.Get("city").String(), + Prov: res.Get("region").String(), + Isp: res.Get("isp").String(), + }, nil +}