This repository has been archived by the owner on Jan 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheck-sfs.go
92 lines (79 loc) · 1.71 KB
/
check-sfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Check for an IP that is in the stopforumspam.com blacklist.
//
package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
"time"
)
//
// Register ourself as a blogspam-plugin.
//
func init() {
registerPlugin(BlogspamPlugin{Name: "80-sfs.js",
Description: "Look for blacklisted IPs via stopforumspam.com",
Author: "Steve Kemp <[email protected]>",
Test: checkSFSBlacklist,
RedisCache: true})
}
//
// Lookup the IP address of the submitter in the stopforumspam.com blacklist.
//
func checkSFSBlacklist(x Submission) (PluginResult, string) {
//
// See if we have an IPv4 address.
//
regex := regexp.MustCompile("^([0-9]+).([0-9]+).([0-9]+).([0-9]+)$")
match := regex.FindStringSubmatch(x.IP)
//
// If that failed then we know we have an IPv6-address, or a missing
// address, so we terminate
//
if len(match) <= 0 {
return Undecided, ""
}
//
// Build a client with sane timeout
//
var netClient = &http.Client{
Timeout: time.Second * 10,
}
//
// The URL we'll fetch
//
url := fmt.Sprintf("http://www.stopforumspam.com/api?ip=%s", x.IP)
//
// Make the request
//
response, err := netClient.Get(url)
//
// Handle error
//
if err != nil {
fmt.Printf("WARNING: HTTP-Error reading from %s - %s", url, err)
return Error, err.Error()
}
//
// Ensure we close the body
//
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("WARNING: HTTP-Error reading body from %s - %s", url, err)
return Error, err.Error()
}
//
// Does it appear?
//
if strings.Contains(string(contents), "<appears>yes</appears>") {
return Spam, "Listed in StopForumSpam.com"
}
//
// Not listed
//
return Undecided, ""
}