Skip to content

Commit 2c06b40

Browse files
committed
Add ipify
1 parent 47bf0aa commit 2c06b40

File tree

6 files changed

+201
-1
lines changed

6 files changed

+201
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ it provides both of cli binary and golang API.
5959
- [IP2Proxy](https://www.ip2location.com/web-service/ip2proxy)
6060
- [ipdata](https://ipdata.co/)
6161
- [ipgeolocation](https://ipgeolocation.io/)
62+
- [ipify.org](https://www.ipify.org/)
6263
- [ipinfo.io](https://ipinfo.io/)
6364
- [IPQualityScore](https://www.ipqualityscore.com/)
6465
- [Ipregistry](https://ipregistry.co/)
@@ -253,7 +254,8 @@ see example dir for more examples.
253254
| `FRAUD_CHECK_IP2PROXY_APIKEY` | [ip2proxy API key](https://www.ip2location.com/web-service/ip2proxy/). |
254255
| `FRAUD_CHECK_IP2PROXY_PACKAGE` | [ip2proxy package parameter](https://www.ip2location.com/web-service/ip2proxy/). |
255256
| `FRAUD_CHECK_IPDATACO_APIKEY` | [ipdata.co API key](https://docs.ipdata.co/). |
256-
| `FRAUD_CHECK_IPGEOLOCATION_APIKEY` | [ipgeolocation API token](https://ipgeolocation.io/documentation.html). |
257+
| `FRAUD_CHECK_IPGEOLOCATION_APIKEY` | [ipgeolocation API key](https://ipgeolocation.io/documentation.html). |
258+
| `FRAUD_CHECK_IPIFY_APIKEY` | [ipify API key](https://geo.ipify.org/docs). |
257259
| `FRAUD_CHECK_IPINFOIO_TOKEN` | [ipinfo.io API token](https://ipinfo.io/developers). |
258260
| `IPQS_APIKEY` | [IPQualityScore API Key](https://www.ipqualityscore.com/documentation/overview). |
259261
| `FRAUD_CHECK_IPSTACK_APIKEY` | [ipstack API key](https://ipstack.com/documentation). |

cmd/providers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/evalphobia/go-ip-fraud-check/provider/ip2proxy"
1111
"github.com/evalphobia/go-ip-fraud-check/provider/ipdataco"
1212
"github.com/evalphobia/go-ip-fraud-check/provider/ipgeolocation"
13+
"github.com/evalphobia/go-ip-fraud-check/provider/ipify"
1314
"github.com/evalphobia/go-ip-fraud-check/provider/ipinfoio"
1415
"github.com/evalphobia/go-ip-fraud-check/provider/ipqualityscore"
1516
"github.com/evalphobia/go-ip-fraud-check/provider/ipregistry"
@@ -24,6 +25,7 @@ const (
2425
providerIP2Proxy = "ip2proxy"
2526
providerIPdataco = "ipdata"
2627
providerIPGeolocation = "ipgeolocation"
28+
providerIPify = "ipify"
2729
providerIPinfoio = "ipinfo"
2830
providerIPQS = "ipqualityscore"
2931
providerIPRegistry = "ipregistry"
@@ -38,6 +40,7 @@ var providerMap = map[string]provider.Provider{
3840
providerIP2Proxy: &ip2proxy.IP2ProxyProvider{},
3941
providerIPdataco: &ipdataco.IPdatacoProvider{},
4042
providerIPGeolocation: &ipgeolocation.IPGeoLocationProvider{},
43+
providerIPify: &ipify.IPifyProvider{},
4144
providerIPinfoio: &ipinfoio.IPinfoioProvider{},
4245
providerIPQS: &ipqualityscore.IPQualityScoreProvider{},
4346
providerIPRegistry: &ipregistry.IPRegistryProvider{},

ipfraudcheck/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
envIP2ProxyAPIPackage = "FRAUD_CHECK_IP2PROXY_PACKAGE"
1515
envIPdatacoAPIKey = "FRAUD_CHECK_IPDATACO_APIKEY"
1616
envIPGeolocationAPIKey = "FRAUD_CHECK_IPGEOLOCATION_APIKEY"
17+
envIPifyAPIKey = "FRAUD_CHECK_IPIFY_APIKEY"
1718
envIPinfoioToken = "FRAUD_CHECK_IPINFOIO_TOKEN"
1819
envIPStackAPIKey = "FRAUD_CHECK_IPSTACK_APIKEY"
1920
envShodanAPIKey = "FRAUD_CHECK_SHODAN_APIKEY"
@@ -37,6 +38,8 @@ type Config struct {
3738
IPdatacoAPIKey string
3839
// ipgeolocation.io
3940
IPGeolocationAPIKey string
41+
// ipify.org
42+
IPifyAPIKey string
4043
// ipinfo.io
4144
IPinfoioToken string
4245
// ipqualityscore.com
@@ -111,6 +114,14 @@ func (c Config) GetIPGeolocationAPIKey() string {
111114
return c.IPGeolocationAPIKey
112115
}
113116

117+
func (c Config) GetIPifyAPIKey() string {
118+
s := os.Getenv(envIPifyAPIKey)
119+
if s != "" {
120+
return s
121+
}
122+
return c.IPifyAPIKey
123+
}
124+
114125
func (c Config) GetIPinfoioToken() string {
115126
s := os.Getenv(envIPinfoioToken)
116127
if s != "" {

provider/ipify/client.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package ipify
2+
3+
import (
4+
"github.com/evalphobia/go-ip-fraud-check/provider/privateclient"
5+
)
6+
7+
const (
8+
defaultBaseURL = "https://geo.ipify.org"
9+
)
10+
11+
type Client struct {
12+
apiKey string
13+
privateclient.RESTClient
14+
}
15+
16+
func NewClient(apiKey string) Client {
17+
return Client{
18+
apiKey: apiKey,
19+
RESTClient: privateclient.RESTClient{
20+
Option: privateclient.Option{
21+
BaseURL: defaultBaseURL,
22+
},
23+
},
24+
}
25+
}
26+
27+
func (c *Client) SetDebug(b bool) {
28+
c.RESTClient.Debug = b
29+
}
30+
31+
func (c Client) LookUpWithSecurity(ipaddr string) (Response, error) {
32+
return c.LookUp("country,city,vpn", Option{
33+
IPAddress: ipaddr,
34+
})
35+
}
36+
37+
func (c Client) LookUpWithCity(ipaddr string) (Response, error) {
38+
return c.LookUp("country,city", Option{
39+
IPAddress: ipaddr,
40+
})
41+
}
42+
43+
func (c Client) LookUpCountry(ipaddr string) (Response, error) {
44+
return c.LookUp("country", Option{
45+
IPAddress: ipaddr,
46+
})
47+
}
48+
49+
func (c Client) LookUp(typ string, opt Option) (Response, error) {
50+
params := make(map[string]string)
51+
params["apiKey"] = c.apiKey
52+
53+
if opt.IPAddress != "" {
54+
params["ipAddress"] = opt.IPAddress
55+
}
56+
if opt.Domain != "" {
57+
params["domain"] = opt.Domain
58+
}
59+
if opt.Email != "" {
60+
params["email"] = opt.Email
61+
}
62+
if opt.EscapedUnicode {
63+
params["escapedUnicode"] = "1"
64+
}
65+
66+
resp := Response{}
67+
err := c.RESTClient.CallGET("/api/v2/"+typ, params, &resp)
68+
return resp, err
69+
}
70+
71+
type Option struct {
72+
IPAddress string
73+
Domain string
74+
Email string
75+
EscapedUnicode bool
76+
}

provider/ipify/provider_ipstack.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package ipify
2+
3+
import (
4+
"errors"
5+
6+
"github.com/evalphobia/go-ip-fraud-check/ipfraudcheck"
7+
"github.com/evalphobia/go-ip-fraud-check/provider"
8+
)
9+
10+
type IPifyProvider struct {
11+
client Client
12+
}
13+
14+
func (p *IPifyProvider) Init(conf provider.Config) error {
15+
c, ok := conf.(ipfraudcheck.Config)
16+
if !ok {
17+
return errors.New("incompatible config type for IPifyProvider")
18+
}
19+
20+
apiKey := c.GetIPifyAPIKey()
21+
if apiKey == "" {
22+
return errors.New("apikey for ipify is empty. you must set directly or use 'FRAUD_CHECK_IPIFY_APIKEY' envvar")
23+
}
24+
cli := NewClient(apiKey)
25+
cli.SetDebug(c.Debug)
26+
p.client = cli
27+
return nil
28+
}
29+
30+
func (p IPifyProvider) String() string {
31+
return "ipify"
32+
}
33+
34+
func (p IPifyProvider) CheckIP(ipaddr string) (provider.FraudCheckResponse, error) {
35+
emptyResult := provider.FraudCheckResponse{}
36+
37+
resp, err := p.client.LookUpWithSecurity(ipaddr)
38+
if err != nil {
39+
return emptyResult, err
40+
}
41+
42+
as := resp.AS
43+
loc := resp.Location
44+
sec := resp.Proxy
45+
return provider.FraudCheckResponse{
46+
ServiceName: p.String(),
47+
IP: resp.IP,
48+
ISP: resp.ISP,
49+
ASNumber: as.ASN,
50+
Country: loc.Country,
51+
City: loc.City,
52+
Region: loc.Region,
53+
Latitude: loc.Latitude,
54+
Longitude: loc.Longitude,
55+
IsProxy: sec.Proxy,
56+
IsVPN: sec.VPN,
57+
IsTor: sec.Tor,
58+
}, nil
59+
}
60+
61+
func (p IPifyProvider) RawCheckIP(ipaddr string) (interface{}, error) {
62+
return p.client.LookUpWithSecurity(ipaddr)
63+
}

provider/ipify/response.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package ipify
2+
3+
type Response struct {
4+
IP string `json:"ip"`
5+
ISP string `json:"isp"`
6+
Domains []string `json:"domains"`
7+
8+
Type string `json:"type"`
9+
ContinentCode string `json:"continent_code"`
10+
ContinentName string `json:"continent_name"`
11+
CountryCode string `json:"country_code"`
12+
CountryName string `json:"country_name"`
13+
RegionCode string `json:"region_code"`
14+
RegionName string `json:"region_name"`
15+
City string `json:"city"`
16+
Zip string `json:"zip"`
17+
AS AS `json:"as"`
18+
Location Location `json:"location"`
19+
Proxy Proxy `json:"proxy"`
20+
}
21+
22+
type AS struct {
23+
ASN int64 `json:"asn"`
24+
Name string `json:"name"`
25+
Route string `json:"route"`
26+
Domain string `json:"domain"`
27+
Type string `json:"type"`
28+
}
29+
30+
type Location struct {
31+
GeonameID int64 `json:"geoname_id"`
32+
Country string `json:"country"`
33+
Region string `json:"region"`
34+
City string `json:"city"`
35+
Latitude float64 `json:"lat"`
36+
Longitude float64 `json:"lng"`
37+
PostalCode string `json:"postalCode"`
38+
Timezone string `json:"timezone"`
39+
}
40+
41+
type Proxy struct {
42+
Proxy bool `json:"proxy"`
43+
VPN bool `json:"vpn"`
44+
Tor bool `json:"tor"`
45+
}

0 commit comments

Comments
 (0)