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