client.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package socks
  2. import (
  3. "context"
  4. "time"
  5. "v2ray.com/core/common"
  6. "v2ray.com/core/common/buf"
  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. // Client is a Socks5 client.
  16. type Client struct {
  17. serverPicker protocol.ServerPicker
  18. }
  19. // NewClient create a new Socks5 client based on the given config.
  20. func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
  21. serverList := protocol.NewServerList()
  22. for _, rec := range config.Server {
  23. serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
  24. }
  25. if serverList.Size() == 0 {
  26. return nil, newError("0 target server")
  27. }
  28. return &Client{
  29. serverPicker: protocol.NewRoundRobinServerPicker(serverList),
  30. }, nil
  31. }
  32. // Process implements proxy.Outbound.Process.
  33. func (c *Client) Process(ctx context.Context, ray ray.OutboundRay, dialer proxy.Dialer) error {
  34. destination, ok := proxy.TargetFromContext(ctx)
  35. if !ok {
  36. return newError("target not specified.")
  37. }
  38. var server *protocol.ServerSpec
  39. var conn internet.Connection
  40. err := retry.ExponentialBackoff(5, 100).On(func() error {
  41. server = c.serverPicker.PickServer()
  42. dest := server.Destination()
  43. rawConn, err := dialer.Dial(ctx, dest)
  44. if err != nil {
  45. return err
  46. }
  47. conn = rawConn
  48. return nil
  49. })
  50. if err != nil {
  51. return newError("failed to find an available destination").Base(err)
  52. }
  53. defer conn.Close()
  54. request := &protocol.RequestHeader{
  55. Version: socks5Version,
  56. Command: protocol.RequestCommandTCP,
  57. Address: destination.Address,
  58. Port: destination.Port,
  59. }
  60. if destination.Network == net.Network_UDP {
  61. request.Command = protocol.RequestCommandUDP
  62. }
  63. user := server.PickUser()
  64. if user != nil {
  65. request.User = user
  66. }
  67. udpRequest, err := ClientHandshake(request, conn, conn)
  68. if err != nil {
  69. return newError("failed to establish connection to server").AtWarning().Base(err)
  70. }
  71. ctx, timer := signal.CancelAfterInactivity(ctx, time.Minute*5)
  72. var requestFunc func() error
  73. var responseFunc func() error
  74. if request.Command == protocol.RequestCommandTCP {
  75. requestFunc = func() error {
  76. return buf.Copy(ray.OutboundInput(), buf.NewWriter(conn), buf.UpdateActivity(timer))
  77. }
  78. responseFunc = func() error {
  79. defer ray.OutboundOutput().Close()
  80. return buf.Copy(buf.NewReader(conn), ray.OutboundOutput(), buf.UpdateActivity(timer))
  81. }
  82. } else if request.Command == protocol.RequestCommandUDP {
  83. udpConn, err := dialer.Dial(ctx, udpRequest.Destination())
  84. if err != nil {
  85. return newError("failed to create UDP connection").Base(err)
  86. }
  87. defer udpConn.Close()
  88. requestFunc = func() error {
  89. return buf.Copy(ray.OutboundInput(), buf.NewSequentialWriter(NewUDPWriter(request, udpConn)), buf.UpdateActivity(timer))
  90. }
  91. responseFunc = func() error {
  92. defer ray.OutboundOutput().Close()
  93. reader := &UDPReader{reader: udpConn}
  94. return buf.Copy(reader, ray.OutboundOutput(), buf.UpdateActivity(timer))
  95. }
  96. }
  97. requestDone := signal.ExecuteAsync(requestFunc)
  98. responseDone := signal.ExecuteAsync(responseFunc)
  99. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  100. return newError("connection ends").Base(err)
  101. }
  102. return nil
  103. }
  104. func init() {
  105. common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  106. return NewClient(ctx, config.(*ClientConfig))
  107. }))
  108. }