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