| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- #ifndef FRPCMANAGER_H
- #define FRPCMANAGER_H
- #include <QObject>
- #include <QProcess>
- #include <QDateTime>
- #include <QStringList>
- class QTimer;
- // frpc 内网穿透进程管理
- // - 二进制以 jniLibs 方式打包(android/libs/arm64-v8a/libfrp.so),安装后位于
- // nativeLibraryDir,该目录允许 exec(规避 Android 10+ 对 data 目录的限制)
- // - 配置文件保存在 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;
- QStringList m_logRing; // 最近日志环形缓冲
- static constexpr int kMaxLogLines = 40;
- static constexpr int kAutoStopMs = 60 * 60 * 1000; // 启动 1 小时后自动停止
- };
- #endif // FRPCMANAGER_H
|