ipn/ipnlocal: add ProxyProtocol support to VIP service TCP handler (#18175)
tcpHandlerForVIPService was missing ProxyProtocol support that tcpHandlerForServe already had. Extract the shared logic into forwardTCPWithProxyProtocol helper and use it in both handlers. Fixes #18172 Signed-off-by: Raj Singh <raj@tailscale.com>
This commit is contained in:
+22
-31
@@ -591,16 +591,7 @@ func (b *LocalBackend) tcpHandlerForVIPService(dstAddr, srcAddr netip.AddrPort)
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
errc := make(chan error, 1)
|
return b.forwardTCPWithProxyProtocol(conn, backConn, tcph.ProxyProtocol(), srcAddr, dport, backDst)
|
||||||
go func() {
|
|
||||||
_, err := io.Copy(backConn, conn)
|
|
||||||
errc <- err
|
|
||||||
}()
|
|
||||||
go func() {
|
|
||||||
_, err := io.Copy(conn, backConn)
|
|
||||||
errc <- err
|
|
||||||
}()
|
|
||||||
return <-errc
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -678,22 +669,27 @@ func (b *LocalBackend) tcpHandlerForServe(dport uint16, srcAddr netip.AddrPort,
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(bradfitz): do the RegisterIPPortIdentity and
|
||||||
|
// UnregisterIPPortIdentity stuff that netstack does
|
||||||
|
return b.forwardTCPWithProxyProtocol(conn, backConn, tcph.ProxyProtocol(), srcAddr, dport, backDst)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// forwardTCPWithProxyProtocol forwards TCP traffic between conn and backConn,
|
||||||
|
// optionally prepending a PROXY protocol header if proxyProtoVer > 0.
|
||||||
|
// The srcAddr is the original client address used to build the PROXY header.
|
||||||
|
func (b *LocalBackend) forwardTCPWithProxyProtocol(conn, backConn net.Conn, proxyProtoVer int, srcAddr netip.AddrPort, dport uint16, backDst string) error {
|
||||||
var proxyHeader []byte
|
var proxyHeader []byte
|
||||||
if ver := tcph.ProxyProtocol(); ver > 0 {
|
if proxyProtoVer > 0 {
|
||||||
// backAddr is the final "destination" of the connection,
|
|
||||||
// which is the connection to the proxied-to backend.
|
|
||||||
backAddr := backConn.RemoteAddr().(*net.TCPAddr)
|
backAddr := backConn.RemoteAddr().(*net.TCPAddr)
|
||||||
|
|
||||||
// We always want to format the PROXY protocol
|
// We always want to format the PROXY protocol header based on
|
||||||
// header based on the IPv4 or IPv6-ness of
|
// the IPv4 or IPv6-ness of the client. The SourceAddr and
|
||||||
// the client. The SourceAddr and
|
// DestinationAddr need to match in type.
|
||||||
// DestinationAddr need to match in type, so we
|
// If this is an IPv6-mapped IPv4 address, unmap it.
|
||||||
// need to be careful to not e.g. set a
|
|
||||||
// SourceAddr of type IPv6 and DestinationAddr
|
|
||||||
// of type IPv4.
|
|
||||||
//
|
|
||||||
// If this is an IPv6-mapped IPv4 address,
|
|
||||||
// though, unmap it.
|
|
||||||
proxySrcAddr := srcAddr
|
proxySrcAddr := srcAddr
|
||||||
if proxySrcAddr.Addr().Is4In6() {
|
if proxySrcAddr.Addr().Is4In6() {
|
||||||
proxySrcAddr = netip.AddrPortFrom(
|
proxySrcAddr = netip.AddrPortFrom(
|
||||||
@@ -713,6 +709,7 @@ func (b *LocalBackend) tcpHandlerForServe(dport uint16, srcAddr netip.AddrPort,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !destAddr.IsValid() {
|
if !destAddr.IsValid() {
|
||||||
|
// Unexpected: we couldn't determine the node's IP address.
|
||||||
// Pick a best-effort destination address of localhost.
|
// Pick a best-effort destination address of localhost.
|
||||||
if is4 {
|
if is4 {
|
||||||
destAddr = netip.AddrFrom4([4]byte{127, 0, 0, 1})
|
destAddr = netip.AddrFrom4([4]byte{127, 0, 0, 1})
|
||||||
@@ -722,7 +719,7 @@ func (b *LocalBackend) tcpHandlerForServe(dport uint16, srcAddr netip.AddrPort,
|
|||||||
}
|
}
|
||||||
|
|
||||||
header := &proxyproto.Header{
|
header := &proxyproto.Header{
|
||||||
Version: byte(ver),
|
Version: byte(proxyProtoVer),
|
||||||
Command: proxyproto.PROXY,
|
Command: proxyproto.PROXY,
|
||||||
SourceAddr: net.TCPAddrFromAddrPort(proxySrcAddr),
|
SourceAddr: net.TCPAddrFromAddrPort(proxySrcAddr),
|
||||||
DestinationAddr: &net.TCPAddr{
|
DestinationAddr: &net.TCPAddr{
|
||||||
@@ -742,14 +739,12 @@ func (b *LocalBackend) tcpHandlerForServe(dport uint16, srcAddr netip.AddrPort,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(bradfitz): do the RegisterIPPortIdentity and
|
|
||||||
// UnregisterIPPortIdentity stuff that netstack does
|
|
||||||
errc := make(chan error, 1)
|
errc := make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
if len(proxyHeader) > 0 {
|
if len(proxyHeader) > 0 {
|
||||||
if _, err := backConn.Write(proxyHeader); err != nil {
|
if _, err := backConn.Write(proxyHeader); err != nil {
|
||||||
errc <- err
|
errc <- err
|
||||||
backConn.Close() // to ensure that the other side gets EOF
|
backConn.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -762,10 +757,6 @@ func (b *LocalBackend) tcpHandlerForServe(dport uint16, srcAddr netip.AddrPort,
|
|||||||
}()
|
}()
|
||||||
return <-errc
|
return <-errc
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *LocalBackend) getServeHandler(r *http.Request) (_ ipn.HTTPHandlerView, at string, ok bool) {
|
func (b *LocalBackend) getServeHandler(r *http.Request) (_ ipn.HTTPHandlerView, at string, ok bool) {
|
||||||
var z ipn.HTTPHandlerView // zero value
|
var z ipn.HTTPHandlerView // zero value
|
||||||
|
|||||||
Reference in New Issue
Block a user