always.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package inbound
  2. import (
  3. "context"
  4. "v2ray.com/core"
  5. "v2ray.com/core/app/proxyman"
  6. "v2ray.com/core/app/proxyman/mux"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/dice"
  9. "v2ray.com/core/common/net"
  10. "v2ray.com/core/common/serial"
  11. "v2ray.com/core/proxy"
  12. "v2ray.com/core/transport/internet"
  13. )
  14. func getStatCounter(v *core.Instance, tag string) (core.StatCounter, core.StatCounter) {
  15. var uplinkCounter core.StatCounter
  16. var downlinkCounter core.StatCounter
  17. policy := v.PolicyManager()
  18. stats := v.Stats()
  19. if len(tag) > 0 && policy.ForSystem().Stats.InboundUplink {
  20. name := "inbound>>>" + tag + ">>>traffic>>>uplink"
  21. c, _ := core.GetOrRegisterStatCounter(stats, name)
  22. if c != nil {
  23. uplinkCounter = c
  24. }
  25. }
  26. if len(tag) > 0 && policy.ForSystem().Stats.InboundDownlink {
  27. name := "inbound>>>" + tag + ">>>traffic>>>downlink"
  28. c, _ := core.GetOrRegisterStatCounter(stats, name)
  29. if c != nil {
  30. downlinkCounter = c
  31. }
  32. }
  33. return uplinkCounter, downlinkCounter
  34. }
  35. type AlwaysOnInboundHandler struct {
  36. proxy proxy.Inbound
  37. workers []worker
  38. mux *mux.Server
  39. tag string
  40. }
  41. func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*AlwaysOnInboundHandler, error) {
  42. rawProxy, err := common.CreateObject(ctx, proxyConfig)
  43. if err != nil {
  44. return nil, err
  45. }
  46. p, ok := rawProxy.(proxy.Inbound)
  47. if !ok {
  48. return nil, newError("not an inbound proxy.")
  49. }
  50. h := &AlwaysOnInboundHandler{
  51. proxy: p,
  52. mux: mux.NewServer(ctx),
  53. tag: tag,
  54. }
  55. uplinkCounter, downlinkCounter := getStatCounter(core.MustFromContext(ctx), tag)
  56. nl := p.Network()
  57. pr := receiverConfig.PortRange
  58. address := receiverConfig.Listen.AsAddress()
  59. if address == nil {
  60. address = net.AnyIP
  61. }
  62. for port := pr.From; port <= pr.To; port++ {
  63. if nl.HasNetwork(net.Network_TCP) {
  64. newError("creating stream worker on ", address, ":", port).AtDebug().WriteToLog()
  65. mss, err := internet.ToMemoryStreamConfig(receiverConfig.StreamSettings)
  66. if err != nil {
  67. return nil, newError("failed to parse stream config").Base(err).AtWarning()
  68. }
  69. worker := &tcpWorker{
  70. address: address,
  71. port: net.Port(port),
  72. proxy: p,
  73. stream: mss,
  74. recvOrigDest: receiverConfig.ReceiveOriginalDestination,
  75. tag: tag,
  76. dispatcher: h.mux,
  77. sniffingConfig: receiverConfig.GetEffectiveSniffingSettings(),
  78. uplinkCounter: uplinkCounter,
  79. downlinkCounter: downlinkCounter,
  80. }
  81. h.workers = append(h.workers, worker)
  82. }
  83. if nl.HasNetwork(net.Network_UDP) {
  84. worker := &udpWorker{
  85. tag: tag,
  86. proxy: p,
  87. address: address,
  88. port: net.Port(port),
  89. recvOrigDest: receiverConfig.ReceiveOriginalDestination,
  90. dispatcher: h.mux,
  91. uplinkCounter: uplinkCounter,
  92. downlinkCounter: downlinkCounter,
  93. }
  94. h.workers = append(h.workers, worker)
  95. }
  96. }
  97. return h, nil
  98. }
  99. // Start implements common.Runnable.
  100. func (h *AlwaysOnInboundHandler) Start() error {
  101. for _, worker := range h.workers {
  102. if err := worker.Start(); err != nil {
  103. return err
  104. }
  105. }
  106. return nil
  107. }
  108. // Close implements common.Closable.
  109. func (h *AlwaysOnInboundHandler) Close() error {
  110. var errors []interface{}
  111. for _, worker := range h.workers {
  112. if err := worker.Close(); err != nil {
  113. errors = append(errors, err)
  114. }
  115. }
  116. if err := h.mux.Close(); err != nil {
  117. errors = append(errors, err)
  118. }
  119. if len(errors) > 0 {
  120. return newError("failed to close all resources").Base(newError(serial.Concat(errors...)))
  121. }
  122. return nil
  123. }
  124. func (h *AlwaysOnInboundHandler) GetRandomInboundProxy() (interface{}, net.Port, int) {
  125. if len(h.workers) == 0 {
  126. return nil, 0, 0
  127. }
  128. w := h.workers[dice.Roll(len(h.workers))]
  129. return w.Proxy(), w.Port(), 9999
  130. }
  131. func (h *AlwaysOnInboundHandler) Tag() string {
  132. return h.tag
  133. }
  134. func (h *AlwaysOnInboundHandler) GetInbound() proxy.Inbound {
  135. return h.proxy
  136. }