sing-box/experimental/libbox/command_server.go

177 lines
4.3 KiB
Go
Raw Normal View History

2023-03-01 10:37:47 +08:00
package libbox
import (
"encoding/binary"
"net"
"os"
"path/filepath"
"sync"
2023-07-02 16:45:30 +08:00
"github.com/sagernet/sing-box/common/urltest"
"github.com/sagernet/sing-box/experimental/clashapi"
2023-03-01 10:37:47 +08:00
"github.com/sagernet/sing-box/log"
2023-04-05 04:38:56 +08:00
"github.com/sagernet/sing/common"
2023-04-10 08:48:58 +08:00
"github.com/sagernet/sing/common/debug"
2023-03-01 10:37:47 +08:00
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/observable"
"github.com/sagernet/sing/common/x/list"
2023-07-02 16:45:30 +08:00
"github.com/sagernet/sing/service"
2023-03-01 10:37:47 +08:00
)
type CommandServer struct {
listener net.Listener
2023-03-03 19:26:54 +08:00
handler CommandServerHandler
2023-03-01 10:37:47 +08:00
access sync.Mutex
2023-10-02 22:27:48 +08:00
savedLines list.List[string]
2023-07-02 16:45:30 +08:00
maxLines int
2023-03-01 10:37:47 +08:00
subscriber *observable.Subscriber[string]
observer *observable.Observer[string]
2023-07-02 16:45:30 +08:00
service *BoxService
2023-10-02 22:27:48 +08:00
// These channels only work with a single client. if multi-client support is needed, replace with Subscriber/Observer
urlTestUpdate chan struct{}
modeUpdate chan struct{}
2023-10-02 22:27:48 +08:00
logReset chan struct{}
2023-03-01 10:37:47 +08:00
}
2023-03-03 19:26:54 +08:00
type CommandServerHandler interface {
ServiceReload() error
2023-09-03 21:06:21 +08:00
GetSystemProxyStatus() *SystemProxyStatus
SetSystemProxyEnabled(isEnabled bool) error
2023-03-03 19:26:54 +08:00
}
2023-07-29 08:37:10 +08:00
func NewCommandServer(handler CommandServerHandler, maxLines int32) *CommandServer {
2023-03-01 10:37:47 +08:00
server := &CommandServer{
2023-07-02 16:45:30 +08:00
handler: handler,
maxLines: int(maxLines),
subscriber: observable.NewSubscriber[string](128),
urlTestUpdate: make(chan struct{}, 1),
modeUpdate: make(chan struct{}, 1),
2023-10-02 22:27:48 +08:00
logReset: make(chan struct{}, 1),
2023-03-01 10:37:47 +08:00
}
server.observer = observable.NewObserver[string](server.subscriber, 64)
return server
}
2023-07-02 16:45:30 +08:00
func (s *CommandServer) SetService(newService *BoxService) {
if newService != nil {
service.PtrFromContext[urltest.HistoryStorage](newService.ctx).SetHook(s.urlTestUpdate)
newService.instance.Router().ClashServer().(*clashapi.Server).SetModeUpdateHook(s.modeUpdate)
2023-10-02 22:27:48 +08:00
s.savedLines.Init()
select {
case s.logReset <- struct{}{}:
default:
}
2023-07-02 16:45:30 +08:00
}
s.service = newService
2023-07-02 16:45:30 +08:00
s.notifyURLTestUpdate()
}
func (s *CommandServer) notifyURLTestUpdate() {
select {
case s.urlTestUpdate <- struct{}{}:
default:
}
}
2023-03-01 10:37:47 +08:00
func (s *CommandServer) Start() error {
2023-07-29 08:37:10 +08:00
if !sTVOS {
return s.listenUNIX()
} else {
return s.listenTCP()
}
}
func (s *CommandServer) listenUNIX() error {
sockPath := filepath.Join(sBasePath, "command.sock")
os.Remove(sockPath)
2023-03-01 10:37:47 +08:00
listener, err := net.ListenUnix("unix", &net.UnixAddr{
2023-07-29 08:37:10 +08:00
Name: sockPath,
2023-03-01 10:37:47 +08:00
Net: "unix",
})
if err != nil {
2023-07-30 20:26:09 +08:00
return E.Cause(err, "listen ", sockPath)
2023-03-01 10:37:47 +08:00
}
err = os.Chown(sockPath, sUserID, sGroupID)
if err != nil {
listener.Close()
os.Remove(sockPath)
return E.Cause(err, "chown")
2023-07-24 15:44:11 +08:00
}
2023-03-04 00:40:47 +08:00
s.listener = listener
2023-03-01 10:37:47 +08:00
go s.loopConnection(listener)
return nil
}
2023-07-29 08:37:10 +08:00
func (s *CommandServer) listenTCP() error {
listener, err := net.Listen("tcp", "127.0.0.1:8964")
if err != nil {
return E.Cause(err, "listen")
}
s.listener = listener
go s.loopConnection(listener)
return nil
}
2023-03-01 10:37:47 +08:00
func (s *CommandServer) Close() error {
2023-04-05 04:38:56 +08:00
return common.Close(
s.listener,
s.observer,
)
2023-03-01 10:37:47 +08:00
}
func (s *CommandServer) loopConnection(listener net.Listener) {
for {
conn, err := listener.Accept()
if err != nil {
return
}
go func() {
hErr := s.handleConnection(conn)
if hErr != nil && !E.IsClosed(err) {
2023-04-10 08:48:58 +08:00
if debug.Enabled {
log.Warn("log-server: process connection: ", hErr)
}
2023-03-01 10:37:47 +08:00
}
}()
}
}
func (s *CommandServer) handleConnection(conn net.Conn) error {
defer conn.Close()
var command uint8
err := binary.Read(conn, binary.BigEndian, &command)
if err != nil {
return E.Cause(err, "read command")
}
switch int32(command) {
case CommandLog:
return s.handleLogConn(conn)
case CommandStatus:
return s.handleStatusConn(conn)
2023-03-03 19:26:54 +08:00
case CommandServiceReload:
return s.handleServiceReload(conn)
case CommandCloseConnections:
return s.handleCloseConnections(conn)
2023-07-02 16:45:30 +08:00
case CommandGroup:
return s.handleGroupConn(conn)
case CommandSelectOutbound:
return s.handleSelectOutbound(conn)
case CommandURLTest:
return s.handleURLTest(conn)
2023-08-12 19:33:43 +08:00
case CommandGroupExpand:
return s.handleSetGroupExpand(conn)
case CommandClashMode:
return s.handleModeConn(conn)
case CommandSetClashMode:
return s.handleSetClashMode(conn)
2023-09-03 21:06:21 +08:00
case CommandGetSystemProxyStatus:
return s.handleGetSystemProxyStatus(conn)
case CommandSetSystemProxyEnabled:
return s.handleSetSystemProxyEnabled(conn)
2023-03-01 10:37:47 +08:00
default:
return E.New("unknown command: ", command)
}
}