-
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
33daee1
commit f6a5c3c
Showing
8 changed files
with
176 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,41 @@ | ||
package abuseipdb | ||
|
||
import ( | ||
"github.com/evalphobia/go-ip-fraud-check/provider/privateclient" | ||
) | ||
|
||
const ( | ||
defaultBaseURL = "https://api.abuseipdb.com" | ||
) | ||
|
||
type Client struct { | ||
apiKey string | ||
privateclient.RESTClient | ||
} | ||
|
||
func NewClient(apiKey string) Client { | ||
return Client{ | ||
apiKey: apiKey, | ||
RESTClient: privateclient.RESTClient{ | ||
Option: privateclient.Option{ | ||
Headers: map[string]string{"KEY": apiKey}, | ||
BaseURL: defaultBaseURL, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (c *Client) SetDebug(b bool) { | ||
c.RESTClient.Debug = b | ||
} | ||
|
||
func (c Client) Check(ipaddr string) (Response, error) { | ||
params := make(map[string]string) | ||
params["ipAddress"] = ipaddr | ||
params["maxAgeInDays"] = "365" | ||
params["verbose"] = "" | ||
|
||
resp := Response{} | ||
err := c.RESTClient.CallGET("/api/v2/check", 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,68 @@ | ||
package abuseipdb | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/evalphobia/go-ip-fraud-check/ipfraudcheck" | ||
"github.com/evalphobia/go-ip-fraud-check/provider" | ||
) | ||
|
||
type AbuseIPDBProvider struct { | ||
client Client | ||
} | ||
|
||
func (p *AbuseIPDBProvider) Init(conf provider.Config) error { | ||
c, ok := conf.(ipfraudcheck.Config) | ||
if !ok { | ||
return errors.New("incompatible config type for AbuseIPDBProvider") | ||
} | ||
|
||
apiKey := c.GetAbuseUPDBAPIKey() | ||
if apiKey == "" { | ||
return errors.New("apikey for AbuseIPDB is empty. you must set directly or use 'FRAUD_CHECK_ABUSEIPDB_APIKEY' envvar") | ||
} | ||
cli := NewClient(apiKey) | ||
cli.SetDebug(c.Debug) | ||
p.client = cli | ||
return nil | ||
} | ||
|
||
func (p AbuseIPDBProvider) String() string { | ||
return "AbuseIPDB" | ||
} | ||
|
||
func (p AbuseIPDBProvider) CheckIP(ipaddr string) (provider.FraudCheckResponse, error) { | ||
emptyResult := provider.FraudCheckResponse{} | ||
|
||
resp, err := p.client.Check(ipaddr) | ||
if err != nil { | ||
return emptyResult, err | ||
} | ||
|
||
d := resp.Data | ||
var comments []string | ||
if d.NumDistinctUsers != 0 { | ||
comments = append(comments, fmt.Sprintf("distinct_users=[%d]", d.NumDistinctUsers)) | ||
} | ||
if d.UsageType != "" { | ||
comments = append(comments, d.UsageType) | ||
} | ||
|
||
return provider.FraudCheckResponse{ | ||
ServiceName: p.String(), | ||
IP: ipaddr, | ||
Hostname: d.Domain, | ||
ISP: d.ISP, | ||
Country: d.CountryCode, | ||
HasOtherThreat: d.TotalReports > 0, | ||
IsHosting: d.IsHosting(), | ||
RiskScore: float64(d.AbuseConfidenceScore) / 100, | ||
ThreatComment: strings.Join(comments, " | "), | ||
}, nil | ||
} | ||
|
||
func (p AbuseIPDBProvider) RawCheckIP(ipaddr string) (interface{}, error) { | ||
return p.client.Check(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,41 @@ | ||
package abuseipdb | ||
|
||
type Response struct { | ||
Data Data `json:"data"` | ||
} | ||
|
||
type Data struct { | ||
IPAddress string `json:"ipAddress"` | ||
IPVersion int64 `json:"ipVersion"` | ||
ISP string `json:"isp"` | ||
Domain string `json:"domain"` | ||
Hostnames []string `json:"hostnames"` | ||
CountryCode string `json:"countryCode"` | ||
CountryName string `json:"countryName"` | ||
IsPublic bool `json:"isPublic"` | ||
IsWhitelisted bool `json:"isWhitelisted"` | ||
AbuseConfidenceScore int64 `json:"abuseConfidenceScore"` | ||
UsageType string `json:"usageType"` | ||
TotalReports int64 `json:"totalReports"` | ||
NumDistinctUsers int64 `json:"numDistinctUsers"` | ||
LastReportedAt string `json:"lastReportedAt"` | ||
Reports []Report `json:"reports"` | ||
} | ||
|
||
func (d Data) IsHosting() bool { | ||
_, ok := hostingMap[d.UsageType] | ||
return ok | ||
} | ||
|
||
type Report struct { | ||
ReportedAt string `json:"reportedAt"` | ||
Comment string `json:"comment"` | ||
Categories []int64 `json:"categories"` | ||
ReporterID int64 `json:"reporterId"` | ||
ReporterCountryCode string `json:"reporterCountryCode"` | ||
ReporterCountryName string `json:"reporterCountryName"` | ||
} | ||
|
||
var hostingMap = map[string]struct{}{ | ||
"Data Center/Web Hosting/Transit": struct{}{}, | ||
} |
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