client.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/errors"
  9. "v2ray.com/core/common/net"
  10. "v2ray.com/core/common/protocol"
  11. "v2ray.com/core/common/retry"
  12. "v2ray.com/core/common/signal"
  13. "v2ray.com/core/proxy"
  14. "v2ray.com/core/transport/internet"
  15. "v2ray.com/core/transport/ray"
  16. )
  17. type Client struct {
  18. serverPicker protocol.ServerPicker
  19. }
  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. client := &Client{
  26. serverPicker: protocol.NewRoundRobinServerPicker(serverList),
  27. }
  28. return client, nil
  29. }
  30. func (c *Client) Process(ctx context.Context, ray ray.OutboundRay, dialer proxy.Dialer) error {
  31. destination, ok := proxy.TargetFromContext(ctx)
  32. if !ok {
  33. return errors.New("Socks|Client: Target not specified.")
  34. }
  35. var server *protocol.ServerSpec
  36. var conn internet.Connection
  37. err := retry.ExponentialBackoff(5, 100).On(func() error {
  38. server = c.serverPicker.PickServer()
  39. dest := server.Destination()
  40. rawConn, err := dialer.Dial(ctx, dest)
  41. if err != nil {
  42. return err
  43. }
  44. conn = rawConn
  45. return nil
  46. })
  47. if err != nil {
  48. return errors.New("failed to find an available destination").Base(err).Path("Socks", "Client")
  49. }
  50. defer conn.Close()
  51. conn.SetReusable(false)
  52. request := &protocol.RequestHeader{
  53. Version: socks5Version,
  54. Command: protocol.RequestCommandTCP,
  55. Address: destination.Address,
  56. Port: destination.Port,
  57. }
  58. if destination.Network == net.Network_UDP {
  59. request.Command = protocol.RequestCommandUDP
  60. }
  61. user := server.PickUser()
  62. if user != nil {
  63. request.User = user
  64. }
  65. udpRequest, err := ClientHandshake(request, conn, conn)
  66. if err != nil {
  67. return errors.New("failed to establish connection to server").AtWarning().Base(err).Path("Socks", "Client")
  68. }
  69. ctx, timer := signal.CancelAfterInactivity(ctx, time.Minute*2)
  70. var requestFunc func() error
  71. var responseFunc func() error
  72. if request.Command == protocol.RequestCommandTCP {
  73. requestFunc = func() error {
  74. return buf.PipeUntilEOF(timer, ray.OutboundInput(), buf.NewWriter(conn))
  75. }
  76. responseFunc = func() error {
  77. defer ray.OutboundOutput().Close()
  78. return buf.PipeUntilEOF(timer, buf.NewReader(conn), ray.OutboundOutput())
  79. }
  80. } else if request.Command == protocol.RequestCommandUDP {
  81. udpConn, err := dialer.Dial(ctx, udpRequest.Destination())
  82. if err != nil {
  83. return errors.New("failed to create UDP connection").Base(err).Path("Socks", "Client")
  84. }
  85. defer udpConn.Close()
  86. requestFunc = func() error {
  87. return buf.PipeUntilEOF(timer, ray.OutboundInput(), &UDPWriter{request: request, writer: udpConn})
  88. }
  89. responseFunc = func() error {
  90. defer ray.OutboundOutput().Close()
  91. reader := &UDPReader{reader: udpConn}
  92. return buf.PipeUntilEOF(timer, reader, ray.OutboundOutput())
  93. }
  94. }
  95. requestDone := signal.ExecuteAsync(requestFunc)
  96. responseDone := signal.ExecuteAsync(responseFunc)
  97. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  98. return errors.New("connection ends").Base(err).Path("Socks", "Client")
  99. }
  100. runtime.KeepAlive(timer)
  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. }