middleware: sanitize max scrape infohashes

Fixes #268.
This commit is contained in:
Jimmy Zelinskie 2017-02-02 02:19:45 -05:00
parent fb30e9fb03
commit 3f29aa358b
4 changed files with 16 additions and 6 deletions

View file

@ -16,6 +16,7 @@ config:
prometheus_addr: 0.0.0.0:6880
max_numwant: 50
default_numwant: 25
max_scrape_infohashes: 50
http:
addr: 0.0.0.0:6881
allow_ip_spoofing: true

View file

@ -14,6 +14,9 @@ chihaya:
# The default number of peers returned in an announce.
default_numwant: 25
# The number of infohashes a single scrape can request before being truncated.
max_scrape_infohashes: 50
# This block defines configuration for the tracker's HTTP interface.
# If you do not wish to run this, delete this section.
http:

View file

@ -83,8 +83,9 @@ var ErrInvalidIP = errors.New("invalid IP")
// IPv6. Sets the Peer.AddressFamily field accordingly. Truncates IPv4
// addresses to have a length of 4 bytes.
type sanitizationHook struct {
maxNumWant uint32
defaultNumWant uint32
maxNumWant uint32
defaultNumWant uint32
maxScrapeInfoHashes uint32
}
func (h *sanitizationHook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) (context.Context, error) {
@ -109,6 +110,10 @@ func (h *sanitizationHook) HandleAnnounce(ctx context.Context, req *bittorrent.A
}
func (h *sanitizationHook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) (context.Context, error) {
if len(req.InfoHashes) > int(h.maxScrapeInfoHashes) {
req.InfoHashes = req.InfoHashes[:h.maxScrapeInfoHashes]
}
return ctx, nil
}

View file

@ -15,9 +15,10 @@ import (
// Config holds the configuration common across all middleware.
type Config struct {
AnnounceInterval time.Duration `yaml:"announce_interval"`
MaxNumWant uint32 `yaml:"max_numwant"`
DefaultNumWant uint32 `yaml:"default_numwant"`
AnnounceInterval time.Duration `yaml:"announce_interval"`
MaxNumWant uint32 `yaml:"max_numwant"`
DefaultNumWant uint32 `yaml:"default_numwant"`
MaxScrapeInfoHashes uint32 `yaml:"max_scrape_infohashes"`
}
var _ frontend.TrackerLogic = &Logic{}
@ -28,7 +29,7 @@ func NewLogic(cfg Config, peerStore storage.PeerStore, preHooks, postHooks []Hoo
l := &Logic{
announceInterval: cfg.AnnounceInterval,
peerStore: peerStore,
preHooks: []Hook{&sanitizationHook{maxNumWant: cfg.MaxNumWant, defaultNumWant: cfg.DefaultNumWant}},
preHooks: []Hook{&sanitizationHook{cfg.MaxNumWant, cfg.DefaultNumWant, cfg.MaxScrapeInfoHashes}},
postHooks: append(postHooks, &swarmInteractionHook{store: peerStore}),
}