inbound_detour_dynamic.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. ichs []proxy.InboundHandler
  19. ich2Recyle []proxy.InboundHandler
  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. handler.ichs = make([]proxy.InboundHandler, config.Allocation.Concurrency)
  29. // To test configuration
  30. ich, err := proxyrepo.CreateInboundHandler(config.Protocol, space, config.Settings, &proxy.InboundHandlerMeta{
  31. Address: config.ListenOn,
  32. Port: 0,
  33. Tag: config.Tag,
  34. StreamSettings: config.StreamSettings,
  35. AllowPassiveConnection: config.AllowPassiveConnection,
  36. })
  37. if err != nil {
  38. log.Error("Point: Failed to create inbound connection handler: ", err)
  39. return nil, err
  40. }
  41. ich.Close()
  42. return handler, nil
  43. }
  44. func (this *InboundDetourHandlerDynamic) pickUnusedPort() v2net.Port {
  45. delta := int(this.config.PortRange.To) - int(this.config.PortRange.From) + 1
  46. for {
  47. r := dice.Roll(delta)
  48. port := this.config.PortRange.From + v2net.Port(r)
  49. _, used := this.portsInUse[port]
  50. if !used {
  51. return port
  52. }
  53. }
  54. }
  55. func (this *InboundDetourHandlerDynamic) GetConnectionHandler() (proxy.InboundHandler, int) {
  56. this.RLock()
  57. defer this.RUnlock()
  58. ich := this.ichs[dice.Roll(len(this.ichs))]
  59. until := this.config.Allocation.Refresh - int((time.Now().Unix()-this.lastRefresh.Unix())/60/1000)
  60. if until < 0 {
  61. until = 0
  62. }
  63. return ich, int(until)
  64. }
  65. func (this *InboundDetourHandlerDynamic) Close() {
  66. this.Lock()
  67. defer this.Unlock()
  68. for _, ich := range this.ichs {
  69. ich.Close()
  70. }
  71. }
  72. func (this *InboundDetourHandlerDynamic) RecyleHandles() {
  73. if this.ich2Recyle != nil {
  74. for _, ich := range this.ich2Recyle {
  75. if ich == nil {
  76. continue
  77. }
  78. port := ich.Port()
  79. ich.Close()
  80. delete(this.portsInUse, port)
  81. }
  82. this.ich2Recyle = nil
  83. }
  84. }
  85. func (this *InboundDetourHandlerDynamic) refresh() error {
  86. this.lastRefresh = time.Now()
  87. config := this.config
  88. this.ich2Recyle = this.ichs
  89. newIchs := make([]proxy.InboundHandler, config.Allocation.Concurrency)
  90. for idx := range newIchs {
  91. err := retry.Timed(5, 100).On(func() error {
  92. port := this.pickUnusedPort()
  93. ich, err := proxyrepo.CreateInboundHandler(config.Protocol, this.space, config.Settings, &proxy.InboundHandlerMeta{
  94. Address: config.ListenOn, Port: port, Tag: config.Tag, StreamSettings: config.StreamSettings})
  95. if err != nil {
  96. delete(this.portsInUse, port)
  97. return err
  98. }
  99. err = ich.Start()
  100. if err != nil {
  101. delete(this.portsInUse, port)
  102. return err
  103. }
  104. this.portsInUse[port] = true
  105. newIchs[idx] = ich
  106. return nil
  107. })
  108. if err != nil {
  109. log.Error("Point: Failed to create inbound connection handler: ", err)
  110. return err
  111. }
  112. }
  113. this.Lock()
  114. this.ichs = newIchs
  115. this.Unlock()
  116. return nil
  117. }
  118. func (this *InboundDetourHandlerDynamic) Start() error {
  119. err := this.refresh()
  120. if err != nil {
  121. log.Error("Point: Failed to refresh dynamic allocations: ", err)
  122. return err
  123. }
  124. go func() {
  125. for {
  126. time.Sleep(time.Duration(this.config.Allocation.Refresh)*time.Minute - 1)
  127. this.RecyleHandles()
  128. err := this.refresh()
  129. if err != nil {
  130. log.Error("Point: Failed to refresh dynamic allocations: ", err)
  131. }
  132. time.Sleep(time.Minute)
  133. }
  134. }()
  135. return nil
  136. }