2024-06-05 20:10:03 +00:00
|
|
|
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
// Package turn contains the public API for pion/turn, a toolkit for building TURN clients and servers
|
|
|
|
package turn
|
|
|
|
|
|
|
|
import (
|
2024-06-05 20:10:03 +00:00
|
|
|
"errors"
|
2022-03-10 09:44:48 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pion/logging"
|
|
|
|
"github.com/pion/turn/v2/internal/allocation"
|
|
|
|
"github.com/pion/turn/v2/internal/proto"
|
|
|
|
"github.com/pion/turn/v2/internal/server"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultInboundMTU = 1600
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server is an instance of the Pion TURN Server
|
|
|
|
type Server struct {
|
|
|
|
log logging.LeveledLogger
|
|
|
|
authHandler AuthHandler
|
|
|
|
realm string
|
|
|
|
channelBindTimeout time.Duration
|
2024-06-05 20:10:03 +00:00
|
|
|
nonceHash *server.NonceHash
|
2022-03-10 09:44:48 +00:00
|
|
|
|
|
|
|
packetConnConfigs []PacketConnConfig
|
|
|
|
listenerConfigs []ListenerConfig
|
|
|
|
allocationManagers []*allocation.Manager
|
2024-05-15 23:15:00 +00:00
|
|
|
inboundMTU int
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates the Pion TURN server
|
2024-05-15 23:15:00 +00:00
|
|
|
//
|
|
|
|
//nolint:gocognit
|
2022-03-10 09:44:48 +00:00
|
|
|
func NewServer(config ServerConfig) (*Server, error) {
|
|
|
|
if err := config.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
loggerFactory := config.LoggerFactory
|
|
|
|
if loggerFactory == nil {
|
|
|
|
loggerFactory = logging.NewDefaultLoggerFactory()
|
|
|
|
}
|
|
|
|
|
|
|
|
mtu := defaultInboundMTU
|
|
|
|
if config.InboundMTU != 0 {
|
|
|
|
mtu = config.InboundMTU
|
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
nonceHash, err := server.NewNonceHash()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
s := &Server{
|
|
|
|
log: loggerFactory.NewLogger("turn"),
|
|
|
|
authHandler: config.AuthHandler,
|
|
|
|
realm: config.Realm,
|
|
|
|
channelBindTimeout: config.ChannelBindTimeout,
|
|
|
|
packetConnConfigs: config.PacketConnConfigs,
|
|
|
|
listenerConfigs: config.ListenerConfigs,
|
2024-06-05 20:10:03 +00:00
|
|
|
nonceHash: nonceHash,
|
2022-03-10 09:44:48 +00:00
|
|
|
inboundMTU: mtu,
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.channelBindTimeout == 0 {
|
|
|
|
s.channelBindTimeout = proto.DefaultLifetime
|
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
for _, cfg := range s.packetConnConfigs {
|
|
|
|
am, err := s.createAllocationManager(cfg.RelayAddressGenerator, cfg.PermissionHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create AllocationManager: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
go func(cfg PacketConnConfig, am *allocation.Manager) {
|
|
|
|
s.readLoop(cfg.PacketConn, am)
|
|
|
|
|
|
|
|
if err := am.Close(); err != nil {
|
|
|
|
s.log.Errorf("Failed to close AllocationManager: %s", err)
|
|
|
|
}
|
|
|
|
}(cfg, am)
|
2024-05-15 23:15:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, cfg := range s.listenerConfigs {
|
|
|
|
am, err := s.createAllocationManager(cfg.RelayAddressGenerator, cfg.PermissionHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create AllocationManager: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
go func(cfg ListenerConfig, am *allocation.Manager) {
|
|
|
|
s.readListener(cfg.Listener, am)
|
|
|
|
|
|
|
|
if err := am.Close(); err != nil {
|
|
|
|
s.log.Errorf("Failed to close AllocationManager: %s", err)
|
|
|
|
}
|
|
|
|
}(cfg, am)
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllocationCount returns the number of active allocations. It can be used to drain the server before closing
|
|
|
|
func (s *Server) AllocationCount() int {
|
2024-05-15 23:15:00 +00:00
|
|
|
allocs := 0
|
|
|
|
for _, am := range s.allocationManagers {
|
|
|
|
allocs += am.AllocationCount()
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
2024-05-15 23:15:00 +00:00
|
|
|
return allocs
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close stops the TURN Server. It cleans up any associated state and closes all connections it is managing
|
|
|
|
func (s *Server) Close() error {
|
|
|
|
var errors []error
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
for _, cfg := range s.packetConnConfigs {
|
|
|
|
if err := cfg.PacketConn.Close(); err != nil {
|
2022-03-10 09:44:48 +00:00
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
for _, cfg := range s.listenerConfigs {
|
|
|
|
if err := cfg.Listener.Close(); err != nil {
|
2022-03-10 09:44:48 +00:00
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := errFailedToClose
|
|
|
|
for _, e := range errors {
|
2024-06-05 20:10:03 +00:00
|
|
|
err = fmt.Errorf("%s; close error (%w) ", err, e) //nolint:errorlint
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
func (s *Server) readListener(l net.Listener, am *allocation.Manager) {
|
2024-05-15 23:15:00 +00:00
|
|
|
for {
|
2024-06-05 20:10:03 +00:00
|
|
|
conn, err := l.Accept()
|
2024-05-15 23:15:00 +00:00
|
|
|
if err != nil {
|
|
|
|
s.log.Debugf("Failed to accept: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
go func() {
|
|
|
|
s.readLoop(NewSTUNConn(conn), am)
|
|
|
|
|
|
|
|
if err := conn.Close(); err != nil && !errors.Is(err, net.ErrClosed) {
|
|
|
|
s.log.Errorf("Failed to close conn: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
2024-05-15 23:15:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) createAllocationManager(addrGenerator RelayAddressGenerator, handler PermissionHandler) (*allocation.Manager, error) {
|
|
|
|
if handler == nil {
|
|
|
|
handler = DefaultPermissionHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
am, err := allocation.NewManager(allocation.ManagerConfig{
|
|
|
|
AllocatePacketConn: addrGenerator.AllocatePacketConn,
|
|
|
|
AllocateConn: addrGenerator.AllocateConn,
|
|
|
|
PermissionHandler: handler,
|
|
|
|
LeveledLogger: s.log,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return am, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.allocationManagers = append(s.allocationManagers, am)
|
|
|
|
|
|
|
|
return am, err
|
|
|
|
}
|
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
func (s *Server) readLoop(p net.PacketConn, allocationManager *allocation.Manager) {
|
|
|
|
buf := make([]byte, s.inboundMTU)
|
|
|
|
for {
|
|
|
|
n, addr, err := p.ReadFrom(buf)
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
2024-06-05 20:10:03 +00:00
|
|
|
s.log.Debugf("Exit read loop on error: %s", err)
|
2022-03-10 09:44:48 +00:00
|
|
|
return
|
|
|
|
case n >= s.inboundMTU:
|
|
|
|
s.log.Debugf("Read bytes exceeded MTU, packet is possibly truncated")
|
2024-06-05 20:10:03 +00:00
|
|
|
continue
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := server.HandleRequest(server.Request{
|
|
|
|
Conn: p,
|
|
|
|
SrcAddr: addr,
|
|
|
|
Buff: buf[:n],
|
|
|
|
Log: s.log,
|
|
|
|
AuthHandler: s.authHandler,
|
|
|
|
Realm: s.realm,
|
|
|
|
AllocationManager: allocationManager,
|
|
|
|
ChannelBindTimeout: s.channelBindTimeout,
|
2024-06-05 20:10:03 +00:00
|
|
|
NonceHash: s.nonceHash,
|
2022-03-10 09:44:48 +00:00
|
|
|
}); err != nil {
|
2024-06-05 20:10:03 +00:00
|
|
|
s.log.Errorf("Failed to handle datagram: %v", err)
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|