client.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 = 4096
  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. CipherSuites: []dtls.CipherSuiteID{
  45. dtls.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256,
  46. dtls.TLS_PSK_WITH_AES_128_CCM,
  47. dtls.TLS_PSK_WITH_AES_128_CCM_8,
  48. dtls.TLS_PSK_WITH_AES_256_CCM_8,
  49. dtls.TLS_PSK_WITH_AES_128_GCM_SHA256,
  50. dtls.TLS_PSK_WITH_AES_128_CBC_SHA256,
  51. },
  52. ExtendedMasterSecret: dtls.RequireExtendedMasterSecret,
  53. ConnectContextMaker: client.contextMaker,
  54. PSK: client.psk,
  55. PSKIdentityHint: []byte(cfg.PSKIdentity),
  56. MTU: cfg.MTU,
  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. go func(conn net.Conn) {
  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("DTL handshake with remote server failed: %v", err)
  99. return
  100. }
  101. util.PairConn(conn, remoteConn, client.idleTimeout)
  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. return client.listener.Close()
  109. }