client.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/app/log"
  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) error {
  31. destination := proxy.DestinationFromContext(ctx)
  32. var server *protocol.ServerSpec
  33. var conn internet.Connection
  34. dialer := proxy.DialerFromContext(ctx)
  35. err := retry.ExponentialBackoff(5, 100).On(func() error {
  36. server = c.serverPicker.PickServer()
  37. dest := server.Destination()
  38. rawConn, err := dialer.Dial(ctx, dest)
  39. if err != nil {
  40. return err
  41. }
  42. conn = rawConn
  43. return nil
  44. })
  45. if err != nil {
  46. log.Warning("Socks|Client: Failed to find an available destination.")
  47. return err
  48. }
  49. defer conn.Close()
  50. conn.SetReusable(false)
  51. request := &protocol.RequestHeader{
  52. Version: socks5Version,
  53. Command: protocol.RequestCommandTCP,
  54. Address: destination.Address,
  55. Port: destination.Port,
  56. }
  57. if destination.Network == net.Network_UDP {
  58. request.Command = protocol.RequestCommandUDP
  59. }
  60. user := server.PickUser()
  61. if user != nil {
  62. request.User = user
  63. }
  64. udpRequest, err := ClientHandshake(request, conn, conn)
  65. if err != nil {
  66. log.Warning("Socks|Client: Failed to establish connection to server: ", err)
  67. return err
  68. }
  69. ctx, cancel := context.WithCancel(ctx)
  70. timer := signal.CancelAfterInactivity(ctx, cancel, 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. log.Info("Socks|Client: Failed to create UDP connection: ", err)
  85. return err
  86. }
  87. defer udpConn.Close()
  88. requestFunc = func() error {
  89. return buf.PipeUntilEOF(timer, ray.OutboundInput(), &UDPWriter{request: request, writer: udpConn})
  90. }
  91. responseFunc = func() error {
  92. defer ray.OutboundOutput().Close()
  93. reader := &UDPReader{reader: udpConn}
  94. return buf.PipeUntilEOF(timer, reader, ray.OutboundOutput())
  95. }
  96. }
  97. requestDone := signal.ExecuteAsync(requestFunc)
  98. responseDone := signal.ExecuteAsync(responseFunc)
  99. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  100. log.Info("Socks|Client: Connection ends with ", err)
  101. return err
  102. }
  103. runtime.KeepAlive(timer)
  104. return nil
  105. }
  106. func init() {
  107. common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  108. return NewClient(ctx, config.(*ClientConfig))
  109. }))
  110. }