Add sing-box API service

This commit is contained in:
世界
2026-06-11 21:11:10 +08:00
parent fca959f62a
commit a485967957
36 changed files with 2075 additions and 849 deletions
+26 -3
View File
@@ -6,10 +6,10 @@ import (
"github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/trafficcontrol"
"github.com/sagernet/sing-box/common/urltest"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/experimental/deprecated"
"github.com/sagernet/sing-box/include"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
@@ -25,9 +25,13 @@ type Instance struct {
instance *box.Box
connectionManager adapter.ConnectionManager
clashServer adapter.ClashServer
trafficManager *trafficcontrol.Manager
cacheFile adapter.CacheFile
pauseManager pause.Manager
urlTestHistoryStorage *urltest.HistoryStorage
urlTestHistoryStorage adapter.URLTestHistoryStorage
outboundManager adapter.OutboundManager
endpointManager adapter.EndpointManager
logFactory log.Factory
}
func (s *StartedService) CheckConfig(configContent string) error {
@@ -71,7 +75,7 @@ type OverrideOptions struct {
func (s *StartedService) newInstance(profileContent string, overrideOptions *OverrideOptions) (*Instance, error) {
ctx := service.ExtendContext(s.ctx)
service.MustRegister[deprecated.Manager](ctx, new(deprecatedManager))
ctx, cancel := context.WithCancel(include.Context(ctx))
ctx, cancel := context.WithCancel(ctx)
options, err := parseConfig(ctx, profileContent)
if err != nil {
cancel()
@@ -120,12 +124,31 @@ func (s *StartedService) newInstance(profileContent string, overrideOptions *Ove
i.instance = boxInstance
i.connectionManager = service.FromContext[adapter.ConnectionManager](ctx)
i.clashServer = service.FromContext[adapter.ClashServer](ctx)
i.trafficManager = service.PtrFromContext[trafficcontrol.Manager](ctx)
i.pauseManager = service.FromContext[pause.Manager](ctx)
i.cacheFile = service.FromContext[adapter.CacheFile](ctx)
i.outboundManager = service.FromContext[adapter.OutboundManager](ctx)
i.endpointManager = service.FromContext[adapter.EndpointManager](ctx)
i.logFactory = boxInstance.LogFactory()
log.SetStdLogger(boxInstance.LogFactory().Logger())
return i, nil
}
func attachInstance(ctx context.Context) *Instance {
return &Instance{
ctx: ctx,
connectionManager: service.FromContext[adapter.ConnectionManager](ctx),
clashServer: service.FromContext[adapter.ClashServer](ctx),
trafficManager: service.PtrFromContext[trafficcontrol.Manager](ctx),
pauseManager: service.FromContext[pause.Manager](ctx),
cacheFile: service.FromContext[adapter.CacheFile](ctx),
urlTestHistoryStorage: service.PtrFromContext[urltest.HistoryStorage](ctx),
outboundManager: service.FromContext[adapter.OutboundManager](ctx),
endpointManager: service.FromContext[adapter.EndpointManager](ctx),
logFactory: service.FromContext[log.Factory](ctx),
}
}
func (i *Instance) Start() error {
return i.instance.Start()
}