inbound_detour_dynamic.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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) Start() error {
  83. this.Lock()
  84. defer this.Unlock()
  85. this.ich2Recycle, this.ichInUse = this.ichInUse, this.ich2Recycle
  86. time.AfterFunc(time.Minute, func() {
  87. this.Lock()
  88. defer this.Unlock()
  89. for _, ich := range this.ich2Recycle {
  90. if ich != nil {
  91. ich.handler.Close()
  92. delete(this.portsInUse, ich.port)
  93. }
  94. }
  95. })
  96. this.lastRefresh = time.Now()
  97. time.AfterFunc(time.Duration(this.config.Allocation.Refresh)*time.Minute, func() {
  98. this.Start()
  99. })
  100. for _, ich := range this.ichInUse {
  101. port := this.pickUnusedPort()
  102. ich.port = port
  103. err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
  104. err := ich.handler.Listen(ich.port)
  105. if err != nil {
  106. log.Error("Point: Failed to start inbound detour on port ", ich.port, ": ", err)
  107. return err
  108. }
  109. return nil
  110. })
  111. if err != nil {
  112. return err
  113. }
  114. }
  115. return nil
  116. }