client.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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, cancel := context.WithCancel(ctx)
  72. timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*5)
  73. var requestFunc func() error
  74. var responseFunc func() error
  75. if request.Command == protocol.RequestCommandTCP {
  76. requestFunc = func() error {
  77. return buf.Copy(ray.OutboundInput(), buf.NewWriter(conn), buf.UpdateActivity(timer))
  78. }
  79. responseFunc = func() error {
  80. defer ray.OutboundOutput().Close()
  81. return buf.Copy(buf.NewReader(conn), ray.OutboundOutput(), buf.UpdateActivity(timer))
  82. }
  83. } else if request.Command == protocol.RequestCommandUDP {
  84. udpConn, err := dialer.Dial(ctx, udpRequest.Destination())
  85. if err != nil {
  86. return newError("failed to create UDP connection").Base(err)
  87. }
  88. defer udpConn.Close()
  89. requestFunc = func() error {
  90. return buf.Copy(ray.OutboundInput(), buf.NewSequentialWriter(NewUDPWriter(request, udpConn)), buf.UpdateActivity(timer))
  91. }
  92. responseFunc = func() error {
  93. defer ray.OutboundOutput().Close()
  94. reader := &UDPReader{reader: udpConn}
  95. return buf.Copy(reader, ray.OutboundOutput(), buf.UpdateActivity(timer))
  96. }
  97. }
  98. requestDone := signal.ExecuteAsync(requestFunc)
  99. responseDone := signal.ExecuteAsync(responseFunc)
  100. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  101. return newError("connection ends").Base(err)
  102. }
  103. return nil
  104. }
  105. func init() {
  106. common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  107. return NewClient(ctx, config.(*ClientConfig))
  108. }))
  109. }