20 func (r *Range) String() string {
21 return fmt.Sprintf("%s-%s (%s)", r.First, r.Last, r.Description)
24 // Create a new IP list. The given ranges must already sorted by the lower
25 // bound IP in each range. Behaviour is undefined for lists of overlapping
27 func New(initSorted []Range) *IPList {
33 func (me *IPList) NumRanges() int {
40 // Return the range the given IP is in. Returns nil if no range is found.
41 func (me *IPList) Lookup(ip net.IP) (r *Range) {
45 // TODO: Perhaps all addresses should be converted to IPv6, if the future
46 // of IP is to always be backwards compatible. But this will cost 4x the
47 // memory for IPv4 addresses?
48 if v4 := ip.To4(); v4 != nil {
54 if v6 := ip.To16(); v6 != nil {
60 // Return the range the given IP is in. Returns nil if no range is found.
61 func (me *IPList) lookup(ip net.IP) (r *Range) {
62 // Find the index of the first range for which the following range exceeds
64 i := sort.Search(len(me.ranges), func(i int) bool {
65 if i+1 >= len(me.ranges) {
68 return bytes.Compare(ip, me.ranges[i+1].First) < 0
70 if i == len(me.ranges) {
74 if bytes.Compare(ip, r.First) < 0 || bytes.Compare(ip, r.Last) > 0 {
80 func minifyIP(ip *net.IP) {
83 *ip = append(make([]byte, 0, 4), v4...)
87 // Parse a line of the PeerGuardian Text Lists (P2P) Format. Returns !ok but
88 // no error if a line doesn't contain a range but isn't erroneous, such as
89 // comment and blank lines.
90 func ParseBlocklistP2PLine(l []byte) (r Range, ok bool, err error) {
91 l = bytes.TrimSpace(l)
92 if len(l) == 0 || bytes.HasPrefix(l, []byte("#")) {
95 // TODO: Something tells me this will end badly when IPv6 blocklists are
97 colon := bytes.LastIndexAny(l, ":")
99 err = errors.New("missing colon")
102 hyphen := bytes.IndexByte(l[colon+1:], '-')
104 err = errors.New("missing hyphen")
108 r.Description = string(l[:colon])
109 r.First = net.ParseIP(string(l[colon+1 : hyphen]))
111 r.Last = net.ParseIP(string(l[hyphen+1:]))
113 if r.First == nil || r.Last == nil || len(r.First) != len(r.Last) {
114 err = errors.New("bad IP range")