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 validated uTP header alone is structural evidence only: DetectClientUDPRequest accepts nothing but a standalone SYN, and only what the client sends may be classified (a remote peer must not be able to frame a user with a crafted packet).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 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 the two-byte transaction ID, 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 its standalone SYN, whose timestamp_difference is zero (nothing was received yet): four zero bytes a QUIC short header or other random-looking datagram almost never has.
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.