2015-06-22 17:44:59 +08:00
|
|
|
// Package iplist handles the P2P Plaintext Format described by
|
|
|
|
// https://en.wikipedia.org/wiki/PeerGuardian#P2P_plaintext_format.
|
2014-11-29 02:04:15 +08:00
|
|
|
package iplist
|
|
|
|
|
|
|
|
import (
|
2015-06-22 17:44:59 +08:00
|
|
|
"bufio"
|
2014-11-29 02:04:15 +08:00
|
|
|
"bytes"
|
2015-03-07 14:09:18 +08:00
|
|
|
"errors"
|
2014-11-29 02:04:15 +08:00
|
|
|
"fmt"
|
2015-06-22 17:44:59 +08:00
|
|
|
"io"
|
2014-11-29 02:04:15 +08:00
|
|
|
"net"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
2015-09-23 16:25:22 +08:00
|
|
|
// An abstraction of IP list implementations.
|
|
|
|
type Ranger interface {
|
|
|
|
// Return a Range containing the IP.
|
2015-10-18 21:00:26 +08:00
|
|
|
Lookup(net.IP) (r Range, ok bool)
|
2015-09-23 16:25:22 +08:00
|
|
|
// If your ranges hurt, use this.
|
|
|
|
NumRanges() int
|
|
|
|
}
|
|
|
|
|
2014-11-29 02:04:15 +08:00
|
|
|
type IPList struct {
|
|
|
|
ranges []Range
|
|
|
|
}
|
|
|
|
|
|
|
|
type Range struct {
|
|
|
|
First, Last net.IP
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
2014-11-30 10:30:04 +08:00
|
|
|
func (r *Range) String() string {
|
|
|
|
return fmt.Sprintf("%s-%s (%s)", r.First, r.Last, r.Description)
|
|
|
|
}
|
|
|
|
|
2015-03-08 14:28:14 +08:00
|
|
|
// 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.
|
2014-11-29 02:04:15 +08:00
|
|
|
func New(initSorted []Range) *IPList {
|
|
|
|
return &IPList{
|
|
|
|
ranges: initSorted,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (ipl *IPList) NumRanges() int {
|
|
|
|
if ipl == nil {
|
2015-02-25 08:25:22 +08:00
|
|
|
return 0
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
return len(ipl.ranges)
|
2015-02-25 08:25:22 +08:00
|
|
|
}
|
|
|
|
|
2014-11-29 02:04:15 +08:00
|
|
|
// Return the range the given IP is in. Returns nil if no range is found.
|
2016-04-19 12:11:11 +08:00
|
|
|
func (ipl *IPList) Lookup(ip net.IP) (r Range, ok bool) {
|
|
|
|
if ipl == nil {
|
2015-10-18 21:00:26 +08:00
|
|
|
return
|
2015-02-25 08:25:22 +08:00
|
|
|
}
|
2015-03-07 14:09:39 +08:00
|
|
|
// 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?
|
2015-03-27 23:54:17 +08:00
|
|
|
v4 := ip.To4()
|
|
|
|
if v4 != nil {
|
2016-04-19 12:11:11 +08:00
|
|
|
r, ok = ipl.lookup(v4)
|
2015-10-18 21:00:26 +08:00
|
|
|
if ok {
|
2015-03-07 14:09:39 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-03-27 23:54:17 +08:00
|
|
|
v6 := ip.To16()
|
|
|
|
if v6 != nil {
|
2016-04-19 12:11:11 +08:00
|
|
|
return ipl.lookup(v6)
|
2015-03-07 14:09:39 +08:00
|
|
|
}
|
2015-03-27 23:54:17 +08:00
|
|
|
if v4 == nil && v6 == nil {
|
2015-10-18 21:00:26 +08:00
|
|
|
r = Range{
|
2015-09-23 16:25:22 +08:00
|
|
|
Description: "bad IP",
|
2015-03-27 23:54:17 +08:00
|
|
|
}
|
2015-10-18 21:00:26 +08:00
|
|
|
ok = true
|
2015-03-27 23:54:17 +08:00
|
|
|
}
|
2015-10-18 21:00:26 +08:00
|
|
|
return
|
2015-03-07 14:09:39 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 16:25:22 +08:00
|
|
|
// Return a range that contains ip, or nil.
|
2015-10-18 21:00:26 +08:00
|
|
|
func lookup(
|
|
|
|
first func(i int) net.IP,
|
|
|
|
full func(i int) Range,
|
|
|
|
n int,
|
|
|
|
ip net.IP,
|
|
|
|
) (
|
|
|
|
r Range, ok bool,
|
|
|
|
) {
|
2014-11-29 02:04:15 +08:00
|
|
|
// Find the index of the first range for which the following range exceeds
|
|
|
|
// it.
|
2015-09-23 16:25:22 +08:00
|
|
|
i := sort.Search(n, func(i int) bool {
|
|
|
|
if i+1 >= n {
|
2014-11-29 02:04:15 +08:00
|
|
|
return true
|
|
|
|
}
|
2015-10-18 21:00:26 +08:00
|
|
|
return bytes.Compare(ip, first(i+1)) < 0
|
2014-11-29 02:04:15 +08:00
|
|
|
})
|
2015-09-23 16:25:22 +08:00
|
|
|
if i == n {
|
2015-10-18 21:00:26 +08:00
|
|
|
return
|
2014-11-29 02:04:15 +08:00
|
|
|
}
|
2015-10-18 21:00:26 +08:00
|
|
|
r = full(i)
|
|
|
|
ok = bytes.Compare(r.First, ip) <= 0 && bytes.Compare(ip, r.Last) <= 0
|
|
|
|
return
|
2015-09-23 16:25:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the range the given IP is in. Returns nil if no range is found.
|
2016-04-19 12:11:11 +08:00
|
|
|
func (ipl *IPList) lookup(ip net.IP) (Range, bool) {
|
2015-10-18 21:00:26 +08:00
|
|
|
return lookup(func(i int) net.IP {
|
2016-04-19 12:11:11 +08:00
|
|
|
return ipl.ranges[i].First
|
2015-10-18 21:00:26 +08:00
|
|
|
}, func(i int) Range {
|
2016-04-19 12:11:11 +08:00
|
|
|
return ipl.ranges[i]
|
|
|
|
}, len(ipl.ranges), ip)
|
2014-11-29 02:04:15 +08:00
|
|
|
}
|
|
|
|
|
2015-03-08 14:43:44 +08:00
|
|
|
func minifyIP(ip *net.IP) {
|
|
|
|
v4 := ip.To4()
|
|
|
|
if v4 != nil {
|
|
|
|
*ip = append(make([]byte, 0, 4), v4...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-29 02:04:15 +08:00
|
|
|
// 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.
|
2015-01-30 22:53:01 +08:00
|
|
|
func ParseBlocklistP2PLine(l []byte) (r Range, ok bool, err error) {
|
|
|
|
l = bytes.TrimSpace(l)
|
|
|
|
if len(l) == 0 || bytes.HasPrefix(l, []byte("#")) {
|
2014-11-29 02:04:15 +08:00
|
|
|
return
|
|
|
|
}
|
2015-03-09 14:36:07 +08:00
|
|
|
// TODO: Check this when IPv6 blocklists are available.
|
2015-03-08 14:43:44 +08:00
|
|
|
colon := bytes.LastIndexAny(l, ":")
|
2015-03-07 14:09:18 +08:00
|
|
|
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
|
2015-01-30 22:53:01 +08:00
|
|
|
r.Description = string(l[:colon])
|
|
|
|
r.First = net.ParseIP(string(l[colon+1 : hyphen]))
|
2015-03-08 14:43:44 +08:00
|
|
|
minifyIP(&r.First)
|
2015-01-30 22:53:01 +08:00
|
|
|
r.Last = net.ParseIP(string(l[hyphen+1:]))
|
2015-03-08 14:43:44 +08:00
|
|
|
minifyIP(&r.Last)
|
|
|
|
if r.First == nil || r.Last == nil || len(r.First) != len(r.Last) {
|
|
|
|
err = errors.New("bad IP range")
|
2014-11-29 02:04:15 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
2015-06-22 17:44:59 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|