inbound_detour_dynamic.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. this.portsInUse[port] = true
  50. return port
  51. }
  52. }
  53. }
  54. func (this *InboundDetourHandlerDynamic) GetConnectionHandler() (proxy.InboundHandler, int) {
  55. this.RLock()
  56. defer this.RUnlock()
  57. ich := this.ichInUse[dice.Roll(len(this.ichInUse))]
  58. until := this.config.Allocation.Refresh - int((time.Now().Unix()-this.lastRefresh.Unix())/60/1000)
  59. if until < 0 {
  60. until = 0
  61. }
  62. return ich, int(until)
  63. }
  64. func (this *InboundDetourHandlerDynamic) Close() {
  65. this.Lock()
  66. defer this.Unlock()
  67. for _, ich := range this.ichInUse {
  68. ich.Close()
  69. }
  70. if this.ich2Recycle != nil {
  71. for _, ich := range this.ich2Recycle {
  72. if ich != nil {
  73. ich.Close()
  74. }
  75. }
  76. }
  77. }
  78. func (this *InboundDetourHandlerDynamic) refresh() error {
  79. this.Lock()
  80. defer this.Unlock()
  81. this.lastRefresh = time.Now()
  82. this.ich2Recycle, this.ichInUse = this.ichInUse, this.ich2Recycle
  83. for _, ich := range this.ichInUse {
  84. delete(this.portsInUse, ich.Port())
  85. ich.Close()
  86. port := this.pickUnusedPort()
  87. err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
  88. err := ich.Listen(port)
  89. if err != nil {
  90. log.Error("Point: Failed to start inbound detour on port ", port, ": ", err)
  91. return err
  92. }
  93. return nil
  94. })
  95. if err != nil {
  96. return err
  97. }
  98. }
  99. return nil
  100. }
  101. func (this *InboundDetourHandlerDynamic) Start() error {
  102. err := this.refresh()
  103. if err != nil {
  104. return err
  105. }
  106. go func() {
  107. for range time.Tick(time.Duration(this.config.Allocation.Refresh) * time.Minute) {
  108. this.refresh()
  109. }
  110. }()
  111. return nil
  112. }