dynamic.go 4.8 KB

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