From cb88a11d6a5aa43416a5ce949d63573bc56ca4fb Mon Sep 17 00:00:00 2001 From: Leo Balduf Date: Sat, 12 Oct 2019 12:52:16 +0900 Subject: [PATCH] frontend: add defaults for parser options --- frontend/http/frontend.go | 27 +++++++++++++++++ frontend/http/parser.go | 7 +++++ frontend/udp/frontend.go | 63 +++++++++++++++++++++++++++++++-------- frontend/udp/parser.go | 7 +++++ 4 files changed, 92 insertions(+), 12 deletions(-) diff --git a/frontend/http/frontend.go b/frontend/http/frontend.go index a70319e..43415da 100644 --- a/frontend/http/frontend.go +++ b/frontend/http/frontend.go @@ -100,6 +100,33 @@ func (cfg Config) Validate() Config { } } + if cfg.MaxNumWant <= 0 { + validcfg.MaxNumWant = defaultMaxNumWant + log.Warn("falling back to default configuration", log.Fields{ + "name": "http.MaxNumWant", + "provided": cfg.MaxNumWant, + "default": validcfg.MaxNumWant, + }) + } + + if cfg.DefaultNumWant <= 0 { + validcfg.DefaultNumWant = defaultDefaultNumWant + log.Warn("falling back to default configuration", log.Fields{ + "name": "http.DefaultNumWant", + "provided": cfg.DefaultNumWant, + "default": validcfg.DefaultNumWant, + }) + } + + if cfg.MaxScrapeInfoHashes <= 0 { + validcfg.MaxScrapeInfoHashes = defaultMaxScrapeInfoHashes + log.Warn("falling back to default configuration", log.Fields{ + "name": "http.MaxScrapeInfoHashes", + "provided": cfg.MaxScrapeInfoHashes, + "default": validcfg.MaxScrapeInfoHashes, + }) + } + return validcfg } diff --git a/frontend/http/parser.go b/frontend/http/parser.go index 1d276cb..a34e3ba 100644 --- a/frontend/http/parser.go +++ b/frontend/http/parser.go @@ -20,6 +20,13 @@ type ParseOptions struct { MaxScrapeInfoHashes uint32 `yaml:"max_scrape_infohashes"` } +// Default parser config constants. +const ( + defaultMaxNumWant uint32 = 100 + defaultDefaultNumWant uint32 = 50 + defaultMaxScrapeInfoHashes uint32 = 50 +) + // ParseAnnounce parses an bittorrent.AnnounceRequest from an http.Request. func ParseAnnounce(r *http.Request, opts ParseOptions) (*bittorrent.AnnounceRequest, error) { qp, err := bittorrent.ParseURLData(r.RequestURI) diff --git a/frontend/udp/frontend.go b/frontend/udp/frontend.go index a75430e..98f8b75 100644 --- a/frontend/udp/frontend.go +++ b/frontend/udp/frontend.go @@ -46,6 +46,55 @@ func (cfg Config) LogFields() log.Fields { } } +// Validate sanity checks values set in a config and returns a new config with +// default values replacing anything that is invalid. +// +// This function warns to the logger when a value is changed. +func (cfg Config) Validate() Config { + validcfg := cfg + + // Generate a private key if one isn't provided by the user. + if cfg.PrivateKey == "" { + rand.Seed(time.Now().UnixNano()) + pkeyRunes := make([]rune, 64) + for i := range pkeyRunes { + pkeyRunes[i] = allowedGeneratedPrivateKeyRunes[rand.Intn(len(allowedGeneratedPrivateKeyRunes))] + } + validcfg.PrivateKey = string(pkeyRunes) + + log.Warn("UDP private key was not provided, using generated key", log.Fields{"key": validcfg.PrivateKey}) + } + + if cfg.MaxNumWant <= 0 { + validcfg.MaxNumWant = defaultMaxNumWant + log.Warn("falling back to default configuration", log.Fields{ + "name": "udp.MaxNumWant", + "provided": cfg.MaxNumWant, + "default": validcfg.MaxNumWant, + }) + } + + if cfg.DefaultNumWant <= 0 { + validcfg.DefaultNumWant = defaultDefaultNumWant + log.Warn("falling back to default configuration", log.Fields{ + "name": "udp.DefaultNumWant", + "provided": cfg.DefaultNumWant, + "default": validcfg.DefaultNumWant, + }) + } + + if cfg.MaxScrapeInfoHashes <= 0 { + validcfg.MaxScrapeInfoHashes = defaultMaxScrapeInfoHashes + log.Warn("falling back to default configuration", log.Fields{ + "name": "udp.MaxScrapeInfoHashes", + "provided": cfg.MaxScrapeInfoHashes, + "default": validcfg.MaxScrapeInfoHashes, + }) + } + + return validcfg +} + // Frontend holds the state of a UDP BitTorrent Frontend. type Frontend struct { socket *net.UDPConn @@ -60,18 +109,8 @@ type Frontend struct { // NewFrontend creates a new instance of an UDP Frontend that asynchronously // serves requests. -func NewFrontend(logic frontend.TrackerLogic, cfg Config) (*Frontend, error) { - // Generate a private key if one isn't provided by the user. - if cfg.PrivateKey == "" { - rand.Seed(time.Now().UnixNano()) - pkeyRunes := make([]rune, 64) - for i := range pkeyRunes { - pkeyRunes[i] = allowedGeneratedPrivateKeyRunes[rand.Intn(len(allowedGeneratedPrivateKeyRunes))] - } - cfg.PrivateKey = string(pkeyRunes) - - log.Warn("UDP private key was not provided, using generated key", log.Fields{"key": cfg.PrivateKey}) - } +func NewFrontend(logic frontend.TrackerLogic, provided Config) (*Frontend, error) { + cfg := provided.Validate() f := &Frontend{ closing: make(chan struct{}), diff --git a/frontend/udp/parser.go b/frontend/udp/parser.go index b5e4830..ca12fc4 100644 --- a/frontend/udp/parser.go +++ b/frontend/udp/parser.go @@ -58,6 +58,13 @@ type ParseOptions struct { MaxScrapeInfoHashes uint32 `yaml:"max_scrape_infohashes"` } +// Default parser config constants. +const ( + defaultMaxNumWant uint32 = 100 + defaultDefaultNumWant uint32 = 50 + defaultMaxScrapeInfoHashes uint32 = 50 +) + // ParseAnnounce parses an AnnounceRequest from a UDP request. // // If v6Action is true, the announce is parsed the