dynamic.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package inbound
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. "v2ray.com/core/app/log"
  7. "v2ray.com/core/app/proxyman"
  8. "v2ray.com/core/app/proxyman/mux"
  9. "v2ray.com/core/common/dice"
  10. "v2ray.com/core/common/errors"
  11. v2net "v2ray.com/core/common/net"
  12. "v2ray.com/core/proxy"
  13. )
  14. type DynamicInboundHandler struct {
  15. tag string
  16. ctx context.Context
  17. cancel context.CancelFunc
  18. proxyConfig interface{}
  19. receiverConfig *proxyman.ReceiverConfig
  20. portMutex sync.Mutex
  21. portsInUse map[v2net.Port]bool
  22. workerMutex sync.RWMutex
  23. worker []worker
  24. lastRefresh time.Time
  25. mux *mux.Server
  26. }
  27. func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*DynamicInboundHandler, error) {
  28. ctx, cancel := context.WithCancel(ctx)
  29. h := &DynamicInboundHandler{
  30. ctx: ctx,
  31. tag: tag,
  32. cancel: cancel,
  33. proxyConfig: proxyConfig,
  34. receiverConfig: receiverConfig,
  35. portsInUse: make(map[v2net.Port]bool),
  36. mux: mux.NewServer(ctx),
  37. }
  38. return h, nil
  39. }
  40. func (h *DynamicInboundHandler) allocatePort() v2net.Port {
  41. from := int(h.receiverConfig.PortRange.From)
  42. delta := int(h.receiverConfig.PortRange.To) - from + 1
  43. h.portMutex.Lock()
  44. defer h.portMutex.Unlock()
  45. for {
  46. r := dice.Roll(delta)
  47. port := v2net.Port(from + r)
  48. _, used := h.portsInUse[port]
  49. if !used {
  50. h.portsInUse[port] = true
  51. return port
  52. }
  53. }
  54. }
  55. func (h *DynamicInboundHandler) waitAnyCloseWorkers(ctx context.Context, cancel context.CancelFunc, workers []worker, duration time.Duration) {
  56. time.Sleep(duration)
  57. cancel()
  58. ports2Del := make([]v2net.Port, len(workers))
  59. for idx, worker := range workers {
  60. ports2Del[idx] = worker.Port()
  61. worker.Close()
  62. }
  63. h.portMutex.Lock()
  64. for _, port := range ports2Del {
  65. delete(h.portsInUse, port)
  66. }
  67. h.portMutex.Unlock()
  68. }
  69. func (h *DynamicInboundHandler) refresh() error {
  70. h.lastRefresh = time.Now()
  71. timeout := time.Minute * time.Duration(h.receiverConfig.AllocationStrategy.GetRefreshValue()) * 2
  72. concurrency := h.receiverConfig.AllocationStrategy.GetConcurrencyValue()
  73. ctx, cancel := context.WithTimeout(h.ctx, timeout)
  74. workers := make([]worker, 0, concurrency)
  75. address := h.receiverConfig.Listen.AsAddress()
  76. if address == nil {
  77. address = v2net.AnyIP
  78. }
  79. for i := uint32(0); i < concurrency; i++ {
  80. port := h.allocatePort()
  81. p, err := proxy.CreateInboundHandler(ctx, h.proxyConfig)
  82. if err != nil {
  83. log.Trace(errors.New("failed to create proxy instance").Base(err).Path("Proxyman", "Inbound", "DynamicInboundHandler").AtWarning())
  84. continue
  85. }
  86. nl := p.Network()
  87. if nl.HasNetwork(v2net.Network_TCP) {
  88. worker := &tcpWorker{
  89. tag: h.tag,
  90. address: address,
  91. port: port,
  92. proxy: p,
  93. stream: h.receiverConfig.StreamSettings,
  94. recvOrigDest: h.receiverConfig.ReceiveOriginalDestination,
  95. dispatcher: h.mux,
  96. }
  97. if err := worker.Start(); err != nil {
  98. log.Trace(errors.New("Proxyman:InboundHandler: Failed to create TCP worker: ", err).AtWarning())
  99. continue
  100. }
  101. workers = append(workers, worker)
  102. }
  103. if nl.HasNetwork(v2net.Network_UDP) {
  104. worker := &udpWorker{
  105. tag: h.tag,
  106. proxy: p,
  107. address: address,
  108. port: port,
  109. recvOrigDest: h.receiverConfig.ReceiveOriginalDestination,
  110. dispatcher: h.mux,
  111. }
  112. if err := worker.Start(); err != nil {
  113. log.Trace(errors.New("Proxyman:InboundHandler: Failed to create UDP worker: ", err).AtWarning())
  114. continue
  115. }
  116. workers = append(workers, worker)
  117. }
  118. }
  119. h.workerMutex.Lock()
  120. h.worker = workers
  121. h.workerMutex.Unlock()
  122. go h.waitAnyCloseWorkers(ctx, cancel, workers, timeout)
  123. return nil
  124. }
  125. func (h *DynamicInboundHandler) monitor() {
  126. for {
  127. select {
  128. case <-h.ctx.Done():
  129. return
  130. case <-time.After(time.Minute * time.Duration(h.receiverConfig.AllocationStrategy.GetRefreshValue())):
  131. h.refresh()
  132. }
  133. }
  134. }
  135. func (h *DynamicInboundHandler) Start() error {
  136. err := h.refresh()
  137. go h.monitor()
  138. return err
  139. }
  140. func (h *DynamicInboundHandler) Close() {
  141. h.cancel()
  142. }
  143. func (h *DynamicInboundHandler) GetRandomInboundProxy() (proxy.Inbound, v2net.Port, int) {
  144. h.workerMutex.RLock()
  145. defer h.workerMutex.RUnlock()
  146. if len(h.worker) == 0 {
  147. return nil, 0, 0
  148. }
  149. w := h.worker[dice.Roll(len(h.worker))]
  150. expire := h.receiverConfig.AllocationStrategy.GetRefreshValue() - uint32(time.Since(h.lastRefresh)/time.Minute)
  151. return w.Proxy(), w.Port(), int(expire)
  152. }