client.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package socks
  2. import (
  3. "context"
  4. "errors"
  5. "runtime"
  6. "time"
  7. "v2ray.com/core/app/log"
  8. "v2ray.com/core/common"
  9. "v2ray.com/core/common/buf"
  10. "v2ray.com/core/common/net"
  11. "v2ray.com/core/common/protocol"
  12. "v2ray.com/core/common/retry"
  13. "v2ray.com/core/common/signal"
  14. "v2ray.com/core/proxy"
  15. "v2ray.com/core/transport/internet"
  16. "v2ray.com/core/transport/ray"
  17. )
  18. type Client struct {
  19. serverPicker protocol.ServerPicker
  20. }
  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. func (c *Client) Process(ctx context.Context, ray ray.OutboundRay, dialer proxy.Dialer) error {
  32. destination, ok := proxy.TargetFromContext(ctx)
  33. if !ok {
  34. return errors.New("Socks|Client: Target not specified.")
  35. }
  36. var server *protocol.ServerSpec
  37. var conn internet.Connection
  38. err := retry.ExponentialBackoff(5, 100).On(func() error {
  39. server = c.serverPicker.PickServer()
  40. dest := server.Destination()
  41. rawConn, err := dialer.Dial(ctx, dest)
  42. if err != nil {
  43. return err
  44. }
  45. conn = rawConn
  46. return nil
  47. })
  48. if err != nil {
  49. log.Warning("Socks|Client: Failed to find an available destination.")
  50. return err
  51. }
  52. defer conn.Close()
  53. conn.SetReusable(false)
  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. log.Warning("Socks|Client: Failed to establish connection to server: ", err)
  70. return err
  71. }
  72. ctx, cancel := context.WithCancel(ctx)
  73. timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*2)
  74. var requestFunc func() error
  75. var responseFunc func() error
  76. if request.Command == protocol.RequestCommandTCP {
  77. requestFunc = func() error {
  78. return buf.PipeUntilEOF(timer, ray.OutboundInput(), buf.NewWriter(conn))
  79. }
  80. responseFunc = func() error {
  81. defer ray.OutboundOutput().Close()
  82. return buf.PipeUntilEOF(timer, buf.NewReader(conn), ray.OutboundOutput())
  83. }
  84. } else if request.Command == protocol.RequestCommandUDP {
  85. udpConn, err := dialer.Dial(ctx, udpRequest.Destination())
  86. if err != nil {
  87. log.Info("Socks|Client: Failed to create UDP connection: ", err)
  88. return err
  89. }
  90. defer udpConn.Close()
  91. requestFunc = func() error {
  92. return buf.PipeUntilEOF(timer, ray.OutboundInput(), &UDPWriter{request: request, writer: udpConn})
  93. }
  94. responseFunc = func() error {
  95. defer ray.OutboundOutput().Close()
  96. reader := &UDPReader{reader: udpConn}
  97. return buf.PipeUntilEOF(timer, reader, ray.OutboundOutput())
  98. }
  99. }
  100. requestDone := signal.ExecuteAsync(requestFunc)
  101. responseDone := signal.ExecuteAsync(responseFunc)
  102. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  103. log.Info("Socks|Client: Connection ends with ", err)
  104. return err
  105. }
  106. runtime.KeepAlive(timer)
  107. return nil
  108. }
  109. func init() {
  110. common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  111. return NewClient(ctx, config.(*ClientConfig))
  112. }))
  113. }