torrentguard

package
v0.1.44 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package torrentguard contains bounded, dependency-free parsers for plaintext BitTorrent protocol signatures. A positive result is based on a validated protocol signature or wire structure; ports are never used as evidence.

These parsers do not identify MSE/PE-encrypted BitTorrent, BitTorrent inside another VPN or proxy, or HTTPS web seeds. Unknown and malformed traffic must be allowed by callers. A uTP SYN is weak structural evidence: the classifier accepts only standalone SYN shapes used by clients, and callers must pass only what the client sends so a remote peer cannot frame a user with a crafted packet.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClassifyClientUDPRequest added in v0.1.28

func ClassifyClientUDPRequest(packet []byte, dstPort uint16) (Protocol, Evidence, bool)

ClassifyClientUDPRequest is DetectClientUDPRequest that knows the destination port (0 = unknown) and also says what matched. DNS ports are never classified.

A tracker is recognized by its connect request only: the 16 bytes with the protocol's 64-bit magic. An announce or a scrape has no fixed marker, so its layout alone is not evidence (a 98-byte DNS query with an EDNS cookie has the shape of an announce), and a real client always connects first: the connect is blocked, so no announce follows it.

func HasBitTorrentHandshake

func HasBitTorrentHandshake(data []byte) bool

HasBitTorrentHandshake reports whether data begins with the exact 20-byte plaintext BitTorrent handshake signature. data must start at the beginning of a TCP byte stream; it may contain only the signature or the rest of the handshake and subsequent stream data as well.

func Port added in v0.1.33

func Port(addr string) string

Port returns a destination's numeric port without exposing its host. It returns an empty string when the address has no valid decimal port.

func WrapTCP added in v0.1.33

func WrapTCP(conn net.Conn, attempt func()) net.Conn

WrapTCP holds only a possible BitTorrent handshake prefix and closes the connection when the complete signature is present. The prefix is released as soon as the stream differs.

Types

type Attempt added in v0.1.33

type Attempt func(protocol Protocol, evidence Evidence, addr string)

type Evidence added in v0.1.28

type Evidence string

Evidence names, as a stable event label, exactly what the detector matched. It reaches the panel next to the destination port so the owner can tell a real torrent from a false alarm; it never carries an address.

const (
	EvidenceTrackerConnect Evidence = "tracker_connect" // BEP 15 connect request with the protocol's 64-bit magic
	EvidenceDHTQuery       Evidence = "dht_query"       // BEP 5 KRPC query (ping, find_node, get_peers, announce_peer)
	EvidenceUTPSyn         Evidence = "utp_syn"         // BEP 29 standalone SYN with no ack and an observed client shape
	EvidenceTCPHandshake   Evidence = "tcp_handshake"   // the 20-byte plaintext BitTorrent handshake at the stream start
)

type KRPCMessage

type KRPCMessage struct {
	Type          KRPCMessageType
	TransactionID string
	Method        string
	ErrorCode     int64
}

KRPCMessage contains the identifying fields of a validated BEP 5 message. TransactionID and Method are copied from the packet into strings.

func ParseKRPCMessage

func ParseKRPCMessage(packet []byte) (KRPCMessage, bool)

ParseKRPCMessage validates a complete, canonical bencoded BEP 5 query, response, or error. It requires a transaction ID of 1 to 16 bytes, a recognized query method and method-specific required fields, or a correctly shaped response/error. Arbitrary bencoded dictionaries and bare d/e prefixes are not classified as KRPC.

type KRPCMessageType

type KRPCMessageType uint8

KRPCMessageType identifies the validated BEP 5 KRPC message kind.

const (
	KRPCQuery KRPCMessageType = iota + 1
	KRPCResponse
	KRPCError
)

type Protocol

type Protocol string

Protocol identifies a positively parsed plaintext BitTorrent transport or UDP protocol. The values are stable event labels.

