inbound_detour_dynamic.go 3.6 KB

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