inbound_detour_dynamic.go 3.5 KB

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