Documentation
¶
Overview ¶
Package server implements the TURN server of RFC 8656 with UDP relayed transport, reached over UDP, TCP or TLS, plus the STUN Binding answer of RFC 8489. Authentication is the long-term credential mechanism with NONCE expiry; the credential store is the Config.Auth hook.
Security defaults: the server never relays to or from loopback, 0.0.0.0/8, RFC 1918, 100.64/10, link-local (169.254.169.254 included), multicast, ULA, the IPv6 translator forms, IPv4-mapped forms of those, or its own addresses; it answers 403 Forbidden. Config.AllowPeers re-enables ranges (tests relay on 127.0.0.1 with it). Every table is bounded.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrProxyHeader = errors.New("server: malformed PROXY protocol header") ErrProxyFamily = errors.New("server: PROXY protocol family or transport not supported") )
Errors of the header reader. Every one closes the connection.
var ErrClosed = errors.New("server: closed")
ErrClosed is returned by the Serve methods after Close.
Functions ¶
Types ¶
type Config ¶
type Config struct {
// Realm is the long-term credential realm sent in 401 and 438 answers.
Realm string
// Auth returns the password of username. Wrap creds.Verify (or any store)
// in this closure. An unknown user and a wrong password are answered
// identically (401).
Auth func(username string) (password string, ok bool)
// Software is the SOFTWARE attribute value (default "go-turn").
Software string
// RelayIP is the address relayed sockets bind to; its family (IPv4 or
// IPv6) is the only family relayed. RelayAdvertiseIP, if set, is the
// address reported in XOR-RELAYED-ADDRESS (a NAT'd or floating address).
RelayIP netip.Addr
RelayAdvertiseIP netip.Addr
// RelayPortMin and RelayPortMax restrict the relayed UDP ports (both zero
// means any ephemeral port).
RelayPortMin, RelayPortMax uint16
// AllowPeers re-enables relaying to and from the listed ranges even when
// the default policy refuses them (it also overrides the own-address
// refusal). Empty in production.
AllowPeers []netip.Prefix
// OwnAddrs lists extra addresses of this server that peers may not be
// (interface addresses are discovered automatically).
OwnAddrs []netip.Addr
// Quotas and bounds. Zero means the default in parentheses.
MaxAllocations int // total allocations (1000)
MaxAllocationsPerUser int // per USERNAME (10)
DefaultLifetime time.Duration // (10 min)
MaxLifetime time.Duration // cap on any lifetime (1 h)
MaxBytesPerSecond int // per allocation, both directions; -1 disables (4 MiB/s)
MaxPermissions int // per allocation (128)
MaxChannels int // per allocation (128)
NonceLifetime time.Duration // (10 min)
MaxConns int // concurrent TCP/TLS connections (1024)
IdleTimeout time.Duration // TCP/TLS idle limit once an allocation exists (10 min)
HandshakeTimeout time.Duration // TCP/TLS idle limit before any allocation (30 s)
RelayBufSize int // largest relayed datagram (4096)
// ProxyProtocolFrom lists the sources (a load balancer such as Caddy or
// HAProxy) whose TCP and TLS connections must begin with a PROXY protocol
// header (v2 or v1) naming the real client. A connection from a listed
// source without a valid header is closed; a header from any other
// source is never honoured (the connection is closed). Once honoured, the
// header's address is the client address for XOR-MAPPED-ADDRESS, quotas
// and logs. For ServeTLS the header precedes the ClientHello (TLS is
// terminated here, not by the proxy). Empty means PROXY protocol is off.
// It never applies to UDP.
ProxyProtocolFrom []netip.Prefix
// Logger receives debug and warning records (default: discard).
Logger *slog.Logger
// Now replaces time.Now (tests).
Now func() time.Time
}
Config configures a Server. Realm, Auth and RelayIP are required.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a TURN server. Create it with New, hand it sockets with ServePacketConn, ServeListener and ServeTLS, stop it with Close.
func (*Server) Close ¶
Close stops every listener, connection and allocation and waits for the goroutines to end.
func (*Server) ServeListener ¶
ServeListener serves STUN and TURN over TCP connections accepted from ln until it is closed. With Config.ProxyProtocolFrom set, connections from the listed sources must start with a PROXY protocol header.
func (*Server) ServePacketConn ¶
func (s *Server) ServePacketConn(pc net.PacketConn) error
ServePacketConn serves STUN and TURN over the UDP socket pc until it is closed. It returns nil when pc is closed by Close.
type Stats ¶
type Stats struct {
Allocations int // live allocations
Allocated uint64 // allocations ever created
PacketsToPeer uint64 // client to peer datagrams relayed
PacketsToClient uint64 // peer to client datagrams relayed
Dropped uint64 // datagrams dropped (no permission, rate cap, bad frame)
ChannelBinds uint64 // successful ChannelBind requests
}
Stats is a snapshot of the server counters.