manager.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. defer m.access.Unlock()
  35. if m.started && m.stage >= stage {
  36. panic("already started")
  37. }
  38. m.started = true
  39. m.stage = stage
  40. for _, inbound := range m.inbounds {
  41. err := adapter.LegacyStart(inbound, stage)
  42. if err != nil {
  43. return E.Cause(err, stage, " inbound/", inbound.Type(), "[", inbound.Tag(), "]")
  44. }
  45. }
  46. return nil
  47. }
  48. func (m *Manager) Close() error {
  49. m.access.Lock()
  50. defer m.access.Unlock()
  51. if !m.started {
  52. return nil
  53. }
  54. m.started = false
  55. inbounds := m.inbounds
  56. m.inbounds = nil
  57. monitor := taskmonitor.New(m.logger, C.StopTimeout)
  58. var err error
  59. for _, inbound := range inbounds {
  60. monitor.Start("close inbound/", inbound.Type(), "[", inbound.Tag(), "]")
  61. err = E.Append(err, inbound.Close(), func(err error) error {
  62. return E.Cause(err, "close inbound/", inbound.Type(), "[", inbound.Tag(), "]")
  63. })
  64. monitor.Finish()
  65. }
  66. return nil
  67. }
  68. func (m *Manager) Inbounds() []adapter.Inbound {
  69. m.access.Lock()
  70. defer m.access.Unlock()
  71. return m.inbounds
  72. }
  73. func (m *Manager) Get(tag string) (adapter.Inbound, bool) {
  74. m.access.Lock()
  75. inbound, found := m.inboundByTag[tag]
  76. m.access.Unlock()
  77. if found {
  78. return inbound, true
  79. }
  80. return m.endpoint.Get(tag)
  81. }
  82. func (m *Manager) Remove(tag string) error {
  83. m.access.Lock()
  84. inbound, found := m.inboundByTag[tag]
  85. if !found {
  86. m.access.Unlock()
  87. return os.ErrInvalid
  88. }
  89. delete(m.inboundByTag, tag)
  90. index := common.Index(m.inbounds, func(it adapter.Inbound) bool {
  91. return it == inbound
  92. })
  93. if index == -1 {
  94. panic("invalid inbound index")
  95. }
  96. m.inbounds = append(m.inbounds[:index], m.inbounds[index+1:]...)
  97. started := m.started
  98. m.access.Unlock()
  99. if started {
  100. return inbound.Close()
  101. }
  102. return nil
  103. }
  104. func (m *Manager) Create(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, outboundType string, options any) error {
  105. inbound, err := m.registry.Create(ctx, router, logger, tag, outboundType, options)
  106. if err != nil {
  107. return err
  108. }
  109. m.access.Lock()
  110. defer m.access.Unlock()
  111. if m.started {
  112. for _, stage := range adapter.ListStartStages {
  113. err = adapter.LegacyStart(inbound, stage)
  114. if err != nil {
  115. return E.Cause(err, stage, " inbound/", inbound.Type(), "[", inbound.Tag(), "]")
  116. }
  117. }
  118. }
  119. if existsInbound, loaded := m.inboundByTag[tag]; loaded {
  120. if m.started {
  121. err = existsInbound.Close()
  122. if err != nil {
  123. return E.Cause(err, "close inbound/", existsInbound.Type(), "[", existsInbound.Tag(), "]")
  124. }
  125. }
  126. existsIndex := common.Index(m.inbounds, func(it adapter.Inbound) bool {
  127. return it == existsInbound
  128. })
  129. if existsIndex == -1 {
  130. panic("invalid inbound index")
  131. }
  132. m.inbounds = append(m.inbounds[:existsIndex], m.inbounds[existsIndex+1:]...)
  133. }
  134. m.inbounds = append(m.inbounds, inbound)
  135. m.inboundByTag[tag] = inbound
  136. return nil
  137. }