2023-03-03 19:26:54 +08:00
|
|
|
package libbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2024-06-24 09:49:15 +08:00
|
|
|
"github.com/sagernet/sing/common/varbin"
|
2023-03-03 19:26:54 +08:00
|
|
|
)
|
|
|
|
|
2023-07-02 16:45:30 +08:00
|
|
|
func (c *CommandClient) ServiceReload() error {
|
|
|
|
conn, err := c.directConnect()
|
2023-03-03 19:26:54 +08:00
|
|
|
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 {
|
2024-06-24 09:49:15 +08:00
|
|
|
errorMessage, err := varbin.ReadValue[string](conn, binary.BigEndian)
|
2023-03-03 19:26:54 +08:00
|
|
|
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 {
|
2024-06-24 09:49:15 +08:00
|
|
|
return varbin.Write(conn, binary.BigEndian, rErr.Error())
|
2023-03-03 19:26:54 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-14 13:40:17 +08:00
|
|
|
|
|
|
|
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 {
|
2024-06-24 09:49:15 +08:00
|
|
|
errorMessage, err := varbin.ReadValue[string](conn, binary.BigEndian)
|
2024-03-14 13:40:17 +08:00
|
|
|
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 {
|
2024-06-24 09:49:15 +08:00
|
|
|
return varbin.Write(conn, binary.BigEndian, rErr.Error())
|
2024-03-14 13:40:17 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|