55package turn
66
77import (
8+ "crypto/tls"
89 "errors"
910 "fmt"
1011 "net"
@@ -14,6 +15,7 @@ import (
1415 "github.com/pion/turn/v4/internal/allocation"
1516 "github.com/pion/turn/v4/internal/proto"
1617 "github.com/pion/turn/v4/internal/server"
18+ "github.com/pion/turn/v4/internal/server/authz"
1719)
1820
1921const (
@@ -23,7 +25,7 @@ const (
2325// Server is an instance of the Pion TURN Server
2426type Server struct {
2527 log logging.LeveledLogger
26- authHandler AuthHandler
28+ authorizer authz. Authorizer
2729 realm string
2830 channelBindTimeout time.Duration
2931 nonceHash * server.NonceHash
@@ -57,9 +59,16 @@ func NewServer(config ServerConfig) (*Server, error) {
5759 return nil , err
5860 }
5961
62+ // determine authorizer, prioritizing the
63+ // (legacy) AuthHandler if it was provided.
64+ authorizer := config .Authorizer
65+ if config .AuthHandler != nil {
66+ authorizer = authz .NewLegacy (config .AuthHandler )
67+ }
68+
6069 s := & Server {
6170 log : loggerFactory .NewLogger ("turn" ),
62- authHandler : config . AuthHandler ,
71+ authorizer : authorizer ,
6372 realm : config .Realm ,
6473 channelBindTimeout : config .ChannelBindTimeout ,
6574 packetConnConfigs : config .PacketConnConfigs ,
@@ -79,7 +88,7 @@ func NewServer(config ServerConfig) (*Server, error) {
7988 }
8089
8190 go func (cfg PacketConnConfig , am * allocation.Manager ) {
82- s .readLoop (cfg .PacketConn , am )
91+ s .readLoop (cfg .PacketConn , am , nil )
8392
8493 if err := am .Close (); err != nil {
8594 s .log .Errorf ("Failed to close AllocationManager: %s" , err )
@@ -151,7 +160,16 @@ func (s *Server) readListener(l net.Listener, am *allocation.Manager) {
151160 }
152161
153162 go func () {
154- s .readLoop (NewSTUNConn (conn ), am )
163+ var tlsConnectionState * tls.ConnectionState
164+
165+ // extract tls connection state if possible
166+ tlsConn , ok := conn .(* tls.Conn )
167+ if ok {
168+ cs := tlsConn .ConnectionState ()
169+ tlsConnectionState = & cs
170+ }
171+
172+ s .readLoop (NewSTUNConn (conn ), am , tlsConnectionState )
155173
156174 // Delete allocation
157175 am .DeleteAllocation (& allocation.FiveTuple {
@@ -202,7 +220,7 @@ func (s *Server) createAllocationManager(addrGenerator RelayAddressGenerator, ha
202220 return am , err
203221}
204222
205- func (s * Server ) readLoop (p net.PacketConn , allocationManager * allocation.Manager ) {
223+ func (s * Server ) readLoop (p net.PacketConn , allocationManager * allocation.Manager , tls * tls. ConnectionState ) {
206224 buf := make ([]byte , s .inboundMTU )
207225 for {
208226 n , addr , err := p .ReadFrom (buf )
@@ -219,8 +237,9 @@ func (s *Server) readLoop(p net.PacketConn, allocationManager *allocation.Manage
219237 Conn : p ,
220238 SrcAddr : addr ,
221239 Buff : buf [:n ],
240+ TLS : tls ,
222241 Log : s .log ,
223- AuthHandler : s . authHandler ,
242+ Authorizer : s . authorizer ,
224243 Realm : s .realm ,
225244 AllocationManager : allocationManager ,
226245 ChannelBindTimeout : s .channelBindTimeout ,
0 commit comments