Files
mochi/bittorrent/sanitize.go
Lawrence, Rendall c50a532181 (tested) complete replace logrus with zerolog
* remove cobra dependency and split execs to mochi and e2e

* add log init synchronization
2022-05-02 03:13:58 +03:00

51 lines
1.2 KiB
Go

package bittorrent
import (
"github.com/sot-tech/mochi/pkg/log"
)
var (
logger = log.NewLogger("bittorrent")
// ErrInvalidIP indicates an invalid IP for an Announce.
ErrInvalidIP = ClientError("invalid IP")
// ErrInvalidPort indicates an invalid Port for an Announce.
ErrInvalidPort = ClientError("invalid port")
)
// SanitizeAnnounce enforces a max and default NumWant and coerces the peer's
// IP address into the proper format.
func SanitizeAnnounce(r *AnnounceRequest, maxNumWant, defaultNumWant uint32) error {
if r.Port == 0 {
return ErrInvalidPort
}
if !r.Validate() {
return ErrInvalidIP
}
if !r.NumWantProvided {
r.NumWant = defaultNumWant
} else if r.NumWant > maxNumWant {
r.NumWant = maxNumWant
}
logger.Debug().Object("request", r).Msg("sanitized announce")
return nil
}
// SanitizeScrape enforces a max number of infohashes for a single scrape
// request and checks if addresses are valid.
func SanitizeScrape(r *ScrapeRequest, maxScrapeInfoHashes uint32) error {
if len(r.InfoHashes) > int(maxScrapeInfoHashes) {
r.InfoHashes = r.InfoHashes[:maxScrapeInfoHashes]
}
if !r.Validate() {
return ErrInvalidIP
}
logger.Debug().Object("request", r).Msg("sanitized scrape")
return nil
}