manager.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package inbound
  2. import (
  3. "context"
  4. "os"
  5. "sync"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/common/taskmonitor"
  8. C "github.com/sagernet/sing-box/constant"
  9. "github.com/sagernet/sing-box/log"
  10. "github.com/sagernet/sing/common"
  11. E "github.com/sagernet/sing/common/exceptions"
  12. )
  13. var _ adapter.InboundManager = (*Manager)(nil)
  14. type Manager struct {
  15. logger log.ContextLogger
  16. registry adapter.InboundRegistry
  17. endpoint adapter.EndpointManager
  18. access sync.Mutex
  19. started bool
  20. stage adapter.StartStage
  21. inbounds []adapter.Inbound
  22. inboundByTag map[string]adapter.Inbound
  23. }
  24. func NewManager(logger log.ContextLogger, registry adapter.InboundRegistry, endpoint adapter.EndpointManager) *Manager {
  25. return &Manager{
  26. logger: logger,
  27. registry: registry,
  28. endpoint: endpoint,
  29. inboundByTag: make(map[string]adapter.Inbound),
  30. }
  31. }
  32. func (m *Manager) Start(stage adapter.StartStage) error {
  33. m.access.Lock()
  34. if m.started && m.stage >= stage {
  35. panic("already started")
  36. }
  37. m.started = true
  38. m.stage = stage
  39. inbounds := m.inbounds
  40. m.access.Unlock()
  41. for _, inbound := range inbounds {
  42. err := adapter.LegacyStart(inbound, stage)
  43. if err != nil {
  44. return E.Cause(err, stage, " inbound/", inbound.Type(), "[", inbound.Tag(), "]")
  45. }
  46. }
  47. return nil
  48. }
  49. func (m *Manager) Close() error {
  50. m.access.Lock()
  51. defer m.access.Unlock()
  52. if !m.started {
  53. return nil
  54. }
  55. m.started = false
  56. inbounds := m.inbounds
  57. m.inbounds = nil
  58. monitor := taskmonitor.New(m.logger, C.StopTimeout)
  59. var err error
  60. for _, inbound := range inbounds {
  61. monitor.Start("close inbound/", inbound.Type(), "[", inbound.Tag(), "]")
  62. err = E.Append(err, inbound.Close(), func(err error) error {
  63. return E.Cause(err, "close inbound/", inbound.Type(), "[", inbound.Tag(), "]")
  64. })
  65. monitor.Finish()
  66. }
  67. return nil
  68. }
  69. func (m *Manager) Inbounds() []adapter.Inbound {
  70. m.access.Lock()
  71. defer m.access.Unlock()
  72. return m.inbounds
  73. }
  74. func (m *Manager) Get(tag string) (adapter.Inbound, bool) {
  75. m.access.Lock()
  76. inbound, found := m.inboundByTag[tag]
  77. m.access.Unlock()
  78. if found {
  79. return inbound, true
  80. }
  81. return m.endpoint.Get(tag)
  82. }
  83. func (m *Manager) Remove(tag string) error {
  84. m.access.Lock()
  85. inbound, found := m.inboundByTag[tag]
  86. if !found {
  87. m.access.Unlock()
  88. return os.ErrInvalid
  89. }
  90. delete(m.inboundByTag, tag)
  91. index := common.Index(m.inbounds, func(it adapter.Inbound) bool {
  92. return it == inbound
  93. })
  94. if index == -1 {
  95. panic("invalid inbound index")
  96. }
  97. m.inbounds = append(m.inbounds[:index], m.inbounds[index+1:]...)
  98. started := m.started
  99. m.access.Unlock()
  100. if started {
  101. return inbound.Close()
  102. }
  103. return nil
  104. }
  105. func (m *Manager) Create(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, outboundType string, options any) error {
  106. inbound, err := m.registry.Create(ctx, router, logger, tag, outboundType, options)
  107. if err != nil {
  108. return err
  109. }
  110. m.access.Lock()
  111. defer m.access.Unlock()
  112. if m.started {
  113. for _, stage := range adapter.ListStartStages {
  114. err = adapter.LegacyStart(inbound, stage)
  115. if err != nil {
  116. return E.Cause(err, stage, " inbound/", inbound.Type(), "[", inbound.Tag(), "]")
  117. }
  118. }
  119. }
  120. if existsInbound, loaded := m.inboundByTag[tag]; loaded {
  121. if m.started {
  122. err = existsInbound.Close()
  123. if err != nil {
  124. return E.Cause(err, "close inbound/", existsInbound.Type(), "[", existsInbound.Tag(), "]")
  125. }
  126. }
  127. existsIndex := common.Index(m.inbounds, func(it adapter.Inbound) bool {
  128. return it == existsInbound
  129. })
  130. if existsIndex == -1 {
  131. panic("invalid inbound index")
  132. }
  133. m.inbounds = append(m.inbounds[:existsIndex], m.inbounds[existsIndex+1:]...)
  134. }
  135. m.inbounds = append(m.inbounds, inbound)
  136. m.inboundByTag[tag] = inbound
  137. return nil
  138. }