Files
mochi/bittorrent/sanitize.go
Lawrence, Rendall 64b27c2df6 (tested) preserve all addresses of peer
* add multiple addresses in request structures and frontend parsers

* move per-ip peer fetch/store from storage to internal hooks

* fetch/store both v1 and v2 info hashes
2022-04-27 00:52:17 +03:00

58 lines
1.4 KiB
Go

package bittorrent
import (
"github.com/sot-tech/mochi/pkg/log"
)
var (
// 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
}
log.Debug("sanitized announce", r, log.Fields{
"port": r.Port,
"addresses": r.RequestAddresses,
"maxNumWant": maxNumWant,
"defaultNumWant": defaultNumWant,
})
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
}
log.Debug("sanitized scrape", r, log.Fields{
"addresses": r.RequestAddresses,
"maxScrapeInfoHashes": maxScrapeInfoHashes,
})
return nil
}