inbound_detour_dynamic.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package point
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/v2ray/v2ray-core/app"
  6. "github.com/v2ray/v2ray-core/common/dice"
  7. "github.com/v2ray/v2ray-core/common/log"
  8. v2net "github.com/v2ray/v2ray-core/common/net"
  9. "github.com/v2ray/v2ray-core/common/retry"
  10. "github.com/v2ray/v2ray-core/proxy"
  11. proxyrepo "github.com/v2ray/v2ray-core/proxy/repo"
  12. )
  13. type InboundDetourHandlerDynamic struct {
  14. sync.RWMutex
  15. space app.Space
  16. config *InboundDetourConfig
  17. portsInUse map[v2net.Port]bool
  18. ichInUse []proxy.InboundHandler
  19. ich2Recycle []proxy.InboundHandler
  20. lastRefresh time.Time
  21. }
  22. func NewInboundDetourHandlerDynamic(space app.Space, config *InboundDetourConfig) (*InboundDetourHandlerDynamic, error) {
  23. handler := &InboundDetourHandlerDynamic{
  24. space: space,
  25. config: config,
  26. portsInUse: make(map[v2net.Port]bool),
  27. }
  28. ichCount := config.Allocation.Concurrency
  29. ichArray := make([]proxy.InboundHandler, ichCount*2)
  30. for idx := range ichArray {
  31. ich, err := proxyrepo.CreateInboundHandler(config.Protocol, space, config.Settings)
  32. if err != nil {
  33. log.Error("Point: Failed to create inbound connection handler: ", err)
  34. return nil, err
  35. }
  36. ichArray[idx] = ich
  37. }
  38. handler.ichInUse = ichArray[:ichCount]
  39. handler.ich2Recycle = ichArray[ichCount:]
  40. return handler, nil
  41. }
  42. func (this *InboundDetourHandlerDynamic) pickUnusedPort() v2net.Port {
  43. delta := int(this.config.PortRange.To) - int(this.config.PortRange.From) + 1
  44. for {
  45. r := dice.Roll(delta)
  46. port := this.config.PortRange.From + v2net.Port(r)
  47. _, used := this.portsInUse[port]
  48. if !used {
  49. return port
  50. }
  51. }
  52. }
  53. func (this *InboundDetourHandlerDynamic) GetConnectionHandler() (proxy.InboundHandler, int) {
  54. this.RLock()
  55. defer this.RUnlock()
  56. ich := this.ichInUse[dice.Roll(len(this.ichInUse))]
  57. until := this.config.Allocation.Refresh - int((time.Now().Unix()-this.lastRefresh.Unix())/60/1000)
  58. if until < 0 {
  59. until = 0
  60. }
  61. return ich, int(until)
  62. }
  63. func (this *InboundDetourHandlerDynamic) Close() {
  64. this.Lock()
  65. defer this.Unlock()
  66. for _, ich := range this.ichInUse {
  67. ich.Close()
  68. }
  69. if this.ich2Recycle != nil {
  70. for _, ich := range this.ich2Recycle {
  71. if ich != nil {
  72. ich.Close()
  73. }
  74. }
  75. }
  76. }
  77. func (this *InboundDetourHandlerDynamic) refresh() error {
  78. this.lastRefresh = time.Now()
  79. for _, ich := range this.ich2Recycle {
  80. port2Delete := ich.Port()
  81. ich.Close()
  82. err := retry.Timed(100 /* times */, 1000 /* ms */).On(func() error {
  83. port := this.pickUnusedPort()
  84. err := ich.Listen(this.config.ListenOn, port)
  85. if err != nil {
  86. log.Error("Point: Failed to start inbound detour on port ", port, ": ", err)
  87. return err
  88. }
  89. this.portsInUse[port] = true
  90. return nil
  91. })
  92. if err != nil {
  93. continue
  94. }
  95. delete(this.portsInUse, port2Delete)
  96. }
  97. this.Lock()
  98. this.ich2Recycle, this.ichInUse = this.ichInUse, this.ich2Recycle
  99. this.Unlock()
  100. return nil
  101. }
  102. func (this *InboundDetourHandlerDynamic) Start() error {
  103. err := this.refresh()
  104. if err != nil {
  105. return err
  106. }
  107. go func() {
  108. for range time.Tick(time.Duration(this.config.Allocation.Refresh) * time.Minute) {
  109. this.refresh()
  110. }
  111. }()
  112. return nil
  113. }