frpcmanager.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #ifndef FRPCMANAGER_H
  2. #define FRPCMANAGER_H
  3. #include <QObject>
  4. #include <QProcess>
  5. #include <QDateTime>
  6. #include <QStringList>
  7. class QTimer;
  8. // frpc 内网穿透进程管理
  9. // - 二进制以 jniLibs 方式打包(android/libs/arm64-v8a/libfrp.so),安装后位于
  10. // nativeLibraryDir,该目录允许 exec(规避 Android 10+ 对 data 目录的限制)
  11. // - 配置文件保存在 AppDataLocation/frpc.toml,由 MQTT JSON-RPC 远程下发:
  12. // frpc_start / frpc_stop / frpc_status / frpc_config
  13. // - 运行超时保护:启动满 1 小时自动停止,避免长期占用资源
  14. class FrpcManager : public QObject
  15. {
  16. Q_OBJECT
  17. public:
  18. explicit FrpcManager(QObject *parent = nullptr);
  19. ~FrpcManager() override;
  20. // 启动 frpc;inlineToml 非空时先写入配置文件再启动。
  21. // 返回空串=成功,否则为错误描述
  22. QString start(const QString &inlineToml);
  23. // 停止 frpc(SIGTERM→3s后SIGKILL)。返回空串=成功,否则为错误描述
  24. QString stop();
  25. // 状态查询:返回紧凑 JSON 字符串(running/pid/uptime/log_tail等)
  26. QString status() const;
  27. // 保存配置;restart 为 true 且运行中则重启。返回空串=成功,否则为错误描述
  28. QString setConfig(const QString &toml, bool restart);
  29. signals:
  30. void stateChanged(bool running);
  31. private:
  32. QString binaryPath() const;
  33. QString configPath() const;
  34. // AppData/frpc.toml 不存在时,释放内嵌默认配置(编译期字符串)到该路径。
  35. // 返回空串=成功(或已存在),否则为错误描述
  36. QString ensureDefaultConfig() const;
  37. void appendLog(const QByteArray &raw);
  38. QProcess *m_proc = nullptr;
  39. QTimer *m_autoStopTimer = nullptr; // 运行超时看门狗:到期自动 stop()
  40. QDateTime m_startTime;
  41. int m_lastExitCode = 0;
  42. bool m_lastCrashed = false;
  43. QStringList m_logRing; // 最近日志环形缓冲
  44. static constexpr int kMaxLogLines = 40;
  45. static constexpr int kAutoStopMs = 60 * 60 * 1000; // 启动 1 小时后自动停止
  46. };
  47. #endif // FRPCMANAGER_H