mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-29 10:38:11 -07:00
(untested) Merge commit e56ad81 from https://github.com/jzelinskie/chihaya
* rename/replace redis keys
This commit is contained in:
+10
-16
@@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
@@ -282,12 +283,12 @@ func (f *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, ps http
|
||||
if f.EnableRequestTiming {
|
||||
start = time.Now()
|
||||
}
|
||||
var af *bittorrent.AddressFamily
|
||||
var addr netip.Addr
|
||||
defer func() {
|
||||
if f.EnableRequestTiming {
|
||||
recordResponseDuration("announce", af, err, time.Since(start))
|
||||
recordResponseDuration("announce", addr, err, time.Since(start))
|
||||
} else {
|
||||
recordResponseDuration("announce", af, err, time.Duration(0))
|
||||
recordResponseDuration("announce", addr, err, time.Duration(0))
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -296,8 +297,7 @@ func (f *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, ps http
|
||||
WriteError(w, err)
|
||||
return
|
||||
}
|
||||
af = new(bittorrent.AddressFamily)
|
||||
*af = req.IP.AddressFamily
|
||||
addr = req.AddrPort.Addr()
|
||||
|
||||
ctx := injectRouteParamsToContext(context.Background(), ps)
|
||||
ctx, resp, err := f.logic.HandleAnnounce(ctx, req)
|
||||
@@ -323,12 +323,12 @@ func (f *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, ps httpro
|
||||
if f.EnableRequestTiming {
|
||||
start = time.Now()
|
||||
}
|
||||
var af *bittorrent.AddressFamily
|
||||
var addr netip.Addr
|
||||
defer func() {
|
||||
if f.EnableRequestTiming {
|
||||
recordResponseDuration("scrape", af, err, time.Since(start))
|
||||
recordResponseDuration("scrape", addr, err, time.Since(start))
|
||||
} else {
|
||||
recordResponseDuration("scrape", af, err, time.Duration(0))
|
||||
recordResponseDuration("scrape", addr, err, time.Duration(0))
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -345,18 +345,12 @@ func (f *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, ps httpro
|
||||
return
|
||||
}
|
||||
|
||||
reqIP := net.ParseIP(host)
|
||||
if reqIP.To4() != nil {
|
||||
req.AddressFamily = bittorrent.IPv4
|
||||
} else if len(reqIP) == net.IPv6len { // implies reqIP.To4() == nil
|
||||
req.AddressFamily = bittorrent.IPv6
|
||||
} else {
|
||||
addr, err = netip.ParseAddr(host)
|
||||
if err != nil || addr.IsUnspecified() {
|
||||
log.Error("http: invalid IP: neither v4 nor v6", log.Fields{"RemoteAddr": r.RemoteAddr})
|
||||
WriteError(w, bittorrent.ErrInvalidIP)
|
||||
return
|
||||
}
|
||||
af = new(bittorrent.AddressFamily)
|
||||
*af = req.AddressFamily
|
||||
|
||||
ctx := injectRouteParamsToContext(context.Background(), ps)
|
||||
ctx, resp, err := f.logic.HandleScrape(ctx, req)
|
||||
|
||||
+44
-32
@@ -2,8 +2,8 @@ package http
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
|
||||
"github.com/sot-tech/mochi/bittorrent"
|
||||
)
|
||||
@@ -28,6 +28,16 @@ const (
|
||||
defaultMaxScrapeInfoHashes = 50
|
||||
)
|
||||
|
||||
var (
|
||||
errNoInfoHash = bittorrent.ClientError("no info hash supplied")
|
||||
errMultipleInfoHashes = bittorrent.ClientError("multiple info hashes supplied")
|
||||
errInvalidPeerID = bittorrent.ClientError("peer ID invalid or not provided")
|
||||
errInvalidParameterLeft = bittorrent.ClientError("parameter 'left' invalid or not provided")
|
||||
errInvalidParameterDownloaded = bittorrent.ClientError("parameter 'downloaded' invalid or not provided")
|
||||
errInvalidParameterUploaded = bittorrent.ClientError("parameter 'uploaded' invalid or not provided")
|
||||
errInvalidParameterNumWant = bittorrent.ClientError("parameter 'num want' invalid or not provided")
|
||||
)
|
||||
|
||||
// ParseAnnounce parses an bittorrent.AnnounceRequest from an http.Request.
|
||||
func ParseAnnounce(r *http.Request, opts ParseOptions) (*bittorrent.AnnounceRequest, error) {
|
||||
qp, err := bittorrent.ParseURLData(r.RequestURI)
|
||||
@@ -41,9 +51,8 @@ func ParseAnnounce(r *http.Request, opts ParseOptions) (*bittorrent.AnnounceRequ
|
||||
var eventStr string
|
||||
eventStr, request.EventProvided = qp.String("event")
|
||||
if request.EventProvided {
|
||||
request.Event, err = bittorrent.NewEvent(eventStr)
|
||||
if err != nil {
|
||||
return nil, bittorrent.ClientError("failed to provide valid client event")
|
||||
if request.Event, err = bittorrent.NewEvent(eventStr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
request.Event = bittorrent.None
|
||||
@@ -56,17 +65,17 @@ func ParseAnnounce(r *http.Request, opts ParseOptions) (*bittorrent.AnnounceRequ
|
||||
// Parse the infohash from the request.
|
||||
infoHashes := qp.InfoHashes()
|
||||
if len(infoHashes) < 1 {
|
||||
return nil, bittorrent.ClientError("no info_hash parameter supplied")
|
||||
return nil, errNoInfoHash
|
||||
}
|
||||
if len(infoHashes) > 1 {
|
||||
return nil, bittorrent.ClientError("multiple info_hash parameters supplied")
|
||||
return nil, errMultipleInfoHashes
|
||||
}
|
||||
request.InfoHash = infoHashes[0]
|
||||
|
||||
// Parse the PeerID from the request.
|
||||
peerID, ok := qp.String("peer_id")
|
||||
if !ok {
|
||||
return nil, bittorrent.ClientError("failed to parse parameter: peer_id")
|
||||
return nil, errInvalidPeerID
|
||||
}
|
||||
request.Peer.ID, err = bittorrent.NewPeerID([]byte(peerID))
|
||||
if err != nil {
|
||||
@@ -75,25 +84,25 @@ func ParseAnnounce(r *http.Request, opts ParseOptions) (*bittorrent.AnnounceRequ
|
||||
// Determine the number of remaining bytes for the client.
|
||||
request.Left, err = qp.Uint("left", 64)
|
||||
if err != nil {
|
||||
return nil, bittorrent.ClientError("failed to parse parameter: left")
|
||||
return nil, errInvalidParameterLeft
|
||||
}
|
||||
|
||||
// Determine the number of bytes downloaded by the client.
|
||||
request.Downloaded, err = qp.Uint("downloaded", 64)
|
||||
if err != nil {
|
||||
return nil, bittorrent.ClientError("failed to parse parameter: downloaded")
|
||||
return nil, errInvalidParameterDownloaded
|
||||
}
|
||||
|
||||
// Determine the number of bytes shared by the client.
|
||||
request.Uploaded, err = qp.Uint("uploaded", 64)
|
||||
if err != nil {
|
||||
return nil, bittorrent.ClientError("failed to parse parameter: uploaded")
|
||||
return nil, errInvalidParameterUploaded
|
||||
}
|
||||
|
||||
// Determine the number of peers the client wants in the response.
|
||||
numwant, err := qp.Uint("numwant", 32)
|
||||
if err != nil && !errors.Is(err, bittorrent.ErrKeyNotFound) {
|
||||
return nil, bittorrent.ClientError("failed to parse parameter: numwant")
|
||||
return nil, errInvalidParameterNumWant
|
||||
}
|
||||
// If there were no errors, the user actually provided the numwant.
|
||||
request.NumWantProvided = err == nil
|
||||
@@ -102,21 +111,22 @@ func ParseAnnounce(r *http.Request, opts ParseOptions) (*bittorrent.AnnounceRequ
|
||||
// Parse the port where the client is listening.
|
||||
port, err := qp.Uint("port", 16)
|
||||
if err != nil {
|
||||
return nil, bittorrent.ClientError("failed to parse parameter: port")
|
||||
return nil, bittorrent.ErrInvalidPort
|
||||
}
|
||||
request.Peer.Port = uint16(port)
|
||||
|
||||
// Parse the IP address where the client is listening.
|
||||
request.Peer.IP.IP, request.IPProvided = requestedIP(r, qp, opts)
|
||||
if request.Peer.IP.IP == nil {
|
||||
return nil, bittorrent.ClientError("failed to parse peer IP address")
|
||||
ip, spoofed, err := requestedIP(r, qp, opts)
|
||||
if err != nil {
|
||||
return nil, bittorrent.ErrInvalidIP
|
||||
}
|
||||
request.Peer.AddrPort = netip.AddrPortFrom(ip, uint16(port))
|
||||
request.IPProvided = spoofed
|
||||
|
||||
if err = bittorrent.SanitizeAnnounce(request, opts.MaxNumWant, opts.DefaultNumWant); err != nil {
|
||||
request = nil
|
||||
}
|
||||
|
||||
if err := bittorrent.SanitizeAnnounce(request, opts.MaxNumWant, opts.DefaultNumWant); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return request, nil
|
||||
return request, err
|
||||
}
|
||||
|
||||
// ParseScrape parses an bittorrent.ScrapeRequest from an http.Request.
|
||||
@@ -128,7 +138,7 @@ func ParseScrape(r *http.Request, opts ParseOptions) (*bittorrent.ScrapeRequest,
|
||||
|
||||
infoHashes := qp.InfoHashes()
|
||||
if len(infoHashes) < 1 {
|
||||
return nil, bittorrent.ClientError("no info_hash parameter supplied")
|
||||
return nil, errNoInfoHash
|
||||
}
|
||||
|
||||
request := &bittorrent.ScrapeRequest{
|
||||
@@ -144,27 +154,29 @@ func ParseScrape(r *http.Request, opts ParseOptions) (*bittorrent.ScrapeRequest,
|
||||
}
|
||||
|
||||
// requestedIP determines the IP address for a BitTorrent client request.
|
||||
func requestedIP(r *http.Request, p bittorrent.Params, opts ParseOptions) (ip net.IP, provided bool) {
|
||||
func requestedIP(r *http.Request, p bittorrent.Params, opts ParseOptions) (netip.Addr, bool, error) {
|
||||
if opts.AllowIPSpoofing {
|
||||
if ipstr, ok := p.String("ip"); ok {
|
||||
return net.ParseIP(ipstr), true
|
||||
addr, err := netip.ParseAddr(ipstr)
|
||||
return addr, true, err
|
||||
}
|
||||
|
||||
if ipstr, ok := p.String("ipv4"); ok {
|
||||
return net.ParseIP(ipstr), true
|
||||
addr, err := netip.ParseAddr(ipstr)
|
||||
return addr, true, err
|
||||
}
|
||||
|
||||
if ipstr, ok := p.String("ipv6"); ok {
|
||||
return net.ParseIP(ipstr), true
|
||||
addr, err := netip.ParseAddr(ipstr)
|
||||
return addr, true, err
|
||||
}
|
||||
}
|
||||
|
||||
if opts.RealIPHeader != "" {
|
||||
if ip := r.Header.Get(opts.RealIPHeader); ip != "" {
|
||||
return net.ParseIP(ip), false
|
||||
}
|
||||
if ipstr := r.Header.Get(opts.RealIPHeader); ipstr != "" && opts.RealIPHeader != "" {
|
||||
addr, err := netip.ParseAddr(ipstr)
|
||||
return addr, false, err
|
||||
}
|
||||
|
||||
host, _, _ := net.SplitHostPort(r.RemoteAddr)
|
||||
return net.ParseIP(host), false
|
||||
addrPort, err := netip.ParseAddrPort(r.RemoteAddr)
|
||||
return addrPort.Addr(), false, err
|
||||
}
|
||||
|
||||
@@ -2,11 +2,13 @@ package http
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
"github.com/sot-tech/mochi/bittorrent"
|
||||
"github.com/sot-tech/mochi/pkg/metrics"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -24,7 +26,7 @@ var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
|
||||
|
||||
// recordResponseDuration records the duration of time to respond to a Request
|
||||
// in milliseconds.
|
||||
func recordResponseDuration(action string, af *bittorrent.AddressFamily, err error, duration time.Duration) {
|
||||
func recordResponseDuration(action string, addr netip.Addr, err error, duration time.Duration) {
|
||||
var errString string
|
||||
if err != nil {
|
||||
var clientErr bittorrent.ClientError
|
||||
@@ -35,16 +37,7 @@ func recordResponseDuration(action string, af *bittorrent.AddressFamily, err err
|
||||
}
|
||||
}
|
||||
|
||||
var afString string
|
||||
if af == nil {
|
||||
afString = "Unknown"
|
||||
} else if *af == bittorrent.IPv4 {
|
||||
afString = "IPv4"
|
||||
} else if *af == bittorrent.IPv6 {
|
||||
afString = "IPv6"
|
||||
}
|
||||
|
||||
promResponseDurationMilliseconds.
|
||||
WithLabelValues(action, afString, errString).
|
||||
WithLabelValues(action, metrics.AddressFamily(addr), errString).
|
||||
Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond))
|
||||
}
|
||||
|
||||
+10
-14
@@ -98,29 +98,25 @@ func WriteScrapeResponse(w http.ResponseWriter, resp *bittorrent.ScrapeResponse)
|
||||
}
|
||||
|
||||
func compact4(peer bittorrent.Peer) (buf []byte) {
|
||||
if ip := peer.IP.To4(); ip == nil {
|
||||
panic("non-IPv4 IP for Peer in IPv4Peers")
|
||||
} else {
|
||||
buf = ip
|
||||
}
|
||||
buf = append(buf, byte(peer.Port>>8), byte(peer.Port))
|
||||
ip := peer.AddrPort.Addr().As4()
|
||||
buf = append(buf, ip[:]...)
|
||||
port := peer.AddrPort.Port()
|
||||
buf = append(buf, byte(port>>8), byte(port&0xff))
|
||||
return
|
||||
}
|
||||
|
||||
func compact6(peer bittorrent.Peer) (buf []byte) {
|
||||
if ip := peer.IP.To16(); ip == nil {
|
||||
panic("non-IPv6 IP for Peer in IPv6Peers")
|
||||
} else {
|
||||
buf = ip
|
||||
}
|
||||
buf = append(buf, byte(peer.Port>>8), byte(peer.Port))
|
||||
ip := peer.AddrPort.Addr().As16()
|
||||
buf = append(buf, ip[:]...)
|
||||
port := peer.AddrPort.Port()
|
||||
buf = append(buf, byte(port>>8), byte(port&0xff))
|
||||
return
|
||||
}
|
||||
|
||||
func dict(peer bittorrent.Peer) map[string]any {
|
||||
return map[string]any{
|
||||
"peer id": string(peer.ID[:]),
|
||||
"ip": peer.IP.String(),
|
||||
"port": peer.Port,
|
||||
"ip": peer.Addr(),
|
||||
"port": peer.Port(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"crypto/hmac"
|
||||
"encoding/binary"
|
||||
"hash"
|
||||
"net"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/minio/sha256-simd"
|
||||
@@ -19,14 +19,14 @@ const ttl = 2 * time.Minute
|
||||
// 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 net.IP, now time.Time, key string) []byte {
|
||||
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 net.IP, now time.Time, maxClockSkew time.Duration, key string) bool {
|
||||
func ValidConnectionID(connectionID []byte, ip netip.Addr, now time.Time, maxClockSkew time.Duration, key string) bool {
|
||||
return NewConnectionIDGenerator(key).Validate(connectionID, ip, now, maxClockSkew)
|
||||
}
|
||||
|
||||
@@ -85,13 +85,14 @@ func (g *ConnectionIDGenerator) reset() {
|
||||
// The generated ID is written to g.connID, which is also returned. g.connID
|
||||
// will be reused, so it must not be referenced after returning the generator
|
||||
// to a pool and will be overwritten be subsequent calls to Generate!
|
||||
func (g *ConnectionIDGenerator) Generate(ip net.IP, now time.Time) []byte {
|
||||
func (g *ConnectionIDGenerator) Generate(ip netip.Addr, now time.Time) []byte {
|
||||
g.reset()
|
||||
|
||||
binary.BigEndian.PutUint32(g.connID, uint32(now.Unix()))
|
||||
|
||||
g.mac.Write(g.connID[:4])
|
||||
g.mac.Write(ip)
|
||||
ipBytes, _ := ip.MarshalBinary()
|
||||
g.mac.Write(ipBytes)
|
||||
g.scratch = g.mac.Sum(g.scratch)
|
||||
copy(g.connID[4:8], g.scratch[:4])
|
||||
|
||||
@@ -100,7 +101,7 @@ func (g *ConnectionIDGenerator) Generate(ip net.IP, now time.Time) []byte {
|
||||
}
|
||||
|
||||
// Validate validates the given connection ID for an IP and the current time.
|
||||
func (g *ConnectionIDGenerator) Validate(connectionID []byte, ip net.IP, now time.Time, maxClockSkew time.Duration) bool {
|
||||
func (g *ConnectionIDGenerator) Validate(connectionID []byte, ip netip.Addr, now time.Time, maxClockSkew time.Duration) bool {
|
||||
ts := time.Unix(int64(binary.BigEndian.Uint32(connectionID[:4])), 0)
|
||||
log.Debug("validating connection ID", log.Fields{"connID": connectionID, "ip": ip, "ts": ts, "now": now})
|
||||
if now.After(ts.Add(ttl)) || ts.After(now.Add(maxClockSkew)) {
|
||||
@@ -110,7 +111,8 @@ func (g *ConnectionIDGenerator) Validate(connectionID []byte, ip net.IP, now tim
|
||||
g.reset()
|
||||
|
||||
g.mac.Write(connectionID[:4])
|
||||
g.mac.Write(ip)
|
||||
ipBytes, _ := ip.MarshalBinary()
|
||||
g.mac.Write(ipBytes)
|
||||
g.scratch = g.mac.Sum(g.scratch)
|
||||
return hmac.Equal(g.scratch[:4], connectionID[4:])
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"crypto/hmac"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -24,18 +24,19 @@ var golden = []struct {
|
||||
}{
|
||||
{0, 1, "127.0.0.1", "", true},
|
||||
{0, 420420, "127.0.0.1", "", false},
|
||||
{0, 0, "[::]", "", true},
|
||||
{0, 0, "::1", "", true},
|
||||
}
|
||||
|
||||
// simpleNewConnectionID generates a new connection ID the explicit way.
|
||||
// This is used to verify correct behaviour of the generator.
|
||||
func simpleNewConnectionID(ip net.IP, now time.Time, key string) []byte {
|
||||
func simpleNewConnectionID(ip netip.Addr, now time.Time, key string) []byte {
|
||||
buf := make([]byte, 8)
|
||||
binary.BigEndian.PutUint32(buf, uint32(now.Unix()))
|
||||
|
||||
mac := hmac.New(sha256.New, []byte(key))
|
||||
mac.Write(buf[:4])
|
||||
mac.Write(ip)
|
||||
ipBytes, _ := ip.MarshalBinary()
|
||||
mac.Write(ipBytes)
|
||||
macBytes := mac.Sum(nil)[:4]
|
||||
copy(buf[4:], macBytes)
|
||||
|
||||
@@ -48,8 +49,8 @@ func simpleNewConnectionID(ip net.IP, now time.Time, key string) []byte {
|
||||
func TestVerification(t *testing.T) {
|
||||
for _, tt := range golden {
|
||||
t.Run(fmt.Sprintf("%s created at %d verified at %d", tt.ip, tt.createdAt, tt.now), func(t *testing.T) {
|
||||
cid := NewConnectionID(net.ParseIP(tt.ip), time.Unix(tt.createdAt, 0), tt.key)
|
||||
got := ValidConnectionID(cid, net.ParseIP(tt.ip), time.Unix(tt.now, 0), time.Minute, tt.key)
|
||||
cid := NewConnectionID(netip.MustParseAddr(tt.ip), time.Unix(tt.createdAt, 0), tt.key)
|
||||
got := ValidConnectionID(cid, netip.MustParseAddr(tt.ip), time.Unix(tt.now, 0), time.Minute, tt.key)
|
||||
if got != tt.valid {
|
||||
t.Errorf("expected validity: %t got validity: %t", tt.valid, got)
|
||||
}
|
||||
@@ -60,8 +61,8 @@ func TestVerification(t *testing.T) {
|
||||
func TestGeneration(t *testing.T) {
|
||||
for _, tt := range golden {
|
||||
t.Run(fmt.Sprintf("%s created at %d", tt.ip, tt.createdAt), func(t *testing.T) {
|
||||
want := simpleNewConnectionID(net.ParseIP(tt.ip), time.Unix(tt.createdAt, 0), tt.key)
|
||||
got := NewConnectionID(net.ParseIP(tt.ip), time.Unix(tt.createdAt, 0), tt.key)
|
||||
want := simpleNewConnectionID(netip.MustParseAddr(tt.ip), time.Unix(tt.createdAt, 0), tt.key)
|
||||
got := NewConnectionID(netip.MustParseAddr(tt.ip), time.Unix(tt.createdAt, 0), tt.key)
|
||||
require.Equal(t, want, got)
|
||||
})
|
||||
}
|
||||
@@ -70,13 +71,13 @@ func TestGeneration(t *testing.T) {
|
||||
func TestReuseGeneratorGenerate(t *testing.T) {
|
||||
for _, tt := range golden {
|
||||
t.Run(fmt.Sprintf("%s created at %d", tt.ip, tt.createdAt), func(t *testing.T) {
|
||||
cid := NewConnectionID(net.ParseIP(tt.ip), time.Unix(tt.createdAt, 0), tt.key)
|
||||
cid := NewConnectionID(netip.MustParseAddr(tt.ip), time.Unix(tt.createdAt, 0), tt.key)
|
||||
require.Len(t, cid, 8)
|
||||
|
||||
gen := NewConnectionIDGenerator(tt.key)
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
connID := gen.Generate(net.ParseIP(tt.ip), time.Unix(tt.createdAt, 0))
|
||||
connID := gen.Generate(netip.MustParseAddr(tt.ip), time.Unix(tt.createdAt, 0))
|
||||
require.Equal(t, cid, connID)
|
||||
}
|
||||
})
|
||||
@@ -87,9 +88,9 @@ func TestReuseGeneratorValidate(t *testing.T) {
|
||||
for _, tt := range golden {
|
||||
t.Run(fmt.Sprintf("%s created at %d verified at %d", tt.ip, tt.createdAt, tt.now), func(t *testing.T) {
|
||||
gen := NewConnectionIDGenerator(tt.key)
|
||||
cid := gen.Generate(net.ParseIP(tt.ip), time.Unix(tt.createdAt, 0))
|
||||
cid := gen.Generate(netip.MustParseAddr(tt.ip), time.Unix(tt.createdAt, 0))
|
||||
for i := 0; i < 3; i++ {
|
||||
got := gen.Validate(cid, net.ParseIP(tt.ip), time.Unix(tt.now, 0), time.Minute)
|
||||
got := gen.Validate(cid, netip.MustParseAddr(tt.ip), time.Unix(tt.now, 0), time.Minute)
|
||||
if got != tt.valid {
|
||||
t.Errorf("expected validity: %t got validity: %t", tt.valid, got)
|
||||
}
|
||||
@@ -99,7 +100,7 @@ func TestReuseGeneratorValidate(t *testing.T) {
|
||||
}
|
||||
|
||||
func BenchmarkSimpleNewConnectionID(b *testing.B) {
|
||||
ip := net.ParseIP("127.0.0.1")
|
||||
ip := netip.MustParseAddr("127.0.0.1")
|
||||
key := "some random string that is hopefully at least this long"
|
||||
createdAt := time.Now()
|
||||
|
||||
@@ -116,7 +117,7 @@ func BenchmarkSimpleNewConnectionID(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkNewConnectionID(b *testing.B) {
|
||||
ip := net.ParseIP("127.0.0.1")
|
||||
ip := netip.MustParseAddr("127.0.0.1")
|
||||
key := "some random string that is hopefully at least this long"
|
||||
createdAt := time.Now()
|
||||
|
||||
@@ -133,7 +134,7 @@ func BenchmarkNewConnectionID(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkConnectionIDGenerator_Generate(b *testing.B) {
|
||||
ip := net.ParseIP("127.0.0.1")
|
||||
ip := netip.MustParseAddr("127.0.0.1")
|
||||
key := "some random string that is hopefully at least this long"
|
||||
createdAt := time.Now()
|
||||
|
||||
@@ -155,7 +156,7 @@ func BenchmarkConnectionIDGenerator_Generate(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkValidConnectionID(b *testing.B) {
|
||||
ip := net.ParseIP("127.0.0.1")
|
||||
ip := netip.MustParseAddr("127.0.0.1")
|
||||
key := "some random string that is hopefully at least this long"
|
||||
createdAt := time.Now()
|
||||
cid := NewConnectionID(ip, createdAt, key)
|
||||
@@ -170,7 +171,7 @@ func BenchmarkValidConnectionID(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkConnectionIDGenerator_Validate(b *testing.B) {
|
||||
ip := net.ParseIP("127.0.0.1")
|
||||
ip := netip.MustParseAddr("127.0.0.1")
|
||||
key := "some random string that is hopefully at least this long"
|
||||
createdAt := time.Now()
|
||||
cid := NewConnectionID(ip, createdAt, key)
|
||||
|
||||
+14
-41
@@ -7,9 +7,9 @@ import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/netip"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -184,7 +184,7 @@ func (t *Frontend) serve() error {
|
||||
|
||||
// Read a UDP packet into a reusable buffer.
|
||||
buffer := pool.Get()
|
||||
n, addr, err := t.socket.ReadFromUDP(*buffer)
|
||||
n, addrPort, err := t.socket.ReadFromUDPAddrPort(*buffer)
|
||||
if err != nil {
|
||||
pool.Put(buffer)
|
||||
var netErr net.Error
|
||||
@@ -206,24 +206,20 @@ func (t *Frontend) serve() error {
|
||||
defer t.wg.Done()
|
||||
defer pool.Put(buffer)
|
||||
|
||||
if ip := addr.IP.To4(); ip != nil {
|
||||
addr.IP = ip
|
||||
}
|
||||
|
||||
// Handle the request.
|
||||
addr := addrPort.Addr()
|
||||
var start time.Time
|
||||
if t.EnableRequestTiming {
|
||||
start = time.Now()
|
||||
}
|
||||
action, af, err := t.handleRequest(
|
||||
// Make sure the IP is copied, not referenced.
|
||||
Request{(*buffer)[:n], append([]byte{}, addr.IP...)},
|
||||
ResponseWriter{t.socket, addr},
|
||||
action, err := t.handleRequest(
|
||||
Request{(*buffer)[:n], addr},
|
||||
ResponseWriter{t.socket, addrPort},
|
||||
)
|
||||
if t.EnableRequestTiming {
|
||||
recordResponseDuration(action, af, err, time.Since(start))
|
||||
recordResponseDuration(action, addr, err, time.Since(start))
|
||||
} else {
|
||||
recordResponseDuration(action, af, err, time.Duration(0))
|
||||
recordResponseDuration(action, addr, err, time.Duration(0))
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -232,23 +228,23 @@ func (t *Frontend) serve() error {
|
||||
// Request represents a UDP payload received by a Tracker.
|
||||
type Request struct {
|
||||
Packet []byte
|
||||
IP net.IP
|
||||
IP netip.Addr
|
||||
}
|
||||
|
||||
// ResponseWriter implements the ability to respond to a Request via the
|
||||
// io.Writer interface.
|
||||
type ResponseWriter struct {
|
||||
socket *net.UDPConn
|
||||
addr *net.UDPAddr
|
||||
socket *net.UDPConn
|
||||
addrPort netip.AddrPort
|
||||
}
|
||||
|
||||
// Write implements the io.Writer interface for a ResponseWriter.
|
||||
func (w ResponseWriter) Write(b []byte) (int, error) {
|
||||
return w.socket.WriteToUDP(b, w.addr)
|
||||
return w.socket.WriteToUDPAddrPort(b, w.addrPort)
|
||||
}
|
||||
|
||||
// handleRequest parses and responds to a UDP Request.
|
||||
func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string, af *bittorrent.AddressFamily, err error) {
|
||||
func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string, err error) {
|
||||
if len(r.Packet) < 16 {
|
||||
// Malformed, no client packets are less than 16 bytes.
|
||||
// We explicitly return nothing in case this is a DoS attempt.
|
||||
@@ -283,16 +279,6 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
|
||||
return
|
||||
}
|
||||
|
||||
af = new(bittorrent.AddressFamily)
|
||||
if r.IP.To4() != nil {
|
||||
*af = bittorrent.IPv4
|
||||
} else if len(r.IP) == net.IPv6len { // implies r.IP.To4() == nil
|
||||
*af = bittorrent.IPv6
|
||||
} else {
|
||||
// Should never happen - we got the IP straight from the UDP packet.
|
||||
panic(fmt.Sprintf("udp: invalid IP: neither v4 nor v6, IP: %#v", r.IP))
|
||||
}
|
||||
|
||||
WriteConnectionID(w, txID, gen.Generate(r.IP, timecache.Now()))
|
||||
|
||||
case announceActionID, announceV6ActionID:
|
||||
@@ -304,8 +290,6 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
|
||||
WriteError(w, txID, err)
|
||||
return
|
||||
}
|
||||
af = new(bittorrent.AddressFamily)
|
||||
*af = req.IP.AddressFamily
|
||||
|
||||
var ctx context.Context
|
||||
var resp *bittorrent.AnnounceResponse
|
||||
@@ -315,7 +299,7 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
|
||||
return
|
||||
}
|
||||
|
||||
WriteAnnounce(w, txID, resp, actionID == announceV6ActionID, req.IP.AddressFamily == bittorrent.IPv6)
|
||||
WriteAnnounce(w, txID, resp, actionID == announceV6ActionID, r.IP.Is6())
|
||||
|
||||
go t.logic.AfterAnnounce(ctx, req, resp)
|
||||
|
||||
@@ -329,17 +313,6 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
|
||||
return
|
||||
}
|
||||
|
||||
if r.IP.To4() != nil {
|
||||
req.AddressFamily = bittorrent.IPv4
|
||||
} else if len(r.IP) == net.IPv6len { // implies r.IP.To4() == nil
|
||||
req.AddressFamily = bittorrent.IPv6
|
||||
} else {
|
||||
// Should never happen - we got the IP straight from the UDP packet.
|
||||
panic(fmt.Sprintf("udp: invalid IP: neither v4 nor v6, IP: %#v", r.IP))
|
||||
}
|
||||
af = new(bittorrent.AddressFamily)
|
||||
*af = req.AddressFamily
|
||||
|
||||
var ctx context.Context
|
||||
var resp *bittorrent.ScrapeResponse
|
||||
ctx, resp, err = t.logic.HandleScrape(context.Background(), req)
|
||||
|
||||
+16
-14
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"sync"
|
||||
|
||||
"github.com/sot-tech/mochi/bittorrent"
|
||||
@@ -41,8 +42,6 @@ var (
|
||||
}
|
||||
|
||||
errMalformedPacket = bittorrent.ClientError("malformed packet")
|
||||
errMalformedIP = bittorrent.ClientError("malformed IP address")
|
||||
errMalformedEvent = bittorrent.ClientError("malformed event ID")
|
||||
errUnknownAction = bittorrent.ClientError("unknown action ID")
|
||||
errBadConnectionID = bittorrent.ClientError("bad connection ID")
|
||||
errUnknownOptionType = bittorrent.ClientError("unknown option type")
|
||||
@@ -89,19 +88,23 @@ func ParseAnnounce(r Request, v6Action bool, opts ParseOptions) (*bittorrent.Ann
|
||||
|
||||
eventID := int(r.Packet[83])
|
||||
if eventID >= len(eventIDs) {
|
||||
return nil, errMalformedEvent
|
||||
return nil, bittorrent.ErrUnknownEvent
|
||||
}
|
||||
|
||||
ip := r.IP
|
||||
ipProvided := false
|
||||
if ipBytes := r.Packet[84:ipEnd]; opts.AllowIPSpoofing {
|
||||
// Make sure the bytes are copied to a new slice.
|
||||
copy(ip, ipBytes)
|
||||
if opts.AllowIPSpoofing {
|
||||
ipBytes := r.Packet[84:ipEnd]
|
||||
spoofed, ok := netip.AddrFromSlice(ipBytes)
|
||||
if !ok {
|
||||
return nil, bittorrent.ErrInvalidIP
|
||||
}
|
||||
ipProvided = true
|
||||
ip = spoofed
|
||||
}
|
||||
if !opts.AllowIPSpoofing && r.IP == nil {
|
||||
if !opts.AllowIPSpoofing && r.IP.IsUnspecified() {
|
||||
// We have no IP address to fallback on.
|
||||
return nil, errMalformedIP
|
||||
return nil, bittorrent.ErrInvalidIP
|
||||
}
|
||||
|
||||
numWant := binary.BigEndian.Uint32(r.Packet[ipEnd+4 : ipEnd+8])
|
||||
@@ -133,18 +136,17 @@ func ParseAnnounce(r Request, v6Action bool, opts ParseOptions) (*bittorrent.Ann
|
||||
NumWantProvided: true,
|
||||
EventProvided: true,
|
||||
Peer: bittorrent.Peer{
|
||||
ID: peerID,
|
||||
IP: bittorrent.IP{IP: ip},
|
||||
Port: port,
|
||||
ID: peerID,
|
||||
AddrPort: netip.AddrPortFrom(ip, port),
|
||||
},
|
||||
Params: params,
|
||||
}
|
||||
|
||||
if err := bittorrent.SanitizeAnnounce(request, opts.MaxNumWant, opts.DefaultNumWant); err != nil {
|
||||
return nil, err
|
||||
if err = bittorrent.SanitizeAnnounce(request, opts.MaxNumWant, opts.DefaultNumWant); err != nil {
|
||||
request = nil
|
||||
}
|
||||
|
||||
return request, nil
|
||||
return request, err
|
||||
}
|
||||
|
||||
type buffer struct {
|
||||
|
||||
@@ -2,11 +2,13 @@ package udp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
"github.com/sot-tech/mochi/bittorrent"
|
||||
"github.com/sot-tech/mochi/pkg/metrics"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -24,7 +26,7 @@ var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
|
||||
|
||||
// recordResponseDuration records the duration of time to respond to a UDP
|
||||
// Request in milliseconds.
|
||||
func recordResponseDuration(action string, af *bittorrent.AddressFamily, err error, duration time.Duration) {
|
||||
func recordResponseDuration(action string, addr netip.Addr, err error, duration time.Duration) {
|
||||
var errString string
|
||||
if err != nil {
|
||||
var clientErr bittorrent.ClientError
|
||||
@@ -35,16 +37,7 @@ func recordResponseDuration(action string, af *bittorrent.AddressFamily, err err
|
||||
}
|
||||
}
|
||||
|
||||
var afString string
|
||||
if af == nil {
|
||||
afString = "Unknown"
|
||||
} else if *af == bittorrent.IPv4 {
|
||||
afString = "IPv4"
|
||||
} else if *af == bittorrent.IPv6 {
|
||||
afString = "IPv6"
|
||||
}
|
||||
|
||||
promResponseDurationMilliseconds.
|
||||
WithLabelValues(action, afString, errString).
|
||||
WithLabelValues(action, metrics.AddressFamily(addr), errString).
|
||||
Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond))
|
||||
}
|
||||
|
||||
@@ -49,8 +49,8 @@ func WriteAnnounce(w io.Writer, txID []byte, resp *bittorrent.AnnounceResponse,
|
||||
}
|
||||
|
||||
for _, peer := range peers {
|
||||
_, _ = buf.Write(peer.IP.IP)
|
||||
_ = binary.Write(buf, binary.BigEndian, peer.Port)
|
||||
_, _ = buf.Write(peer.Addr().AsSlice())
|
||||
_ = binary.Write(buf, binary.BigEndian, peer.Port())
|
||||
}
|
||||
|
||||
_, _ = w.Write(buf.Bytes())
|
||||
|
||||
Reference in New Issue
Block a user