inbound_detour_dynamic.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package core
  2. import (
  3. "sync"
  4. "time"
  5. "v2ray.com/core/app"
  6. "v2ray.com/core/common/dice"
  7. "v2ray.com/core/common/log"
  8. v2net "v2ray.com/core/common/net"
  9. "v2ray.com/core/common/retry"
  10. "v2ray.com/core/proxy"
  11. )
  12. type InboundDetourHandlerDynamic struct {
  13. sync.RWMutex
  14. space app.Space
  15. config *InboundConnectionConfig
  16. portsInUse map[v2net.Port]bool
  17. ichs []proxy.InboundHandler
  18. ich2Recyle []proxy.InboundHandler
  19. lastRefresh time.Time
  20. }
  21. func NewInboundDetourHandlerDynamic(space app.Space, config *InboundConnectionConfig) (*InboundDetourHandlerDynamic, error) {
  22. handler := &InboundDetourHandlerDynamic{
  23. space: space,
  24. config: config,
  25. portsInUse: make(map[v2net.Port]bool),
  26. }
  27. handler.ichs = make([]proxy.InboundHandler, config.GetAllocationStrategyValue().Concurrency.GetValue())
  28. // To test configuration
  29. ichConfig, err := config.GetTypedSettings()
  30. if err != nil {
  31. return nil, err
  32. }
  33. ich, err := proxy.CreateInboundHandler(config.Settings.Type, space, ichConfig, &proxy.InboundHandlerMeta{
  34. Address: config.GetListenOnValue(),
  35. Port: 0,
  36. Tag: config.Tag,
  37. StreamSettings: config.StreamSettings,
  38. AllowPassiveConnection: config.AllowPassiveConnection,
  39. })
  40. if err != nil {
  41. log.Error("Point: Failed to create inbound connection handler: ", err)
  42. return nil, err
  43. }
  44. ich.Close()
  45. return handler, nil
  46. }
  47. func (v *InboundDetourHandlerDynamic) pickUnusedPort() v2net.Port {
  48. delta := int(v.config.PortRange.To) - int(v.config.PortRange.From) + 1
  49. for {
  50. r := dice.Roll(delta)
  51. port := v.config.PortRange.FromPort() + v2net.Port(r)
  52. _, used := v.portsInUse[port]
  53. if !used {
  54. return port
  55. }
  56. }
  57. }
  58. func (v *InboundDetourHandlerDynamic) GetConnectionHandler() (proxy.InboundHandler, int) {
  59. v.RLock()
  60. defer v.RUnlock()
  61. ich := v.ichs[dice.Roll(len(v.ichs))]
  62. until := int(v.config.GetAllocationStrategyValue().Refresh.GetValue()) - int((time.Now().Unix()-v.lastRefresh.Unix())/60/1000)
  63. if until < 0 {
  64. until = 0
  65. }
  66. return ich, int(until)
  67. }
  68. func (v *InboundDetourHandlerDynamic) Close() {
  69. v.Lock()
  70. defer v.Unlock()
  71. for _, ich := range v.ichs {
  72. ich.Close()
  73. }
  74. }
  75. func (v *InboundDetourHandlerDynamic) RecyleHandles() {
  76. if v.ich2Recyle != nil {
  77. for _, ich := range v.ich2Recyle {
  78. if ich == nil {
  79. continue
  80. }
  81. port := ich.Port()
  82. ich.Close()
  83. delete(v.portsInUse, port)
  84. }
  85. v.ich2Recyle = nil
  86. }
  87. }
  88. func (v *InboundDetourHandlerDynamic) refresh() error {
  89. v.lastRefresh = time.Now()
  90. config := v.config
  91. v.ich2Recyle = v.ichs
  92. newIchs := make([]proxy.InboundHandler, config.GetAllocationStrategyValue().Concurrency.GetValue())
  93. for idx := range newIchs {
  94. err := retry.Timed(5, 100).On(func() error {
  95. port := v.pickUnusedPort()
  96. ichConfig, _ := config.GetTypedSettings()
  97. ich, err := proxy.CreateInboundHandler(config.Settings.Type, v.space, ichConfig, &proxy.InboundHandlerMeta{
  98. Address: config.GetListenOnValue(), Port: port, Tag: config.Tag, StreamSettings: config.StreamSettings})
  99. if err != nil {
  100. delete(v.portsInUse, port)
  101. return err
  102. }
  103. err = ich.Start()
  104. if err != nil {
  105. delete(v.portsInUse, port)
  106. return err
  107. }
  108. v.portsInUse[port] = true
  109. newIchs[idx] = ich
  110. return nil
  111. })
  112. if err != nil {
  113. log.Error("Point: Failed to create inbound connection handler: ", err)
  114. return err
  115. }
  116. }
  117. v.Lock()
  118. v.ichs = newIchs
  119. v.Unlock()
  120. return nil
  121. }
  122. func (v *InboundDetourHandlerDynamic) Start() error {
  123. err := v.refresh()
  124. if err != nil {
  125. log.Error("Point: Failed to refresh dynamic allocations: ", err)
  126. return err
  127. }
  128. go func() {
  129. for {
  130. time.Sleep(time.Duration(v.config.GetAllocationStrategyValue().Refresh.GetValue())*time.Minute - 1)
  131. v.RecyleHandles()
  132. err := v.refresh()
  133. if err != nil {
  134. log.Error("Point: Failed to refresh dynamic allocations: ", err)
  135. }
  136. time.Sleep(time.Minute)
  137. }
  138. }()
  139. return nil
  140. }