mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-26 02:21:28 +00:00
Add stop platform command
This commit is contained in:
parent
b9b2b77814
commit
dd0a07624e
|
@ -5,6 +5,7 @@ package libbox
|
||||||
const (
|
const (
|
||||||
CommandLog int32 = iota
|
CommandLog int32 = iota
|
||||||
CommandStatus
|
CommandStatus
|
||||||
|
CommandServiceStop
|
||||||
CommandServiceReload
|
CommandServiceReload
|
||||||
CommandCloseConnections
|
CommandCloseConnections
|
||||||
)
|
)
|
||||||
|
|
|
@ -27,6 +27,7 @@ type CommandServer struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommandServerHandler interface {
|
type CommandServerHandler interface {
|
||||||
|
ServiceStop() error
|
||||||
ServiceReload() error
|
ServiceReload() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,6 +51,7 @@ func (s *CommandServer) Start() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
s.listener = listener
|
||||||
go s.loopConnection(listener)
|
go s.loopConnection(listener)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -85,6 +87,8 @@ func (s *CommandServer) handleConnection(conn net.Conn) error {
|
||||||
return s.handleLogConn(conn)
|
return s.handleLogConn(conn)
|
||||||
case CommandStatus:
|
case CommandStatus:
|
||||||
return s.handleStatusConn(conn)
|
return s.handleStatusConn(conn)
|
||||||
|
case CommandServiceStop:
|
||||||
|
return s.handleServiceStop(conn)
|
||||||
case CommandServiceReload:
|
case CommandServiceReload:
|
||||||
return s.handleServiceReload(conn)
|
return s.handleServiceReload(conn)
|
||||||
case CommandCloseConnections:
|
case CommandCloseConnections:
|
||||||
|
|
|
@ -22,7 +22,7 @@ func readStatus() StatusMessage {
|
||||||
var memStats runtime.MemStats
|
var memStats runtime.MemStats
|
||||||
runtime.ReadMemStats(&memStats)
|
runtime.ReadMemStats(&memStats)
|
||||||
var message StatusMessage
|
var message StatusMessage
|
||||||
message.Memory = int64(memStats.Sys - memStats.HeapReleased)
|
message.Memory = int64(memStats.StackInuse + memStats.HeapInuse + memStats.HeapIdle - memStats.HeapReleased)
|
||||||
message.Goroutines = int32(runtime.NumGoroutine())
|
message.Goroutines = int32(runtime.NumGoroutine())
|
||||||
message.Connections = int32(conntrack.Count())
|
message.Connections = int32(conntrack.Count())
|
||||||
return message
|
return message
|
||||||
|
|
50
experimental/libbox/command_stop.go
Normal file
50
experimental/libbox/command_stop.go
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
//go:build darwin
|
||||||
|
|
||||||
|
package libbox
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"net"
|
||||||
|
"runtime/debug"
|
||||||
|
|
||||||
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
|
"github.com/sagernet/sing/common/rw"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ClientServiceStop(sharedDirectory string) error {
|
||||||
|
conn, err := clientConnect(sharedDirectory)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
err = binary.Write(conn, binary.BigEndian, uint8(CommandServiceStop))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var hasError bool
|
||||||
|
err = binary.Read(conn, binary.BigEndian, &hasError)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if hasError {
|
||||||
|
errorMessage, err := rw.ReadVString(conn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return E.New(errorMessage)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *CommandServer) handleServiceStop(conn net.Conn) error {
|
||||||
|
rErr := s.handler.ServiceStop()
|
||||||
|
err := binary.Write(conn, binary.BigEndian, rErr != nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if rErr != nil {
|
||||||
|
return rw.WriteVString(conn, rErr.Error())
|
||||||
|
}
|
||||||
|
debug.FreeOSMemory()
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -3,7 +3,6 @@ package libbox
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"runtime"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box"
|
"github.com/sagernet/sing-box"
|
||||||
|
@ -28,7 +27,6 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
platformInterface.WriteLog("Hello " + runtime.GOOS + "/" + runtime.GOARCH)
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
instance, err := box.New(ctx, options, &platformInterfaceWrapper{platformInterface, platformInterface.UseProcFS()})
|
instance, err := box.New(ctx, options, &platformInterfaceWrapper{platformInterface, platformInterface.UseProcFS()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue