mirror of
https://github.com/sot-tech/mochi.git
synced 2026-05-20 06:44:48 -07:00
(tested) Refactor code
* merge 9d04e4c from https://github.com/jzelinskie/chihaya * sanitize ip address on Scrape requests * remove NewConnectionID and ValidConnectionID functions from production code
This commit is contained in:
@@ -46,6 +46,11 @@ func SanitizeScrape(r *ScrapeRequest, maxScrapeInfoHashes uint32) error {
|
|||||||
r.InfoHashes = r.InfoHashes[:maxScrapeInfoHashes]
|
r.InfoHashes = r.InfoHashes[:maxScrapeInfoHashes]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r.AddrPort = netip.AddrPortFrom(r.Addr(), r.Port())
|
||||||
|
if !r.Addr().IsValid() || r.Addr().IsUnspecified() {
|
||||||
|
return ErrInvalidIP
|
||||||
|
}
|
||||||
|
|
||||||
log.Debug("sanitized scrape", r, log.Fields{
|
log.Debug("sanitized scrape", r, log.Fields{
|
||||||
"maxScrapeInfoHashes": maxScrapeInfoHashes,
|
"maxScrapeInfoHashes": maxScrapeInfoHashes,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -178,5 +178,5 @@ func requestedIP(r *http.Request, p bittorrent.Params, opts ParseOptions) (netip
|
|||||||
}
|
}
|
||||||
|
|
||||||
addrPort, err := netip.ParseAddrPort(r.RemoteAddr)
|
addrPort, err := netip.ParseAddrPort(r.RemoteAddr)
|
||||||
return addrPort.Addr().Unmap(), false, err
|
return addrPort.Addr(), false, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ func WriteError(w http.ResponseWriter, err error) {
|
|||||||
log.Error("http: internal error", log.Err(err))
|
log.Error("http: internal error", log.Err(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
if err = bencode.NewEncoder(w).Encode(map[string]any{
|
if err = bencode.NewEncoder(w).Encode(map[string]any{
|
||||||
"failure reason": message,
|
"failure reason": message,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
|
|||||||
@@ -15,21 +15,6 @@ import (
|
|||||||
// ttl is the duration a connection ID should be valid according to BEP 15.
|
// ttl is the duration a connection ID should be valid according to BEP 15.
|
||||||
const ttl = 2 * time.Minute
|
const ttl = 2 * time.Minute
|
||||||
|
|
||||||
// NewConnectionID creates an 8-byte connection identifier for UDP packets as
|
|
||||||
// described by BEP 15.
|
|
||||||
// This is a wrapper around creating a new ConnectionIDGenerator and generating
|
|
||||||
// an ID. It is recommended to use the generator for performance.
|
|
||||||
func NewConnectionID(ip netip.Addr, now time.Time, key string) []byte {
|
|
||||||
return NewConnectionIDGenerator(key).Generate(ip, now)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValidConnectionID determines whether a connection identifier is legitimate.
|
|
||||||
// This is a wrapper around creating a new ConnectionIDGenerator and validating
|
|
||||||
// the ID. It is recommended to use the generator for performance.
|
|
||||||
func ValidConnectionID(connectionID []byte, ip netip.Addr, now time.Time, maxClockSkew time.Duration, key string) bool {
|
|
||||||
return NewConnectionIDGenerator(key).Validate(connectionID, ip, now, maxClockSkew)
|
|
||||||
}
|
|
||||||
|
|
||||||
// A ConnectionIDGenerator is a reusable generator and validator for connection
|
// A ConnectionIDGenerator is a reusable generator and validator for connection
|
||||||
// IDs as described in BEP 15.
|
// IDs as described in BEP 15.
|
||||||
// It is not thread safe, but is safe to be pooled and reused by other
|
// It is not thread safe, but is safe to be pooled and reused by other
|
||||||
|
|||||||
@@ -27,6 +27,21 @@ var golden = []struct {
|
|||||||
{0, 0, "::1", "", true},
|
{0, 0, "::1", "", true},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewConnectionID creates an 8-byte connection identifier for UDP packets as
|
||||||
|
// described by BEP 15.
|
||||||
|
// This is a wrapper around creating a new ConnectionIDGenerator and generating
|
||||||
|
// an ID. It is recommended to use the generator for performance.
|
||||||
|
func NewConnectionID(ip netip.Addr, now time.Time, key string) []byte {
|
||||||
|
return NewConnectionIDGenerator(key).Generate(ip, now)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidConnectionID determines whether a connection identifier is legitimate.
|
||||||
|
// This is a wrapper around creating a new ConnectionIDGenerator and validating
|
||||||
|
// the ID. It is recommended to use the generator for performance.
|
||||||
|
func ValidConnectionID(connectionID []byte, ip netip.Addr, now time.Time, maxClockSkew time.Duration, key string) bool {
|
||||||
|
return NewConnectionIDGenerator(key).Validate(connectionID, ip, now, maxClockSkew)
|
||||||
|
}
|
||||||
|
|
||||||
// simpleNewConnectionID generates a new connection ID the explicit way.
|
// simpleNewConnectionID generates a new connection ID the explicit way.
|
||||||
// This is used to verify correct behaviour of the generator.
|
// This is used to verify correct behaviour of the generator.
|
||||||
func simpleNewConnectionID(ip netip.Addr, now time.Time, key string) []byte {
|
func simpleNewConnectionID(ip netip.Addr, now time.Time, key string) []byte {
|
||||||
|
|||||||
@@ -229,7 +229,7 @@ func (ps *peerStore) shardIndex(infoHash bittorrent.InfoHash, addr netip.Addr) u
|
|||||||
// half is dedicated to IPv4 swarms and the second half is dedicated to
|
// half is dedicated to IPv4 swarms and the second half is dedicated to
|
||||||
// IPv6 swarms.
|
// IPv6 swarms.
|
||||||
idx := binary.BigEndian.Uint32([]byte(infoHash[:4])) % (uint32(len(ps.shards)) / 2)
|
idx := binary.BigEndian.Uint32([]byte(infoHash[:4])) % (uint32(len(ps.shards)) / 2)
|
||||||
if addr.Is6() && !addr.Is4In6() {
|
if addr.Is6() {
|
||||||
idx += uint32(len(ps.shards) / 2)
|
idx += uint32(len(ps.shards) / 2)
|
||||||
}
|
}
|
||||||
return idx
|
return idx
|
||||||
|
|||||||
Reference in New Issue
Block a user