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/proxy"
  10. proxyrepo "github.com/v2ray/v2ray-core/proxy/repo"
  11. )
  12. type InboundDetourHandlerDynamic struct {
  13. sync.RWMutex
  14. space app.Space
  15. config *InboundDetourConfig
  16. portsInUse map[v2net.Port]bool
  17. ichs []proxy.InboundHandler
  18. ich2Recyle []proxy.InboundHandler
  19. lastRefresh time.Time
  20. }
  21. func NewInboundDetourHandlerDynamic(space app.Space, config *InboundDetourConfig) (*InboundDetourHandlerDynamic, error) {
  22. handler := &InboundDetourHandlerDynamic{
  23. space: space,
  24. config: config,
  25. portsInUse: make(map[v2net.Port]bool),
  26. }
  27. handler.ichs = make([]proxy.InboundHandler, config.Allocation.Concurrency)
  28. // To test configuration
  29. ich, err := proxyrepo.CreateInboundHandler(config.Protocol, space, config.Settings, &proxy.InboundHandlerMeta{
  30. Address: config.ListenOn,
  31. Port: 0,
  32. Tag: config.Tag})
  33. if err != nil {
  34. log.Error("Point: Failed to create inbound connection handler: ", err)
  35. return nil, err
  36. }
  37. ich.Close()
  38. return handler, nil
  39. }
  40. func (this *InboundDetourHandlerDynamic) pickUnusedPort() v2net.Port {
  41. delta := int(this.config.PortRange.To) - int(this.config.PortRange.From) + 1
  42. for {
  43. r := dice.Roll(delta)
  44. port := this.config.PortRange.From + v2net.Port(r)
  45. _, used := this.portsInUse[port]
  46. if !used {
  47. return port
  48. }
  49. }
  50. }
  51. func (this *InboundDetourHandlerDynamic) GetConnectionHandler() (proxy.InboundHandler, int) {
  52. this.RLock()
  53. defer this.RUnlock()
  54. ich := this.ichs[dice.Roll(len(this.ichs))]
  55. until := this.config.Allocation.Refresh - int((time.Now().Unix()-this.lastRefresh.Unix())/60/1000)
  56. if until < 0 {
  57. until = 0
  58. }
  59. return ich, int(until)
  60. }
  61. func (this *InboundDetourHandlerDynamic) Close() {
  62. this.Lock()
  63. defer this.Unlock()
  64. for _, ich := range this.ichs {
  65. ich.Close()
  66. }
  67. }
  68. func (this *InboundDetourHandlerDynamic) RecyleHandles() {
  69. if this.ich2Recyle != nil {
  70. for _, ich := range this.ich2Recyle {
  71. if ich == nil {
  72. continue
  73. }
  74. port := ich.Port()
  75. ich.Close()
  76. delete(this.portsInUse, port)
  77. }
  78. this.ich2Recyle = nil
  79. }
  80. }
  81. func (this *InboundDetourHandlerDynamic) refresh() error {
  82. this.lastRefresh = time.Now()
  83. config := this.config
  84. this.ich2Recyle = this.ichs
  85. newIchs := make([]proxy.InboundHandler, config.Allocation.Concurrency)
  86. for idx, _ := range newIchs {
  87. port := this.pickUnusedPort()
  88. ich, err := proxyrepo.CreateInboundHandler(config.Protocol, this.space, config.Settings, &proxy.InboundHandlerMeta{
  89. Address: config.ListenOn, Port: port, Tag: config.Tag})
  90. if err != nil {
  91. log.Error("Point: Failed to create inbound connection handler: ", err)
  92. return err
  93. }
  94. err = ich.Start()
  95. if err != nil {
  96. log.Error("Point: Failed to start inbound connection handler: ", err)
  97. return err
  98. }
  99. this.portsInUse[port] = true
  100. newIchs[idx] = ich
  101. }
  102. this.Lock()
  103. this.ichs = newIchs
  104. this.Unlock()
  105. return nil
  106. }
  107. func (this *InboundDetourHandlerDynamic) Start() error {
  108. err := this.refresh()
  109. if err != nil {
  110. log.Error("Point: Failed to refresh dynamic allocations: ", err)
  111. return err
  112. }
  113. go func() {
  114. for {
  115. time.Sleep(time.Duration(this.config.Allocation.Refresh)*time.Minute - 1)
  116. this.RecyleHandles()
  117. err := this.refresh()
  118. if err != nil {
  119. log.Error("Point: Failed to refresh dynamic allocations: ", err)
  120. }
  121. }
  122. }()
  123. return nil
  124. }