turnclient

package
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package turnclient opens client sessions on a TURN server over any of the TURN transports (TURN-UDP, TURN-TCP, TURN-TLS, TURN-DTLS). It is the TURN-client machinery under the clusters with a tunnel, and the client the tests and tools use to drive traffic through a STUNner gateway.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Protocol is the transport to reach the TURN server over: TURN-UDP, TURN-TCP, TURN-TLS
	// or TURN-DTLS.
	Protocol stnrv2.Protocol
	// ServerAddr is the TURN server address in host:port form. The host may be an IP address
	// of either family or a DNS name.
	ServerAddr string
	// Username and Password are the optional long-term credentials.
	Username, Password string
	// Realm is the STUN/TURN realm.
	Realm string
	// ServerName is the SNI server name for the TLS/DTLS transports (unless it is an IP
	// address).
	ServerName string
	// Insecure allows TLS certificate verification to be skipped for the TLS/DTLS transports.
	Insecure bool
	// RootCAs verify the TURN server over TLS/DTLS, nil for the system roots.
	RootCAs *x509.CertPool
	// Certificates are presented to the TURN server over TLS/DTLS, none by default.
	Certificates []tls.Certificate
	// LoggerFactory is an optional external logger.
	LoggerFactory logging.LoggerFactory
	// Net is the network to reach the TURN server over, nil for the OS network.
	Net transport.Net
}

Config specifies the upstream TURN server to dial.

func NewConfig

func NewConfig(t *stnrv2.TunnelConfig, auth *stnrv2.AuthConfig) (Config, error)

NewConfig builds the client config of a tunnel's upstream TURN server, presenting auth (nil for none). Credentials are generated per call, so ephemeral auth gets per-allocation credentials.

Realm is a seed only: the server states its realm in the 401 challenge and the client overwrites the configured value with it before computing the message integrity.

type Conn

type Conn struct {
	net.Conn
	// contains filtered or unexported fields
}

Conn is an RFC 6062 relayed TCP connection that owns its TURN session: Close closes the data connection, then the allocation, the TURN client, and its transport.

func (*Conn) Channel

func (c *Conn) Channel(net.Addr) (uint16, bool)

Channel reports no framing: RFC 6062 relays peer traffic over a dedicated data connection, there are no channels on a stream allocation.

func (*Conn) Close

func (c *Conn) Close() error

func (*Conn) TransportAddrs

func (c *Conn) TransportAddrs() (local, remote net.Addr)

TransportAddrs returns the wire addresses of the relayed connection: the RFC 6062 data connection runs to the TURN server, so the remote is the server, not the peer.

type Dialer

type Dialer struct {
	Config
}

Dialer opens client sessions on the TURN server named in its Config, with net-shaped entry points: ListenPacket mirrors net.ListenConfig.ListenPacket and returns the session's UDP allocation, DialContext mirrors net.Dialer.DialContext and returns an RFC 6062 relayed TCP connection to a peer. Either conn owns the whole TURN session: closing it tears down the allocation, the TURN client, and its transport. The context is checked between the dial steps; the underlying TURN library does not support mid-step cancellation.

func (Dialer) BindingRequest

func (d Dialer) BindingRequest(ctx context.Context) (net.Addr, error)

BindingRequest sends a STUN binding request over a fresh session and returns the reflexive address the server saw, closing the session afterwards. It needs no credentials: a STUN server answers it without authentication.

func (Dialer) DialContext

func (d Dialer) DialContext(ctx context.Context, network, address string) (*Conn, error)

DialContext makes a TCP (RFC 6062) allocation on the TURN server and opens a relayed connection to the peer at address (host:port). Only "tcp" is a valid network, and only over TURN-TCP and TURN-TLS: RFC 6062 requires the control connection to be TCP or TLS.

func (Dialer) ListenPacket

func (d Dialer) ListenPacket(ctx context.Context, _, _ string) (*PacketConn, error)

ListenPacket makes a UDP allocation on the TURN server and returns it as the session's packet conn: ReadFrom/WriteTo relay through the server (the client auto-creates a server-side permission for each new peer written to), and LocalAddr is the server-relayed transport address. The network and local address of the allocation are the server's business, so both parameters are ignored; they exist to mirror net.ListenConfig.ListenPacket.

type PacketConn

type PacketConn struct {
	net.PacketConn // the upstream allocation
	// contains filtered or unexported fields
}

PacketConn is a UDP allocation that owns its TURN session: Close closes the allocation, then the TURN client and its transport. TransportAddrs and Channel describe the session's wire, so the dataplane can stack its relay leg on the session as it is.

func (*PacketConn) Channel

func (c *PacketConn) Channel(peer net.Addr) (uint16, bool)

Channel reports the channel the session frames traffic towards peer with, and whether that framing is live. The client assigns a number at the first write towards a peer and completes the ChannelBind a round trip later, sending Send indications until it does, so a number alone does not mean the wire carries ChannelData yet.

func (*PacketConn) Close

func (c *PacketConn) Close() error

func (*PacketConn) TransportAddrs

func (c *PacketConn) TransportAddrs() (local, remote net.Addr)

TransportAddrs returns the wire addresses of the TURN session: the transport socket's local address and the server's address. This is the 5-tuple a kernel offload must match on; the allocation's own LocalAddr is the server-side relayed address, useless for local offload.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址