dynamic.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. "v2ray.com/core/transport/internet"
  14. )
  15. type DynamicInboundHandler struct {
  16. tag string
  17. v *core.Instance
  18. proxyConfig interface{}
  19. receiverConfig *proxyman.ReceiverConfig
  20. streamSettings *internet.MemoryStreamConfig
  21. portMutex sync.Mutex
  22. portsInUse map[net.Port]bool
  23. workerMutex sync.RWMutex
  24. worker []worker
  25. lastRefresh time.Time
  26. mux *mux.Server
  27. task *task.Periodic
  28. }
  29. func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*DynamicInboundHandler, error) {
  30. v := core.MustFromContext(ctx)
  31. h := &DynamicInboundHandler{
  32. tag: tag,
  33. proxyConfig: proxyConfig,
  34. receiverConfig: receiverConfig,
  35. portsInUse: make(map[net.Port]bool),
  36. mux: mux.NewServer(ctx),
  37. v: v,
  38. }
  39. mss, err := internet.ToMemoryStreamConfig(receiverConfig.StreamSettings)
  40. if err != nil {
  41. return nil, newError("failed to parse stream settings").Base(err).AtWarning()
  42. }
  43. h.streamSettings = mss
  44. h.task = &task.Periodic{
  45. Interval: time.Minute * time.Duration(h.receiverConfig.AllocationStrategy.GetRefreshValue()),
  46. Execute: h.refresh,
  47. }
  48. return h, nil
  49. }
  50. func (h *DynamicInboundHandler) allocatePort() net.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 := net.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) closeWorkers(workers []worker) {
  66. ports2Del := make([]net.Port, len(workers))
  67. for idx, worker := range workers {
  68. ports2Del[idx] = worker.Port()
  69. if err := worker.Close(); err != nil {
  70. newError("failed to close worker").Base(err).WriteToLog()
  71. }
  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. workers := make([]worker, 0, concurrency)
  84. address := h.receiverConfig.Listen.AsAddress()
  85. if address == nil {
  86. address = net.AnyIP
  87. }
  88. uplinkCounter, downlinkCounter := getStatCounter(h.v, h.tag)
  89. for i := uint32(0); i < concurrency; i++ {
  90. port := h.allocatePort()
  91. rawProxy, err := core.CreateObject(h.v, h.proxyConfig)
  92. if err != nil {
  93. newError("failed to create proxy instance").Base(err).AtWarning().WriteToLog()
  94. continue
  95. }
  96. p := rawProxy.(proxy.Inbound)
  97. nl := p.Network()
  98. if nl.HasNetwork(net.Network_TCP) {
  99. worker := &tcpWorker{
  100. tag: h.tag,
  101. address: address,
  102. port: port,
  103. proxy: p,
  104. stream: h.streamSettings,
  105. recvOrigDest: h.receiverConfig.ReceiveOriginalDestination,
  106. dispatcher: h.mux,
  107. sniffingConfig: h.receiverConfig.GetEffectiveSniffingSettings(),
  108. uplinkCounter: uplinkCounter,
  109. downlinkCounter: downlinkCounter,
  110. }
  111. if err := worker.Start(); err != nil {
  112. newError("failed to create TCP worker").Base(err).AtWarning().WriteToLog()
  113. continue
  114. }
  115. workers = append(workers, worker)
  116. }
  117. if nl.HasNetwork(net.Network_UDP) {
  118. worker := &udpWorker{
  119. tag: h.tag,
  120. proxy: p,
  121. address: address,
  122. port: port,
  123. recvOrigDest: h.receiverConfig.ReceiveOriginalDestination,
  124. dispatcher: h.mux,
  125. uplinkCounter: uplinkCounter,
  126. downlinkCounter: downlinkCounter,
  127. }
  128. if err := worker.Start(); err != nil {
  129. newError("failed to create UDP worker").Base(err).AtWarning().WriteToLog()
  130. continue
  131. }
  132. workers = append(workers, worker)
  133. }
  134. }
  135. h.workerMutex.Lock()
  136. h.worker = workers
  137. h.workerMutex.Unlock()
  138. time.AfterFunc(timeout, func() {
  139. h.closeWorkers(workers)
  140. })
  141. return nil
  142. }
  143. func (h *DynamicInboundHandler) Start() error {
  144. return h.task.Start()
  145. }
  146. func (h *DynamicInboundHandler) Close() error {
  147. return h.task.Close()
  148. }
  149. func (h *DynamicInboundHandler) GetRandomInboundProxy() (interface{}, net.Port, int) {
  150. h.workerMutex.RLock()
  151. defer h.workerMutex.RUnlock()
  152. if len(h.worker) == 0 {
  153. return nil, 0, 0
  154. }
  155. w := h.worker[dice.Roll(len(h.worker))]
  156. expire := h.receiverConfig.AllocationStrategy.GetRefreshValue() - uint32(time.Since(h.lastRefresh)/time.Minute)
  157. return w.Proxy(), w.Port(), int(expire)
  158. }
  159. func (h *DynamicInboundHandler) Tag() string {
  160. return h.tag
  161. }