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:
Sapan Diwakar
2026-09-09 16:16:44 +08:00
committed by GitHub
parent 5121860448
commit adf44a2eb6
6 changed files with 230 additions and 21 deletions
+14
View File
@@ -279,6 +279,19 @@ type TURNConfig struct {
RelayPortRangeEnd uint16 `yaml:"relay_range_end,omitempty"`
ExternalTLS bool `yaml:"external_tls,omitempty"`
BindAddresses []string `yaml:"bind_addresses,omitempty"`
// ProxyProtocol makes the TURN TCP listener require a PROXY protocol (v1 or v2)
// header on every connection and use the client address it carries. Needed when
// a proxy in front of TURN dials from its own address (a TLS-terminating L4
// proxy, a TCP reverse proxy): TURN echoes the address it sees back to the
// client as XOR-MAPPED-ADDRESS, and browsers such as Firefox reject a loopback
// or wildcard address there and abandon the allocation. Connections without
// the header are rejected.
ProxyProtocol bool `yaml:"proxy_protocol,omitempty"`
// ProxyProtocolTrustedCIDRs lists the proxies whose PROXY header is believed.
// Connections from any other address are closed, so a client that reaches
// tls_port directly cannot claim an arbitrary source address. Defaults to
// loopback only, for a proxy on the same host.
ProxyProtocolTrustedCIDRs []string `yaml:"proxy_protocol_trusted_cidrs,omitempty"`
// PerUserRelayAllocationLimit caps the number of concurrent relay allocations
// a single participant credential may hold, keyed by the participant ID. This
// stops one authenticated participant from consuming the shared relay-port
@@ -585,6 +598,7 @@ var DefaultConfig = Config{
TURN: TURNConfig{
Enabled: false,
BindAddresses: []string{"0.0.0.0"},
ProxyProtocolTrustedCIDRs: []string{"127.0.0.0/8", "::1/128"},
TTLSeconds: DefaultTURNTTLSeconds,
PerUserRelayAllocationLimit: DefaultTURNPerUserRelayAllocationLimit,
},