21 func (r *Range) String() string {
22 return fmt.Sprintf("%s-%s (%s)", r.First, r.Last, r.Description)
25 // Create a new IP list. The given range must already sorted by the lower IP
26 // in the range. Behaviour is undefined for lists of overlapping ranges.
27 func New(initSorted []Range) *IPList {
33 // Return the range the given IP is in. Returns nil if no range is found.
34 func (me *IPList) Lookup(ip net.IP) (r *Range) {
35 // Find the index of the first range for which the following range exceeds
37 i := sort.Search(len(me.ranges), func(i int) bool {
38 if i+1 >= len(me.ranges) {
41 return bytes.Compare(ip, me.ranges[i+1].First) < 0
43 if i == len(me.ranges) {
47 if bytes.Compare(ip, r.First) < 0 || bytes.Compare(ip, r.Last) > 0 {
53 // Parse a line of the PeerGuardian Text Lists (P2P) Format. Returns !ok but
54 // no error if a line doesn't contain a range but isn't erroneous, such as
55 // comment and blank lines.
56 func ParseBlocklistP2PLine(l string) (r Range, ok bool, err error) {
57 l = strings.TrimSpace(l)
58 if l == "" || strings.HasPrefix(l, "#") {
61 sms := regexp.MustCompile(`(.*):([\d.]+)-([\d.]+)`).FindStringSubmatch(l)
63 err = fmt.Errorf("error parsing %q", l)
66 r.Description = sms[1]
67 r.First = net.ParseIP(sms[2])
68 r.Last = net.ParseIP(sms[3])
69 if r.First == nil || r.Last == nil {