client.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package socks
  2. import (
  3. "context"
  4. "runtime"
  5. "time"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/buf"
  8. "v2ray.com/core/common/net"
  9. "v2ray.com/core/common/protocol"
  10. "v2ray.com/core/common/retry"
  11. "v2ray.com/core/common/signal"
  12. "v2ray.com/core/proxy"
  13. "v2ray.com/core/transport/internet"
  14. "v2ray.com/core/transport/ray"
  15. )
  16. // Client is a Socks5 client.
  17. type Client struct {
  18. serverPicker protocol.ServerPicker
  19. }
  20. // NewClient create a new Socks5 client based on the given config.
  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. // Process implements proxy.Outbound.Process.
  32. func (c *Client) Process(ctx context.Context, ray ray.OutboundRay, dialer proxy.Dialer) error {
  33. destination, ok := proxy.TargetFromContext(ctx)
  34. if !ok {
  35. return newError("target not specified.")
  36. }
  37. var server *protocol.ServerSpec
  38. var conn internet.Connection
  39. err := retry.ExponentialBackoff(5, 100).On(func() error {
  40. server = c.serverPicker.PickServer()
  41. dest := server.Destination()
  42. rawConn, err := dialer.Dial(ctx, dest)
  43. if err != nil {
  44. return err
  45. }
  46. conn = rawConn
  47. return nil
  48. })
  49. if err != nil {
  50. return newError("failed to find an available destination").Base(err)
  51. }
  52. defer conn.Close()
  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. return newError("failed to establish connection to server").AtWarning().Base(err)
  69. }
  70. ctx, timer := signal.CancelAfterInactivity(ctx, time.Minute*2)
  71. var requestFunc func() error
  72. var responseFunc func() error
  73. if request.Command == protocol.RequestCommandTCP {
  74. requestFunc = func() error {
  75. return buf.PipeUntilEOF(timer, ray.OutboundInput(), buf.NewWriter(conn))
  76. }
  77. responseFunc = func() error {
  78. defer ray.OutboundOutput().Close()
  79. return buf.PipeUntilEOF(timer, buf.NewReader(conn), ray.OutboundOutput())
  80. }
  81. } else if request.Command == protocol.RequestCommandUDP {
  82. udpConn, err := dialer.Dial(ctx, udpRequest.Destination())
  83. if err != nil {
  84. return newError("failed to create UDP connection").Base(err)
  85. }
  86. defer udpConn.Close()
  87. requestFunc = func() error {
  88. return buf.PipeUntilEOF(timer, ray.OutboundInput(), &UDPWriter{request: request, writer: udpConn})
  89. }
  90. responseFunc = func() error {
  91. defer ray.OutboundOutput().Close()
  92. reader := &UDPReader{reader: udpConn}
  93. return buf.PipeUntilEOF(timer, reader, ray.OutboundOutput())
  94. }
  95. }
  96. requestDone := signal.ExecuteAsync(requestFunc)
  97. responseDone := signal.ExecuteAsync(responseFunc)
  98. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  99. return newError("connection ends").Base(err)
  100. }
  101. runtime.KeepAlive(timer)
  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. }