]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Move IP blocklist loading into iplist package
authorMatt Joiner <anacrolix@gmail.com>
Mon, 22 Jun 2015 09:44:59 +0000 (19:44 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 22 Jun 2015 09:44:59 +0000 (19:44 +1000)
client.go
iplist/iplist.go

index 171abd71fb525f372612bf36121efd090562eaf5..2c9d522e33e1b5c8f1426322e377f19ae41e1494 100644 (file)
--- a/client.go
+++ b/client.go
@@ -377,33 +377,7 @@ func (cl *Client) setEnvBlocklist() (err error) {
                return
        }
        defer f.Close()
-       var ranges []iplist.Range
-       uniqStrs := make(map[string]string)
-       scanner := bufio.NewScanner(f)
-       lineNum := 1
-       for scanner.Scan() {
-               r, ok, lineErr := iplist.ParseBlocklistP2PLine(scanner.Bytes())
-               if lineErr != nil {
-                       err = fmt.Errorf("error reading torrent blocklist 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 {
-               err = fmt.Errorf("error reading torrent blocklist: %s", err)
-               return
-       }
-       cl.ipBlockList = iplist.New(ranges)
+       cl.ipBlockList, err = iplist.NewFromReader(f)
        return
 }
 
index 93fb9b31ad8bfb7a63e5bcbf90eb18d4d2b04c5b..18d6b0a383c4a8455161fa355b2bcf6d28652acc 100644 (file)
@@ -1,9 +1,13 @@
+// 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"
 )
@@ -123,3 +127,36 @@ func ParseBlocklistP2PLine(l []byte) (r Range, ok bool, err error) {
        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
+}