Refactor lifecycle to scoped cleanup

This commit is contained in:
世界
2026-10-09 12:36:03 +08:00
parent a364ff4794
commit a691a44023
115 changed files with 1595 additions and 2030 deletions
+2 -8
View File
@@ -21,7 +21,6 @@ import (
"github.com/sagernet/sing-box/experimental/clashmode"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/json"
"github.com/sagernet/sing/service"
@@ -147,7 +146,7 @@ func (s *Server) Name() string {
return "clash server"
}
func (s *Server) Start(stage adapter.StartStage) error {
func (s *Server) Start(stage adapter.StartStage, scope *adapter.Scope) error {
if stage != adapter.StartStateStarted {
return nil
}
@@ -168,6 +167,7 @@ func (s *Server) Start(stage adapter.StartStage) error {
if err != nil {
return E.Cause(err, "external controller listen error")
}
scope.Add(s.httpServer.Close)
s.logger.Info("restful api listening at ", listener.Addr())
go func() {
err = s.httpServer.Serve(listener)
@@ -179,12 +179,6 @@ func (s *Server) Start(stage adapter.StartStage) error {
return nil
}
func (s *Server) Close() error {
return common.Close(
common.PtrOrNil(s.httpServer),
)
}
func authentication(serverSecret string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {