Skip to content

Commit

Permalink
Add dbip
Browse files Browse the repository at this point in the history
  • Loading branch information
evalphobia committed Oct 29, 2021
1 parent 70afbee commit bd3bc63
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ it provides both of cli binary and golang API.

- [AbuseIPDB](https://www.abuseipdb.com/)
- [Big Data Cloud](https://www.bigdatacloud.com/)
- [dbip](https://db-ip.com)
- [IP2Proxy](https://www.ip2location.com/web-service/ip2proxy)
- [ip-api.com](https://ip-api.com/)
- [ipdata](https://ipdata.co/)
- [ipgeolocation](https://ipgeolocation.io/)
- [ipify.org](https://www.ipify.org/)
Expand Down Expand Up @@ -251,6 +253,7 @@ see example dir for more examples.
|:--|:--|
| `FRAUD_CHECK_ABUSEIPDB_APIKEY` | [AbuseIPDB API Key](https://docs.abuseipdb.com/). |
| `BIGDATACLOUD_APIKEY` | [Big Data Cloud API Key](https://www.bigdatacloud.com/sdk). |
| `FRAUD_CHECK_DBIP_APIKEY` | [dbip API key](https://db-ip.com/api/doc.php). |
| `FRAUD_CHECK_IP2PROXY_APIKEY` | [ip2proxy API key](https://www.ip2location.com/web-service/ip2proxy/). |
| `FRAUD_CHECK_IP2PROXY_PACKAGE` | [ip2proxy package parameter](https://www.ip2location.com/web-service/ip2proxy/). |
| `FRAUD_CHECK_IPDATACO_APIKEY` | [ipdata.co API key](https://docs.ipdata.co/). |
Expand Down
3 changes: 3 additions & 0 deletions cmd/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/evalphobia/go-ip-fraud-check/provider"
"github.com/evalphobia/go-ip-fraud-check/provider/abuseipdb"
"github.com/evalphobia/go-ip-fraud-check/provider/bigdatacloud"
"github.com/evalphobia/go-ip-fraud-check/provider/dbip"
"github.com/evalphobia/go-ip-fraud-check/provider/ip2proxy"
"github.com/evalphobia/go-ip-fraud-check/provider/ipapicom"
"github.com/evalphobia/go-ip-fraud-check/provider/ipdataco"
Expand All @@ -23,6 +24,7 @@ import (
const (
providerAbuseIPDB = "abuseipdb"
providerBigDataCloud = "bigdatacloud"
providerDBIP = "dbip"
providerIP2Proxy = "ip2proxy"
providerIPAPICom = "ip-api"
providerIPdataco = "ipdata"
Expand All @@ -39,6 +41,7 @@ const (
var providerMap = map[string]provider.Provider{
providerAbuseIPDB: &abuseipdb.AbuseIPDBProvider{},
providerBigDataCloud: &bigdatacloud.BigDataCloudProvider{},
providerDBIP: &dbip.DBIPProvider{},
providerIP2Proxy: &ip2proxy.IP2ProxyProvider{},
providerIPAPICom: &ipapicom.IPAPIComProvider{},
providerIPdataco: &ipdataco.IPdatacoProvider{},
Expand Down
11 changes: 11 additions & 0 deletions ipfraudcheck/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

const (
envAbuseIPDBAPIKey = "FRAUD_CHECK_ABUSEIPDB_APIKEY"
envDBIPAPIKey = "FRAUD_CHECK_DBIP_APIKEY"
envIP2ProxyAPIKey = "FRAUD_CHECK_IP2PROXY_APIKEY"
envIP2ProxyAPIPackage = "FRAUD_CHECK_IP2PROXY_PACKAGE"
envIPAPIComAPIKey = "FRAUD_CHECK_IPAPICOM_APIKEY"
Expand All @@ -30,6 +31,8 @@ const (
type Config struct {
// abuseipdb.com
AbuseIPDBAPIKey string
// db-ip.com
DBIPAPIKey string
// bigdatacloud.com
BigDataCloudAPIKey string
// ip2location.com
Expand Down Expand Up @@ -82,6 +85,14 @@ func (c Config) GetAbuseIPDBAPIKey() string {
return c.AbuseIPDBAPIKey
}

func (c Config) GetDBIPAPIKey() string {
s := os.Getenv(envDBIPAPIKey)
if s != "" {
return s
}
return c.DBIPAPIKey
}

func (c Config) GetIP2ProxyAPIKey() string {
s := os.Getenv(envIP2ProxyAPIKey)
if s != "" {
Expand Down
37 changes: 37 additions & 0 deletions provider/dbip/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dbip

import (
"github.com/evalphobia/go-ip-fraud-check/provider/privateclient"
)

const (
defaultBaseURL = "https://api.db-ip.com"
)

type Client struct {
apiKey string
privateclient.RESTClient
}

func NewClient(apiKey string) Client {
return Client{
apiKey: apiKey,
RESTClient: privateclient.RESTClient{
Option: privateclient.Option{
BaseURL: defaultBaseURL,
},
},
}
}

func (c *Client) SetDebug(b bool) {
c.RESTClient.Debug = b
}

func (c Client) LookUp(ipaddr string) (Response, error) {
params := make(map[string]string)

resp := Response{}
err := c.RESTClient.CallGET("/v2/"+c.apiKey+"/"+ipaddr, params, &resp)
return resp, err
}
72 changes: 72 additions & 0 deletions provider/dbip/provider_dbip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package dbip

import (
"errors"
"fmt"

"github.com/evalphobia/go-ip-fraud-check/ipfraudcheck"
"github.com/evalphobia/go-ip-fraud-check/provider"
)

type DBIPProvider struct {
client Client
}

func (p *DBIPProvider) Init(conf provider.Config) error {
c, ok := conf.(ipfraudcheck.Config)
if !ok {
return errors.New("incompatible config type for DBIPProvider")
}

apiKey := c.GetDBIPAPIKey()
if apiKey == "" {
return errors.New("apikey for dbip is empty. you must set directly or use 'FRAUD_CHECK_DBIP_APIKEY' envvar")
}
cli := NewClient(apiKey)
cli.SetDebug(c.Debug)
p.client = cli
return nil
}

func (p DBIPProvider) String() string {
return "dbip"
}

func (p DBIPProvider) CheckIP(ipaddr string) (provider.FraudCheckResponse, error) {
emptyResult := provider.FraudCheckResponse{}

resp, err := p.client.LookUp(ipaddr)
if err != nil {
return emptyResult, err
}

comment := ""
switch {
case len(resp.ThreatDetails) != 0:
comment = fmt.Sprintf("threats:%+v", resp.ThreatDetails)
}

return provider.FraudCheckResponse{
ServiceName: p.String(),
IP: resp.IPAddress,
ISP: resp.ISP,
Organization: resp.Organization,
ASNumber: resp.ASNumber,
Country: resp.CountryCode,
City: resp.City,
Latitude: resp.Latitude,
Longitude: resp.Longitude,
RiskScore: resp.GetRiskScore(),
IsProxy: resp.IsHTTPProxy(),
IsVPN: resp.IsVPN(),
IsHosting: resp.IsHosting(),
IsTor: resp.IsTor(),
IsBot: resp.IsBot(),
HasOtherThreat: resp.HasOtherThreat(),
ThreatComment: comment,
}, nil
}

func (p DBIPProvider) RawCheckIP(ipaddr string) (interface{}, error) {
return p.client.LookUp(ipaddr)
}
82 changes: 82 additions & 0 deletions provider/dbip/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package dbip

type Response struct {
IPAddress string `json:"ipAddress"`
ContinentCode string `json:"continentCode"`
ContinentName string `json:"continentName"`
CountryCode string `json:"countryCode"`
CountryName string `json:"countryName"`
IsEuMember bool `json:"isEuMember"`
CurrencyCode string `json:"currencyCode"`
CurrencyName string `json:"currencyName"`
PhonePrefix string `json:"phonePrefix"`
Languages []string `json:"languages"`
StateProvCode string `json:"stateProvCode"`
StateProv string `json:"stateProv"`
District string `json:"district"`
City string `json:"city"`
GeonameID int64 `json:"geonameId"`
ZipCode string `json:"zipCode"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
GMTOffset int64 `json:"gmtOffset"`
TimeZone string `json:"timeZone"`
WeatherCode string `json:"weatherCode"`
ASNumber int64 `json:"asNumber"`
ASName string `json:"asName"`
ISP string `json:"isp"`
LinkType string `json:"linkType"`
UsageType string `json:"usageType"`
Organization string `json:"organization"`
IsCrawler bool `json:"isCrawler"`
CrawlerName string `json:"crawlerName"`
IsProxy bool `json:"isProxy"`
ProxyType string `json:"proxyType"`
ThreatLevel string `json:"threatLevel"`
ThreatDetails []string `json:"threatDetails"`
}

func (r Response) IsHTTPProxy() bool {
return r.ProxyType == "http"
}

func (r Response) IsVPN() bool {
return r.ProxyType == "vpn"
}

func (r Response) IsTor() bool {
return r.ProxyType == "tor"
}

func (r Response) IsHosting() bool {
return r.UsageType == "hosting"
}

func (r Response) IsBot() bool {
for _, v := range r.ThreatDetails {
if v == "bot" {
return true
}
}
return false
}

func (r Response) HasOtherThreat() bool {
for _, v := range r.ThreatDetails {
if v == "attack-source" {
return true
}
}
return false
}

func (r Response) GetRiskScore() float64 {
switch r.ThreatLevel {
case "medium":
return 0.5
case "high":
return 0.75
default:
return 0
}
}

0 comments on commit bd3bc63

Please sign in to comment.