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-ip.go
119 lines (99 loc) · 2.01 KB
/
check-ip.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// Check for local IP blacklist.
//
package main
import (
"fmt"
"net"
"regexp"
"strings"
)
//
// Register ourself as a blogspam-plugin.
//
func init() {
registerPlugin(BlogspamPlugin{Name: "20-ip.js",
Description: "Look for blacklisted IP addresses",
Author: "Steve Kemp <[email protected]>",
Test: checkBlacklist})
}
//
// Test that the submitter isn't blacklisted, by IP.
//
func checkBlacklist(x Submission) (PluginResult, string) {
//
// Map to store any IPs we're to blacklist
//
tmp := make(map[string]int)
//
// Do we have options?
//
if len(x.Options) > 0 {
//
// Split the string into an array, based on commas
//
options := strings.Split(x.Options, ",")
//
// Now look for key=val
//
for _, option := range options {
re := regexp.MustCompile("blacklist=([^=]+)$")
match := re.FindStringSubmatch(option)
if len(match) > 0 {
tmp[match[1]] = 1
}
}
}
//
// The source IP we're going to test against the blacklisted entries.
//
source := net.ParseIP(x.IP)
//
// If we have some blacklisted IPs..
//
for ip := range tmp {
// Only parse the CIDR if it looks like one.
if strings.Contains(ip, "/") {
// Parse the network
_, subnet, err := net.ParseCIDR(ip)
if err != nil {
return Error, fmt.Sprintf("Failed to parse CIDR %s", ip)
}
// Is it in there?
if subnet.Contains(source) {
return Spam, "IP blacklisted"
}
} else {
// Is it a literal match?
if x.IP == ip {
return Spam, "IP blacklisted"
}
}
}
//
// If Redis is not available we're done
//
if redisHandle == nil {
return Undecided, ""
}
//
// Since we have redis-enabled we'll now look for the remote IP too.
//
// The key is named `blacklist-$IP`
//
key := fmt.Sprintf("blacklist-%s", x.IP)
//
// Run the lookup
//
result, _ := redisHandle.Get(key).Result()
//
// If there was a result then it is spam
//
if len(result) > 0 {
return Spam, result
}
//
// Not blocked by options, or previous attempts
//
return Undecided, ""
}