command_server.go 4.1 KB

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