-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspamhaus_origin.go
89 lines (76 loc) · 1.6 KB
/
spamhaus_origin.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
package main
import (
"errors"
"sort"
"strings"
)
type SpamhausOrigin struct {
httpJson HttpJson
Emitter
}
func SpamhausOriginCreate() (*SpamhausOrigin, error) {
return &SpamhausOrigin{
httpJson: HttpJsonCreate(),
Emitter: Emitter{
id: "cloudflare",
},
}, nil
}
func (s *SpamhausOrigin) runUrl(url string, blocks *Blocks) error {
body, err := s.httpJson.FetchBody(url, "GET")
if err != nil {
return err
}
for _, line := range strings.Split(string(body), "\n") {
if len(line) == 0 || line[0] == ';' {
continue
}
pieces := strings.Split(line, " ; ")
if len(pieces) != 2 {
return errors.New("Unexpected droplist line")
}
value := "Spamhaus" + "/" + pieces[1]
block, err := BlockCreateWithCidr(&pieces[0], &value)
if err != nil {
return err
}
*blocks = append(*blocks, block)
}
return nil
}
func (s *SpamhausOrigin) Run(ipv4Only bool) error {
header := Header{
general: `#
# IP to Spamhaus mapping
#
# https://www.spamhaus.org/drop/drop.lasso
# https://www.spamhaus.org/drop/edrop.lasso
# https://www.spamhaus.org/drop/dropv6.txt
#
`,
columns: "# cidr Spamhaus/id\n",
}
if err := s.Header(header); err != nil {
return err
}
blocks := make(Blocks, 0)
err := s.runUrl("https://www.spamhaus.org/drop/drop.lasso", &blocks)
if err != nil {
return err
}
err = s.runUrl("https://www.spamhaus.org/drop/edrop.lasso", &blocks)
if err != nil {
return err
}
if !ipv4Only {
err = s.runUrl("https://www.spamhaus.org/drop/dropv6.txt", &blocks)
if err != nil {
return err
}
}
sort.Sort(blocks)
for _, block := range blocks {
s.Emit(block)
}
return s.Done()
}