frpcmanager.h 1.8 KB

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