inbound_detour_dynamic.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. return err
  95. }
  96. err = ich.Start()
  97. if err != nil {
  98. return err
  99. }
  100. this.portsInUse[port] = true
  101. newIchs[idx] = ich
  102. return nil
  103. })
  104. if err != nil {
  105. log.Error("Point: Failed to create inbound connection handler: ", err)
  106. return err
  107. }
  108. }
  109. this.Lock()
  110. this.ichs = newIchs
  111. this.Unlock()
  112. return nil
  113. }
  114. func (this *InboundDetourHandlerDynamic) Start() error {
  115. err := this.refresh()
  116. if err != nil {
  117. log.Error("Point: Failed to refresh dynamic allocations: ", err)
  118. return err
  119. }
  120. go func() {
  121. for {
  122. time.Sleep(time.Duration(this.config.Allocation.Refresh)*time.Minute - 1)
  123. this.RecyleHandles()
  124. err := this.refresh()
  125. if err != nil {
  126. log.Error("Point: Failed to refresh dynamic allocations: ", err)
  127. }
  128. time.Sleep(time.Minute)
  129. }
  130. }()
  131. return nil
  132. }