client.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package socks
  2. import (
  3. "context"
  4. "errors"
  5. "v2ray.com/core/common"
  6. "v2ray.com/core/common/buf"
  7. "v2ray.com/core/common/log"
  8. "v2ray.com/core/common/net"
  9. "v2ray.com/core/common/protocol"
  10. "v2ray.com/core/common/retry"
  11. "v2ray.com/core/common/signal"
  12. "v2ray.com/core/proxy"
  13. "v2ray.com/core/transport/internet"
  14. "v2ray.com/core/transport/ray"
  15. )
  16. type Client struct {
  17. serverPicker protocol.ServerPicker
  18. meta *proxy.OutboundHandlerMeta
  19. }
  20. func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
  21. meta := proxy.OutboundMetaFromContext(ctx)
  22. if meta == nil {
  23. return nil, errors.New("Socks|Client: No outbound meta in context.")
  24. }
  25. serverList := protocol.NewServerList()
  26. for _, rec := range config.Server {
  27. serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
  28. }
  29. client := &Client{
  30. serverPicker: protocol.NewRoundRobinServerPicker(serverList),
  31. meta: meta,
  32. }
  33. return client, nil
  34. }
  35. func (c *Client) Dispatch(destination net.Destination, ray ray.OutboundRay) {
  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 := internet.Dial(c.meta.Address, dest, c.meta.GetDialerOptions())
  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
  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
  71. }
  72. var requestFunc func() error
  73. var responseFunc func() error
  74. if request.Command == protocol.RequestCommandTCP {
  75. requestFunc = func() error {
  76. return buf.PipeUntilEOF(ray.OutboundInput(), buf.NewWriter(conn))
  77. }
  78. responseFunc = func() error {
  79. defer ray.OutboundOutput().Close()
  80. return buf.PipeUntilEOF(buf.NewReader(conn), ray.OutboundOutput())
  81. }
  82. } else if request.Command == protocol.RequestCommandUDP {
  83. udpConn, err := internet.Dial(c.meta.Address, udpRequest.Destination(), c.meta.GetDialerOptions())
  84. if err != nil {
  85. log.Info("Socks|Client: Failed to create UDP connection: ", err)
  86. return
  87. }
  88. defer udpConn.Close()
  89. requestFunc = func() error {
  90. return buf.PipeUntilEOF(ray.OutboundInput(), &UDPWriter{request: request, writer: udpConn})
  91. }
  92. responseFunc = func() error {
  93. defer ray.OutboundOutput().Close()
  94. reader := &UDPReader{reader: net.NewTimeOutReader(16, udpConn)}
  95. return buf.PipeUntilEOF(reader, ray.OutboundOutput())
  96. }
  97. }
  98. requestDone := signal.ExecuteAsync(requestFunc)
  99. responseDone := signal.ExecuteAsync(responseFunc)
  100. if err := signal.ErrorOrFinish2(requestDone, responseDone); err != nil {
  101. log.Info("Socks|Client: Connection ends with ", err)
  102. ray.OutboundInput().CloseError()
  103. ray.OutboundOutput().CloseError()
  104. }
  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. }