command_server.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package libbox
  2. import (
  3. "encoding/binary"
  4. "net"
  5. "os"
  6. "path/filepath"
  7. "sync"
  8. "github.com/sagernet/sing-box/common/urltest"
  9. "github.com/sagernet/sing-box/experimental/clashapi"
  10. "github.com/sagernet/sing-box/log"
  11. "github.com/sagernet/sing/common"
  12. "github.com/sagernet/sing/common/debug"
  13. E "github.com/sagernet/sing/common/exceptions"
  14. "github.com/sagernet/sing/common/observable"
  15. "github.com/sagernet/sing/common/x/list"
  16. "github.com/sagernet/sing/service"
  17. )
  18. type CommandServer struct {
  19. listener net.Listener
  20. handler CommandServerHandler
  21. access sync.Mutex
  22. savedLines list.List[string]
  23. maxLines int
  24. subscriber *observable.Subscriber[string]
  25. observer *observable.Observer[string]
  26. service *BoxService
  27. // These channels only work with a single client. if multi-client support is needed, replace with Subscriber/Observer
  28. urlTestUpdate chan struct{}
  29. modeUpdate chan struct{}
  30. logReset chan struct{}
  31. }
  32. type CommandServerHandler interface {
  33. ServiceReload() error
  34. PostServiceClose()
  35. GetSystemProxyStatus() *SystemProxyStatus
  36. SetSystemProxyEnabled(isEnabled bool) error
  37. }
  38. func NewCommandServer(handler CommandServerHandler, maxLines int32) *CommandServer {
  39. server := &CommandServer{
  40. handler: handler,
  41. maxLines: int(maxLines),
  42. subscriber: observable.NewSubscriber[string](128),
  43. urlTestUpdate: make(chan struct{}, 1),
  44. modeUpdate: make(chan struct{}, 1),
  45. logReset: make(chan struct{}, 1),
  46. }
  47. server.observer = observable.NewObserver[string](server.subscriber, 64)
  48. return server
  49. }
  50. func (s *CommandServer) SetService(newService *BoxService) {
  51. if newService != nil {
  52. service.PtrFromContext[urltest.HistoryStorage](newService.ctx).SetHook(s.urlTestUpdate)
  53. newService.instance.Router().ClashServer().(*clashapi.Server).SetModeUpdateHook(s.modeUpdate)
  54. }
  55. s.service = newService
  56. s.notifyURLTestUpdate()
  57. }
  58. func (s *CommandServer) ResetLog() {
  59. s.savedLines.Init()
  60. select {
  61. case s.logReset <- struct{}{}:
  62. default:
  63. }
  64. }
  65. func (s *CommandServer) notifyURLTestUpdate() {
  66. select {
  67. case s.urlTestUpdate <- struct{}{}:
  68. default:
  69. }
  70. }
  71. func (s *CommandServer) Start() error {
  72. if !sTVOS {
  73. return s.listenUNIX()
  74. } else {
  75. return s.listenTCP()
  76. }
  77. }
  78. func (s *CommandServer) listenUNIX() error {
  79. sockPath := filepath.Join(sBasePath, "command.sock")
  80. os.Remove(sockPath)
  81. listener, err := net.ListenUnix("unix", &net.UnixAddr{
  82. Name: sockPath,
  83. Net: "unix",
  84. })
  85. if err != nil {
  86. return E.Cause(err, "listen ", sockPath)
  87. }
  88. err = os.Chown(sockPath, sUserID, sGroupID)
  89. if err != nil {
  90. listener.Close()
  91. os.Remove(sockPath)
  92. return E.Cause(err, "chown")
  93. }
  94. s.listener = listener
  95. go s.loopConnection(listener)
  96. return nil
  97. }
  98. func (s *CommandServer) listenTCP() error {
  99. listener, err := net.Listen("tcp", "127.0.0.1:8964")
  100. if err != nil {
  101. return E.Cause(err, "listen")
  102. }
  103. s.listener = listener
  104. go s.loopConnection(listener)
  105. return nil
  106. }
  107. func (s *CommandServer) Close() error {
  108. return common.Close(
  109. s.listener,
  110. s.observer,
  111. )
  112. }
  113. func (s *CommandServer) loopConnection(listener net.Listener) {
  114. for {
  115. conn, err := listener.Accept()
  116. if err != nil {
  117. return
  118. }
  119. go func() {
  120. hErr := s.handleConnection(conn)
  121. if hErr != nil && !E.IsClosed(err) {
  122. if debug.Enabled {
  123. log.Warn("log-server: process connection: ", hErr)
  124. }
  125. }
  126. }()
  127. }
  128. }
  129. func (s *CommandServer) handleConnection(conn net.Conn) error {
  130. defer conn.Close()
  131. var command uint8
  132. err := binary.Read(conn, binary.BigEndian, &command)
  133. if err != nil {
  134. return E.Cause(err, "read command")
  135. }
  136. switch int32(command) {
  137. case CommandLog:
  138. return s.handleLogConn(conn)
  139. case CommandStatus:
  140. return s.handleStatusConn(conn)
  141. case CommandServiceReload:
  142. return s.handleServiceReload(conn)
  143. case CommandServiceClose:
  144. return s.handleServiceClose(conn)
  145. case CommandCloseConnections:
  146. return s.handleCloseConnections(conn)
  147. case CommandGroup:
  148. return s.handleGroupConn(conn)
  149. case CommandSelectOutbound:
  150. return s.handleSelectOutbound(conn)
  151. case CommandURLTest:
  152. return s.handleURLTest(conn)
  153. case CommandGroupExpand:
  154. return s.handleSetGroupExpand(conn)
  155. case CommandClashMode:
  156. return s.handleModeConn(conn)
  157. case CommandSetClashMode:
  158. return s.handleSetClashMode(conn)
  159. case CommandGetSystemProxyStatus:
  160. return s.handleGetSystemProxyStatus(conn)
  161. case CommandSetSystemProxyEnabled:
  162. return s.handleSetSystemProxyEnabled(conn)
  163. default:
  164. return E.New("unknown command: ", command)
  165. }
  166. }