command_client.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package libbox
  2. import (
  3. "encoding/binary"
  4. "net"
  5. "os"
  6. "path/filepath"
  7. "time"
  8. "github.com/sagernet/sing/common"
  9. E "github.com/sagernet/sing/common/exceptions"
  10. )
  11. type CommandClient struct {
  12. handler CommandClientHandler
  13. conn net.Conn
  14. options CommandClientOptions
  15. }
  16. type CommandClientOptions struct {
  17. Command int32
  18. StatusInterval int64
  19. }
  20. type CommandClientHandler interface {
  21. Connected()
  22. Disconnected(message string)
  23. ClearLogs()
  24. WriteLogs(messageList StringIterator)
  25. WriteStatus(message *StatusMessage)
  26. WriteGroups(message OutboundGroupIterator)
  27. InitializeClashMode(modeList StringIterator, currentMode string)
  28. UpdateClashMode(newMode string)
  29. WriteConnections(message *Connections)
  30. }
  31. func NewStandaloneCommandClient() *CommandClient {
  32. return new(CommandClient)
  33. }
  34. func NewCommandClient(handler CommandClientHandler, options *CommandClientOptions) *CommandClient {
  35. return &CommandClient{
  36. handler: handler,
  37. options: common.PtrValueOrDefault(options),
  38. }
  39. }
  40. func (c *CommandClient) directConnect() (net.Conn, error) {
  41. if !sTVOS {
  42. return net.DialUnix("unix", nil, &net.UnixAddr{
  43. Name: filepath.Join(sBasePath, "command.sock"),
  44. Net: "unix",
  45. })
  46. } else {
  47. return net.Dial("tcp", "127.0.0.1:8964")
  48. }
  49. }
  50. func (c *CommandClient) directConnectWithRetry() (net.Conn, error) {
  51. var (
  52. conn net.Conn
  53. err error
  54. )
  55. for i := 0; i < 10; i++ {
  56. conn, err = c.directConnect()
  57. if err == nil {
  58. return conn, nil
  59. }
  60. time.Sleep(time.Duration(100+i*50) * time.Millisecond)
  61. }
  62. return nil, err
  63. }
  64. func (c *CommandClient) Connect() error {
  65. common.Close(c.conn)
  66. conn, err := c.directConnectWithRetry()
  67. if err != nil {
  68. return err
  69. }
  70. c.conn = conn
  71. err = binary.Write(conn, binary.BigEndian, uint8(c.options.Command))
  72. if err != nil {
  73. return err
  74. }
  75. switch c.options.Command {
  76. case CommandLog:
  77. err = binary.Write(conn, binary.BigEndian, c.options.StatusInterval)
  78. if err != nil {
  79. return E.Cause(err, "write interval")
  80. }
  81. c.handler.Connected()
  82. go c.handleLogConn(conn)
  83. case CommandStatus:
  84. err = binary.Write(conn, binary.BigEndian, c.options.StatusInterval)
  85. if err != nil {
  86. return E.Cause(err, "write interval")
  87. }
  88. c.handler.Connected()
  89. go c.handleStatusConn(conn)
  90. case CommandGroup:
  91. err = binary.Write(conn, binary.BigEndian, c.options.StatusInterval)
  92. if err != nil {
  93. return E.Cause(err, "write interval")
  94. }
  95. c.handler.Connected()
  96. go c.handleGroupConn(conn)
  97. case CommandClashMode:
  98. var (
  99. modeList []string
  100. currentMode string
  101. )
  102. modeList, currentMode, err = readClashModeList(conn)
  103. if err != nil {
  104. return err
  105. }
  106. if sFixAndroidStack {
  107. go func() {
  108. c.handler.Connected()
  109. c.handler.InitializeClashMode(newIterator(modeList), currentMode)
  110. if len(modeList) == 0 {
  111. conn.Close()
  112. c.handler.Disconnected(os.ErrInvalid.Error())
  113. }
  114. }()
  115. } else {
  116. c.handler.Connected()
  117. c.handler.InitializeClashMode(newIterator(modeList), currentMode)
  118. if len(modeList) == 0 {
  119. conn.Close()
  120. c.handler.Disconnected(os.ErrInvalid.Error())
  121. }
  122. }
  123. if len(modeList) == 0 {
  124. return nil
  125. }
  126. go c.handleModeConn(conn)
  127. case CommandConnections:
  128. err = binary.Write(conn, binary.BigEndian, c.options.StatusInterval)
  129. if err != nil {
  130. return E.Cause(err, "write interval")
  131. }
  132. c.handler.Connected()
  133. go c.handleConnectionsConn(conn)
  134. }
  135. return nil
  136. }
  137. func (c *CommandClient) Disconnect() error {
  138. return common.Close(c.conn)
  139. }