inbound_detour_dynamic.go 4.0 KB

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