dynamic.go 4.5 KB

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