Compare commits

Author SHA1 Message Date
Hermes Agent 7a3b486952 feat(update): check-update 回传清单指纹,同版本号换文件也能更新
之前客户端在 Check() 里按版本号自行判重:latest_version == current_version 就
直接返回不更新,导致服务端重传的同号包永远下不来。

- Util: 新增 UrlEncodeQuery(查询参数百分号编码)
- Auth::CheckUpdate(version, manifestHash):带上本地清单指纹请求
- Update: UpdateInfo 增加 manifestHash;本地 update_state.json 记录指纹;
  版本号相同但服务端指纹变化时照常更新;新包落地后立即记住新指纹

已用 mingw-w64 g++ 14 / C++17 编译通过(Util/Auth/Update 三个 TU),
Windows 侧仍需 native\build.bat 出包。
2026-11-21 03:25:30 +08:00
6 changed files with 66 additions and 6 deletions
+6 -2
View File
@@ -370,8 +370,12 @@ AuthResult Auth::Info() {
return Call("info", json::object(), std::string(), true);
}
AuthResult Auth::CheckUpdate(const std::string& version) {
return Call("check-update?version=" + version, json::object(), std::string(), true);
AuthResult Auth::CheckUpdate(const std::string& version, const std::string& manifestHash) {
std::string path = "check-update?version=" + UrlEncodeQuery(version);
if (!manifestHash.empty()) {
path += "&manifest_hash=" + UrlEncodeQuery(manifestHash);
}
return Call(path, json::object(), std::string(), true);
}
std::string Auth::ResolveUrl(const std::string& url) {
+4 -2
View File
@@ -35,8 +35,10 @@ public:
static AuthResult Recharge(const std::string& username, const std::string& cardKey);
static AuthResult Heartbeat();
static AuthResult Info();
// 版本检测:version 为当前客户端版本号
static AuthResult CheckUpdate(const std::string& version);
// 版本检测:version 为当前客户端版本号,manifestHash 为本地当前清单指纹(可空)
// 带上指纹后,服务端能发现"版本号没变但文件被重新上传"的情况
static AuthResult CheckUpdate(const std::string& version,
const std::string& manifestHash = std::string());
// 把服务端返回的相对地址(如 /uploads/xxx.zip)补全为完整 URL
static std::string ResolveUrl(const std::string& url);
};
+35 -2
View File
@@ -10,6 +10,7 @@
#include <vector>
#include "Auth.h"
#include "Config.h"
#include "Json.h"
#include "Log.h"
#include "Util.h"
@@ -43,6 +44,24 @@ long long Int64(const json& obj, const char* key) {
return static_cast<long long>(it->get<double>());
}
// 本机记录的服务端清单指纹:用于判断"版本号没变但服务器换过包"
constexpr const char* kUpdateStateFile = "update_state.json";
std::string LoadStoredManifestHash() {
const json st = Config::GetJsonGlobal(kUpdateStateFile, json::object());
return Str(st, "manifest_hash");
}
void SaveStoredManifestHash(const std::string& hash, const std::string& version) {
if (hash.empty()) return;
json st = Config::GetJsonGlobal(kUpdateStateFile, json::object());
if (!st.is_object()) st = json::object();
if (Str(st, "manifest_hash") == hash && Str(st, "version") == version) return;
st["manifest_hash"] = hash;
if (!version.empty()) st["version"] = version;
Config::WriteTextGlobal(kUpdateStateFile, st.dump(2));
}
bool EnsureDir(const std::wstring& dir) {
if (::CreateDirectoryW(dir.c_str(), nullptr)) return true;
return ::GetLastError() == ERROR_ALREADY_EXISTS;
@@ -323,8 +342,9 @@ std::string Update::CurrentVersion() {
UpdateInfo Update::Check() {
UpdateInfo out;
out.currentVersion = CurrentVersion();
const std::string storedHash = LoadStoredManifestHash();
const AuthResult result = Auth::CheckUpdate(out.currentVersion);
const AuthResult result = Auth::CheckUpdate(out.currentVersion, storedHash);
if (!result.ok) {
out.error = result.message;
Log::Warn("版本检测失败:" + result.message);
@@ -335,6 +355,7 @@ UpdateInfo Update::Check() {
out.latestVersion = Str(data, "latest_version");
out.notes = Str(data, "update_notes");
out.fileHash = Str(data, "file_hash");
out.manifestHash = Str(data, "manifest_hash");
out.entryFile = Str(data, "entry_file");
out.fileSize = Int64(data, "file_size");
out.forced = Str(data, "update_strategy") == "forced";
@@ -344,7 +365,16 @@ UpdateInfo Update::Check() {
if (hasUpdate == data.end() || !hasUpdate->is_boolean() || !hasUpdate->get<bool>()) {
return out;
}
if (out.latestVersion.empty() || LowerAscii(out.latestVersion) == LowerAscii(out.currentVersion)) {
// 版本号相同也要看清单指纹:同版本号重新上传过文件时服务端会返回新的 manifest_hash,
// 此时应当照常更新,否则客户端会永远停在旧文件上
const bool sameVersion = LowerAscii(out.latestVersion) == LowerAscii(out.currentVersion);
const bool contentChanged = !out.manifestHash.empty() && !storedHash.empty() &&
LowerAscii(out.manifestHash) != LowerAscii(storedHash);
if (out.latestVersion.empty() || (sameVersion && !contentChanged)) {
// 已是最新:记住服务端当前指纹,供下次识别"文件被重新上传"
if (sameVersion && !out.manifestHash.empty()) {
SaveStoredManifestHash(out.manifestHash, out.latestVersion);
}
return out;
}
@@ -438,6 +468,9 @@ bool Update::Apply(const UpdateInfo& info, const std::function<void(int)>& progr
}
if (progress) progress(100);
// 新文件已落地:立刻记住这次的清单指纹,重启后不会因指纹不一致重复下载
SaveStoredManifestHash(info.manifestHash, info.latestVersion);
STARTUPINFOW si{};
si.cb = sizeof(si);
PROCESS_INFORMATION pi{};
+1
View File
@@ -14,6 +14,7 @@ struct UpdateInfo {
std::string notes;
std::string downloadUrl;
std::string fileHash;
std::string manifestHash; // 服务端版本清单指纹,用于识别同版本号换文件
std::string entryFile;
long long fileSize = 0;
std::string error;
+17
View File
@@ -401,4 +401,21 @@ void PlayAudioFile(const std::string& path) {
PlaySystemAlert();
}
std::string UrlEncodeQuery(std::string_view s) {
static const char* hex = "0123456789ABCDEF";
std::string out;
out.reserve(s.size() * 3);
for (unsigned char c : s) {
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
c == '-' || c == '_' || c == '.' || c == '~') {
out.push_back(static_cast<char>(c));
} else {
out.push_back('%');
out.push_back(hex[(c >> 4) & 0x0F]);
out.push_back(hex[c & 0x0F]);
}
}
return out;
}
}
+3
View File
@@ -25,6 +25,9 @@ std::string EscapeHtml(std::string_view value);
std::string Trim(std::string_view s);
// 查询参数百分号编码(保留 RFC3986 unreserved 字符)
std::string UrlEncodeQuery(std::string_view s);
std::wstring GetLocalAppDataDir();
// 程序所在目录