const (
	ProtocolBitTorrentTCP     Protocol = "bittorrent_tcp"
	ProtocolBitTorrentDHT     Protocol = "bittorrent_dht"
	ProtocolBitTorrentUTP     Protocol = "bittorrent_utp"
	ProtocolBitTorrentTracker Protocol = "bittorrent_tracker"
)

func DetectClientUDPRequest

func DetectClientUDPRequest(packet []byte) (Protocol, bool)

DetectClientUDPRequest recognizes client-to-network BitTorrent requests: call it only on datagrams the client sent. It avoids treating ordinary DHT replies or arbitrary uTP DATA/STATE packets as proof (every WireGuard handshake initiation, "01 00 00 00", is a structurally valid uTP DATA header). A new uTP flow is recognized only by a standalone SYN with ack_nr 0 and one of the two observed client header shapes.

It does not know where the datagram goes; callers that do should use ClassifyClientUDPRequest.

type UDPConn added in v0.1.33

type UDPConn interface {
	ReadFrom([]byte) (int, string, error)
	WriteTo([]byte, string) (int, error)
	Close() error
}

UDPConn is the common datagram surface used by node egresses and Hysteria2.

func WrapUDP added in v0.1.33

func WrapUDP(conn UDPConn, attempt Attempt) UDPConn

WrapUDP drops only datagrams carrying a validated BitTorrent signature.

type UDPTrackerAction

type UDPTrackerAction uint32

UDPTrackerAction is a BEP 15 UDP tracker action code.

const (
	UDPTrackerConnect  UDPTrackerAction = 0
	UDPTrackerAnnounce UDPTrackerAction = 1
	UDPTrackerScrape   UDPTrackerAction = 2
	UDPTrackerError    UDPTrackerAction = 3
)

type UDPTrackerPacket

type UDPTrackerPacket struct {
	Action        UDPTrackerAction
	TransactionID uint32
	IsError       bool
}

UDPTrackerPacket contains the validated action and transaction ID from a BEP 15 packet. IsError is set only for a correlated error response.

func ParseUDPTrackerRequest

func ParseUDPTrackerRequest(packet []byte) (UDPTrackerPacket, bool)

ParseUDPTrackerRequest recognizes structurally valid BEP 15 client requests. Connect requests require the protocol's 64-bit magic value. Announce and scrape requests have no fixed magic, so they are accepted only with their exact specified lengths and valid action-specific fields.

func ParseUDPTrackerResponse

func ParseUDPTrackerResponse(packet []byte, requestAction UDPTrackerAction, transactionID uint32) (UDPTrackerPacket, bool)

ParseUDPTrackerResponse validates a BEP 15 response against the action and transaction ID of a request previously observed on the same UDP flow. This correlation is required because response headers alone are ambiguous. Error responses may correspond to any supported request action.

type UTPHeader

type UTPHeader struct {
	Type           UTPType
	ConnectionID   uint16
	SequenceNumber uint16
	AckNumber      uint16
	Timestamp      uint32
	TimestampDiff  uint32
	WindowSize     uint32
	ExtensionCount uint8
	HeaderLength   int
}

UTPHeader contains fields parsed from a structurally valid BEP 29 v1 header.

func ParseUTPHeader

func ParseUTPHeader(packet []byte) (UTPHeader, bool)

ParseUTPHeader validates a BEP 29 version-1 header and its chained extension records. DATA may have payload after the parsed header; FIN, STATE, RESET and SYN packets may not. This validates wire structure, not peer identity, so callers should use the result in flow context where false positives matter.

The layout is BEP 29's (and libutp's): type|ver, extension, connection_id, timestamp_microseconds, timestamp_difference_microseconds, wnd_size, seq_nr, ack_nr.

type UTPType

type UTPType uint8

UTPType is the packet type from the high four bits of a BEP 29 version-1 uTP header.

const (
	UTPData UTPType = iota
	UTPFin
	UTPState
	UTPReset
	UTPSyn
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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