Update: drop Qt dependency, rewrite as pure Win32 using v142 toolset. Replace Update.ui/qrc and miniz-based Zip with Api/Crypto/Http/Json/Unzip/UpdateWindow modules. CrownClient/CrownServer: update Verify and main sources, retarget x64 configs to v142 (VS2019) with SDK 10.0.19041.0, fix VMProtect SDK paths.
172 lines
6.0 KiB
C++
172 lines
6.0 KiB
C++
#include "Api.h"
|
|
|
|
#include "Config.h"
|
|
#include "Crypto.h"
|
|
#include "Http.h"
|
|
#include "Json.h"
|
|
#include "Utils.h"
|
|
|
|
namespace updater {
|
|
|
|
namespace {
|
|
|
|
void ParseStringList(const Json& value, std::vector<std::wstring>& out) {
|
|
out.clear();
|
|
if (value.isArray()) {
|
|
for (size_t i = 0; i < value.arraySize(); ++i) {
|
|
std::wstring item = Utils::Utf8ToWide(value[static_cast<size_t>(i)].stringValue());
|
|
item = Utils::Trim(item);
|
|
if (!item.empty()) out.push_back(item);
|
|
}
|
|
return;
|
|
}
|
|
if (value.isString()) {
|
|
// 后台把 JSON 数组存成了 text 字段,这里再解析一次
|
|
std::string raw = value.stringValue();
|
|
Json parsed;
|
|
std::string err;
|
|
if (!raw.empty() && Json::Parse(raw, parsed, err) && parsed.isArray()) {
|
|
ParseStringList(parsed, out);
|
|
return;
|
|
}
|
|
std::wstring item = Utils::Trim(Utils::Utf8ToWide(raw));
|
|
if (!item.empty()) out.push_back(item);
|
|
}
|
|
}
|
|
|
|
std::wstring ReadString(const Json& obj, const char* key) {
|
|
if (!obj.isObject()) return std::wstring();
|
|
return Utils::Utf8ToWide(obj[key].stringValue());
|
|
}
|
|
|
|
unsigned long long ReadUInt64(const Json& obj, const char* key) {
|
|
if (!obj.isObject()) return 0;
|
|
return static_cast<unsigned long long>(obj[key].intValue(0));
|
|
}
|
|
|
|
}
|
|
|
|
std::wstring Api::BuildCheckUpdateUrl(const std::wstring& appKey,
|
|
const std::wstring& clientVersion) {
|
|
std::wstring url = std::wstring(UPDATER_SERVER_URL) + L"/api/v1/app/" +
|
|
Utils::UrlEncode(appKey) + L"/check-update";
|
|
if (!clientVersion.empty()) {
|
|
url += L"?version=" + Utils::UrlEncode(clientVersion);
|
|
}
|
|
return url;
|
|
}
|
|
|
|
std::wstring Api::ResolveUrl(const std::wstring& url) {
|
|
std::wstring trimmed = Utils::Trim(url);
|
|
if (trimmed.empty()) return std::wstring();
|
|
if (Utils::IsAbsoluteUrl(trimmed)) return trimmed;
|
|
|
|
std::wstring base = UPDATER_SERVER_URL;
|
|
while (!base.empty() && (base.back() == L'/' || base.back() == L'\\')) base.pop_back();
|
|
if (trimmed.front() != L'/') trimmed.insert(trimmed.begin(), L'/');
|
|
return base + trimmed;
|
|
}
|
|
|
|
Api::Result Api::CheckUpdate(const std::wstring& appKey, const std::wstring& clientVersion) {
|
|
Result result;
|
|
|
|
Http::SetTimeoutSec(UPDATER_TIMEOUT_SEC);
|
|
|
|
std::wstring url = BuildCheckUpdateUrl(appKey, clientVersion);
|
|
HttpResult http;
|
|
std::wstring err;
|
|
if (!Http::Get(url, http, err)) {
|
|
result.error = L"连接服务器失败:" + err;
|
|
return result;
|
|
}
|
|
|
|
Json root;
|
|
std::string parseErr;
|
|
if (!Json::Parse(http.body, root, parseErr)) {
|
|
result.error = L"服务器返回内容无法解析";
|
|
return result;
|
|
}
|
|
|
|
// 服务端开启通信加密时,data 是一整段 base64 密文,解密后才是真正的响应
|
|
#if UPDATER_ENCRYPT_TYPE != 0
|
|
if (root.isObject() && root["data"].isString()) {
|
|
std::string plain;
|
|
std::string cryptoErr;
|
|
std::string key = Utils::WideToUtf8(UPDATER_ENCRYPT_KEY);
|
|
if (!Crypto::Decrypt(static_cast<EncryptType>(UPDATER_ENCRYPT_TYPE), key,
|
|
root["data"].stringValue(), plain, cryptoErr)) {
|
|
result.error = L"响应解密失败:" + Utils::Utf8ToWide(cryptoErr);
|
|
return result;
|
|
}
|
|
if (!Json::Parse(plain, root, parseErr)) {
|
|
result.error = L"服务器返回内容无法解析";
|
|
return result;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
result.httpStatus = http.status;
|
|
result.code = static_cast<int>(root["code"].intValue(0));
|
|
result.message = Utils::Utf8ToWide(root["message"].stringValue());
|
|
|
|
// 业务层失败(如 404 应用不存在、400 版本号参数错误)
|
|
if (result.code != 200 && result.code != 0) {
|
|
result.error = result.message.empty() ? L"请求失败" : result.message;
|
|
return result;
|
|
}
|
|
|
|
// 正常响应字段在 data 里,兼容后台直接把字段放在根上的情况
|
|
const Json& data = root["data"].isObject() ? root["data"] : root;
|
|
if (!data.isObject()) {
|
|
result.error = L"服务器返回内容无法解析";
|
|
return result;
|
|
}
|
|
|
|
UpdateInfo& info = result.info;
|
|
info.hasUpdate = data["has_update"].boolValue(false);
|
|
info.appName = ReadString(data, "app_name");
|
|
info.currentVersion = ReadString(data, "current_version");
|
|
info.latestVersion = ReadString(data, "latest_version");
|
|
|
|
info.downloadUrl = ResolveUrl(ReadString(data, "download_url"));
|
|
info.downloadSize = ReadUInt64(data, "file_size");
|
|
info.downloadHash = ReadString(data, "file_hash");
|
|
|
|
info.entryFile = ReadString(data, "entry_file");
|
|
ParseStringList(data["entry_files"], info.entryFiles);
|
|
ParseStringList(data["desktop_shortcut_files"], info.desktopShortcutFiles);
|
|
|
|
info.updateNotes = ReadString(data, "update_notes");
|
|
info.changelog = ReadString(data, "changelog");
|
|
info.updateStrategy = ReadString(data, "update_strategy");
|
|
info.updateType = ReadString(data, "update_type");
|
|
info.updateMethod = ReadString(data, "update_method");
|
|
|
|
info.isPatch = data["is_patch"].boolValue(false);
|
|
info.patchUrl = ResolveUrl(ReadString(data, "patch_url"));
|
|
info.patchSize = ReadUInt64(data, "patch_size");
|
|
info.patchHash = ReadString(data, "patch_hash");
|
|
|
|
info.fullPackageUrl = ResolveUrl(ReadString(data, "full_package_url"));
|
|
info.fullPackageSize = ReadUInt64(data, "full_package_size");
|
|
info.fullPackageHash = ReadString(data, "full_package_hash");
|
|
|
|
// 补丁包信息缺失时用主下载地址兜底
|
|
if (info.isPatch && info.patchUrl.empty()) {
|
|
info.patchUrl = info.downloadUrl;
|
|
info.patchSize = info.downloadSize;
|
|
info.patchHash = info.downloadHash;
|
|
}
|
|
// 完整包信息缺失时用主下载地址兜底
|
|
if (info.fullPackageUrl.empty() && !info.isPatch) {
|
|
info.fullPackageUrl = info.downloadUrl;
|
|
info.fullPackageSize = info.downloadSize;
|
|
info.fullPackageHash = info.downloadHash;
|
|
}
|
|
|
|
result.ok = true;
|
|
return result;
|
|
}
|
|
|
|
}
|