server.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Package server implements the private API to implement a TURN server
  2. package server
  3. import (
  4. "fmt"
  5. "net"
  6. "sync"
  7. "time"
  8. "github.com/pion/logging"
  9. "github.com/pion/stun"
  10. "github.com/pion/turn/v2/internal/allocation"
  11. "github.com/pion/turn/v2/internal/proto"
  12. )
  13. // Request contains all the state needed to process a single incoming datagram
  14. type Request struct {
  15. // Current Request State
  16. Conn net.PacketConn
  17. SrcAddr net.Addr
  18. Buff []byte
  19. // Server State
  20. AllocationManager *allocation.Manager
  21. Nonces *sync.Map
  22. // User Configuration
  23. AuthHandler func(username string, realm string, srcAddr net.Addr) (key []byte, ok bool)
  24. Log logging.LeveledLogger
  25. Realm string
  26. ChannelBindTimeout time.Duration
  27. }
  28. // HandleRequest processes the give Request
  29. func HandleRequest(r Request) error {
  30. r.Log.Debugf("received %d bytes of udp from %s on %s", len(r.Buff), r.SrcAddr.String(), r.Conn.LocalAddr().String())
  31. if proto.IsChannelData(r.Buff) {
  32. return handleDataPacket(r)
  33. }
  34. return handleTURNPacket(r)
  35. }
  36. func handleDataPacket(r Request) error {
  37. r.Log.Debugf("received DataPacket from %s", r.SrcAddr.String())
  38. c := proto.ChannelData{Raw: r.Buff}
  39. if err := c.Decode(); err != nil {
  40. return fmt.Errorf("%w: %v", errFailedToCreateChannelData, err)
  41. }
  42. err := handleChannelData(r, &c)
  43. if err != nil {
  44. err = fmt.Errorf("%w from %v: %v", errUnableToHandleChannelData, r.SrcAddr, err)
  45. }
  46. return err
  47. }
  48. func handleTURNPacket(r Request) error {
  49. r.Log.Debug("handleTURNPacket")
  50. m := &stun.Message{Raw: append([]byte{}, r.Buff...)}
  51. if err := m.Decode(); err != nil {
  52. return fmt.Errorf("%w: %v", errFailedToCreateSTUNPacket, err)
  53. }
  54. h, err := getMessageHandler(m.Type.Class, m.Type.Method)
  55. if err != nil {
  56. return fmt.Errorf("%w %v-%v from %v: %v", errUnhandledSTUNPacket, m.Type.Method, m.Type.Class, r.SrcAddr, err)
  57. }
  58. err = h(r, m)
  59. if err != nil {
  60. return fmt.Errorf("%w %v-%v from %v: %v", errFailedToHandle, m.Type.Method, m.Type.Class, r.SrcAddr, err)
  61. }
  62. return nil
  63. }
  64. func getMessageHandler(class stun.MessageClass, method stun.Method) (func(r Request, m *stun.Message) error, error) {
  65. switch class {
  66. case stun.ClassIndication:
  67. switch method {
  68. case stun.MethodSend:
  69. return handleSendIndication, nil
  70. default:
  71. return nil, fmt.Errorf("%w: %s", errUnexpectedMethod, method)
  72. }
  73. case stun.ClassRequest:
  74. switch method {
  75. case stun.MethodAllocate:
  76. return handleAllocateRequest, nil
  77. case stun.MethodRefresh:
  78. return handleRefreshRequest, nil
  79. case stun.MethodCreatePermission:
  80. return handleCreatePermissionRequest, nil
  81. case stun.MethodChannelBind:
  82. return handleChannelBindRequest, nil
  83. case stun.MethodBinding:
  84. return handleBindingRequest, nil
  85. default:
  86. return nil, fmt.Errorf("%w: %s", errUnexpectedMethod, method)
  87. }
  88. default:
  89. return nil, fmt.Errorf("%w: %s", errUnexpectedClass, class)
  90. }
  91. }