inbound_detour_dynamic.go 3.7 KB

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