command_client.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. ClearLog()
  24. WriteLog(message string)
  25. WriteStatus(message *StatusMessage)
  26. WriteGroups(message OutboundGroupIterator)
  27. InitializeClashMode(modeList StringIterator, currentMode string)
  28. UpdateClashMode(newMode string)
  29. }
  30. func NewStandaloneCommandClient() *CommandClient {
  31. return new(CommandClient)
  32. }
  33. func NewCommandClient(handler CommandClientHandler, options *CommandClientOptions) *CommandClient {
  34. return &CommandClient{
  35. handler: handler,
  36. options: common.PtrValueOrDefault(options),
  37. }
  38. }
  39. func (c *CommandClient) directConnect() (net.Conn, error) {
  40. if !sTVOS {
  41. return net.DialUnix("unix", nil, &net.UnixAddr{
  42. Name: filepath.Join(sBasePath, "command.sock"),
  43. Net: "unix",
  44. })
  45. } else {
  46. return net.Dial("tcp", "127.0.0.1:8964")
  47. }
  48. }
  49. func (c *CommandClient) directConnectWithRetry() (net.Conn, error) {
  50. var (
  51. conn net.Conn
  52. err error
  53. )
  54. for i := 0; i < 10; i++ {
  55. conn, err = c.directConnect()
  56. if err == nil {
  57. return conn, nil
  58. }
  59. time.Sleep(time.Duration(100+i*50) * time.Millisecond)
  60. }
  61. return nil, err
  62. }
  63. func (c *CommandClient) Connect() error {
  64. common.Close(c.conn)
  65. conn, err := c.directConnectWithRetry()
  66. if err != nil {
  67. return err
  68. }
  69. c.conn = conn
  70. err = binary.Write(conn, binary.BigEndian, uint8(c.options.Command))
  71. if err != nil {
  72. return err
  73. }
  74. switch c.options.Command {
  75. case CommandLog:
  76. c.handler.Connected()
  77. go c.handleLogConn(conn)
  78. case CommandStatus:
  79. err = binary.Write(conn, binary.BigEndian, c.options.StatusInterval)
  80. if err != nil {
  81. return E.Cause(err, "write interval")
  82. }
  83. c.handler.Connected()
  84. go c.handleStatusConn(conn)
  85. case CommandGroup:
  86. err = binary.Write(conn, binary.BigEndian, c.options.StatusInterval)
  87. if err != nil {
  88. return E.Cause(err, "write interval")
  89. }
  90. c.handler.Connected()
  91. go c.handleGroupConn(conn)
  92. case CommandClashMode:
  93. var (
  94. modeList []string
  95. currentMode string
  96. )
  97. modeList, currentMode, err = readClashModeList(conn)
  98. if err != nil {
  99. return err
  100. }
  101. c.handler.Connected()
  102. c.handler.InitializeClashMode(newIterator(modeList), currentMode)
  103. if len(modeList) == 0 {
  104. conn.Close()
  105. c.handler.Disconnected(os.ErrInvalid.Error())
  106. return nil
  107. }
  108. go c.handleModeConn(conn)
  109. }
  110. return nil
  111. }
  112. func (c *CommandClient) Disconnect() error {
  113. return common.Close(c.conn)
  114. }