diff --git a/contrib/helm/chihaya/values.yaml b/contrib/helm/chihaya/values.yaml index 4e5d2ec..cdd8a0b 100644 --- a/contrib/helm/chihaya/values.yaml +++ b/contrib/helm/chihaya/values.yaml @@ -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 diff --git a/example_config.yaml b/example_config.yaml index 6f4d6d3..4f84d02 100644 --- a/example_config.yaml +++ b/example_config.yaml @@ -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: diff --git a/middleware/hooks.go b/middleware/hooks.go index 3cd31b0..3336917 100644 --- a/middleware/hooks.go +++ b/middleware/hooks.go @@ -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 } diff --git a/middleware/middleware.go b/middleware/middleware.go index 3a73afb..eb47524 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -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}), }