#ifndef FRPCMANAGER_H #define FRPCMANAGER_H #include #include #include #include class QTimer; // frpc 内网穿透进程管理 // - 二进制以 jniLibs 方式打包(android/libs/armeabi-v7a/libfrp.so),安装后位于 // nativeLibraryDir,该目录允许 exec(规避 Android 10+ 对 data 目录的限制) // 本项目是 32 位(armeabi-v7a)构建,二进制由 tools/frpc-build/build_frpc32.sh // 交叉编译产出(必须 CGO_ENABLED=1 且打 Android DNS 补丁,原因见该目录 README) // - 配置文件保存在 AppDataLocation/frpc.toml,由 MQTT JSON-RPC 远程下发: // frpc_start / frpc_stop / frpc_status / frpc_config // - 运行超时保护:启动满 1 小时自动停止,避免长期占用资源 class FrpcManager : public QObject { Q_OBJECT public: explicit FrpcManager(QObject *parent = nullptr); ~FrpcManager() override; // 启动 frpc;inlineToml 非空时先写入配置文件再启动。 // 返回空串=成功,否则为错误描述 QString start(const QString &inlineToml); // 停止 frpc(SIGTERM→3s后SIGKILL)。返回空串=成功,否则为错误描述 QString stop(); // 状态查询:返回紧凑 JSON 字符串(running/pid/uptime/log_tail等) QString status() const; // 保存配置;restart 为 true 且运行中则重启。返回空串=成功,否则为错误描述 QString setConfig(const QString &toml, bool restart); signals: void stateChanged(bool running); private: QString binaryPath() const; QString configPath() const; // AppData/frpc.toml 不存在时,释放内嵌默认配置(编译期字符串)到该路径。 // 返回空串=成功(或已存在),否则为错误描述 QString ensureDefaultConfig() const; void appendLog(const QByteArray &raw); QProcess *m_proc = nullptr; QTimer *m_autoStopTimer = nullptr; // 运行超时看门狗:到期自动 stop() QDateTime m_startTime; int m_lastExitCode = 0; bool m_lastCrashed = false; quint64 m_runSeq = 0; // 启动序号:用于让"启动后复查"定时器在 stop/重启后失效 QStringList m_logRing; // 最近日志环形缓冲 static constexpr int kMaxLogLines = 40; static constexpr int kAutoStopMs = 60 * 60 * 1000; // 启动 1 小时后自动停止 }; #endif // FRPCMANAGER_H