Documentation
¶
Overview ¶
Package turn contains the public API for pion/turn, a toolkit for building TURN clients and servers
Index ¶
- Constants
- func DefaultPermissionHandler(net.Addr, net.IP) (ok bool)
- func GenerateAuthKey(username, realm, password string) []byte
- func GenerateLongTermCredentials(sharedSecret string, duration time.Duration) (string, string, error)
- func GenerateLongTermTURNRESTCredentials(sharedSecret string, user string, duration time.Duration) (string, string, error)
- type AllocateConnConfig
- type AllocateListenerConfig
- type AuthHandler
- type Client
- func (c *Client) Allocate() (net.PacketConn, error)
- func (c *Client) AllocateTCP() (*client.TCPAllocation, error)
- func (c *Client) Close()
- func (c *Client) CreatePermission(addrs ...net.Addr) error
- func (c *Client) HandleInbound(data []byte, from net.Addr) (bool, error)
- func (c *Client) Listen() error
- func (c *Client) OnDeallocated(net.Addr)
- func (c *Client) PerformTransaction(msg *stun.Message, to net.Addr, ignoreResult bool) (client.TransactionResult, error)
- func (c *Client) Realm() stun.Realm
- func (c *Client) STUNServerAddr() net.Addr
- func (c *Client) SendBindingRequest() (net.Addr, error)
- func (c *Client) SendBindingRequestTo(to net.Addr) (net.Addr, error)
- func (c *Client) TURNServerAddr() net.Addr
- func (c *Client) Username() stun.Username
- func (c *Client) WriteTo(data []byte, to net.Addr) (int, error)
- type ClientConfig
- type EventHandler
- type ListenerConfig
- type PacketConnConfig
- type PermissionHandler
- type QuotaHandler
- type RelayAddressGenerator
- type RelayAddressGeneratorNone
- func (r *RelayAddressGeneratorNone) AllocateConn(conf AllocateConnConfig) (net.Conn, error)
- func (r *RelayAddressGeneratorNone) AllocateListener(conf AllocateListenerConfig) (net.Listener, net.Addr, error)
- func (r *RelayAddressGeneratorNone) AllocatePacketConn(conf AllocateListenerConfig) (net.PacketConn, net.Addr, error)
- func (r *RelayAddressGeneratorNone) Validate() error
- type RelayAddressGeneratorPortRange
- func (r *RelayAddressGeneratorPortRange) AllocateConn(conf AllocateConnConfig) (net.Conn, error)
- func (r *RelayAddressGeneratorPortRange) AllocateListener(conf AllocateListenerConfig) (net.Listener, net.Addr, error)
- func (r *RelayAddressGeneratorPortRange) AllocatePacketConn(conf AllocateListenerConfig) (net.PacketConn, net.Addr, error)
- func (r *RelayAddressGeneratorPortRange) Validate() error
- type RelayAddressGeneratorStatic
- func (r *RelayAddressGeneratorStatic) AllocateConn(conf AllocateConnConfig) (net.Conn, error)
- func (r *RelayAddressGeneratorStatic) AllocateListener(conf AllocateListenerConfig) (net.Listener, net.Addr, error)
- func (r *RelayAddressGeneratorStatic) AllocatePacketConn(conf AllocateListenerConfig) (net.PacketConn, net.Addr, error)
- func (r *RelayAddressGeneratorStatic) Validate() error
- type RequestAttributes
- type RequestedAddressFamily
- type STUNConn
- type Server
- type ServerConfig
Constants ¶
const ( RequestedAddressFamilyIPv4 = proto.RequestedFamilyIPv4 RequestedAddressFamilyIPv6 = proto.RequestedFamilyIPv6 )
Values for RequestedAddressFamily as defined in RFC 6156 Section 4.1.1.
Variables ¶
This section is empty.
Functions ¶
func DefaultPermissionHandler ¶
DefaultPermissionHandler is convince function that grants permission to all peers.
func GenerateAuthKey ¶
GenerateAuthKey is a convenience function to easily generate keys in the format used by AuthHandler.
Types ¶
type AllocateConnConfig ¶
type AllocateConnConfig = allocation.AllocateConnConfig
AllocateConnConfig defines the parameters passed to the TCP connection generator.
type AllocateListenerConfig ¶
type AllocateListenerConfig = allocation.AllocateListenerConfig
AllocateListenerConfig defines the parameters passed to the relay address allocator.
type AuthHandler ¶
type AuthHandler = auth.AuthHandler
AuthHandler is a callback used to handle incoming auth requests, allowing users to customize Pion TURN with custom behavior.
func LongTermTURNRESTAuthHandler ¶
func LongTermTURNRESTAuthHandler(sharedSecret string, logger logging.LeveledLogger) AuthHandler
LongTermTURNRESTAuthHandler returns a turn.AuthAuthHandler that can be used to authenticate time-windowed ephemeral credentials generated by the TURN REST API as described in https://datatracker.ietf.org/doc/html/draft-uberti-behave-turn-rest-00
The supported format of is timestamp:username, where username is an arbitrary user id and the timestamp specifies the expiry of the credential.
func NewLongTermAuthHandler ¶
func NewLongTermAuthHandler(sharedSecret string, logger logging.LeveledLogger) AuthHandler
NewLongTermAuthHandler returns a turn.AuthAuthHandler used with Long Term (or Time Windowed) Credentials. See: https://datatracker.ietf.org/doc/html/rfc8489#section-9.2 .
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a STUN server client.
func NewClient ¶
func NewClient(config *ClientConfig) (*Client, error)
NewClient returns a new Client instance. listeningAddress is the address and port to listen on, default "0.0.0.0:0".
func (*Client) Allocate ¶
func (c *Client) Allocate() (net.PacketConn, error)
Allocate sends a TURN allocation request to the given transport address.
func (*Client) AllocateTCP ¶
func (c *Client) AllocateTCP() (*client.TCPAllocation, error)
AllocateTCP creates a new TCP allocation at the TURN server.
func (*Client) CreatePermission ¶
CreatePermission Issues a CreatePermission request for the supplied addresses as described in https://datatracker.ietf.org/doc/html/rfc5766#section-9
func (*Client) HandleInbound ¶
HandleInbound handles data received. This method handles incoming packet de-multiplex it by the source address and the types of the message. This return a boolean (handled or not) and if there was an error. Caller should check if the packet was handled by this client or not. If not handled, it is assumed that the packet is application data. If an error is returned, the caller should discard the packet regardless.
func (*Client) Listen ¶
Listen will have this client start listening on the conn provided via the config. This is optional. If not used, you will need to call HandleInbound method to supply incoming data, instead.
func (*Client) OnDeallocated ¶
OnDeallocated is called when de-allocation of relay address has been complete. (Called by UDPConn).
func (*Client) PerformTransaction ¶
func (c *Client) PerformTransaction(msg *stun.Message, to net.Addr, ignoreResult bool) (client.TransactionResult, error, )
PerformTransaction performs STUN transaction.
func (*Client) STUNServerAddr ¶
STUNServerAddr return the STUN server address.
func (*Client) SendBindingRequest ¶
SendBindingRequest sends a new STUN request to the STUN server.
func (*Client) SendBindingRequestTo ¶
SendBindingRequestTo sends a new STUN request to the given transport address.
func (*Client) TURNServerAddr ¶
TURNServerAddr return the TURN server address.
type ClientConfig ¶
type ClientConfig struct {
STUNServerAddr string // STUN server address (e.g. "stun.abc.com:3478")
TURNServerAddr string // TURN server address (e.g. "turn.abc.com:3478")
Username string
Password string
Realm string
Software string
RTO time.Duration
Conn net.PacketConn // Listening socket (net.PacketConn)
Net transport.Net
LoggerFactory logging.LoggerFactory
// PermissionTimeout sets the refresh interval of permissions. Defaults to 2 minutes.
PermissionRefreshInterval time.Duration
// RequestedAddressFamily is the address family to request in allocations (IPv4 or IPv6).
// If not specified (zero value), the client will attempt to infer from the PacketConn's
// local address, falling back to IPv4 if inference fails. See RFC 6156.
RequestedAddressFamily RequestedAddressFamily
// contains filtered or unexported fields
}
ClientConfig is a bag of config parameters for Client.
type EventHandler ¶
type EventHandler = allocation.EventHandler
EventHandler is a set of callbacks that the server will call at certain hook points during an allocation's lifecycle.
type ListenerConfig ¶
type ListenerConfig struct {
Listener net.Listener
// When an allocation is generated the RelayAddressGenerator
// creates the net.Listener and returns the IP/Port it is available at
RelayAddressGenerator RelayAddressGenerator
// PermissionHandler is a callback to filter peer addresses. Can be set as nil, in which
// case the DefaultPermissionHandler is automatically instantiated to admit all peer
// connections
PermissionHandler PermissionHandler
}
ListenerConfig is a single net.Listener to accept connections on. This will be used for TCP, TLS and DTLS listeners.
type PacketConnConfig ¶
type PacketConnConfig struct {
PacketConn net.PacketConn
// When an allocation is generated the RelayAddressGenerator
// creates the net.PacketConn and returns the IP/Port it is available at
RelayAddressGenerator RelayAddressGenerator
// PermissionHandler is a callback to filter peer addresses. Can be set as nil, in which
// case the DefaultPermissionHandler is automatically instantiated to admit all peer
// connections
PermissionHandler PermissionHandler
}
PacketConnConfig is a single net.PacketConn to listen/write on. This will be used for UDP listeners.
type PermissionHandler ¶
PermissionHandler is a callback to filter incoming CreatePermission and ChannelBindRequest requests based on the client IP address and port and the peer IP address the client intends to connect to. If the client is behind a NAT then the filter acts on the server reflexive ("mapped") address instead of the real client IP address and port. Note that TURN permissions are per-allocation and per-peer-IP-address, to mimic the address-restricted filtering mechanism of NATs that comply with [RFC4787], see https://tools.ietf.org/html/rfc5766#section-2.3.
type QuotaHandler ¶
QuotaHandler is a callback allows allocations to be rejected when a per-user quota is exceeded. If the callback returns true the allocation request is accepted, otherwise it is rejected and a 486 (Allocation Quota Reached) error is returned to the user.
type RelayAddressGenerator ¶
type RelayAddressGenerator interface {
// Validate confirms that the RelayAddressGenerator is properly initialized
Validate() error
// Allocate a PacketConn (UDP) RelayAddress
AllocatePacketConn(AllocateListenerConfig) (net.PacketConn, net.Addr, error)
// Allocate a Listener (TCP) RelayAddress
AllocateListener(AllocateListenerConfig) (net.Listener, net.Addr, error)
// Allocate a Conn (TCP) relay connection
AllocateConn(AllocateConnConfig) (net.Conn, error)
}
RelayAddressGenerator is used to generate a RelayAddress when creating an allocation. You can use one of the provided ones or provide your own.
type RelayAddressGeneratorNone ¶
type RelayAddressGeneratorNone struct {
// Address is passed to Listen/ListenPacket when creating the Relay
Address string
Net transport.Net
}
RelayAddressGeneratorNone returns the listener with no modifications.
func (*RelayAddressGeneratorNone) AllocateConn ¶
func (r *RelayAddressGeneratorNone) AllocateConn(conf AllocateConnConfig) (net.Conn, error)
AllocateConn creates a new outgoing TCP connection bound to the relay address to send traffic to a peer.
func (*RelayAddressGeneratorNone) AllocateListener ¶
func (r *RelayAddressGeneratorNone) AllocateListener(conf AllocateListenerConfig) (net.Listener, net.Addr, error)
AllocateListener generates a new Listener to receive traffic on and the IP/Port to populate the allocation response with.
func (*RelayAddressGeneratorNone) AllocatePacketConn ¶
func (r *RelayAddressGeneratorNone) AllocatePacketConn(conf AllocateListenerConfig) ( net.PacketConn, net.Addr, error, )
AllocatePacketConn generates a new PacketConn to receive traffic on and the IP/Port to populate the allocation response with.
func (*RelayAddressGeneratorNone) Validate ¶
func (r *RelayAddressGeneratorNone) Validate() error
Validate is called on server startup and confirms the RelayAddressGenerator is properly configured.
type RelayAddressGeneratorPortRange ¶
type RelayAddressGeneratorPortRange struct {
// RelayAddress is the IP returned to the user when the relay is created
RelayAddress net.IP
// MinPort the minimum port to allocate
MinPort uint16
// MaxPort the maximum (inclusive) port to allocate
MaxPort uint16
// MaxRetries the amount of tries to allocate a random port in the defined range
MaxRetries int
// Rand the random source of numbers
Rand randutil.MathRandomGenerator
// Address is passed to Listen/ListenPacket when creating the Relay
Address string
Net transport.Net
}
RelayAddressGeneratorPortRange can be used to only allocate connections inside a defined port range. Similar to the RelayAddressGeneratorStatic a static ip address can be set.
func (*RelayAddressGeneratorPortRange) AllocateConn ¶
func (r *RelayAddressGeneratorPortRange) AllocateConn(conf AllocateConnConfig) (net.Conn, error)
AllocateConn creates a new outgoing TCP connection bound to the relay address to send traffic to a peer.
func (*RelayAddressGeneratorPortRange) AllocateListener ¶
func (r *RelayAddressGeneratorPortRange) AllocateListener( conf AllocateListenerConfig, ) (net.Listener, net.Addr, error)
AllocateListener generates a new Listener to receive traffic on and the IP/Port to populate the allocation response with.
func (*RelayAddressGeneratorPortRange) AllocatePacketConn ¶
func (r *RelayAddressGeneratorPortRange) AllocatePacketConn( conf AllocateListenerConfig, ) (net.PacketConn, net.Addr, error)
AllocatePacketConn generates a new PacketConn to receive traffic on and the IP/Port to populate the allocation response with.
func (*RelayAddressGeneratorPortRange) Validate ¶
func (r *RelayAddressGeneratorPortRange) Validate() error
Validate is called on server startup and confirms the RelayAddressGenerator is properly configured.
type RelayAddressGeneratorStatic ¶
type RelayAddressGeneratorStatic struct {
// RelayAddress is the IP returned to the user when the relay is created
RelayAddress net.IP
// Address is passed to Listen/ListenPacket when creating the Relay
Address string
Net transport.Net
}
RelayAddressGeneratorStatic can be used to return static IP address each time a relay is created. This can be used when you have a single static IP address that you want to use.
func (*RelayAddressGeneratorStatic) AllocateConn ¶
func (r *RelayAddressGeneratorStatic) AllocateConn(conf AllocateConnConfig) (net.Conn, error)
AllocateConn creates a new outgoing TCP connection bound to the relay address to send traffic to a peer.
func (*RelayAddressGeneratorStatic) AllocateListener ¶
func (r *RelayAddressGeneratorStatic) AllocateListener(conf AllocateListenerConfig) (net.Listener, net.Addr, error)
AllocateListener generates a new Listener to receive traffic on and the IP/Port to populate the allocation response with.
func (*RelayAddressGeneratorStatic) AllocatePacketConn ¶
func (r *RelayAddressGeneratorStatic) AllocatePacketConn( conf AllocateListenerConfig, ) (net.PacketConn, net.Addr, error)
AllocatePacketConn generates a new PacketConn to receive traffic on and the IP/Port to populate the allocation response with.
func (*RelayAddressGeneratorStatic) Validate ¶
func (r *RelayAddressGeneratorStatic) Validate() error
Validate is called on server startup and confirms the RelayAddressGenerator is properly configured.
type RequestAttributes ¶
type RequestAttributes = auth.RequestAttributes
RequestAttributes represents attributes of a TURN request which may be useful for authorizing the underlying request.
type RequestedAddressFamily ¶
type RequestedAddressFamily = proto.RequestedAddressFamily
RequestedAddressFamily represents the REQUESTED-ADDRESS-FAMILY Attribute as defined in RFC 6156 Section 4.1.1.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is an instance of the Pion TURN Server.
func NewServer ¶
func NewServer(config ServerConfig) (*Server, error)
NewServer creates the Pion TURN server.
func (*Server) AllocationCount ¶
AllocationCount returns the number of active allocations. It can be used to drain the server before closing.
type ServerConfig ¶
type ServerConfig struct {
// PacketConnConfigs and ListenerConfigs are a list of all the turn listeners
// Each listener can have custom behavior around the creation of Relays
PacketConnConfigs []PacketConnConfig
ListenerConfigs []ListenerConfig
// LoggerFactory must be set for logging from this server.
LoggerFactory logging.LoggerFactory
// Realm sets the realm for this server
Realm string
// AuthHandler is a callback used to handle incoming auth requests,
// allowing users to customize Pion TURN with custom behavior
AuthHandler AuthHandler
// QuotaHandler is a callback used to reject new allocations when a
// per-user quota is exceeded.
QuotaHandler QuotaHandler
// EventHandlers is a set of callbacks for tracking allocation lifecycle.
EventHandler EventHandler
// ChannelBindTimeout sets the lifetime of channel binding. Defaults to 10 minutes.
ChannelBindTimeout time.Duration
// PermissionTimeout sets the lifetime of permission. Defaults to 10 minutes.
PermissionTimeout time.Duration
// AllocationLife sets the lifetime of allocation. Defaults to 10 minutes.
AllocationLifetime time.Duration
// Sets the server inbound MTU(Maximum transmition unit). Defaults to 1600 bytes.
InboundMTU int
}
ServerConfig configures the Pion TURN Server.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
lt-cred-generator
command
Package main implements a CLI tool for generating long-term credentials.
|
Package main implements a CLI tool for generating long-term credentials. |
|
mutual-tls-auth/turn-client
command
Package main implements a TURN client with TLS certificate-based authentication
|
Package main implements a TURN client with TLS certificate-based authentication |
|
mutual-tls-auth/turn-server
command
Package main implements an example TURN server with TLS certificate-based authentication
|
Package main implements an example TURN server with TLS certificate-based authentication |
|
stun-only-server
command
Package main implements a simple TURN server
|
Package main implements a simple TURN server |
|
turn-client/ipv6
command
Package main implements a TURN client using UDP with IPv6 support (RFC 6156)
|
Package main implements a TURN client using UDP with IPv6 support (RFC 6156) |
|
turn-client/tcp
command
Package main implements a TURN client with support for TCP
|
Package main implements a TURN client with support for TCP |
|
turn-client/tcp-alloc
command
Package main implements a TURN client with support for TCP
|
Package main implements a TURN client with support for TCP |
|
turn-client/tls
command
Package main implements a TURN client with TLS support
|
Package main implements a TURN client with TLS support |
|
turn-client/udp
command
Package main implements a TURN client using UDP
|
Package main implements a TURN client using UDP |
|
turn-server/add-software-attribute
command
Package main implements a TURN server adding a software attribute.
|
Package main implements a TURN server adding a software attribute. |
|
turn-server/bw-quota
command
Package main implements a TURN server with per-user bandwidth quotas.
|
Package main implements a TURN server with per-user bandwidth quotas. |
|
turn-server/ipv6
command
Package main implements a simple TURN server with IPv6 support (RFC 6156)
|
Package main implements a simple TURN server with IPv6 support (RFC 6156) |
|
turn-server/log
command
Package main implements a TURN server with logging.
|
Package main implements a TURN server with logging. |
|
turn-server/lt-cred
command
Package main implements a TURN server using long-term credentials.
|
Package main implements a TURN server using long-term credentials. |
|
turn-server/lt-cred-turn-rest
command
Package main implements a TURN server using ephemeral credentials.
|
Package main implements a TURN server using ephemeral credentials. |
|
turn-server/perm-filter
command
This example demonstrates the use of a permission handler in the PION TURN server.
|
This example demonstrates the use of a permission handler in the PION TURN server. |
|
turn-server/port-range
command
Package main implements a TURN server with a specified port range.
|
Package main implements a TURN server with a specified port range. |
|
turn-server/simple
command
Package main implements a simple TURN server
|
Package main implements a simple TURN server |
|
turn-server/simple-multithreaded
command
Package main implements a multi-threaded TURN server
|
Package main implements a multi-threaded TURN server |
|
turn-server/simple-quota
command
Package main implements a simple TURN server with per-user allocation quotas.
|
Package main implements a simple TURN server with per-user allocation quotas. |
|
turn-server/tcp
command
Package main implements an example TURN server supporting TCP
|
Package main implements an example TURN server supporting TCP |
|
turn-server/tls
command
Package main implements a TURN server with TLS support
|
Package main implements a TURN server with TLS support |
|
internal
|
|
|
allocation
Package allocation contains all CRUD operations for allocations
|
Package allocation contains all CRUD operations for allocations |
|
auth
Package auth provides internal authentication / authorization types and utilities for the TURN server.
|
Package auth provides internal authentication / authorization types and utilities for the TURN server. |
|
client
Package client implements the API for a TURN client
|
Package client implements the API for a TURN client |
|
ipnet
Package ipnet contains helper functions around net and IP
|
Package ipnet contains helper functions around net and IP |
|
proto
Package proto implements RFC 5766 Traversal Using Relays around NAT.
|
Package proto implements RFC 5766 Traversal Using Relays around NAT. |
|
server
Package server implements the private API to implement a TURN server
|
Package server implements the private API to implement a TURN server |
