-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70afbee
commit bd3bc63
Showing
6 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |