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 is structural evidence only and should be considered in flow context when a false positive matters.
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 for use at a proxy's outbound UDP write point. It avoids treating ordinary DHT replies or arbitrary uTP DATA/STATE packets as proof. A new uTP flow is recognized only by its standalone SYN packet.
func DetectUDP ¶
DetectUDP returns the protocol whose strict parser accepts packet. Tracker detection covers structurally valid client requests; standalone tracker responses need ParseUDPTrackerResponse and a matching observed transaction. A uTP result validates the header shape and should be used in flow context.
type TCPHandshakeDetector ¶
type TCPHandshakeDetector struct {
// contains filtered or unexported fields
}
TCPHandshakeDetector incrementally checks the beginning of one reassembled TCP byte stream for the plaintext BitTorrent handshake signature. Feed each in-order, non-duplicate payload chunk once, starting at stream offset zero. It retains no packet data. A mismatch permanently rejects that stream; a match remains true on later calls. Use a fresh value for each TCP stream.
func (*TCPHandshakeDetector) Feed ¶
func (d *TCPHandshakeDetector) Feed(chunk []byte) bool
Feed adds the next ordered TCP payload chunk and reports whether the stream has begun with the exact plaintext BitTorrent handshake 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 ¶
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.