server_config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package turn
  2. import (
  3. "crypto/md5" //nolint:gosec,gci
  4. "fmt"
  5. "net"
  6. "strings"
  7. "time"
  8. "github.com/pion/logging"
  9. )
  10. // RelayAddressGenerator is used to generate a RelayAddress when creating an allocation.
  11. // You can use one of the provided ones or provide your own.
  12. type RelayAddressGenerator interface {
  13. // Validate confirms that the RelayAddressGenerator is properly initialized
  14. Validate() error
  15. // Allocate a PacketConn (UDP) RelayAddress
  16. AllocatePacketConn(network string, requestedPort int) (net.PacketConn, net.Addr, error)
  17. // Allocate a Conn (TCP) RelayAddress
  18. AllocateConn(network string, requestedPort int) (net.Conn, net.Addr, error)
  19. }
  20. // PacketConnConfig is a single net.PacketConn to listen/write on. This will be used for UDP listeners
  21. type PacketConnConfig struct {
  22. PacketConn net.PacketConn
  23. // When an allocation is generated the RelayAddressGenerator
  24. // creates the net.PacketConn and returns the IP/Port it is available at
  25. RelayAddressGenerator RelayAddressGenerator
  26. }
  27. func (c *PacketConnConfig) validate() error {
  28. if c.PacketConn == nil {
  29. return errConnUnset
  30. }
  31. if c.RelayAddressGenerator == nil {
  32. return errRelayAddressGeneratorUnset
  33. }
  34. return c.RelayAddressGenerator.Validate()
  35. }
  36. // ListenerConfig is a single net.Listener to accept connections on. This will be used for TCP, TLS and DTLS listeners
  37. type ListenerConfig struct {
  38. Listener net.Listener
  39. // When an allocation is generated the RelayAddressGenerator
  40. // creates the net.PacketConn and returns the IP/Port it is available at
  41. RelayAddressGenerator RelayAddressGenerator
  42. }
  43. func (c *ListenerConfig) validate() error {
  44. if c.Listener == nil {
  45. return errListenerUnset
  46. }
  47. if c.RelayAddressGenerator == nil {
  48. return errRelayAddressGeneratorUnset
  49. }
  50. return c.RelayAddressGenerator.Validate()
  51. }
  52. // AuthHandler is a callback used to handle incoming auth requests, allowing users to customize Pion TURN with custom behavior
  53. type AuthHandler func(username, realm string, srcAddr net.Addr) (key []byte, ok bool)
  54. // GenerateAuthKey is a convince function to easily generate keys in the format used by AuthHandler
  55. func GenerateAuthKey(username, realm, password string) []byte {
  56. // #nosec
  57. h := md5.New()
  58. fmt.Fprint(h, strings.Join([]string{username, realm, password}, ":"))
  59. return h.Sum(nil)
  60. }
  61. // ServerConfig configures the Pion TURN Server
  62. type ServerConfig struct {
  63. // PacketConnConfigs and ListenerConfigs are a list of all the turn listeners
  64. // Each listener can have custom behavior around the creation of Relays
  65. PacketConnConfigs []PacketConnConfig
  66. ListenerConfigs []ListenerConfig
  67. // LoggerFactory must be set for logging from this server.
  68. LoggerFactory logging.LoggerFactory
  69. // Realm sets the realm for this server
  70. Realm string
  71. // AuthHandler is a callback used to handle incoming auth requests, allowing users to customize Pion TURN with custom behavior
  72. AuthHandler AuthHandler
  73. // ChannelBindTimeout sets the lifetime of channel binding. Defaults to 10 minutes.
  74. ChannelBindTimeout time.Duration
  75. // Sets the server inbound MTU(Maximum transmition unit). Defaults to 1600 bytes.
  76. InboundMTU int
  77. }
  78. func (s *ServerConfig) validate() error {
  79. if len(s.PacketConnConfigs) == 0 && len(s.ListenerConfigs) == 0 {
  80. return errNoAvailableConns
  81. }
  82. for _, s := range s.PacketConnConfigs {
  83. if err := s.validate(); err != nil {
  84. return err
  85. }
  86. }
  87. for _, s := range s.ListenerConfigs {
  88. if err := s.validate(); err != nil {
  89. return err
  90. }
  91. }
  92. return nil
  93. }