client.go 3.2 KB

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