Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ func parseV1IPAddress(protocol AddressFamilyAndProtocol, addrStr string) (net.IP
if addr.Is6() || addr.Is4In6() {
return net.IP(addr.AsSlice()), nil
}
if addr.Is4() {
a16 := addr.As16()
return net.IP(a16[:]), nil
}
}

return nil, ErrInvalidAddress
Expand Down
43 changes: 38 additions & 5 deletions v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ var (

fixtureUnknown = "PROXY UNKNOWN" + crlf
fixtureUnknownWithAddresses = "PROXY UNKNOWN " + IPv4AddressesAndInvalidPorts + crlf

fixtureTCP4Simple = "PROXY TCP4 1.2.3.4 5.6.7.8 12345 22" + crlf
fixtureTCP6IPv6SrcIPv4Dst = "PROXY TCP6 2001:db8::1 192.0.2.1 51512 22" + crlf
fixtureTCP6Loopback = "PROXY TCP6 ::1 ::1 1234 5678" + crlf
)

var invalidParseV1Tests = []struct {
Expand Down Expand Up @@ -75,11 +79,6 @@ var invalidParseV1Tests = []struct {
reader: newBufioReader([]byte("PROXY TCP4 invalid invalid 65533 65533" + crlf)),
expectedError: ErrInvalidAddress,
},
{
desc: "TCP6 with IPv4 addresses",
reader: newBufioReader([]byte("PROXY TCP6 " + IPv4AddressesAndPorts + crlf)),
expectedError: ErrInvalidAddress,
},
{
desc: "TCP4 with IPv6 addresses",
reader: newBufioReader([]byte("PROXY TCP4 " + IPv6AddressesAndPorts + crlf)),
Expand Down Expand Up @@ -177,6 +176,40 @@ var validParseAndWriteV1Tests = []struct {
DestinationAddr: nil,
},
},
{
desc: "TCP4 simple",
reader: bufio.NewReader(strings.NewReader(fixtureTCP4Simple)),
expectedHeader: &Header{
Version: 1,
Command: PROXY,
TransportProtocol: TCPv4,
SourceAddr: &net.TCPAddr{IP: net.ParseIP("1.2.3.4").To4(), Port: 12345},
DestinationAddr: &net.TCPAddr{IP: net.ParseIP("5.6.7.8").To4(), Port: 22},
},
},
{
desc: "TCP6 IPv6 src IPv4 dst mixed",
reader: bufio.NewReader(strings.NewReader(fixtureTCP6IPv6SrcIPv4Dst)),
expectedHeader: &Header{
Version: 1,
Command: PROXY,
TransportProtocol: TCPv6,
SourceAddr: &net.TCPAddr{IP: net.ParseIP("2001:db8::1").To16(), Port: 51512},
DestinationAddr: &net.TCPAddr{IP: net.ParseIP("192.0.2.1").To16(), Port: 22},
},
skipWrite: true,
},
{
desc: "TCP6 loopback",
reader: bufio.NewReader(strings.NewReader(fixtureTCP6Loopback)),
expectedHeader: &Header{
Version: 1,
Command: PROXY,
TransportProtocol: TCPv6,
SourceAddr: &net.TCPAddr{IP: net.ParseIP("::1").To16(), Port: 1234},
DestinationAddr: &net.TCPAddr{IP: net.ParseIP("::1").To16(), Port: 5678},
},
},
}

func TestParseV1Valid(t *testing.T) {
Expand Down