client.go 2.7 KB

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