2014-11-28 18:04:15 +00:00
|
|
|
package iplist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-03-07 06:09:18 +00:00
|
|
|
"errors"
|
2014-11-28 18:04:15 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IPList struct {
|
|
|
|
ranges []Range
|
|
|
|
}
|
|
|
|
|
|
|
|
type Range struct {
|
|
|
|
First, Last net.IP
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
2014-11-30 02:30:04 +00:00
|
|
|
func (r *Range) String() string {
|
|
|
|
return fmt.Sprintf("%s-%s (%s)", r.First, r.Last, r.Description)
|
|
|
|
}
|
|
|
|
|
2015-03-08 06:28:14 +00: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-28 18:04:15 +00:00
|
|
|
func New(initSorted []Range) *IPList {
|
|
|
|
return &IPList{
|
|
|
|
ranges: initSorted,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 00:25:22 +00:00
|
|
|
func (me *IPList) NumRanges() int {
|
|
|
|
if me == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return len(me.ranges)
|
|
|
|
}
|
|
|
|
|
2014-11-28 18:04:15 +00:00
|
|
|
// Return the range the given IP is in. Returns nil if no range is found.
|
|
|
|
func (me *IPList) Lookup(ip net.IP) (r *Range) {
|
2015-02-25 00:25:22 +00:00
|
|
|
if me == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-07 06:09:39 +00: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 15:54:17 +00:00
|
|
|
v4 := ip.To4()
|
|
|
|
if v4 != nil {
|
2015-03-07 06:09:39 +00:00
|
|
|
r = me.lookup(v4)
|
|
|
|
if r != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-03-27 15:54:17 +00:00
|
|
|
v6 := ip.To16()
|
|
|
|
if v6 != nil {
|
2015-03-07 06:09:39 +00:00
|
|
|
return me.lookup(v6)
|
|
|
|
}
|
2015-03-27 15:54:17 +00:00
|
|
|
if v4 == nil && v6 == nil {
|
|
|
|
return &Range{
|
|
|
|
Description: fmt.Sprintf("unsupported IP: %s", ip),
|
|
|
|
}
|
|
|
|
}
|
2015-03-07 06:09:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the range the given IP is in. Returns nil if no range is found.
|
|
|
|
func (me *IPList) lookup(ip net.IP) (r *Range) {
|
2014-11-28 18:04:15 +00:00
|
|
|
// Find the index of the first range for which the following range exceeds
|
|
|
|
// it.
|
|
|
|
i := sort.Search(len(me.ranges), func(i int) bool {
|
|
|
|
if i+1 >= len(me.ranges) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return bytes.Compare(ip, me.ranges[i+1].First) < 0
|
|
|
|
})
|
|
|
|
if i == len(me.ranges) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r = &me.ranges[i]
|
|
|
|
if bytes.Compare(ip, r.First) < 0 || bytes.Compare(ip, r.Last) > 0 {
|
|
|
|
r = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-08 06:43:44 +00:00
|
|
|
func minifyIP(ip *net.IP) {
|
|
|
|
v4 := ip.To4()
|
|
|
|
if v4 != nil {
|
|
|
|
*ip = append(make([]byte, 0, 4), v4...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-28 18:04:15 +00: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 14:53:01 +00: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-28 18:04:15 +00:00
|
|
|
return
|
|
|
|
}
|
2015-03-09 06:36:07 +00:00
|
|
|
// TODO: Check this when IPv6 blocklists are available.
|
2015-03-08 06:43:44 +00:00
|
|
|
colon := bytes.LastIndexAny(l, ":")
|
2015-03-07 06:09:18 +00: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 14:53:01 +00:00
|
|
|
r.Description = string(l[:colon])
|
|
|
|
r.First = net.ParseIP(string(l[colon+1 : hyphen]))
|
2015-03-08 06:43:44 +00:00
|
|
|
minifyIP(&r.First)
|
2015-01-30 14:53:01 +00:00
|
|
|
r.Last = net.ParseIP(string(l[hyphen+1:]))
|
2015-03-08 06:43:44 +00: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-28 18:04:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|