udp

package
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2024 License: BSD-2-Clause Imports: 28 Imported by: 0

Documentation

Overview

Package udp implements a BitTorrent tracker via the UDP protocol as described in BEP 15.

Index

Constants

View Source
const (
	// Name - registered name of the frontend
	Name = "udp"
)

Variables

View Source
var ErrInvalidQueryEscape = bittorrent.ClientError("invalid query escape")

ErrInvalidQueryEscape is returned when a query string contains invalid escapes.

Functions

func NewFrontend

func NewFrontend(c conf.MapConfig, logic *middleware.Logic) (frontend.Frontend, error)

NewFrontend builds and starts udp bittorrent frontend from provided configuration

Types

type Config

type Config struct {
	frontend.ListenOptions
	PrivateKey   string        `cfg:"private_key"`
	MaxClockSkew time.Duration `cfg:"max_clock_skew"`
	frontend.ParseOptions
}

Config represents all the configurable options for a UDP BitTorrent Tracker.

func (Config) Validate

func (cfg Config) Validate() (validCfg Config)

Validate sanity checks values set in a config and returns a new config with default values replacing anything that is invalid.

type ConnectionIDGenerator

type ConnectionIDGenerator struct {
	// contains filtered or unexported fields
}

A ConnectionIDGenerator is a reusable generator and validator for connection IDs as described in BEP 15. It is not thread safe, but is safe to be pooled and reused by other goroutines. It manages its state itself, so it can be taken from and returned to a pool without any cleanup. After initial creation, it can generate connection IDs without allocating. See Generate and Validate for usage notes and guarantees.

func NewConnectionIDGenerator

func NewConnectionIDGenerator(key []byte, maxClockSkew time.Duration) *ConnectionIDGenerator

NewConnectionIDGenerator creates a new connection ID generator.

func (*ConnectionIDGenerator) Generate

func (g *ConnectionIDGenerator) Generate(ip netip.Addr, now time.Time) (out []byte)

Generate generates an 8-byte connection ID as described in BEP 15 for the given IP and the current time.

The first byte is random salt, next 2 bytes - truncated unix timestamp when ID was generated, last 5 bytes are a truncated HMAC token created from salt (1 byte), full unix timestamp (8 bytes) and source IP (4/16 bytes).

Salt used to mitigate generation same MAC if there are several clients from same IP sent requests within one second.

Truncated HMAC is known to be safe for 2^(-n) where n is the size in bits of the truncated HMAC token. In this use case we have 40 bits, thus a forgery probability of approximately 1 in 4 billion.

The generated ID is written to g.buffer, which is also returned. g.buffer 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 (*ConnectionIDGenerator) Validate

func (g *ConnectionIDGenerator) Validate(connectionID []byte, ip netip.Addr, now time.Time) bool

Validate validates the given connection ID for an IP and the current time.

type Request

type Request struct {
	Packet []byte
	IP     netip.Addr
}

Request represents a UDP payload received by a Tracker.

type ResponseWriter

type ResponseWriter struct {
	// contains filtered or unexported fields
}

ResponseWriter implements the ability to respond to a Request via the io.Writer interface.

func (ResponseWriter) Write

func (w ResponseWriter) Write(b []byte) (int, error)

Write implements the io.Writer interface for a ResponseWriter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL