inbound_detour_dynamic.go 3.4 KB

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