一、打包配置(desktop/installer/package.config.json) - 两个环境各自的域名:Development = http://127.0.0.1:7300,Production = 占位正式域名(发布前替换)。 - keepVersions = 5。 - build.ps1 -Environment Production 自动用生产域名打 exe 与下载地址;-ServerBaseUrl 可临时覆盖。 二、build.ps1 - 新增 -Environment / -Version / -Keep / -ReleaseNotes;版本默认取 csproj,可覆盖, 通过 -p:Version 注入程序集(AssemblyVersion/FileVersion 一起变)。 - 打完后:算 MD5、写 version.json(version/downloadUrl/md5/size/publishedAt/buildId/environment/releaseNotes), 复制 MSI → backend/app/public/download/,version.json → backend/app/public/version/。 - 本地 out/ 与后端目录各只保留最近 5 个安装包(按版本号排序清理)。 - 补丁版保护:后端已有更高版本时,不覆盖 version.json(避免补打旧版把版本降回去)。 三、后端静态分发 + 缓存(关键) - Egg 的 static 生产默认强缓存一年,version.json 若走静态会**永远检测不到更新**。 且实测 koa-static 命中文件后**不再 next()**,后置中间件改不了它的响应头(先写了 middleware 验证不可行,已删)。 → version.json 放到 app/public/version/(不在静态目录),由路由 GET /download/version.json 提供: Cache-Control: no-store, no-cache, must-revalidate, max-age=0 + Pragma/Expires,且不产生 ETag/Last-Modified。 - MSI 仍走 egg-static(prefix /download/,maxAge 31536000),文件名带版本号可长缓存。 - middleware/frontend.js 放行 /download/*,否则会被 SPA 入口接管。 - 实测响应头:version.json 无 ETag/Last-Modified、cache-control: no-store…; MSI cache-control: public, max-age=31536000。 四、桌面端自动更新(Services/UpdateChecker.cs + App) - 启动登录后后台请求 {后端}/download/version.json?t=毫秒时间戳(带 no-cache 头), 比较程序集版本;有新版则弹窗(版本/说明/大小),点是 → 浏览器打开 downloadUrl。 - 失败静默(网络问题不打扰);日志记录发现新版本/已打开下载。 - 实测:安装 1.0.1 → version.json 1.0.1 不提示;改成 1.0.2 后重启 → 日志「发现新版本 1.0.2(当前 1.0.1.0)→ 已打开下载」。 五、.gitignore - 忽略后端分发产物:backend/app/public/{download,version}/* 但保留 .gitkeep。 ⚠️ backend/config/config.default.js 本次提交同时带入了该文件里已有的未提交改动 (后端默认端口 7001 → 7300 及注释),因为 static 配置必须写在同一个文件里; 其它 backend/extension/frontend 的未提交 WIP 未包含在本次提交中。
77 lines
3.6 KiB
C#
77 lines
3.6 KiB
C#
using System;
|
||
using System.Net.Http;
|
||
using System.Text.Json;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace YuncangTrace.LocalService.Services;
|
||
|
||
/// <summary>发现的新版本(来自后端静态 `version.json`)</summary>
|
||
/// <param name="Version">新版本号(如 1.0.1)</param>
|
||
/// <param name="DownloadUrl">安装包地址(一般是后端 `/download/YuncangTraceSetup-x.y.z.msi`)</param>
|
||
/// <param name="Md5">安装包 MD5(可用于下载后校验)</param>
|
||
/// <param name="Size">安装包字节数(0 = 未知)</param>
|
||
/// <param name="Notes">更新说明(可为空)</param>
|
||
public sealed record UpdateInfo(string Version, string? DownloadUrl, string? Md5, long Size, string? Notes);
|
||
|
||
/// <summary>
|
||
/// 自动更新检查:请求后端的静态 `GET /download/version.json`,与当前程序版本比对。
|
||
///
|
||
/// `version.json` 由 `desktop/installer/build.ps1` 生成,内容形如:
|
||
/// <code>
|
||
/// { "version": "1.0.1", "downloadUrl": "https://站点/download/YuncangTraceSetup-1.0.1.msi",
|
||
/// "md5": "…", "size": 56712345, "publishedAt": "…", "buildId": "…", "releaseNotes": "…" }
|
||
/// </code>
|
||
///
|
||
/// ⚠️ 缓存是这里最容易踩的坑(Egg 的 static 生产默认强缓存一年):
|
||
/// - URL 带 `?t=毫秒时间戳`;
|
||
/// - 请求头带 `no-cache`;
|
||
/// - 后端 `middleware/download.js` 对 version.json 回 `no-store` 并去掉 ETag/Last-Modified。
|
||
/// 三层都做,是为了兼容中间可能存在的代理 / CDN。
|
||
/// </summary>
|
||
public static class UpdateChecker
|
||
{
|
||
/// <summary>版本信息地址(跟当前构建环境的后端地址走)</summary>
|
||
public static string VersionUrl(string serverBaseUrl)
|
||
=> serverBaseUrl.TrimEnd('/') + "/download/version.json";
|
||
|
||
/// <summary>
|
||
/// 检查是否有更新。
|
||
/// </summary>
|
||
/// <returns>有新版本返回它;已是最新 / 请求失败 / 数据不合法都返回 <c>null</c></returns>
|
||
public static async Task<UpdateInfo?> CheckAsync(
|
||
string serverBaseUrl,
|
||
Version current,
|
||
CancellationToken cancellationToken = default)
|
||
{
|
||
var url = $"{VersionUrl(serverBaseUrl)}?t={DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}";
|
||
|
||
using var http = new HttpClient { Timeout = TimeSpan.FromSeconds(8) };
|
||
using var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||
request.Headers.TryAddWithoutValidation("Cache-Control", "no-cache, no-store");
|
||
request.Headers.TryAddWithoutValidation("Pragma", "no-cache");
|
||
|
||
using var response = await http.SendAsync(request, HttpCompletionOption.ResponseContentRead, cancellationToken)
|
||
.ConfigureAwait(false);
|
||
if (!response.IsSuccessStatusCode) return null;
|
||
|
||
var json = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||
using var doc = JsonDocument.Parse(json);
|
||
var root = doc.RootElement;
|
||
|
||
if (!root.TryGetProperty("version", out var versionEl)) return null;
|
||
|
||
var text = versionEl.GetString();
|
||
if (string.IsNullOrWhiteSpace(text)) return null;
|
||
if (!Version.TryParse(text.Trim(), out var remote)) return null;
|
||
if (remote <= current) return null;
|
||
|
||
return new UpdateInfo(
|
||
text.Trim(),
|
||
root.TryGetProperty("downloadUrl", out var d) ? d.GetString() : null,
|
||
root.TryGetProperty("md5", out var m) ? m.GetString() : null,
|
||
root.TryGetProperty("size", out var s) && s.TryGetInt64(out var size) ? size : 0,
|
||
root.TryGetProperty("releaseNotes", out var n) ? n.GetString() : null);
|
||
}
|
||
}
|