Last active
August 29, 2015 14:07
-
-
Save techjanitor/105885394a785fa9b0db to your computer and use it in GitHub Desktop.
Query Stop Forum Spam
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
func CheckStopForumSpam(ip string) error { | |
sfs_endpoint, err := url.Parse("http://api.stopforumspam.org/api") | |
if err != nil { | |
return errors.New("error parsing sfs endpoint") | |
} | |
queryValues := url.Values{} | |
if ip == "" { | |
return errors.New("no ip provided") | |
} | |
queryValues.Add("ip", ip) | |
queryValues.Add("f", "json") | |
sfs_endpoint.RawQuery = queryValues.Encode() | |
res, err := http.Get(sfs_endpoint.String()) | |
if err != nil { | |
return errors.New("error reaching sfs") | |
} | |
defer res.Body.Close() | |
res.Header.Add("User-Agent", "Pram/1.2") | |
body, err := ioutil.ReadAll(res.Body) | |
if err != nil { | |
return errors.New("error parsing sfs response") | |
} | |
sfs_data := StopForumSpam{} | |
err = json.Unmarshal(body, &sfs_data) | |
if err != nil { | |
return errors.New("error parsing sfs data") | |
} | |
if sfs_data.Ip.Confidence > SfsConfidence { | |
return errors.New("ip on blacklist") | |
} | |
return nil | |
} | |
// Stop Forum Spam return format | |
type StopForumSpam struct { | |
Ip struct { | |
Appears float64 `json:"appears"` | |
Confidence float64 `json:"confidence"` | |
Frequency float64 `json:"frequency"` | |
Lastseen string `json:"lastseen"` | |
} `json:"ip"` | |
Success float64 `json:"success"` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment