(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
This commit is contained in:
Lawrence, Rendall
2022-04-27 00:52:17 +03:00
parent b365abd296
commit 64b27c2df6
19 changed files with 469 additions and 415 deletions

View File

@@ -1,8 +1,6 @@
package bittorrent
import (
"net/netip"
"github.com/sot-tech/mochi/pkg/log"
)
@@ -17,23 +15,23 @@ var (
// 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 {
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
}
r.AddrPort = netip.AddrPortFrom(r.Addr(), r.Port())
if !r.Addr().IsValid() || r.Addr().IsUnspecified() {
return ErrInvalidIP
}
log.Debug("sanitized announce", r, log.Fields{
"ipPort": r.AddrPort,
"port": r.Port,
"addresses": r.RequestAddresses,
"maxNumWant": maxNumWant,
"defaultNumWant": defaultNumWant,
})
@@ -41,19 +39,18 @@ func SanitizeAnnounce(r *AnnounceRequest, maxNumWant, defaultNumWant uint32) err
}
// SanitizeScrape enforces a max number of infohashes for a single scrape
// request.
// 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]
}
r.Addr = r.Addr.Unmap()
if !r.Addr.IsValid() || r.Addr.IsUnspecified() {
if !r.Validate() {
return ErrInvalidIP
}
log.Debug("sanitized scrape", r, log.Fields{
"ip": r.Addr,
"addresses": r.RequestAddresses,
"maxScrapeInfoHashes": maxScrapeInfoHashes,
})
return nil