invoker.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "hnyfkj.com.cn/rtu/linux/utils/jsonrpc2"
  8. "hnyfkj.com.cn/rtu/linux/utils/shell"
  9. )
  10. var (
  11. rpc_ping = "executor.ping"
  12. rpc_exec = "executor.exec"
  13. rpc_stop = "executor.interrupt"
  14. rpc_quit = "executor.close"
  15. )
  16. // 串行执行
  17. func (c *MQTTCoupler) needSerialize(method string) bool {
  18. switch method {
  19. case rpc_ping, rpc_stop:
  20. return false
  21. case rpc_exec, rpc_quit:
  22. return true
  23. default:
  24. return true
  25. }
  26. }
  27. // 超时结束
  28. func (c *MQTTCoupler) needTimeoutEnd(method string) bool {
  29. switch method {
  30. case rpc_exec:
  31. return false
  32. case rpc_ping, rpc_stop, rpc_quit:
  33. return true
  34. default:
  35. return true
  36. }
  37. }
  38. // 心跳检测
  39. func (c *MQTTCoupler) ping() (*jsonrpc2.Response, error) {
  40. params := struct {
  41. ClientID string `json:"client_id"`
  42. }{
  43. ClientID: c.clientID,
  44. }
  45. return c.doCmd(rpc_ping, params)
  46. }
  47. // 执行命令
  48. func (c *MQTTCoupler) exec(
  49. cmd string) (*shell.ExecuteResult, error) {
  50. if strings.ContainsAny(cmd, "&") {
  51. return nil, fmt.Errorf("禁止启动后台任务")
  52. }
  53. if strings.ContainsAny(cmd, "|>;") || strings.Contains(cmd, "\n") {
  54. safeCmd := strings.ReplaceAll(cmd, "'", "'\\''")
  55. cmd = fmt.Sprintf("sh -c '%s'", safeCmd) // 包装成 shell 命令, 支持管道等高级功能
  56. }
  57. params := struct {
  58. ClientID string `json:"client_id"`
  59. shell.ExecuteParams
  60. }{
  61. ClientID: c.clientID,
  62. ExecuteParams: shell.ExecuteParams{
  63. Cmd: cmd,
  64. Timeout: int(shell.DefaultTimeout / time.Second),
  65. },
  66. }
  67. timeout := getCmdTimeoutByPrefix(cmd)
  68. if timeout > 0 {
  69. params.Timeout = timeout
  70. }
  71. exrs := shell.ExecuteResult{}
  72. resp, err := c.doCmd(rpc_exec, params)
  73. if err != nil {
  74. return &exrs, err
  75. }
  76. if resp.Error != nil { ////////////////// 错误应答
  77. exrs.ExitCode = int(resp.Error.Code)
  78. exrs.Stderr = resp.Error.Message
  79. } else if len(resp.Result) > 0 { //////// 正确应答
  80. if err := json.Unmarshal(resp.Result, &exrs); err != nil {
  81. exrs.ExitCode = 1
  82. exrs.Stderr = err.Error()
  83. }
  84. }
  85. return &exrs, nil
  86. }
  87. // 中断执行
  88. func (c *MQTTCoupler) stop() (*jsonrpc2.Response, error) {
  89. params := struct {
  90. ClientID string `json:"client_id"`
  91. }{
  92. ClientID: c.clientID,
  93. }
  94. return c.doCmd(rpc_stop, params)
  95. }
  96. // 关闭退出
  97. func (c *MQTTCoupler) quit() (*jsonrpc2.Response, error) {
  98. params := struct {
  99. ClientID string `json:"client_id"`
  100. }{
  101. ClientID: c.clientID,
  102. }
  103. return c.doCmd(rpc_quit, params)
  104. }