forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiplist.go
185 lines (172 loc) · 4.04 KB
/
iplist.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Package iplist handles the P2P Plaintext Format described by
// https://en.wikipedia.org/wiki/PeerGuardian#P2P_plaintext_format.
package iplist
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"net"
"sort"
)
// An abstraction of IP list implementations.
type Ranger interface {
// Return a Range containing the IP.
Lookup(net.IP) (r Range, ok bool)
// If your ranges hurt, use this.
NumRanges() int
}
type IPList struct {
ranges []Range
}
type Range struct {
First, Last net.IP
Description string
}
func (r Range) String() string {
return fmt.Sprintf("%s-%s: %s", r.First, r.Last, r.Description)
}
// Create a new IP list. The given ranges must already sorted by the lower
// bound IP in each range. Behaviour is undefined for lists of overlapping
// ranges.
func New(initSorted []Range) *IPList {
return &IPList{
ranges: initSorted,
}
}
func (ipl *IPList) NumRanges() int {
if ipl == nil {
return 0
}
return len(ipl.ranges)
}
// Return the range the given IP is in. ok if false if no range is found.
func (ipl *IPList) Lookup(ip net.IP) (r Range, ok bool) {
if ipl == nil {
return
}
// TODO: Perhaps all addresses should be converted to IPv6, if the future
// of IP is to always be backwards compatible. But this will cost 4x the
// memory for IPv4 addresses?
v4 := ip.To4()
if v4 != nil {
r, ok = ipl.lookup(v4)
if ok {
return
}
}
v6 := ip.To16()
if v6 != nil {
return ipl.lookup(v6)
}
if v4 == nil && v6 == nil {
r = Range{
Description: "bad IP",
}
ok = true
}
return
}
// Return a range that contains ip, or nil.
func lookup(
first func(i int) net.IP,
full func(i int) Range,
n int,
ip net.IP,
) (
r Range, ok bool,
) {
// Find the index of the first range for which the following range exceeds
// it.
i := sort.Search(n, func(i int) bool {
if i+1 >= n {
return true
}
return bytes.Compare(ip, first(i+1)) < 0
})
if i == n {
return
}
r = full(i)
ok = bytes.Compare(r.First, ip) <= 0 && bytes.Compare(ip, r.Last) <= 0
return
}
// Return the range the given IP is in. Returns nil if no range is found.
func (ipl *IPList) lookup(ip net.IP) (Range, bool) {
return lookup(func(i int) net.IP {
return ipl.ranges[i].First
}, func(i int) Range {
return ipl.ranges[i]
}, len(ipl.ranges), ip)
}
func minifyIP(ip *net.IP) {
v4 := ip.To4()
if v4 != nil {
*ip = append(make([]byte, 0, 4), v4...)
}
}
// Parse a line of the PeerGuardian Text Lists (P2P) Format. Returns !ok but
// no error if a line doesn't contain a range but isn't erroneous, such as
// comment and blank lines.
func ParseBlocklistP2PLine(l []byte) (r Range, ok bool, err error) {
l = bytes.TrimSpace(l)
if len(l) == 0 || bytes.HasPrefix(l, []byte("#")) {
return
}
// TODO: Check this when IPv6 blocklists are available.
colon := bytes.LastIndexAny(l, ":")
if colon == -1 {
err = errors.New("missing colon")
return
}
hyphen := bytes.IndexByte(l[colon+1:], '-')
if hyphen == -1 {
err = errors.New("missing hyphen")
return
}
hyphen += colon + 1
r.Description = string(l[:colon])
r.First = net.ParseIP(string(l[colon+1 : hyphen]))
minifyIP(&r.First)
r.Last = net.ParseIP(string(l[hyphen+1:]))
minifyIP(&r.Last)
if r.First == nil || r.Last == nil || len(r.First) != len(r.Last) {
err = errors.New("bad IP range")
return
}
ok = true
return
}
// Creates an IPList from a line-delimited P2P Plaintext file.
func NewFromReader(f io.Reader) (ret *IPList, err error) {
var ranges []Range
// There's a lot of similar descriptions, so we maintain a pool and reuse
// them to reduce memory overhead.
uniqStrs := make(map[string]string)
scanner := bufio.NewScanner(f)
lineNum := 1
for scanner.Scan() {
r, ok, lineErr := ParseBlocklistP2PLine(scanner.Bytes())
if lineErr != nil {
err = fmt.Errorf("error parsing line %d: %s", lineNum, lineErr)
return
}
lineNum++
if !ok {
continue
}
if s, ok := uniqStrs[r.Description]; ok {
r.Description = s
} else {
uniqStrs[r.Description] = r.Description
}
ranges = append(ranges, r)
}
err = scanner.Err()
if err != nil {
return
}
ret = New(ranges)
return
}