client.go 2.8 KB

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