1
0

client.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package client
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "net"
  7. "net/netip"
  8. "sync"
  9. "time"
  10. "github.com/Snawoot/dtlspipe/util"
  11. "github.com/pion/dtls/v2"
  12. "github.com/pion/transport/v2/udp"
  13. )
  14. const (
  15. MaxPktBuf = 65536
  16. Backlog = 1024
  17. )
  18. type Client struct {
  19. listener net.Listener
  20. dtlsConfig *dtls.Config
  21. rAddr string
  22. psk func([]byte) ([]byte, error)
  23. timeout time.Duration
  24. idleTimeout time.Duration
  25. baseCtx context.Context
  26. cancelCtx func()
  27. staleMode util.StaleMode
  28. workerWG sync.WaitGroup
  29. }
  30. func New(cfg *Config) (*Client, error) {
  31. cfg = cfg.populateDefaults()
  32. baseCtx, cancelCtx := context.WithCancel(cfg.BaseContext)
  33. client := &Client{
  34. rAddr: cfg.RemoteAddress,
  35. timeout: cfg.Timeout,
  36. psk: cfg.PSKCallback,
  37. idleTimeout: cfg.IdleTimeout,
  38. baseCtx: baseCtx,
  39. cancelCtx: cancelCtx,
  40. staleMode: cfg.StaleMode,
  41. }
  42. lAddrPort, err := netip.ParseAddrPort(cfg.BindAddress)
  43. if err != nil {
  44. cancelCtx()
  45. return nil, fmt.Errorf("can't parse bind address: %w", err)
  46. }
  47. client.dtlsConfig = &dtls.Config{
  48. ExtendedMasterSecret: dtls.RequireExtendedMasterSecret,
  49. ConnectContextMaker: client.contextMaker,
  50. PSK: client.psk,
  51. PSKIdentityHint: []byte(cfg.PSKIdentity),
  52. MTU: cfg.MTU,
  53. CipherSuites: cfg.CipherSuites,
  54. EllipticCurves: cfg.EllipticCurves,
  55. }
  56. lc := udp.ListenConfig{
  57. Backlog: Backlog,
  58. }
  59. listener, err := lc.Listen("udp", net.UDPAddrFromAddrPort(lAddrPort))
  60. if err != nil {
  61. cancelCtx()
  62. return nil, fmt.Errorf("client listen failed: %w", err)
  63. }
  64. client.listener = listener
  65. go client.listen()
  66. return client, nil
  67. }
  68. func (client *Client) listen() {
  69. defer client.Close()
  70. for client.baseCtx.Err() == nil {
  71. conn, err := client.listener.Accept()
  72. if err != nil {
  73. log.Printf("conn accept failed: %v", err)
  74. continue
  75. }
  76. client.workerWG.Add(1)
  77. go func(conn net.Conn) {
  78. defer client.workerWG.Done()
  79. defer conn.Close()
  80. client.serve(conn)
  81. }(conn)
  82. }
  83. }
  84. func (client *Client) serve(conn net.Conn) {
  85. log.Printf("[+] conn %s <=> %s", conn.LocalAddr(), conn.RemoteAddr())
  86. defer log.Printf("[-] conn %s <=> %s", conn.LocalAddr(), conn.RemoteAddr())
  87. defer conn.Close()
  88. dialCtx, cancel := context.WithTimeout(client.baseCtx, client.timeout)
  89. defer cancel()
  90. remoteConn, err := (&net.Dialer{}).DialContext(dialCtx, "udp", client.rAddr)
  91. if err != nil {
  92. log.Printf("remote dial failed: %v", err)
  93. return
  94. }
  95. defer remoteConn.Close()
  96. remoteConn, err = dtls.ClientWithContext(dialCtx, remoteConn, client.dtlsConfig)
  97. if err != nil {
  98. log.Printf("DTLS handshake with remote server failed: %v", err)
  99. return
  100. }
  101. util.PairConn(client.baseCtx, conn, remoteConn, client.idleTimeout, client.staleMode)
  102. }
  103. func (client *Client) contextMaker() (context.Context, func()) {
  104. return context.WithTimeout(client.baseCtx, client.timeout)
  105. }
  106. func (client *Client) Close() error {
  107. client.cancelCtx()
  108. err := client.listener.Close()
  109. client.workerWG.Wait()
  110. return err
  111. }