Go
Check and report with net/http, including a small middleware.
Plain net/http, no dependencies.
Check an address
func isipsafeScore(ctx context.Context, ip string) (int, error) {
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, "GET", "https://isipsafe.com/api/v1/check?ip="+url.QueryEscape(ip), nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
var out struct {
Data struct {
Score int `json:"abuseConfidenceScore"`
} `json:"data"`
}
err = json.NewDecoder(resp.Body).Decode(&out)
return out.Data.Score, err
}
Middleware
func blockListed(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
if score, err := isipsafeScore(r.Context(), ip); err == nil && score >= 75 {
http.Error(w, "access denied", http.StatusForbidden)
return
}
next.ServeHTTP(w, r) // fail open on errors
})
}
Add a small cache (a map with expiry, or a library like golang-lru) before using this on hot paths.
Report an address
form := url.Values{"ip": {ip}, "categories": {"web-app-attack,brute-force"}, "comment": {"40 failed logins"}}
resp, err := http.PostForm("https://isipsafe.com/api/v1/report?key="+os.Getenv("ISIPSAFE_KEY"), form)
Need a hand?
If something here does not work with your version of Go, or you have improved it, tell us. We update these pages with what people send.
Go and its logo are trademarks of their respective owners. isipsafe is not affiliated with or endorsed by them.