dynamic.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package inbound
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. "v2ray.com/core/app/proxyman"
  7. "v2ray.com/core/common/dice"
  8. "v2ray.com/core/common/log"
  9. v2net "v2ray.com/core/common/net"
  10. "v2ray.com/core/proxy"
  11. )
  12. type workerWithContext struct {
  13. ctx context.Context
  14. cancel context.CancelFunc
  15. worker worker
  16. }
  17. func (w *workerWithContext) Close() {
  18. w.cancel()
  19. w.worker.Close()
  20. }
  21. type DynamicInboundHandler struct {
  22. sync.Mutex
  23. tag string
  24. ctx context.Context
  25. cancel context.CancelFunc
  26. proxyConfig interface{}
  27. receiverConfig *proxyman.ReceiverConfig
  28. portsInUse map[v2net.Port]bool
  29. worker []*workerWithContext
  30. worker2Recycle []*workerWithContext
  31. lastRefresh time.Time
  32. }
  33. func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*DynamicInboundHandler, error) {
  34. ctx, cancel := context.WithCancel(ctx)
  35. h := &DynamicInboundHandler{
  36. ctx: ctx,
  37. tag: tag,
  38. cancel: cancel,
  39. proxyConfig: proxyConfig,
  40. receiverConfig: receiverConfig,
  41. portsInUse: make(map[v2net.Port]bool),
  42. }
  43. return h, nil
  44. }
  45. func (h *DynamicInboundHandler) allocatePort() v2net.Port {
  46. from := int(h.receiverConfig.PortRange.From)
  47. delta := int(h.receiverConfig.PortRange.To) - from + 1
  48. h.Lock()
  49. defer h.Unlock()
  50. for {
  51. r := dice.Roll(delta)
  52. port := v2net.Port(from + r)
  53. _, used := h.portsInUse[port]
  54. if !used {
  55. h.portsInUse[port] = true
  56. return port
  57. }
  58. }
  59. }
  60. func (h *DynamicInboundHandler) refresh() error {
  61. h.lastRefresh = time.Now()
  62. ports2Del := make([]v2net.Port, 0, 16)
  63. for _, worker := range h.worker2Recycle {
  64. worker.Close()
  65. ports2Del = append(ports2Del, worker.worker.Port())
  66. }
  67. h.Lock()
  68. for _, port := range ports2Del {
  69. delete(h.portsInUse, port)
  70. }
  71. h.Unlock()
  72. h.worker2Recycle, h.worker = h.worker, h.worker2Recycle[:0]
  73. address := h.receiverConfig.Listen.AsAddress()
  74. if address == nil {
  75. address = v2net.AnyIP
  76. }
  77. for i := uint32(0); i < h.receiverConfig.AllocationStrategy.GetConcurrencyValue(); i++ {
  78. ctx, cancel := context.WithCancel(h.ctx)
  79. port := h.allocatePort()
  80. p, err := proxy.CreateInboundHandler(ctx, h.proxyConfig)
  81. if err != nil {
  82. log.Warning("Proxyman|DefaultInboundHandler: Failed to create proxy instance: ", err)
  83. continue
  84. }
  85. nl := p.Network()
  86. if nl.HasNetwork(v2net.Network_TCP) {
  87. worker := &tcpWorker{
  88. tag: h.tag,
  89. address: address,
  90. port: port,
  91. proxy: p,
  92. stream: h.receiverConfig.StreamSettings,
  93. recvOrigDest: h.receiverConfig.ReceiveOriginalDestination,
  94. allowPassiveConn: h.receiverConfig.AllowPassiveConnection,
  95. }
  96. if err := worker.Start(); err != nil {
  97. return err
  98. }
  99. h.worker = append(h.worker, &workerWithContext{
  100. ctx: ctx,
  101. cancel: cancel,
  102. worker: worker,
  103. })
  104. }
  105. if nl.HasNetwork(v2net.Network_UDP) {
  106. worker := &udpWorker{
  107. tag: h.tag,
  108. proxy: p,
  109. address: address,
  110. port: port,
  111. recvOrigDest: h.receiverConfig.ReceiveOriginalDestination,
  112. }
  113. if err := worker.Start(); err != nil {
  114. return err
  115. }
  116. h.worker = append(h.worker, &workerWithContext{
  117. ctx: ctx,
  118. cancel: cancel,
  119. worker: worker,
  120. })
  121. }
  122. }
  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. w := h.worker[dice.Roll(len(h.worker))]
  145. expire := h.receiverConfig.AllocationStrategy.GetRefreshValue() - uint32(time.Since(h.lastRefresh)/time.Minute)
  146. return w.worker.Proxy(), w.worker.Port(), int(expire)
  147. }