From 99a06e3f504af587b050c8a79adbc96ab80245f4 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 22 Jun 2015 19:44:59 +1000 Subject: [PATCH] Move IP blocklist loading into iplist package --- client.go | 28 +--------------------------- iplist/iplist.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/client.go b/client.go index 171abd71..2c9d522e 100644 --- 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 } diff --git a/iplist/iplist.go b/iplist/iplist.go index 93fb9b31..18d6b0a3 100644 --- a/iplist/iplist.go +++ b/iplist/iplist.go @@ -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 +} -- 2.48.1