Skip to content

Commit

Permalink
Improve ipv6 compat addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
Freeaqingme committed Apr 29, 2022
1 parent c35e14d commit 129dcf8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
60 changes: 50 additions & 10 deletions v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,27 @@ func (header *Header) formatVersion1() ([]byte, error) {
sourceIP = sourceIP.To16()
destIP = destIP.To16()
}

if sourceIP == nil || destIP == nil {
return nil, ErrInvalidAddress
}

ipToString := func(ip net.IP) string {
if header.TransportProtocol == TCPv6 && ip.To4() != nil {
return fmt.Sprintf("::FFFF:%s", ip.String())
}

return ip.String()
}

buf := bytes.NewBuffer(make([]byte, 0, 108))
buf.Write(SIGV1)
buf.WriteString(separator)
buf.WriteString(proto)
buf.WriteString(separator)
buf.WriteString(sourceIP.String())
buf.WriteString(ipToString(sourceIP))
buf.WriteString(separator)
buf.WriteString(destIP.String())
buf.WriteString(ipToString(destIP))
buf.WriteString(separator)
buf.WriteString(strconv.Itoa(sourceAddr.Port))
buf.WriteString(separator)
Expand All @@ -222,15 +231,46 @@ func parseV1PortNumber(portStr string) (int, error) {
}

func parseV1IPAddress(protocol AddressFamilyAndProtocol, addrStr string) (net.IP, error) {
ip := net.ParseIP(addrStr)
switch protocol {
case TCPv4:
ip = ip.To4()
case TCPv6:
ip = ip.To16()

addr := net.ParseIP(addrStr)
if addr == nil {
return nil, ErrInvalidAddress
}
if ip == nil {

if protocol == TCPv4 && addr.To4() == nil {
return nil, ErrInvalidAddress
} else if protocol == TCPv4 {
return addr, nil
}

if protocol == TCPv6 && !strings.Contains(addr.String(), ".") {
return addr, nil
}
return ip, nil

// This check is not foolproof, but it's all we can using the net/IP library
if protocol == TCPv6 && !strings.Contains(strings.ToLower(addrStr), ":ffff:") {
return nil, ErrInvalidAddress
}

if protocol == TCPv6 && isIpv4InIpv6(addr) {
return addr, nil
}

return nil, ErrInvalidAddress
}

func isIpv4InIpv6(ip net.IP) bool {
isZeros := func(p net.IP) bool {
for i := 0; i < len(p); i++ {
if p[i] != 0 {
return false
}
}
return true
}

return len(ip) == net.IPv6len &&
isZeros(ip[0:10]) &&
ip[10] == 0xff &&
ip[11] == 0xff
}
14 changes: 7 additions & 7 deletions v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
IPv4AddressesAndInvalidPorts = strings.Join([]string{IP4_ADDR, IP4_ADDR, strconv.Itoa(INVALID_PORT), strconv.Itoa(INVALID_PORT)}, separator)
IPv6AddressesAndPorts = strings.Join([]string{IP6_ADDR, IP6_ADDR, strconv.Itoa(PORT), strconv.Itoa(PORT)}, separator)
IPv6LongAddressesAndPorts = strings.Join([]string{IP6_LONG_ADDR, IP6_LONG_ADDR, strconv.Itoa(PORT), strconv.Itoa(PORT)}, separator)
TCP6CompatAddressesAndPorts = strings.Join([]string{IP6_COMPAT_ADDR, IP6_ADDR, strconv.Itoa(PORT), strconv.Itoa(PORT)}, separator)
TCP6CompatAddressesAndPorts = strings.Join([]string{IP6_COMPAT_ADDR, IP6_COMPAT_ADDR, strconv.Itoa(PORT), strconv.Itoa(PORT)}, separator)

fixtureTCP4V1 = "PROXY TCP4 " + IPv4AddressesAndPorts + crlf + "GET /"
fixtureTCP6V1 = "PROXY TCP6 " + IPv6AddressesAndPorts + crlf + "GET /"
Expand Down Expand Up @@ -69,11 +69,11 @@ var invalidParseV1Tests = []struct {
reader: newBufioReader([]byte("PROXY TCP4 " + IPv4AddressesAndPorts)),
expectedError: ErrCantReadVersion1Header,
},
//{
// desc: "TCP6 with IPv4 addresses",
// reader: newBufioReader([]byte("PROXY TCP6 " + IPv4AddressesAndPorts + 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 @@ -158,7 +158,7 @@ var validParseAndWriteV1Tests = []struct {
Command: PROXY,
TransportProtocol: TCPv6,
SourceAddr: v6CompatAddr,
DestinationAddr: v6addr,
DestinationAddr: v6CompatAddr,
},
},
}
Expand Down

0 comments on commit 129dcf8

Please sign in to comment.