udp

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 2, 2022 License: BSD-2-Clause Imports: 23 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 ParseAnnounce

func ParseAnnounce(r Request, v6Action bool, opts frontend.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 frontend.ParseOptions) (*bittorrent.ScrapeRequest, error)

ParseScrape parses a ScrapeRequest from a UDP request.

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
	PrivateKey          string        `cfg:"private_key"`
	MaxClockSkew        time.Duration `cfg:"max_clock_skew"`
	EnableRequestTiming bool          `cfg:"enable_request_timing"`
	frontend.ParseOptions
}

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

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 netip.Addr, 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 netip.Addr, 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, c conf.MapConfig) (*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 shut down a currently running Frontend.

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.

Directories

Path Synopsis
Package bytepool defines pool for storing and reusing raw bytes
Package bytepool defines pool for storing and reusing raw bytes

Jump to

Keyboard shortcuts

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