udp

package
v0.0.0-...-384af84 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2023 License: BSD-2-Clause Imports: 20 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConnectionID

func NewConnectionID(ip net.IP, now time.Time, key string) []byte

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 ParseAnnounce

func ParseAnnounce(r Request, v6Action bool, opts ParseOptions) (*bittorrent.AnnounceRequest, error)

ParseAnnounce parses an AnnounceRequest from a UDP request.

If v6Action is true, the announce is parsed the "old opentracker way": https://web.archive.org/web/20170503181830/http://opentracker.blog.h3q.com/2007/12/28/the-ipv6-situation/

func ParseScrape

func ParseScrape(r Request, opts ParseOptions) (*bittorrent.ScrapeRequest, error)

ParseScrape parses a ScrapeRequest from a UDP request.

func ValidConnectionID

func ValidConnectionID(connectionID []byte, ip net.IP, now time.Time, maxClockSkew time.Duration, key string) bool

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 WriteAnnounce

func WriteAnnounce(w io.Writer, txID []byte, resp *bittorrent.AnnounceResponse, v6Action, v6Peers bool)

WriteAnnounce encodes an announce response according to BEP 15. The peers returned will be resp.IPv6Peers or resp.IPv4Peers, depending on whether v6Peers is set. If v6Action is set, the action will be 4, according to https://web.archive.org/web/20170503181830/http://opentracker.blog.h3q.com/2007/12/28/the-ipv6-situation/

func WriteConnectionID

func WriteConnectionID(w io.Writer, txID, connID []byte)

WriteConnectionID encodes a new connection response according to BEP 15.

func WriteError

func WriteError(w io.Writer, txID []byte, err error)

WriteError writes the failure reason as a null-terminated string.

func WriteScrape

func WriteScrape(w io.Writer, txID []byte, resp *bittorrent.ScrapeResponse)

WriteScrape encodes a scrape response according to BEP 15.

Types

type Config

type Config struct {
	Addr                string        `yaml:"addr"`
	PrivateKey          string        `yaml:"private_key"`
	MaxClockSkew        time.Duration `yaml:"max_clock_skew"`
	EnableRequestTiming bool          `yaml:"enable_request_timing"`
	ParseOptions        `yaml:",inline"`
}

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

func (Config) LogFields

func (cfg Config) LogFields() log.Fields

LogFields renders the current config as a set of Logrus fields.

func (Config) Validate

func (cfg Config) Validate() Config

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

This function warns to the logger when a value is changed.

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 string) *ConnectionIDGenerator

NewConnectionIDGenerator creates a new connection ID generator.

func (*ConnectionIDGenerator) Generate

func (g *ConnectionIDGenerator) Generate(ip net.IP, now time.Time) []byte

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

The first 4 bytes of the connection identifier is a unix timestamp and the last 4 bytes are a truncated HMAC token created from the aforementioned unix timestamp and the source IP address of the UDP packet.

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 32 bits, thus a forgery probability of approximately 1 in 4 billion.

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 (*ConnectionIDGenerator) Validate

func (g *ConnectionIDGenerator) Validate(connectionID []byte, ip net.IP, now time.Time, maxClockSkew time.Duration) bool

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

type Frontend

type Frontend struct {
	Config
	// contains filtered or unexported fields
}

Frontend holds the state of a UDP BitTorrent Frontend.

func NewFrontend

func NewFrontend(logic frontend.TrackerLogic, provided Config) (*Frontend, error)

NewFrontend creates a new instance of an UDP Frontend that asynchronously serves requests.

func (*Frontend) Stop

func (t *Frontend) Stop() stop.Result

Stop provides a thread-safe way to shutdown a currently running Frontend.

type ParseOptions

type ParseOptions struct {
	AllowIPSpoofing     bool   `yaml:"allow_ip_spoofing"`
	MaxNumWant          uint32 `yaml:"max_numwant"`
	DefaultNumWant      uint32 `yaml:"default_numwant"`
	MaxScrapeInfoHashes uint32 `yaml:"max_scrape_infohashes"`
}

ParseOptions is the configuration used to parse an Announce Request.

If AllowIPSpoofing is true, IPs provided via params will be used.

type Request

type Request struct {
	Packet []byte
	IP     net.IP
}

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.

Directories

Path Synopsis
Package bytepool implements an optimization for byte slice allocations by caching those slices for later reuse.
Package bytepool implements an optimization for byte slice allocations by caching those slices for later reuse.

Jump to

Keyboard shortcuts

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