server.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package server
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "log"
  7. "net"
  8. "net/netip"
  9. "time"
  10. "github.com/pion/dtls/v2"
  11. "github.com/pion/dtls/v2/pkg/protocol"
  12. "github.com/pion/dtls/v2/pkg/protocol/recordlayer"
  13. "github.com/pion/transport/v2/udp"
  14. )
  15. type Server struct {
  16. listener net.Listener
  17. dtlsConfig *dtls.Config
  18. rAddr string
  19. psk []byte
  20. timeout time.Duration
  21. idleTimeout time.Duration
  22. baseCtx context.Context
  23. cancelCtx func()
  24. }
  25. func New(cfg *Config) (*Server, error) {
  26. cfg = cfg.populateDefaults()
  27. baseCtx, cancelCtx := context.WithCancel(cfg.BaseContext)
  28. srv := &Server{
  29. rAddr: cfg.RemoteAddress,
  30. timeout: cfg.Timeout,
  31. psk: cfg.PSK,
  32. idleTimeout: cfg.IdleTimeout,
  33. baseCtx: baseCtx,
  34. cancelCtx: cancelCtx,
  35. }
  36. lAddrPort, err := netip.ParseAddrPort(cfg.BindAddress)
  37. if err != nil {
  38. cancelCtx()
  39. return nil, fmt.Errorf("can't parse bind address: %w", err)
  40. }
  41. srv.dtlsConfig = &dtls.Config{
  42. CipherSuites: []dtls.CipherSuiteID{
  43. dtls.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256,
  44. dtls.TLS_PSK_WITH_AES_128_CCM,
  45. dtls.TLS_PSK_WITH_AES_128_CCM_8,
  46. dtls.TLS_PSK_WITH_AES_256_CCM_8,
  47. dtls.TLS_PSK_WITH_AES_128_GCM_SHA256,
  48. dtls.TLS_PSK_WITH_AES_128_CBC_SHA256,
  49. },
  50. ExtendedMasterSecret: dtls.RequireExtendedMasterSecret,
  51. ConnectContextMaker: srv.contextMaker,
  52. PSK: srv.getPSK,
  53. }
  54. lc := udp.ListenConfig{
  55. AcceptFilter: func(packet []byte) bool {
  56. pkts, err := recordlayer.UnpackDatagram(packet)
  57. if err != nil || len(pkts) < 1 {
  58. return false
  59. }
  60. h := &recordlayer.Header{}
  61. if err := h.Unmarshal(pkts[0]); err != nil {
  62. return false
  63. }
  64. return h.ContentType == protocol.ContentTypeHandshake
  65. },
  66. }
  67. listener, err := lc.Listen("udp", net.UDPAddrFromAddrPort(lAddrPort))
  68. if err != nil {
  69. cancelCtx()
  70. return nil, fmt.Errorf("server listen failed: %w", err)
  71. }
  72. srv.listener = listener
  73. go srv.listen()
  74. return srv, nil
  75. }
  76. func (srv *Server) listen() {
  77. defer srv.Close()
  78. for srv.baseCtx.Err() == nil {
  79. conn, err := srv.listener.Accept()
  80. if err != nil {
  81. log.Printf("conn accept failed: %v", err)
  82. continue
  83. }
  84. go func(conn net.Conn) {
  85. conn, err := dtls.Server(conn, srv.dtlsConfig)
  86. if err != nil {
  87. log.Printf("DTLS accept error: %v", err)
  88. return
  89. }
  90. srv.serve(conn)
  91. }(conn)
  92. }
  93. }
  94. func (srv *Server) serve(conn net.Conn) {
  95. log.Printf("[+] conn %s <=> %s", conn.LocalAddr(), conn.RemoteAddr())
  96. defer log.Printf("[-] conn %s <=> %s", conn.LocalAddr(), conn.RemoteAddr())
  97. defer conn.Close()
  98. io.Copy(conn, conn)
  99. }
  100. func (srv *Server) contextMaker() (context.Context, func()) {
  101. return context.WithTimeout(srv.baseCtx, srv.timeout)
  102. }
  103. func (srv *Server) getPSK(hint []byte) ([]byte, error) {
  104. return srv.psk, nil
  105. }
  106. func (srv *Server) Close() error {
  107. srv.cancelCtx()
  108. return srv.listener.Close()
  109. }