inbound_detour_dynamic.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 []*InboundConnectionHandlerWithPort
  19. ich2Recycle []*InboundConnectionHandlerWithPort
  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([]*InboundConnectionHandlerWithPort, ichCount*2)
  30. for idx, _ := range ichArray {
  31. //port := handler.pickUnusedPort()
  32. ich, err := proxyrepo.CreateInboundConnectionHandler(config.Protocol, space, config.Settings)
  33. if err != nil {
  34. log.Error("Point: Failed to create inbound connection handler: ", err)
  35. return nil, err
  36. }
  37. ichArray[idx] = &InboundConnectionHandlerWithPort{
  38. port: 0,
  39. handler: ich,
  40. }
  41. }
  42. handler.ichInUse = ichArray[:ichCount]
  43. handler.ich2Recycle = ichArray[ichCount:]
  44. return handler, nil
  45. }
  46. func (this *InboundDetourHandlerDynamic) pickUnusedPort() v2net.Port {
  47. delta := int(this.config.PortRange.To) - int(this.config.PortRange.From) + 1
  48. for {
  49. r := dice.Roll(delta)
  50. port := this.config.PortRange.From + v2net.Port(r)
  51. _, used := this.portsInUse[port]
  52. if !used {
  53. this.portsInUse[port] = true
  54. return port
  55. }
  56. }
  57. }
  58. func (this *InboundDetourHandlerDynamic) GetConnectionHandler() (proxy.InboundConnectionHandler, int) {
  59. this.RLock()
  60. defer this.RUnlock()
  61. ich := this.ichInUse[dice.Roll(len(this.ichInUse))]
  62. until := this.config.Allocation.Refresh - int((time.Now().Unix()-this.lastRefresh.Unix())/60/1000)
  63. if until < 0 {
  64. until = 0
  65. }
  66. return ich.handler, int(until)
  67. }
  68. func (this *InboundDetourHandlerDynamic) Close() {
  69. this.Lock()
  70. defer this.Unlock()
  71. for _, ich := range this.ichInUse {
  72. ich.handler.Close()
  73. }
  74. if this.ich2Recycle != nil {
  75. for _, ich := range this.ich2Recycle {
  76. if ich != nil && ich.handler != nil {
  77. ich.handler.Close()
  78. }
  79. }
  80. }
  81. }
  82. func (this *InboundDetourHandlerDynamic) refresh() error {
  83. this.Lock()
  84. defer this.Unlock()
  85. this.lastRefresh = time.Now()
  86. this.ich2Recycle, this.ichInUse = this.ichInUse, this.ich2Recycle
  87. for _, ich := range this.ichInUse {
  88. ich.port = this.pickUnusedPort()
  89. err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
  90. err := ich.handler.Listen(ich.port)
  91. if err != nil {
  92. log.Error("Point: Failed to start inbound detour on port ", ich.port, ": ", err)
  93. return err
  94. }
  95. return nil
  96. })
  97. if err != nil {
  98. return err
  99. }
  100. }
  101. return nil
  102. }
  103. func (this *InboundDetourHandlerDynamic) Start() error {
  104. err := this.refresh()
  105. if err != nil {
  106. return err
  107. }
  108. go func() {
  109. for range time.Tick(time.Duration(this.config.Allocation.Refresh) * time.Minute) {
  110. this.refresh()
  111. <-time.After(time.Minute)
  112. for _, ich := range this.ich2Recycle {
  113. if ich != nil {
  114. ich.handler.Close()
  115. delete(this.portsInUse, ich.port)
  116. }
  117. }
  118. }
  119. }()
  120. return nil
  121. }