(minor) sanitize code

This commit is contained in:
Lawrence, Rendall
2022-05-02 13:30:11 +03:00
parent c50a532181
commit 463d478dad
4 changed files with 35 additions and 14 deletions

View File

@@ -1,7 +1,6 @@
package bittorrent
import (
"fmt"
"net/netip"
"sort"
"time"
@@ -21,16 +20,6 @@ func (a RequestAddress) Validate() bool {
return a.IsValid() && !a.IsUnspecified()
}
func (a RequestAddress) String() string {
var p string
if a.Provided {
p = "(provided)"
} else {
p = "(detected)"
}
return fmt.Sprint(a.Addr.String(), p)
}
// MarshalZerologObject writes fields into zerolog event
func (a RequestAddress) MarshalZerologObject(e *zerolog.Event) {
e.Stringer("address", a.Addr).Bool("provided", a.Provided)

View File

@@ -5,7 +5,7 @@ import (
)
var (
logger = log.NewLogger("bittorrent")
logger = log.NewLogger("request sanitizer")
// ErrInvalidIP indicates an invalid IP for an Announce.
ErrInvalidIP = ClientError("invalid IP")
@@ -16,6 +16,7 @@ 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 {
logger.Trace().Object("request", r).Msg("source announce")
if r.Port == 0 {
return ErrInvalidPort
}
@@ -30,13 +31,14 @@ func SanitizeAnnounce(r *AnnounceRequest, maxNumWant, defaultNumWant uint32) err
r.NumWant = maxNumWant
}
logger.Debug().Object("request", r).Msg("sanitized announce")
logger.Trace().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 {
logger.Trace().Object("request", r).Msg("source scrape")
if len(r.InfoHashes) > int(maxScrapeInfoHashes) {
r.InfoHashes = r.InfoHashes[:maxScrapeInfoHashes]
}
@@ -45,6 +47,6 @@ func SanitizeScrape(r *ScrapeRequest, maxScrapeInfoHashes uint32) error {
return ErrInvalidIP
}
logger.Debug().Object("request", r).Msg("sanitized scrape")
logger.Trace().Object("request", r).Msg("sanitized scrape")
return nil
}

View File

@@ -11,6 +11,7 @@ import (
"os"
"strings"
"sync"
// needs for async file logging
_ "code.cloudfoundry.org/go-diodes"
"github.com/rs/zerolog"

View File

@@ -198,6 +198,10 @@ func (ps *peerStore) PutSeeder(ih bittorrent.InfoHash, p bittorrent.Peer) error
panic("attempted to interact with stopped memory store")
default:
}
logger.Trace().
Stringer("infoHash", ih).
Object("peer", p).
Msg("put seeder")
shard := ps.shards[ps.shardIndex(ih, p.Addr().Is6())]
shard.Lock()
@@ -227,6 +231,10 @@ func (ps *peerStore) DeleteSeeder(ih bittorrent.InfoHash, p bittorrent.Peer) err
panic("attempted to interact with stopped memory store")
default:
}
logger.Trace().
Stringer("infoHash", ih).
Object("peer", p).
Msg("delete seeder")
shard := ps.shards[ps.shardIndex(ih, p.Addr().Is6())]
shard.Lock()
@@ -256,6 +264,10 @@ func (ps *peerStore) PutLeecher(ih bittorrent.InfoHash, p bittorrent.Peer) error
panic("attempted to interact with stopped memory store")
default:
}
logger.Trace().
Stringer("infoHash", ih).
Object("peer", p).
Msg("put leecher")
shard := ps.shards[ps.shardIndex(ih, p.Addr().Is6())]
shard.Lock()
@@ -285,6 +297,10 @@ func (ps *peerStore) DeleteLeecher(ih bittorrent.InfoHash, p bittorrent.Peer) er
panic("attempted to interact with stopped memory store")
default:
}
logger.Trace().
Stringer("infoHash", ih).
Object("peer", p).
Msg("delete leecher")
shard := ps.shards[ps.shardIndex(ih, p.Addr().Is6())]
shard.Lock()
@@ -314,6 +330,10 @@ func (ps *peerStore) GraduateLeecher(ih bittorrent.InfoHash, p bittorrent.Peer)
panic("attempted to interact with stopped memory store")
default:
}
logger.Trace().
Stringer("infoHash", ih).
Object("peer", p).
Msg("graduate leecher")
shard := ps.shards[ps.shardIndex(ih, p.Addr().Is6())]
shard.Lock()
@@ -376,6 +396,12 @@ func (ps *peerStore) AnnouncePeers(ih bittorrent.InfoHash, seeder bool, numWant
panic("attempted to interact with stopped memory store")
default:
}
logger.Trace().
Stringer("infoHash", ih).
Bool("seeder", seeder).
Int("numWant", numWant).
Bool("v6", v6).
Msg("announce peers")
peers = ps.getPeers(ps.shards[ps.shardIndex(ih, v6)], ih, numWant, seeder)
@@ -399,6 +425,9 @@ func (ps *peerStore) ScrapeSwarm(ih bittorrent.InfoHash) (leechers uint32, seede
panic("attempted to interact with stopped memory store")
default:
}
logger.Trace().
Stringer("infoHash", ih).
Msg("scrape swarm")
leechers, seeders = ps.countPeers(ih, true)
l, s := ps.countPeers(ih, false)