1
0

client.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. timeLimit time.Duration
  30. }
  31. func New(cfg *Config) (*Client, error) {
  32. cfg = cfg.populateDefaults()
  33. baseCtx, cancelCtx := context.WithCancel(cfg.BaseContext)
  34. client := &Client{
  35. rAddr: cfg.RemoteAddress,
  36. timeout: cfg.Timeout,
  37. psk: cfg.PSKCallback,
  38. idleTimeout: cfg.IdleTimeout,
  39. baseCtx: baseCtx,
  40. cancelCtx: cancelCtx,
  41. staleMode: cfg.StaleMode,
  42. timeLimit: cfg.TimeLimit,
  43. }
  44. lAddrPort, err := netip.ParseAddrPort(cfg.BindAddress)
  45. if err != nil {
  46. cancelCtx()
  47. return nil, fmt.Errorf("can't parse bind address: %w", err)
  48. }
  49. client.dtlsConfig = &dtls.Config{
  50. ExtendedMasterSecret: dtls.RequireExtendedMasterSecret,
  51. ConnectContextMaker: client.contextMaker,
  52. PSK: client.psk,
  53. PSKIdentityHint: []byte(cfg.PSKIdentity),
  54. MTU: cfg.MTU,
  55. CipherSuites: cfg.CipherSuites,
  56. EllipticCurves: cfg.EllipticCurves,
  57. }
  58. lc := udp.ListenConfig{
  59. Backlog: Backlog,
  60. }
  61. listener, err := lc.Listen("udp", net.UDPAddrFromAddrPort(lAddrPort))
  62. if err != nil {
  63. cancelCtx()
  64. return nil, fmt.Errorf("client listen failed: %w", err)
  65. }
  66. client.listener = listener
  67. go client.listen()
  68. return client, nil
  69. }
  70. func (client *Client) listen() {
  71. defer client.Close()
  72. for client.baseCtx.Err() == nil {
  73. conn, err := client.listener.Accept()
  74. if err != nil {
  75. log.Printf("conn accept failed: %v", err)
  76. continue
  77. }
  78. client.workerWG.Add(1)
  79. go func(conn net.Conn) {
  80. defer client.workerWG.Done()
  81. defer conn.Close()
  82. client.serve(conn)
  83. }(conn)
  84. }
  85. }
  86. func (client *Client) serve(conn net.Conn) {
  87. log.Printf("[+] conn %s <=> %s", conn.LocalAddr(), conn.RemoteAddr())
  88. defer log.Printf("[-] conn %s <=> %s", conn.LocalAddr(), conn.RemoteAddr())
  89. defer conn.Close()
  90. ctx := client.baseCtx
  91. if client.timeLimit != 0 {
  92. newCtx, cancel := context.WithTimeout(ctx, client.timeLimit)
  93. defer cancel()
  94. ctx = newCtx
  95. }
  96. dialCtx, cancel := context.WithTimeout(ctx, client.timeout)
  97. defer cancel()
  98. remoteConn, err := (&net.Dialer{}).DialContext(dialCtx, "udp", client.rAddr)
  99. if err != nil {
  100. log.Printf("remote dial failed: %v", err)
  101. return
  102. }
  103. defer remoteConn.Close()
  104. remoteConn, err = dtls.ClientWithContext(dialCtx, remoteConn, client.dtlsConfig)
  105. if err != nil {
  106. log.Printf("DTLS handshake with remote server failed: %v", err)
  107. return
  108. }
  109. util.PairConn(ctx, conn, remoteConn, client.idleTimeout, client.staleMode)
  110. }
  111. func (client *Client) contextMaker() (context.Context, func()) {
  112. return context.WithTimeout(client.baseCtx, client.timeout)
  113. }
  114. func (client *Client) Close() error {
  115. client.cancelCtx()
  116. err := client.listener.Close()
  117. client.workerWG.Wait()
  118. return err
  119. }