turn: accept PROXY protocol on the TCP listener (#4852)
* turn: accept PROXY protocol on the TCP listener Behind a TLS-terminating or reverse proxy that dials from its own address, the embedded TURN server reports the proxy's address to the client as XOR-MAPPED-ADDRESS. Firefox rejects a loopback or wildcard mapped address and abandons the allocation, so relay-only clients never get a relay candidate (#4851). Add turn.proxy_protocol. When set, the TCP listener requires a PROXY protocol v1/v2 header on every connection and uses the client address it carries; connections without the header are rejected. The header is read before TLS, so it works with both the built-in TLS listener and external_tls. * turn: only trust PROXY headers from configured proxies A PROXY header from any peer that can reach the port would let a direct client claim an arbitrary source address. Add turn.proxy_protocol_trusted_cidrs, defaulting to loopback, and close connections from any other address before reading the header.
This commit is contained in:
+64
-21
@@ -26,6 +26,7 @@ import (
|
||||
"github.com/jxskiss/base62"
|
||||
"github.com/pion/stun/v3"
|
||||
"github.com/pion/turn/v5"
|
||||
"github.com/pires/go-proxyproto"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/livekit/protocol/auth"
|
||||
@@ -162,26 +163,9 @@ func NewTurnServer(conf *config.Config, authHandler turn.AuthHandler, standalone
|
||||
}
|
||||
|
||||
if turnConf.TLSPort > 0 {
|
||||
var listener net.Listener
|
||||
var listenerErr error
|
||||
|
||||
if turnConf.ExternalTLS {
|
||||
listener, listenerErr = net.Listen("tcp", net.JoinHostPort(addr, strconv.Itoa(turnConf.TLSPort)))
|
||||
} else {
|
||||
cert, err := tls.LoadX509KeyPair(turnConf.CertFile, turnConf.KeyFile)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "TURN tls cert required")
|
||||
}
|
||||
|
||||
listener, listenerErr = tls.Listen("tcp", net.JoinHostPort(addr, strconv.Itoa(turnConf.TLSPort)),
|
||||
&tls.Config{
|
||||
MinVersion: tls.VersionTLS12,
|
||||
Certificates: []tls.Certificate{cert},
|
||||
})
|
||||
}
|
||||
|
||||
if listenerErr != nil {
|
||||
return nil, errors.Wrap(listenerErr, "could not listen on TURN TCP port")
|
||||
listener, err := newTURNTCPListener(turnConf, net.JoinHostPort(addr, strconv.Itoa(turnConf.TLSPort)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if standalone {
|
||||
listener = telemetry.NewListener(listener)
|
||||
@@ -194,7 +178,7 @@ func NewTurnServer(conf *config.Config, authHandler turn.AuthHandler, standalone
|
||||
}
|
||||
serverConfig.ListenerConfigs = append(serverConfig.ListenerConfigs, listenerConfig)
|
||||
|
||||
logValues = append(logValues, "turn.portTLS", turnConf.TLSPort, "turn.externalTLS", turnConf.ExternalTLS)
|
||||
logValues = append(logValues, "turn.portTLS", turnConf.TLSPort, "turn.externalTLS", turnConf.ExternalTLS, "turn.proxyProtocol", turnConf.ProxyProtocol)
|
||||
}
|
||||
|
||||
if turnConf.UDPPort > 0 {
|
||||
@@ -221,6 +205,65 @@ func NewTurnServer(conf *config.Config, authHandler turn.AuthHandler, standalone
|
||||
return turn.NewServer(serverConfig)
|
||||
}
|
||||
|
||||
// newTURNTCPListener returns the TCP listener for TURN/TLS. The PROXY protocol
|
||||
// header, when enabled, is read before TLS so the client address is known to
|
||||
// the TLS layer and to TURN regardless of who terminates TLS.
|
||||
func newTURNTCPListener(turnConf config.TURNConfig, address string) (net.Listener, error) {
|
||||
var tlsConfig *tls.Config
|
||||
if !turnConf.ExternalTLS {
|
||||
cert, err := tls.LoadX509KeyPair(turnConf.CertFile, turnConf.KeyFile)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "TURN tls cert required")
|
||||
}
|
||||
tlsConfig = &tls.Config{
|
||||
MinVersion: tls.VersionTLS12,
|
||||
Certificates: []tls.Certificate{cert},
|
||||
}
|
||||
}
|
||||
|
||||
var proxyPolicy proxyproto.ConnPolicyFunc
|
||||
if turnConf.ProxyProtocol {
|
||||
trusted, err := parsePeerCIDRs("turn.proxy_protocol_trusted_cidrs", turnConf.ProxyProtocolTrustedCIDRs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(trusted) == 0 {
|
||||
return nil, errors.New("turn.proxy_protocol requires at least one entry in turn.proxy_protocol_trusted_cidrs")
|
||||
}
|
||||
proxyPolicy = proxyProtocolPolicy(trusted)
|
||||
}
|
||||
|
||||
listener, err := net.Listen("tcp", address)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not listen on TURN TCP port")
|
||||
}
|
||||
if proxyPolicy != nil {
|
||||
listener = &proxyproto.Listener{Listener: listener, ConnPolicy: proxyPolicy}
|
||||
}
|
||||
if tlsConfig != nil {
|
||||
listener = tls.NewListener(listener, tlsConfig)
|
||||
}
|
||||
return listener, nil
|
||||
}
|
||||
|
||||
// proxyProtocolPolicy requires the PROXY header from trusted proxies and closes
|
||||
// every other connection, so the header cannot be forged by a direct client.
|
||||
func proxyProtocolPolicy(trusted []*net.IPNet) proxyproto.ConnPolicyFunc {
|
||||
return func(opts proxyproto.ConnPolicyOptions) (proxyproto.Policy, error) {
|
||||
tcpAddr, ok := opts.Upstream.(*net.TCPAddr)
|
||||
if !ok {
|
||||
return proxyproto.REJECT, fmt.Errorf("%w: unexpected address %v", proxyproto.ErrInvalidUpstream, opts.Upstream)
|
||||
}
|
||||
for _, ipnet := range trusted {
|
||||
if ipnet.Contains(tcpAddr.IP) {
|
||||
return proxyproto.REQUIRE, nil
|
||||
}
|
||||
}
|
||||
// wrapping ErrInvalidUpstream closes this connection and keeps the listener accepting
|
||||
return proxyproto.REJECT, fmt.Errorf("%w: %s is not a trusted proxy", proxyproto.ErrInvalidUpstream, tcpAddr.IP)
|
||||
}
|
||||
}
|
||||
|
||||
func getTURNAuthHandlerFunc(handler *TURNAuthHandler) turn.AuthHandler {
|
||||
return handler.HandleAuth
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user