Skip to content

Commit

Permalink
skip blocklist parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Nov 7, 2019
1 parent 010467a commit 6147937
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
14 changes: 10 additions & 4 deletions internal/blocklist/blocklist.go
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"sync"
Expand All @@ -17,11 +16,15 @@ var errNotIPv4Address = errors.New("address is not ipv4")

// Blocklist holds a list of IP ranges in a Segment Tree structure for faster lookups.
type Blocklist struct {
Logger Logger

tree stree.Stree
m sync.RWMutex
count int
}

type Logger func(format string, v ...interface{})

// New returns a new Blocklist.
func New() *Blocklist {
return &Blocklist{}
Expand Down Expand Up @@ -53,7 +56,7 @@ func (b *Blocklist) Reload(r io.Reader) (int, error) {
b.m.Lock()
defer b.m.Unlock()

tree, n, err := load(r)
tree, n, err := load(r, b.Logger)
if err != nil {
return n, err
}
Expand All @@ -63,7 +66,7 @@ func (b *Blocklist) Reload(r io.Reader) (int, error) {
return n, nil
}

func load(r io.Reader) (stree.Stree, int, error) {
func load(r io.Reader, logger Logger) (stree.Stree, int, error) {
var tree stree.Stree
var n int
scanner := bufio.NewScanner(r)
Expand All @@ -77,7 +80,10 @@ func load(r io.Reader) (stree.Stree, int, error) {
}
r, err := parseCIDR(l)
if err != nil {
return tree, n, fmt.Errorf("cannot parse blocklist line (%q): %s", string(l), err.Error())
if logger != nil {
logger("cannot parse blocklist line (%q): %s", string(l), err.Error())
}
continue
}
tree.AddRange(stree.ValueType(r.first), stree.ValueType(r.last))
n++
Expand Down
1 change: 1 addition & 0 deletions torrent/session.go
Expand Up @@ -154,6 +154,7 @@ func NewSession(cfg Config) (*Session, error) {
ports[int(p)] = struct{}{}
}
bl := blocklist.New()
bl.Logger = l.Errorf
var blTracker *blocklist.Blocklist
if cfg.BlocklistEnabledForTrackers {
blTracker = bl
Expand Down

0 comments on commit 6147937

Please sign in to comment.