diff --git a/go.mod b/go.mod index 5083295..a6f3942 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,13 @@ require ( golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 ) -require golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect +require ( + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.7.1 // indirect + github.com/tidwall/gjson v1.14.1 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect + golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect +) diff --git a/go.sum b/go.sum index a16995f..660d001 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,18 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8= github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo= +github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= @@ -13,6 +26,11 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 h1:nonptSpoQ4vQjyraW20DXPAglgQfVnM9ZC6MmNLMR60= +golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/ipgeo/ipgeo.go b/ipgeo/ipgeo.go index f7211e9..d07df90 100644 --- a/ipgeo/ipgeo.go +++ b/ipgeo/ipgeo.go @@ -1,13 +1,13 @@ package ipgeo type IPGeoData struct { - Asnumber string `json:"asnumber"` - Country string `json:"country"` - Prov string `json:"prov"` - City string `json:"city"` - District string `json:"district"` - Owner string `json:"owner"` - Isp string `json:"isp"` + Asnumber string + Country string + Prov string + City string + District string + Owner string + Isp string } -type Source = func(ip string) (IPGeoData, error) +type Source = func(ip string) (*IPGeoData, error) diff --git a/ipgeo/ipgeo_test.go b/ipgeo/ipgeo_test.go new file mode 100644 index 0000000..766e1b8 --- /dev/null +++ b/ipgeo/ipgeo_test.go @@ -0,0 +1,23 @@ +package ipgeo + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestLeoIP(t *testing.T) { + res, err := LeoIP("1.1.1.1") + assert.Nil(t, err) + assert.NotNil(t, res) + assert.NotEmpty(t, res.Asnumber) + assert.NotEmpty(t, res.Isp) +} + +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.City) + assert.NotEmpty(t, res.Prov) +} diff --git a/ipgeo/ipinfo.go b/ipgeo/ipinfo.go new file mode 100644 index 0000000..fbcd13b --- /dev/null +++ b/ipgeo/ipinfo.go @@ -0,0 +1 @@ +package ipgeo diff --git a/ipgeo/ipinsight.go b/ipgeo/ipinsight.go new file mode 100644 index 0000000..6dd11f7 --- /dev/null +++ b/ipgeo/ipinsight.go @@ -0,0 +1,44 @@ +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"` +} + +func IPInSight(ip string) (*IPGeoData, error) { + resp, err := http.Get("https://ipinsight.io/query?ip=" + ip) + if err != nil { + return nil, err + } + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + iPInSightData := &ipInSightData{} + err = json.Unmarshal(body, &iPInSightData) + if err != nil { + return nil, err + } + + return &IPGeoData{ + Country: iPInSightData.CountryName, + City: iPInSightData.CityName, + Prov: iPInSightData.RegionName, + }, nil +} diff --git a/ipgeo/ipsb.go b/ipgeo/ipsb.go new file mode 100644 index 0000000..fbcd13b --- /dev/null +++ b/ipgeo/ipsb.go @@ -0,0 +1 @@ +package ipgeo diff --git a/ipgeo/leo.go b/ipgeo/leo.go new file mode 100644 index 0000000..2033234 --- /dev/null +++ b/ipgeo/leo.go @@ -0,0 +1,30 @@ +package ipgeo + +import ( + "github.com/tidwall/gjson" + "io/ioutil" + "net/http" +) + +func LeoIP(ip string) (*IPGeoData, error) { + resp, err := http.Get("https://api.leo.moe/ip/?ip=" + 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("asnumber").String(), + Country: res.Get("country").String(), + Prov: res.Get("prov").String(), + City: res.Get("city").String(), + District: res.Get("district").String(), + Owner: res.Get("owner").String(), + Isp: res.Get("isp").String(), + }, nil +} diff --git a/methods/udp/udp.go b/methods/udp/udp.go index dcb3574..fb029e9 100644 --- a/methods/udp/udp.go +++ b/methods/udp/udp.go @@ -5,7 +5,7 @@ import ( "math/rand" "net" "sync" - time "time" + "time" "github.com/google/gopacket" "github.com/google/gopacket/layers"