mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-10-30 23:16:56 +00:00
Close idle connections of unreferenced outbounds and DNS servers
This commit is contained in:
@@ -9,11 +9,13 @@ import (
|
||||
"github.com/sagernet/sing-box/option"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/logger"
|
||||
"github.com/sagernet/sing/service"
|
||||
)
|
||||
|
||||
var (
|
||||
_ adapter.HTTPClientManager = (*Manager)(nil)
|
||||
_ adapter.LifecycleService = (*Manager)(nil)
|
||||
_ adapter.Referrer = (*Manager)(nil)
|
||||
)
|
||||
|
||||
type Manager struct {
|
||||
@@ -177,3 +179,27 @@ func (m *Manager) Close() error {
|
||||
m.sharedTransports = nil
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *Manager) References() []string {
|
||||
m.access.Lock()
|
||||
defer m.access.Unlock()
|
||||
var references []string
|
||||
var needDefault bool
|
||||
for _, transport := range m.managedTransports {
|
||||
if transport.detour != "" {
|
||||
references = append(references, transport.detour)
|
||||
} else if transport.defaultOutbound {
|
||||
needDefault = true
|
||||
}
|
||||
}
|
||||
if needDefault {
|
||||
outboundManager := service.FromContext[adapter.OutboundManager](m.ctx)
|
||||
if outboundManager != nil {
|
||||
defaultOutbound := outboundManager.Default()
|
||||
if defaultOutbound != nil {
|
||||
references = append(references, defaultOutbound.Tag())
|
||||
}
|
||||
}
|
||||
}
|
||||
return references
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user