frpcmanager.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/armeabi-v7a/libfrp.so),安装后位于
  10. // nativeLibraryDir,该目录允许 exec(规避 Android 10+ 对 data 目录的限制)
  11. // 本项目是 32 位(armeabi-v7a)构建,二进制由 tools/frpc-build/build_frpc32.sh
  12. // 交叉编译产出(必须 CGO_ENABLED=1 且打 Android DNS 补丁,原因见该目录 README)
  13. // - 配置文件保存在 AppDataLocation/frpc.toml,由 MQTT JSON-RPC 远程下发:
  14. // frpc_start / frpc_stop / frpc_status / frpc_config
  15. // - 运行超时保护:启动满 1 小时自动停止,避免长期占用资源
  16. class FrpcManager : public QObject
  17. {
  18. Q_OBJECT
  19. public:
  20. explicit FrpcManager(QObject *parent = nullptr);
  21. ~FrpcManager() override;
  22. // 启动 frpc;inlineToml 非空时先写入配置文件再启动。
  23. // 返回空串=成功,否则为错误描述
  24. QString start(const QString &inlineToml);
  25. // 停止 frpc(SIGTERM→3s后SIGKILL)。返回空串=成功,否则为错误描述
  26. QString stop();
  27. // 状态查询:返回紧凑 JSON 字符串(running/pid/uptime/log_tail等)
  28. QString status() const;
  29. // 保存配置;restart 为 true 且运行中则重启。返回空串=成功,否则为错误描述
  30. QString setConfig(const QString &toml, bool restart);
  31. signals:
  32. void stateChanged(bool running);
  33. private:
  34. QString binaryPath() const;
  35. QString configPath() const;
  36. // AppData/frpc.toml 不存在时,释放内嵌默认配置(编译期字符串)到该路径。
  37. // 返回空串=成功(或已存在),否则为错误描述
  38. QString ensureDefaultConfig() const;
  39. void appendLog(const QByteArray &raw);
  40. QProcess *m_proc = nullptr;
  41. QTimer *m_autoStopTimer = nullptr; // 运行超时看门狗:到期自动 stop()
  42. QDateTime m_startTime;
  43. int m_lastExitCode = 0;
  44. bool m_lastCrashed = false;
  45. quint64 m_runSeq = 0; // 启动序号:用于让"启动后复查"定时器在 stop/重启后失效
  46. QStringList m_logRing; // 最近日志环形缓冲
  47. static constexpr int kMaxLogLines = 40;
  48. static constexpr int kAutoStopMs = 60 * 60 * 1000; // 启动 1 小时后自动停止
  49. };
  50. #endif // FRPCMANAGER_H