Corrected addresses used in PROXY header (#262)

This commit is contained in:
Geoff Bourne 2024-02-03 19:37:44 -06:00 committed by GitHub
parent 51b8d44c77
commit b2153f89cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -7,7 +7,6 @@ import (
"golang.ngrok.com/ngrok/config" "golang.ngrok.com/ngrok/config"
"io" "io"
"net" "net"
"strconv"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -248,21 +247,29 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
// PROXY protocol implementation // PROXY protocol implementation
if c.sendProxyProto { if c.sendProxyProto {
remoteHostStr, _, _ := net.SplitHostPort(backendHostPort)
sourceAddrStr, sourcePortStr, _ := net.SplitHostPort(clientAddr.String()) // Determine transport protocol for the PROXY header by "analyzing" the frontend connection's address
sourcePort, _ := strconv.Atoi(sourcePortStr) transportProtocol := proxyproto.TCPv4
ourHostIpPart, _, err := net.SplitHostPort(frontendConn.LocalAddr().String())
if err != nil {
logrus.
WithError(err).
WithField("localAddr", frontendConn.LocalAddr()).
Error("Failed to extract host part of our address")
_ = backendConn.Close()
return
}
ourFrontendIp := net.ParseIP(ourHostIpPart)
if ourFrontendIp.To4() == nil {
transportProtocol = proxyproto.TCPv6
}
header := &proxyproto.Header{ header := &proxyproto.Header{
Version: 2, Version: 2,
Command: proxyproto.PROXY, Command: proxyproto.PROXY,
TransportProtocol: proxyproto.TCPv4, TransportProtocol: transportProtocol,
SourceAddr: &net.TCPAddr{ SourceAddr: clientAddr,
IP: net.ParseIP(sourceAddrStr), DestinationAddr: frontendConn.LocalAddr(), // our end of the client's connection
Port: sourcePort,
},
DestinationAddr: &net.TCPAddr{
IP: net.ParseIP(remoteHostStr),
},
} }
_, err = header.WriteTo(backendConn) _, err = header.WriteTo(backendConn)