client.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package socks
  2. import (
  3. "context"
  4. "runtime"
  5. "time"
  6. "v2ray.com/core/app/log"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/buf"
  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 := proxy.DestinationFromContext(ctx)
  32. var server *protocol.ServerSpec
  33. var conn internet.Connection
  34. err := retry.ExponentialBackoff(5, 100).On(func() error {
  35. server = c.serverPicker.PickServer()
  36. dest := server.Destination()
  37. rawConn, err := dialer.Dial(ctx, dest)
  38. if err != nil {
  39. return err
  40. }
  41. conn = rawConn
  42. return nil
  43. })
  44. if err != nil {
  45. log.Warning("Socks|Client: Failed to find an available destination.")
  46. return err
  47. }
  48. defer conn.Close()
  49. conn.SetReusable(false)
  50. request := &protocol.RequestHeader{
  51. Version: socks5Version,
  52. Command: protocol.RequestCommandTCP,
  53. Address: destination.Address,
  54. Port: destination.Port,
  55. }
  56. if destination.Network == net.Network_UDP {
  57. request.Command = protocol.RequestCommandUDP
  58. }
  59. user := server.PickUser()
  60. if user != nil {
  61. request.User = user
  62. }
  63. udpRequest, err := ClientHandshake(request, conn, conn)
  64. if err != nil {
  65. log.Warning("Socks|Client: Failed to establish connection to server: ", err)
  66. return err
  67. }
  68. ctx, cancel := context.WithCancel(ctx)
  69. timer := signal.CancelAfterInactivity(ctx, cancel, 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. log.Info("Socks|Client: Failed to create UDP connection: ", err)
  84. return 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. log.Info("Socks|Client: Connection ends with ", err)
  100. return err
  101. }
  102. runtime.KeepAlive(timer)
  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. }