Added support for STUN protocol detection

This commit is contained in:
2025-10-10 14:39:31 +02:00
parent 7b5578859e
commit 23bd918b20
2 changed files with 186 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
package protocol
import "testing"
func TestDetectSTUN(t *testing.T) {
// A valid STUN binding request.
bindingRequest := []byte{
0x00, 0x01, // Message Type
0x00, 0x00, // Message Length (0 bytes)
0x21, 0x12, 0xa4, 0x42, // Magic Cookie
0x4b, 0x49, 0x6b, 0x72, 0x7a, 0x6a, 0x56, 0x37, 0x41, 0x61, 0x6e, 0x38, // Transaction ID
}
// A valid STUN binding success response.
bindingSuccessResponse := []byte{
0x01, 0x01, // Message Type
0x00, 0x0c, // Message Length (12 bytes)
0x21, 0x12, 0xa4, 0x42, // Magic Cookie
0x4b, 0x49, 0x6b, 0x72, 0x7a, 0x6a, 0x56, 0x37, 0x41, 0x61, 0x6e, 0x38, // Transaction ID
// --- Attributes (12 bytes) ---
0x00, 0x01, // Attribute Type MAPPED-ADDRESS
0x00, 0x08, // Attribute Length (8 bytes)
0x00, // Reserved (always 0)
0x01, // Protocol Family: IPv4 (0)
0x11, 0xfc, // Port Number: 4604
0x46, 0xc7, 0x80, 0x2e, // IP: 70.199.128.46
}
// A truncated STUN binding success response.
truncatedBindingSuccessResponse := []byte{
0x01, 0x01, // Message Type
0x00, 0x0c, // Message Length (12 bytes)
0x21, 0x12, 0xa4, 0x42, // Magic Cookie
0x4b, 0x49, 0x6b, 0x72, 0x7a, 0x6a, 0x56, 0x37, 0x41, 0x61, 0x6e, 0x38, // Transaction ID
// --- Attributes (12 bytes) ---
0x00, 0x01, // Attribute Type MAPPED-ADDRESS
0x00, 0x08, // Attribute Length (8 bytes)
0x00, // Reserved (always 0)
/* truncated */
}
// Am invalid STUN binding request (invalid magic).
invalidMagicBindingRequest := []byte{
0x00, 0x01, // Message Type
0x00, 0x00, // Message Length (0 bytes)
0x2a, 0x12, 0xa4, 0x42, // Magic Cookie
0x4b, 0x49, 0x6b, 0x72, 0x7a, 0x6a, 0x56, 0x37, 0x41, 0x61, 0x6e, 0x38, // Transaction ID
}
tests := []*testCase{
{
Name: "STUN binding request",
Direction: Client,
Data: bindingRequest,
DstPort: 3478,
WantType: TypeSTUN,
WantConfidence: .9,
},
{
Name: "STUN binding success response",
Direction: Server,
Data: bindingSuccessResponse,
SrcPort: 3478,
WantType: TypeSTUN,
WantConfidence: .9,
},
{
Name: "Truncated STUN binding success response",
Direction: Server,
Data: truncatedBindingSuccessResponse,
SrcPort: 3478,
WantType: TypeSTUN,
WantConfidence: .7,
},
{
Name: "Invalid magic STUN binding request",
Direction: Client,
Data: invalidMagicBindingRequest,
DstPort: 3478,
WantError: ErrUnknown,
},
}
testRunner(t, tests)
}