client.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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("target not specified.").Path("Proxy", "Socks", "Client")
  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("Proxy", "Socks", "Client")
  49. }
  50. defer conn.Close()
  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. return errors.New("failed to establish connection to server").AtWarning().Base(err).Path("Proxy", "Socks", "Client")
  67. }
  68. ctx, timer := signal.CancelAfterInactivity(ctx, time.Minute*2)
  69. var requestFunc func() error
  70. var responseFunc func() error
  71. if request.Command == protocol.RequestCommandTCP {
  72. requestFunc = func() error {
  73. return buf.PipeUntilEOF(timer, ray.OutboundInput(), buf.NewWriter(conn))
  74. }
  75. responseFunc = func() error {
  76. defer ray.OutboundOutput().Close()
  77. return buf.PipeUntilEOF(timer, buf.NewReader(conn), ray.OutboundOutput())
  78. }
  79. } else if request.Command == protocol.RequestCommandUDP {
  80. udpConn, err := dialer.Dial(ctx, udpRequest.Destination())
  81. if err != nil {
  82. return errors.New("failed to create UDP connection").Base(err).Path("Proxy", "Socks", "Client")
  83. }
  84. defer udpConn.Close()
  85. requestFunc = func() error {
  86. return buf.PipeUntilEOF(timer, ray.OutboundInput(), &UDPWriter{request: request, writer: udpConn})
  87. }
  88. responseFunc = func() error {
  89. defer ray.OutboundOutput().Close()
  90. reader := &UDPReader{reader: udpConn}
  91. return buf.PipeUntilEOF(timer, reader, ray.OutboundOutput())
  92. }
  93. }
  94. requestDone := signal.ExecuteAsync(requestFunc)
  95. responseDone := signal.ExecuteAsync(responseFunc)
  96. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  97. return errors.New("connection ends").Base(err).Path("Proxy", "Socks", "Client")
  98. }
  99. runtime.KeepAlive(timer)
  100. return nil
  101. }
  102. func init() {
  103. common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  104. return NewClient(ctx, config.(*ClientConfig))
  105. }))
  106. }