client.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/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. }
  19. func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
  20. serverList := protocol.NewServerList()
  21. for _, rec := range config.Server {
  22. serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
  23. }
  24. client := &Client{
  25. serverPicker: protocol.NewRoundRobinServerPicker(serverList),
  26. }
  27. return client, nil
  28. }
  29. func (c *Client) Process(ctx context.Context, ray ray.OutboundRay, dialer proxy.Dialer) error {
  30. destination, ok := proxy.TargetFromContext(ctx)
  31. if !ok {
  32. return newError("target not specified.")
  33. }
  34. var server *protocol.ServerSpec
  35. var conn internet.Connection
  36. err := retry.ExponentialBackoff(5, 100).On(func() error {
  37. server = c.serverPicker.PickServer()
  38. dest := server.Destination()
  39. rawConn, err := dialer.Dial(ctx, dest)
  40. if err != nil {
  41. return err
  42. }
  43. conn = rawConn
  44. return nil
  45. })
  46. if err != nil {
  47. return newError("failed to find an available destination").Base(err)
  48. }
  49. defer conn.Close()
  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. return newError("failed to establish connection to server").AtWarning().Base(err)
  66. }
  67. ctx, timer := signal.CancelAfterInactivity(ctx, time.Minute*2)
  68. var requestFunc func() error
  69. var responseFunc func() error
  70. if request.Command == protocol.RequestCommandTCP {
  71. requestFunc = func() error {
  72. return buf.PipeUntilEOF(timer, ray.OutboundInput(), buf.NewWriter(conn))
  73. }
  74. responseFunc = func() error {
  75. defer ray.OutboundOutput().Close()
  76. return buf.PipeUntilEOF(timer, buf.NewReader(conn), ray.OutboundOutput())
  77. }
  78. } else if request.Command == protocol.RequestCommandUDP {
  79. udpConn, err := dialer.Dial(ctx, udpRequest.Destination())
  80. if err != nil {
  81. return newError("failed to create UDP connection").Base(err)
  82. }
  83. defer udpConn.Close()
  84. requestFunc = func() error {
  85. return buf.PipeUntilEOF(timer, ray.OutboundInput(), &UDPWriter{request: request, writer: udpConn})
  86. }
  87. responseFunc = func() error {
  88. defer ray.OutboundOutput().Close()
  89. reader := &UDPReader{reader: udpConn}
  90. return buf.PipeUntilEOF(timer, reader, ray.OutboundOutput())
  91. }
  92. }
  93. requestDone := signal.ExecuteAsync(requestFunc)
  94. responseDone := signal.ExecuteAsync(responseFunc)
  95. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  96. return newError("connection ends").Base(err)
  97. }
  98. runtime.KeepAlive(timer)
  99. return nil
  100. }
  101. func init() {
  102. common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  103. return NewClient(ctx, config.(*ClientConfig))
  104. }))
  105. }