client.go | 2 +- iplist/iplist.go | 17 ++++++++--------- iplist/iplist_test.go | 2 +- diff --git a/client.go b/client.go index cd65f55de54994213f2b8c759148b0129b673c2b..513e7871d2231177ad8abccffb396b421ff22788 100644 --- a/client.go +++ b/client.go @@ -347,7 +347,7 @@ defer f.Close() var ranges []iplist.Range scanner := bufio.NewScanner(f) for scanner.Scan() { - r, ok, lineErr := iplist.ParseBlocklistP2PLine(scanner.Text()) + r, ok, lineErr := iplist.ParseBlocklistP2PLine(scanner.Bytes()) if lineErr != nil { err = fmt.Errorf("error reading torrent blocklist line: %s", lineErr) return diff --git a/iplist/iplist.go b/iplist/iplist.go index 9fe9871efe88e2e85bce725fc4cd78a0b2dd6e7e..ed8ac06e8db807a3c839a4eadb0983c0673bd030 100644 --- a/iplist/iplist.go +++ b/iplist/iplist.go @@ -5,7 +5,6 @@ "bytes" "fmt" "net" "sort" - "strings" ) type IPList struct { @@ -52,16 +51,16 @@ // 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. -func ParseBlocklistP2PLine(l string) (r Range, ok bool, err error) { - l = strings.TrimSpace(l) - if len(l) == 0 || strings.HasPrefix(l, "#") { +func ParseBlocklistP2PLine(l []byte) (r Range, ok bool, err error) { + l = bytes.TrimSpace(l) + if len(l) == 0 || bytes.HasPrefix(l, []byte("#")) { return } - colon := strings.IndexByte(l, ':') - hyphen := strings.IndexByte(l[colon+1:], '-') + colon + 1 - r.Description = l[:colon] - r.First = net.ParseIP(l[colon+1 : hyphen]) - r.Last = net.ParseIP(l[hyphen+1:]) + colon := bytes.IndexByte(l, ':') + hyphen := bytes.IndexByte(l[colon+1:], '-') + colon + 1 + r.Description = string(l[:colon]) + r.First = net.ParseIP(string(l[colon+1 : hyphen])) + r.Last = net.ParseIP(string(l[hyphen+1:])) if r.First == nil || r.Last == nil { return } diff --git a/iplist/iplist_test.go b/iplist/iplist_test.go index cc934c104d652ee27181ef126f8596404541a8ff..08f5b80fdeb61a608db7de214ab4c5be791b418f 100644 --- a/iplist/iplist_test.go +++ b/iplist/iplist_test.go @@ -16,7 +16,7 @@ func sampleRanges(tb testing.TB) (ranges []Range, err error) { scanner := bufio.NewScanner(strings.NewReader(sample)) for scanner.Scan() { - r, ok, _ := ParseBlocklistP2PLine(scanner.Text()) + r, ok, _ := ParseBlocklistP2PLine(scanner.Bytes()) if ok { // tb.Log(r) ranges = append(ranges, r)