mirror of
https://github.com/sot-tech/mochi.git
synced 2026-05-20 14:54:48 -07:00
(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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user