frontend: add defaults for parser options
This commit is contained in:
parent
eed141dbe4
commit
cb88a11d6a
4 changed files with 92 additions and 12 deletions
|
@ -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
|
return validcfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,13 @@ type ParseOptions struct {
|
||||||
MaxScrapeInfoHashes uint32 `yaml:"max_scrape_infohashes"`
|
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.
|
// ParseAnnounce parses an bittorrent.AnnounceRequest from an http.Request.
|
||||||
func ParseAnnounce(r *http.Request, opts ParseOptions) (*bittorrent.AnnounceRequest, error) {
|
func ParseAnnounce(r *http.Request, opts ParseOptions) (*bittorrent.AnnounceRequest, error) {
|
||||||
qp, err := bittorrent.ParseURLData(r.RequestURI)
|
qp, err := bittorrent.ParseURLData(r.RequestURI)
|
||||||
|
|
|
@ -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.
|
// Frontend holds the state of a UDP BitTorrent Frontend.
|
||||||
type Frontend struct {
|
type Frontend struct {
|
||||||
socket *net.UDPConn
|
socket *net.UDPConn
|
||||||
|
@ -60,18 +109,8 @@ type Frontend struct {
|
||||||
|
|
||||||
// NewFrontend creates a new instance of an UDP Frontend that asynchronously
|
// NewFrontend creates a new instance of an UDP Frontend that asynchronously
|
||||||
// serves requests.
|
// serves requests.
|
||||||
func NewFrontend(logic frontend.TrackerLogic, cfg Config) (*Frontend, error) {
|
func NewFrontend(logic frontend.TrackerLogic, provided Config) (*Frontend, error) {
|
||||||
// Generate a private key if one isn't provided by the user.
|
cfg := provided.Validate()
|
||||||
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})
|
|
||||||
}
|
|
||||||
|
|
||||||
f := &Frontend{
|
f := &Frontend{
|
||||||
closing: make(chan struct{}),
|
closing: make(chan struct{}),
|
||||||
|
|
|
@ -58,6 +58,13 @@ type ParseOptions struct {
|
||||||
MaxScrapeInfoHashes uint32 `yaml:"max_scrape_infohashes"`
|
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.
|
// ParseAnnounce parses an AnnounceRequest from a UDP request.
|
||||||
//
|
//
|
||||||
// If v6Action is true, the announce is parsed the
|
// If v6Action is true, the announce is parsed the
|
||||||
|
|
Loading…
Reference in a new issue