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
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 ¶
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.
Types ¶
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.
func DetectClientUDPRequest ¶
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 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 ¶
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.