完善ipgeo

新增ipsb、ipinfo,ipinsight从原生json解析库改为gjson以统一
This commit is contained in:
sjlleo
2022-05-13 11:49:15 +08:00
parent 13af96ae15
commit 2e681b48c5
4 changed files with 85 additions and 26 deletions

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}