command_server.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. GetSystemProxyStatus() *SystemProxyStatus
  35. SetSystemProxyEnabled(isEnabled bool) error
  36. }
  37. func NewCommandServer(handler CommandServerHandler, maxLines int32) *CommandServer {
  38. server := &CommandServer{
  39. handler: handler,
  40. maxLines: int(maxLines),
  41. subscriber: observable.NewSubscriber[string](128),
  42. urlTestUpdate: make(chan struct{}, 1),
  43. modeUpdate: make(chan struct{}, 1),
  44. logReset: make(chan struct{}, 1),
  45. }
  46. server.observer = observable.NewObserver[string](server.subscriber, 64)
  47. return server
  48. }
  49. func (s *CommandServer) SetService(newService *BoxService) {
  50. if newService != nil {
  51. service.PtrFromContext[urltest.HistoryStorage](newService.ctx).SetHook(s.urlTestUpdate)
  52. newService.instance.Router().ClashServer().(*clashapi.Server).SetModeUpdateHook(s.modeUpdate)
  53. s.savedLines.Init()
  54. select {
  55. case s.logReset <- struct{}{}:
  56. default:
  57. }
  58. }
  59. s.service = newService
  60. s.notifyURLTestUpdate()
  61. }
  62. func (s *CommandServer) notifyURLTestUpdate() {
  63. select {
  64. case s.urlTestUpdate <- struct{}{}:
  65. default:
  66. }
  67. }
  68. func (s *CommandServer) Start() error {
  69. if !sTVOS {
  70. return s.listenUNIX()
  71. } else {
  72. return s.listenTCP()
  73. }
  74. }
  75. func (s *CommandServer) listenUNIX() error {
  76. sockPath := filepath.Join(sBasePath, "command.sock")
  77. os.Remove(sockPath)
  78. listener, err := net.ListenUnix("unix", &net.UnixAddr{
  79. Name: sockPath,
  80. Net: "unix",
  81. })
  82. if err != nil {
  83. return E.Cause(err, "listen ", sockPath)
  84. }
  85. if sUserID > 0 {
  86. err = os.Chown(sockPath, sUserID, sGroupID)
  87. if err != nil {
  88. listener.Close()
  89. os.Remove(sockPath)
  90. return E.Cause(err, "chown")
  91. }
  92. }
  93. s.listener = listener
  94. go s.loopConnection(listener)
  95. return nil
  96. }
  97. func (s *CommandServer) listenTCP() error {
  98. listener, err := net.Listen("tcp", "127.0.0.1:8964")
  99. if err != nil {
  100. return E.Cause(err, "listen")
  101. }
  102. s.listener = listener
  103. go s.loopConnection(listener)
  104. return nil
  105. }
  106. func (s *CommandServer) Close() error {
  107. return common.Close(
  108. s.listener,
  109. s.observer,
  110. )
  111. }
  112. func (s *CommandServer) loopConnection(listener net.Listener) {
  113. for {
  114. conn, err := listener.Accept()
  115. if err != nil {
  116. return
  117. }
  118. go func() {
  119. hErr := s.handleConnection(conn)
  120. if hErr != nil && !E.IsClosed(err) {
  121. if debug.Enabled {
  122. log.Warn("log-server: process connection: ", hErr)
  123. }
  124. }
  125. }()
  126. }
  127. }
  128. func (s *CommandServer) handleConnection(conn net.Conn) error {
  129. defer conn.Close()
  130. var command uint8
  131. err := binary.Read(conn, binary.BigEndian, &command)
  132. if err != nil {
  133. return E.Cause(err, "read command")
  134. }
  135. switch int32(command) {
  136. case CommandLog:
  137. return s.handleLogConn(conn)
  138. case CommandStatus:
  139. return s.handleStatusConn(conn)
  140. case CommandServiceReload:
  141. return s.handleServiceReload(conn)
  142. case CommandCloseConnections:
  143. return s.handleCloseConnections(conn)
  144. case CommandGroup:
  145. return s.handleGroupConn(conn)
  146. case CommandSelectOutbound:
  147. return s.handleSelectOutbound(conn)
  148. case CommandURLTest:
  149. return s.handleURLTest(conn)
  150. case CommandGroupExpand:
  151. return s.handleSetGroupExpand(conn)
  152. case CommandClashMode:
  153. return s.handleModeConn(conn)
  154. case CommandSetClashMode:
  155. return s.handleSetClashMode(conn)
  156. case CommandGetSystemProxyStatus:
  157. return s.handleGetSystemProxyStatus(conn)
  158. case CommandSetSystemProxyEnabled:
  159. return s.handleSetSystemProxyEnabled(conn)
  160. default:
  161. return E.New("unknown command: ", command)
  162. }
  163. }