client.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package socks
  2. import (
  3. "context"
  4. "runtime"
  5. "time"
  6. "v2ray.com/core/app/log"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/buf"
  9. "v2ray.com/core/common/errors"
  10. "v2ray.com/core/common/net"
  11. "v2ray.com/core/common/protocol"
  12. "v2ray.com/core/common/retry"
  13. "v2ray.com/core/common/signal"
  14. "v2ray.com/core/proxy"
  15. "v2ray.com/core/transport/internet"
  16. "v2ray.com/core/transport/ray"
  17. )
  18. type Client struct {
  19. serverPicker protocol.ServerPicker
  20. }
  21. func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
  22. serverList := protocol.NewServerList()
  23. for _, rec := range config.Server {
  24. serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
  25. }
  26. client := &Client{
  27. serverPicker: protocol.NewRoundRobinServerPicker(serverList),
  28. }
  29. return client, nil
  30. }
  31. func (c *Client) Process(ctx context.Context, ray ray.OutboundRay, dialer proxy.Dialer) error {
  32. destination, ok := proxy.TargetFromContext(ctx)
  33. if !ok {
  34. return errors.New("Socks|Client: Target not specified.")
  35. }
  36. var server *protocol.ServerSpec
  37. var conn internet.Connection
  38. err := retry.ExponentialBackoff(5, 100).On(func() error {
  39. server = c.serverPicker.PickServer()
  40. dest := server.Destination()
  41. rawConn, err := dialer.Dial(ctx, dest)
  42. if err != nil {
  43. return err
  44. }
  45. conn = rawConn
  46. return nil
  47. })
  48. if err != nil {
  49. return errors.Base(err).Message("Socks|Client: Failed to find an available destination.")
  50. }
  51. defer conn.Close()
  52. conn.SetReusable(false)
  53. request := &protocol.RequestHeader{
  54. Version: socks5Version,
  55. Command: protocol.RequestCommandTCP,
  56. Address: destination.Address,
  57. Port: destination.Port,
  58. }
  59. if destination.Network == net.Network_UDP {
  60. request.Command = protocol.RequestCommandUDP
  61. }
  62. user := server.PickUser()
  63. if user != nil {
  64. request.User = user
  65. }
  66. udpRequest, err := ClientHandshake(request, conn, conn)
  67. if err != nil {
  68. log.Warning("Socks|Client: Failed to establish connection to server: ", err)
  69. return err
  70. }
  71. ctx, cancel := context.WithCancel(ctx)
  72. timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*2)
  73. var requestFunc func() error
  74. var responseFunc func() error
  75. if request.Command == protocol.RequestCommandTCP {
  76. requestFunc = func() error {
  77. return buf.PipeUntilEOF(timer, ray.OutboundInput(), buf.NewWriter(conn))
  78. }
  79. responseFunc = func() error {
  80. defer ray.OutboundOutput().Close()
  81. return buf.PipeUntilEOF(timer, buf.NewReader(conn), ray.OutboundOutput())
  82. }
  83. } else if request.Command == protocol.RequestCommandUDP {
  84. udpConn, err := dialer.Dial(ctx, udpRequest.Destination())
  85. if err != nil {
  86. log.Info("Socks|Client: Failed to create UDP connection: ", err)
  87. return err
  88. }
  89. defer udpConn.Close()
  90. requestFunc = func() error {
  91. return buf.PipeUntilEOF(timer, ray.OutboundInput(), &UDPWriter{request: request, writer: udpConn})
  92. }
  93. responseFunc = func() error {
  94. defer ray.OutboundOutput().Close()
  95. reader := &UDPReader{reader: udpConn}
  96. return buf.PipeUntilEOF(timer, reader, ray.OutboundOutput())
  97. }
  98. }
  99. requestDone := signal.ExecuteAsync(requestFunc)
  100. responseDone := signal.ExecuteAsync(responseFunc)
  101. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  102. log.Info("Socks|Client: Connection ends with ", err)
  103. return err
  104. }
  105. runtime.KeepAlive(timer)
  106. return nil
  107. }
  108. func init() {
  109. common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  110. return NewClient(ctx, config.(*ClientConfig))
  111. }))
  112. }