Add reload platform command

This commit is contained in:
世界
2023-03-03 19:26:54 +08:00
parent 2366835121
commit b9b2b77814
13 changed files with 286 additions and 19 deletions

View File

@@ -5,4 +5,6 @@ package libbox
const (
CommandLog int32 = iota
CommandStatus
CommandServiceReload
CommandCloseConnections
)

View File

@@ -12,10 +12,10 @@ import (
)
type CommandClient struct {
sockPath string
handler CommandClientHandler
conn net.Conn
options CommandClientOptions
sharedDirectory string
handler CommandClientHandler
conn net.Conn
options CommandClientOptions
}
type CommandClientOptions struct {
@@ -32,17 +32,21 @@ type CommandClientHandler interface {
func NewCommandClient(sharedDirectory string, handler CommandClientHandler, options *CommandClientOptions) *CommandClient {
return &CommandClient{
sockPath: filepath.Join(sharedDirectory, "command.sock"),
handler: handler,
options: common.PtrValueOrDefault(options),
sharedDirectory: sharedDirectory,
handler: handler,
options: common.PtrValueOrDefault(options),
}
}
func (c *CommandClient) Connect() error {
conn, err := net.DialUnix("unix", nil, &net.UnixAddr{
Name: c.sockPath,
func clientConnect(sharedDirectory string) (net.Conn, error) {
return net.DialUnix("unix", nil, &net.UnixAddr{
Name: filepath.Join(sharedDirectory, "command.sock"),
Net: "unix",
})
}
func (c *CommandClient) Connect() error {
conn, err := clientConnect(c.sharedDirectory)
if err != nil {
return err
}

View File

@@ -0,0 +1,30 @@
//go:build darwin
package libbox
import (
"encoding/binary"
"net"
runtimeDebug "runtime/debug"
"time"
"github.com/sagernet/sing-box/common/dialer/conntrack"
)
func ClientCloseConnections(sharedDirectory string) error {
conn, err := clientConnect(sharedDirectory)
if err != nil {
return err
}
defer conn.Close()
return binary.Write(conn, binary.BigEndian, uint8(CommandCloseConnections))
}
func (s *CommandServer) handleCloseConnections(conn net.Conn) error {
conntrack.Close()
go func() {
time.Sleep(time.Second)
runtimeDebug.FreeOSMemory()
}()
return nil
}

View File

@@ -0,0 +1,48 @@
//go:build darwin
package libbox
import (
"encoding/binary"
"net"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/rw"
)
func ClientServiceReload(sharedDirectory string) error {
conn, err := clientConnect(sharedDirectory)
if err != nil {
return err
}
defer conn.Close()
err = binary.Write(conn, binary.BigEndian, uint8(CommandServiceReload))
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) handleServiceReload(conn net.Conn) error {
rErr := s.handler.ServiceReload()
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

@@ -18,6 +18,7 @@ import (
type CommandServer struct {
sockPath string
listener net.Listener
handler CommandServerHandler
access sync.Mutex
savedLines *list.List[string]
@@ -25,9 +26,14 @@ type CommandServer struct {
observer *observable.Observer[string]
}
func NewCommandServer(sharedDirectory string) *CommandServer {
type CommandServerHandler interface {
ServiceReload() error
}
func NewCommandServer(sharedDirectory string, handler CommandServerHandler) *CommandServer {
server := &CommandServer{
sockPath: filepath.Join(sharedDirectory, "command.sock"),
handler: handler,
savedLines: new(list.List[string]),
subscriber: observable.NewSubscriber[string](128),
}
@@ -79,6 +85,10 @@ func (s *CommandServer) handleConnection(conn net.Conn) error {
return s.handleLogConn(conn)
case CommandStatus:
return s.handleStatusConn(conn)
case CommandServiceReload:
return s.handleServiceReload(conn)
case CommandCloseConnections:
return s.handleCloseConnections(conn)
default:
return E.New("unknown command: ", command)
}

View File

@@ -8,12 +8,14 @@ import (
"runtime"
"time"
"github.com/sagernet/sing-box/common/dialer/conntrack"
E "github.com/sagernet/sing/common/exceptions"
)
type StatusMessage struct {
Memory int64
Goroutines int32
Memory int64
Goroutines int32
Connections int32
}
func readStatus() StatusMessage {
@@ -22,6 +24,7 @@ func readStatus() StatusMessage {
var message StatusMessage
message.Memory = int64(memStats.Sys - memStats.HeapReleased)
message.Goroutines = int32(runtime.NumGoroutine())
message.Connections = int32(conntrack.Count())
return message
}