client.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package socks
  2. import (
  3. "v2ray.com/core/app"
  4. "v2ray.com/core/common/buf"
  5. "v2ray.com/core/common/log"
  6. "v2ray.com/core/common/net"
  7. "v2ray.com/core/common/protocol"
  8. "v2ray.com/core/common/retry"
  9. "v2ray.com/core/common/signal"
  10. "v2ray.com/core/proxy"
  11. "v2ray.com/core/transport/internet"
  12. "v2ray.com/core/transport/ray"
  13. )
  14. type Client struct {
  15. serverPicker protocol.ServerPicker
  16. meta *proxy.OutboundHandlerMeta
  17. }
  18. func NewClient(config *ClientConfig, space app.Space, meta *proxy.OutboundHandlerMeta) (*Client, error) {
  19. serverList := protocol.NewServerList()
  20. for _, rec := range config.Server {
  21. serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
  22. }
  23. client := &Client{
  24. serverPicker: protocol.NewRoundRobinServerPicker(serverList),
  25. meta: meta,
  26. }
  27. return client, nil
  28. }
  29. func (c *Client) Dispatch(destination net.Destination, ray ray.OutboundRay) {
  30. var server *protocol.ServerSpec
  31. var conn internet.Connection
  32. err := retry.ExponentialBackoff(5, 100).On(func() error {
  33. server = c.serverPicker.PickServer()
  34. dest := server.Destination()
  35. rawConn, err := internet.Dial(c.meta.Address, dest, c.meta.GetDialerOptions())
  36. if err != nil {
  37. return err
  38. }
  39. conn = rawConn
  40. return nil
  41. })
  42. if err != nil {
  43. log.Warning("Socks|Client: Failed to find an available destination.")
  44. return
  45. }
  46. defer conn.Close()
  47. conn.SetReusable(false)
  48. request := &protocol.RequestHeader{
  49. Version: socks5Version,
  50. Command: protocol.RequestCommandTCP,
  51. Address: destination.Address,
  52. Port: destination.Port,
  53. }
  54. if destination.Network == net.Network_UDP {
  55. request.Command = protocol.RequestCommandUDP
  56. }
  57. user := server.PickUser()
  58. if user != nil {
  59. request.User = user
  60. }
  61. udpRequest, err := ClientHandshake(request, conn, conn)
  62. if err != nil {
  63. log.Warning("Socks|Client: Failed to establish connection to server: ", err)
  64. return
  65. }
  66. var requestFunc func() error
  67. var responseFunc func() error
  68. if request.Command == protocol.RequestCommandTCP {
  69. requestFunc = func() error {
  70. return buf.PipeUntilEOF(ray.OutboundInput(), buf.NewWriter(conn))
  71. }
  72. responseFunc = func() error {
  73. defer ray.OutboundOutput().Close()
  74. return buf.PipeUntilEOF(buf.NewReader(conn), ray.OutboundOutput())
  75. }
  76. } else if request.Command == protocol.RequestCommandUDP {
  77. udpConn, err := internet.Dial(c.meta.Address, udpRequest.Destination(), c.meta.GetDialerOptions())
  78. if err != nil {
  79. log.Info("Socks|Client: Failed to create UDP connection: ", err)
  80. return
  81. }
  82. defer udpConn.Close()
  83. requestFunc = func() error {
  84. return buf.PipeUntilEOF(ray.OutboundInput(), &UDPWriter{request: request, writer: udpConn})
  85. }
  86. responseFunc = func() error {
  87. defer ray.OutboundOutput().Close()
  88. reader := &UDPReader{reader: net.NewTimeOutReader(16, udpConn)}
  89. return buf.PipeUntilEOF(reader, ray.OutboundOutput())
  90. }
  91. }
  92. requestDone := signal.ExecuteAsync(requestFunc)
  93. responseDone := signal.ExecuteAsync(responseFunc)
  94. if err := signal.ErrorOrFinish2(requestDone, responseDone); err != nil {
  95. log.Info("Socks|Client: Connection ends with ", err)
  96. ray.OutboundInput().CloseError()
  97. ray.OutboundOutput().CloseError()
  98. }
  99. }
  100. type ClientFactory struct{}
  101. func (ClientFactory) Create(space app.Space, rawConfig interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  102. return NewClient(rawConfig.(*ClientConfig), space, meta)
  103. }