@@ -11,6 +11,7 @@ import (
1111 "time"
1212
1313 "github.com/pion/logging"
14+ "github.com/pion/stun"
1415)
1516
1617// RelayAddressGenerator is used to generate a RelayAddress when creating an allocation.
@@ -26,6 +27,15 @@ type RelayAddressGenerator interface {
2627 AllocateConn (network string , requestedPort int ) (net.Conn , net.Addr , error )
2728}
2829
30+ // AllocationHandler is a callback used to handle incoming allocation requests, allowing users to
31+ // customize Pion TURN with custom behavior. If the returned error code is nonzero then the request
32+ // is rejected with the given error code. This is useful to, e.g., return an "Allocation Quota
33+ // Reached" when the number of allocations from the client address surpasses a limit. If the error
34+ // code is "Try Alternate" then the reject response will also contain an ALTERNATE-SERVER attribute
35+ // with the returned alternate server address. This is useful to redirect the client to another
36+ // TURN server.
37+ type AllocationHandler func (clientAddr net.Addr ) (alternateServer net.Addr , errorCode stun.ErrorCode )
38+
2939// PermissionHandler is a callback to filter incoming CreatePermission and ChannelBindRequest
3040// requests based on the client IP address and port and the peer IP address the client intends to
3141// connect to. If the client is behind a NAT then the filter acts on the server reflexive
@@ -34,11 +44,6 @@ type RelayAddressGenerator interface {
3444// of NATs that comply with [RFC4787], see https://tools.ietf.org/html/rfc5766#section-2.3.
3545type PermissionHandler func (clientAddr net.Addr , peerIP net.IP ) (ok bool )
3646
37- // DefaultPermissionHandler is convince function that grants permission to all peers
38- func DefaultPermissionHandler (net.Addr , net.IP ) (ok bool ) {
39- return true
40- }
41-
4247// PacketConnConfig is a single net.PacketConn to listen/write on. This will be used for UDP listeners
4348type PacketConnConfig struct {
4449 PacketConn net.PacketConn
@@ -47,9 +52,12 @@ type PacketConnConfig struct {
4752 // creates the net.PacketConn and returns the IP/Port it is available at
4853 RelayAddressGenerator RelayAddressGenerator
4954
50- // PermissionHandler is a callback to filter peer addresses. Can be set as nil, in which
51- // case the DefaultPermissionHandler is automatically instantiated to admit all peer
52- // connections
55+ // AllocationHandler is a callback to filter client addresses or redirect clients to an
56+ // alternate server.
57+ AllocationHandler AllocationHandler
58+
59+ // PermissionHandler is a callback to filter peer addresses. Specifying no permission
60+ // handler will admit all peer connections.
5361 PermissionHandler PermissionHandler
5462}
5563
@@ -72,9 +80,12 @@ type ListenerConfig struct {
7280 // creates the net.PacketConn and returns the IP/Port it is available at
7381 RelayAddressGenerator RelayAddressGenerator
7482
75- // PermissionHandler is a callback to filter peer addresses. Can be set as nil, in which
76- // case the DefaultPermissionHandler is automatically instantiated to admit all peer
77- // connections
83+ // AllocationHandler is a callback to filter client addresses or redirect clients to an
84+ // alternate server.
85+ AllocationHandler AllocationHandler
86+
87+ // PermissionHandler is a callback to filter peer addresses. Specifying no permission
88+ // handler will admit all peer connections.
7889 PermissionHandler PermissionHandler
7990}
8091
@@ -114,7 +125,7 @@ type ServerConfig struct {
114125 // Realm sets the realm for this server
115126 Realm string
116127
117- // AuthHandler is a callback used to handle incoming auth requests, allowing users to customize Pion TURN with custom behavior
128+ // AuthHandler is a callback used to handle incoming auth requests, allowing users to customize Pion TURN with custom behavior.
118129 AuthHandler AuthHandler
119130
120131 // ChannelBindTimeout sets the lifetime of channel binding. Defaults to 10 minutes.
0 commit comments