mirror of
https://github.com/fosrl/gerbil.git
synced 2026-05-15 04:39:59 +00:00
Add pp to relay
This commit is contained in:
@@ -3,6 +3,8 @@ package proxy
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/fosrl/gerbil/proxyproto"
|
||||
)
|
||||
|
||||
func TestBuildProxyProtocolHeader(t *testing.T) {
|
||||
@@ -56,7 +58,7 @@ func TestBuildProxyProtocolHeader(t *testing.T) {
|
||||
t.Fatalf("Failed to resolve target address: %v", err)
|
||||
}
|
||||
|
||||
result := buildProxyProtocolHeader(clientTCP, targetTCP)
|
||||
result := proxyproto.BuildV1Header(clientTCP, targetTCP)
|
||||
if result != tt.expected {
|
||||
t.Errorf("Expected %q, got %q", tt.expected, result)
|
||||
}
|
||||
@@ -69,7 +71,7 @@ func TestBuildProxyProtocolHeaderUnknownType(t *testing.T) {
|
||||
clientAddr := &net.UDPAddr{IP: net.ParseIP("192.168.1.100"), Port: 12345}
|
||||
targetAddr := &net.UDPAddr{IP: net.ParseIP("10.0.0.1"), Port: 443}
|
||||
|
||||
result := buildProxyProtocolHeader(clientAddr, targetAddr)
|
||||
result := proxyproto.BuildV1Header(clientAddr, targetAddr)
|
||||
expected := "PROXY UNKNOWN\r\n"
|
||||
|
||||
if result != expected {
|
||||
@@ -78,13 +80,8 @@ func TestBuildProxyProtocolHeaderUnknownType(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBuildProxyProtocolHeaderFromInfo(t *testing.T) {
|
||||
proxy, err := NewSNIProxy(8443, "", "", "127.0.0.1", 443, nil, true, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create SNI proxy: %v", err)
|
||||
}
|
||||
|
||||
// Test IPv4 case
|
||||
proxyInfo := &ProxyProtocolInfo{
|
||||
info := &proxyproto.Info{
|
||||
Protocol: "TCP4",
|
||||
SrcIP: "10.0.0.1",
|
||||
DestIP: "192.168.1.100",
|
||||
@@ -93,7 +90,7 @@ func TestBuildProxyProtocolHeaderFromInfo(t *testing.T) {
|
||||
}
|
||||
|
||||
targetAddr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:8080")
|
||||
header := proxy.buildProxyProtocolHeaderFromInfo(proxyInfo, targetAddr)
|
||||
header := proxyproto.BuildV1HeaderFromInfo(info, targetAddr)
|
||||
|
||||
expected := "PROXY TCP4 10.0.0.1 127.0.0.1 12345 8080\r\n"
|
||||
if header != expected {
|
||||
@@ -101,7 +98,7 @@ func TestBuildProxyProtocolHeaderFromInfo(t *testing.T) {
|
||||
}
|
||||
|
||||
// Test IPv6 case
|
||||
proxyInfo = &ProxyProtocolInfo{
|
||||
info = &proxyproto.Info{
|
||||
Protocol: "TCP6",
|
||||
SrcIP: "2001:db8::1",
|
||||
DestIP: "2001:db8::2",
|
||||
@@ -110,10 +107,99 @@ func TestBuildProxyProtocolHeaderFromInfo(t *testing.T) {
|
||||
}
|
||||
|
||||
targetAddr, _ = net.ResolveTCPAddr("tcp6", "[::1]:8080")
|
||||
header = proxy.buildProxyProtocolHeaderFromInfo(proxyInfo, targetAddr)
|
||||
header = proxyproto.BuildV1HeaderFromInfo(info, targetAddr)
|
||||
|
||||
expected = "PROXY TCP6 2001:db8::1 ::1 12345 8080\r\n"
|
||||
if header != expected {
|
||||
t.Errorf("Expected header '%s', got '%s'", expected, header)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseV2UDPHeader(t *testing.T) {
|
||||
// Build a minimal PROXY v2 header for IPv4 UDP
|
||||
// Magic (12) + ver/cmd (1) + fam/proto (1) + len (2) + src IP (4) + dst IP (4) + src port (2) + dst port (2) = 28 bytes
|
||||
header := []byte{
|
||||
// Magic signature
|
||||
0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A,
|
||||
// Version 2 (0x2x), PROXY command (0x01)
|
||||
0x21,
|
||||
// AF_INET (0x1x), DGRAM/UDP (0x02)
|
||||
0x12,
|
||||
// Address block length: 12 bytes (4+4+2+2)
|
||||
0x00, 0x0C,
|
||||
// Source IP: 192.168.1.100
|
||||
192, 168, 1, 100,
|
||||
// Destination IP: 10.0.0.1
|
||||
10, 0, 0, 1,
|
||||
// Source port: 4500
|
||||
0x11, 0x94,
|
||||
// Destination port: 21820
|
||||
0x55, 0x3C,
|
||||
}
|
||||
|
||||
// Append a fake application payload
|
||||
payload := []byte{0x01, 0x02, 0x03}
|
||||
data := append(header, payload...)
|
||||
|
||||
info, remaining, ok := proxyproto.ParseV2UDPHeader(data)
|
||||
if !ok {
|
||||
t.Fatal("Expected ParseV2UDPHeader to return ok=true")
|
||||
}
|
||||
if info == nil {
|
||||
t.Fatal("Expected non-nil Info")
|
||||
}
|
||||
if info.Protocol != "UDP4" {
|
||||
t.Errorf("Expected protocol UDP4, got %s", info.Protocol)
|
||||
}
|
||||
if info.SrcIP != "192.168.1.100" {
|
||||
t.Errorf("Expected SrcIP 192.168.1.100, got %s", info.SrcIP)
|
||||
}
|
||||
if info.DestIP != "10.0.0.1" {
|
||||
t.Errorf("Expected DestIP 10.0.0.1, got %s", info.DestIP)
|
||||
}
|
||||
if info.SrcPort != 4500 {
|
||||
t.Errorf("Expected SrcPort 4500, got %d", info.SrcPort)
|
||||
}
|
||||
if info.DestPort != 21820 {
|
||||
t.Errorf("Expected DestPort 21820, got %d", info.DestPort)
|
||||
}
|
||||
if len(remaining) != len(payload) {
|
||||
t.Errorf("Expected %d remaining bytes, got %d", len(payload), len(remaining))
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseV2UDPHeaderNoHeader(t *testing.T) {
|
||||
// Data that does NOT start with v2 magic should be returned as-is
|
||||
data := []byte{0x01, 0x02, 0x03}
|
||||
info, remaining, ok := proxyproto.ParseV2UDPHeader(data)
|
||||
if ok {
|
||||
t.Error("Expected ok=false for non-v2 data")
|
||||
}
|
||||
if info != nil {
|
||||
t.Error("Expected nil Info for non-v2 data")
|
||||
}
|
||||
if len(remaining) != len(data) {
|
||||
t.Errorf("Expected remaining to equal original data length %d, got %d", len(data), len(remaining))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsV2Header(t *testing.T) {
|
||||
valid := []byte{
|
||||
0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A,
|
||||
// extra bytes beyond the magic
|
||||
0x21, 0x12,
|
||||
}
|
||||
if !proxyproto.IsV2Header(valid) {
|
||||
t.Error("Expected IsV2Header=true for valid magic")
|
||||
}
|
||||
|
||||
invalid := []byte{0x01, 0x02, 0x03}
|
||||
if proxyproto.IsV2Header(invalid) {
|
||||
t.Error("Expected IsV2Header=false for non-magic data")
|
||||
}
|
||||
|
||||
tooShort := []byte{0x0D, 0x0A}
|
||||
if proxyproto.IsV2Header(tooShort) {
|
||||
t.Error("Expected IsV2Header=false for too-short data")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user