platform: Improve stop on apple platforms

This commit is contained in:
世界 2024-03-14 13:40:17 +08:00
parent fdc451f7c6
commit ceffcc0ad2
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 55 additions and 0 deletions

View file

@ -4,6 +4,7 @@ const (
CommandLog int32 = iota CommandLog int32 = iota
CommandStatus CommandStatus
CommandServiceReload CommandServiceReload
CommandServiceClose
CommandCloseConnections CommandCloseConnections
CommandGroup CommandGroup
CommandSelectOutbound CommandSelectOutbound

View file

@ -44,3 +44,41 @@ func (s *CommandServer) handleServiceReload(conn net.Conn) error {
} }
return nil return nil
} }
func (c *CommandClient) ServiceClose() error {
conn, err := c.directConnect()
if err != nil {
return err
}
defer conn.Close()
err = binary.Write(conn, binary.BigEndian, uint8(CommandServiceClose))
if err != nil {
return err
}
var hasError bool
err = binary.Read(conn, binary.BigEndian, &hasError)
if err != nil {
return nil
}
if hasError {
errorMessage, err := rw.ReadVString(conn)
if err != nil {
return nil
}
return E.New(errorMessage)
}
return nil
}
func (s *CommandServer) handleServiceClose(conn net.Conn) error {
rErr := s.service.Close()
s.handler.PostServiceClose()
err := binary.Write(conn, binary.BigEndian, rErr != nil)
if err != nil {
return err
}
if rErr != nil {
return rw.WriteVString(conn, rErr.Error())
}
return nil
}

View file

@ -37,6 +37,7 @@ type CommandServer struct {
type CommandServerHandler interface { type CommandServerHandler interface {
ServiceReload() error ServiceReload() error
PostServiceClose()
GetSystemProxyStatus() *SystemProxyStatus GetSystemProxyStatus() *SystemProxyStatus
SetSystemProxyEnabled(isEnabled bool) error SetSystemProxyEnabled(isEnabled bool) error
} }
@ -155,6 +156,8 @@ func (s *CommandServer) handleConnection(conn net.Conn) error {
return s.handleStatusConn(conn) return s.handleStatusConn(conn)
case CommandServiceReload: case CommandServiceReload:
return s.handleServiceReload(conn) return s.handleServiceReload(conn)
case CommandServiceClose:
return s.handleServiceClose(conn)
case CommandCloseConnections: case CommandCloseConnections:
return s.handleCloseConnections(conn) return s.handleCloseConnections(conn)
case CommandGroup: case CommandGroup:

View file

@ -3,14 +3,17 @@ package libbox
import ( import (
"context" "context"
"net/netip" "net/netip"
"os"
"runtime" "runtime"
runtimeDebug "runtime/debug" runtimeDebug "runtime/debug"
"syscall" "syscall"
"time"
"github.com/sagernet/sing-box" "github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/process" "github.com/sagernet/sing-box/common/process"
"github.com/sagernet/sing-box/common/urltest" "github.com/sagernet/sing-box/common/urltest"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/experimental/libbox/internal/procfs" "github.com/sagernet/sing-box/experimental/libbox/internal/procfs"
"github.com/sagernet/sing-box/experimental/libbox/platform" "github.com/sagernet/sing-box/experimental/libbox/platform"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
@ -72,6 +75,16 @@ func (s *BoxService) Start() error {
} }
func (s *BoxService) Close() error { func (s *BoxService) Close() error {
done := make(chan struct{})
defer close(done)
go func() {
select {
case <-done:
return
case <-time.After(C.DefaultStopFatalTimeout):
os.Exit(1)
}
}()
s.cancel() s.cancel()
s.urlTestHistoryStorage.Close() s.urlTestHistoryStorage.Close()
return s.instance.Close() return s.instance.Close()