dynamic.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package inbound
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. "github.com/xtls/xray-core/app/proxyman"
  7. "github.com/xtls/xray-core/common/dice"
  8. "github.com/xtls/xray-core/common/mux"
  9. "github.com/xtls/xray-core/common/net"
  10. "github.com/xtls/xray-core/common/task"
  11. "github.com/xtls/xray-core/core"
  12. "github.com/xtls/xray-core/proxy"
  13. "github.com/xtls/xray-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. ctx context.Context
  29. }
  30. func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*DynamicInboundHandler, error) {
  31. v := core.MustFromContext(ctx)
  32. h := &DynamicInboundHandler{
  33. tag: tag,
  34. proxyConfig: proxyConfig,
  35. receiverConfig: receiverConfig,
  36. portsInUse: make(map[net.Port]bool),
  37. mux: mux.NewServer(ctx),
  38. v: v,
  39. ctx: ctx,
  40. }
  41. mss, err := internet.ToMemoryStreamConfig(receiverConfig.StreamSettings)
  42. if err != nil {
  43. return nil, newError("failed to parse stream settings").Base(err).AtWarning()
  44. }
  45. if receiverConfig.ReceiveOriginalDestination {
  46. if mss.SocketSettings == nil {
  47. mss.SocketSettings = &internet.SocketConfig{}
  48. }
  49. if mss.SocketSettings.Tproxy == internet.SocketConfig_Off {
  50. mss.SocketSettings.Tproxy = internet.SocketConfig_Redirect
  51. }
  52. mss.SocketSettings.ReceiveOriginalDestAddress = true
  53. }
  54. h.streamSettings = mss
  55. h.task = &task.Periodic{
  56. Interval: time.Minute * time.Duration(h.receiverConfig.AllocationStrategy.GetRefreshValue()),
  57. Execute: h.refresh,
  58. }
  59. return h, nil
  60. }
  61. func (h *DynamicInboundHandler) allocatePort() net.Port {
  62. allPorts := []int32{}
  63. for _, pr := range h.receiverConfig.PortList.Range {
  64. for i := pr.From; i <= pr.To; i++ {
  65. allPorts = append(allPorts, int32(i))
  66. }
  67. }
  68. h.portMutex.Lock()
  69. defer h.portMutex.Unlock()
  70. for {
  71. r := dice.Roll(len(allPorts))
  72. port := net.Port(allPorts[r])
  73. _, used := h.portsInUse[port]
  74. if !used {
  75. h.portsInUse[port] = true
  76. return port
  77. }
  78. }
  79. }
  80. func (h *DynamicInboundHandler) closeWorkers(workers []worker) {
  81. ports2Del := make([]net.Port, len(workers))
  82. for idx, worker := range workers {
  83. ports2Del[idx] = worker.Port()
  84. if err := worker.Close(); err != nil {
  85. newError("failed to close worker").Base(err).WriteToLog()
  86. }
  87. }
  88. h.portMutex.Lock()
  89. for _, port := range ports2Del {
  90. delete(h.portsInUse, port)
  91. }
  92. h.portMutex.Unlock()
  93. }
  94. func (h *DynamicInboundHandler) refresh() error {
  95. h.lastRefresh = time.Now()
  96. timeout := time.Minute * time.Duration(h.receiverConfig.AllocationStrategy.GetRefreshValue()) * 2
  97. concurrency := h.receiverConfig.AllocationStrategy.GetConcurrencyValue()
  98. workers := make([]worker, 0, concurrency)
  99. address := h.receiverConfig.Listen.AsAddress()
  100. if address == nil {
  101. address = net.AnyIP
  102. }
  103. uplinkCounter, downlinkCounter := getStatCounter(h.v, h.tag)
  104. for i := uint32(0); i < concurrency; i++ {
  105. port := h.allocatePort()
  106. rawProxy, err := core.CreateObject(h.v, h.proxyConfig)
  107. if err != nil {
  108. newError("failed to create proxy instance").Base(err).AtWarning().WriteToLog()
  109. continue
  110. }
  111. p := rawProxy.(proxy.Inbound)
  112. nl := p.Network()
  113. if net.HasNetwork(nl, net.Network_TCP) {
  114. worker := &tcpWorker{
  115. tag: h.tag,
  116. address: address,
  117. port: port,
  118. proxy: p,
  119. stream: h.streamSettings,
  120. recvOrigDest: h.receiverConfig.ReceiveOriginalDestination,
  121. dispatcher: h.mux,
  122. sniffingConfig: h.receiverConfig.GetEffectiveSniffingSettings(),
  123. uplinkCounter: uplinkCounter,
  124. downlinkCounter: downlinkCounter,
  125. ctx: h.ctx,
  126. }
  127. if err := worker.Start(); err != nil {
  128. newError("failed to create TCP worker").Base(err).AtWarning().WriteToLog()
  129. continue
  130. }
  131. workers = append(workers, worker)
  132. }
  133. if net.HasNetwork(nl, net.Network_UDP) {
  134. worker := &udpWorker{
  135. tag: h.tag,
  136. proxy: p,
  137. address: address,
  138. port: port,
  139. dispatcher: h.mux,
  140. sniffingConfig: h.receiverConfig.GetEffectiveSniffingSettings(),
  141. uplinkCounter: uplinkCounter,
  142. downlinkCounter: downlinkCounter,
  143. stream: h.streamSettings,
  144. ctx: h.ctx,
  145. }
  146. if err := worker.Start(); err != nil {
  147. newError("failed to create UDP worker").Base(err).AtWarning().WriteToLog()
  148. continue
  149. }
  150. workers = append(workers, worker)
  151. }
  152. }
  153. h.workerMutex.Lock()
  154. h.worker = workers
  155. h.workerMutex.Unlock()
  156. time.AfterFunc(timeout, func() {
  157. h.closeWorkers(workers)
  158. })
  159. return nil
  160. }
  161. func (h *DynamicInboundHandler) Start() error {
  162. return h.task.Start()
  163. }
  164. func (h *DynamicInboundHandler) Close() error {
  165. return h.task.Close()
  166. }
  167. func (h *DynamicInboundHandler) GetRandomInboundProxy() (interface{}, net.Port, int) {
  168. h.workerMutex.RLock()
  169. defer h.workerMutex.RUnlock()
  170. if len(h.worker) == 0 {
  171. return nil, 0, 0
  172. }
  173. w := h.worker[dice.Roll(len(h.worker))]
  174. expire := h.receiverConfig.AllocationStrategy.GetRefreshValue() - uint32(time.Since(h.lastRefresh)/time.Minute)
  175. return w.Proxy(), w.Port(), int(expire)
  176. }
  177. func (h *DynamicInboundHandler) Tag() string {
  178. return h.tag
  179. }