feat(update): 在线升级 —— 界面一键检查版本、自动覆盖安装并重启

老板要求:软件里点「在线升级」校验版本号,直接在线覆盖安装、重启;
升级服务放在 https://md.wangjinding.com。

一、服务端(md.wangjinding.com)
  · 升级清单 GET /update/latest.json(nginx 单独 location,强制 no-store,带 CORS)
      { version, notes, pub_date, platforms.windows-x86_64: { signature, url, size, sha256 } }
  · 安装包走现有 /download/;另加稳定别名 latest-setup.exe / latest-portable.exe
    —— 官网页固定链到别名,以后发版不用改 HTML
  · tools/release/publish.py:一条命令完成「拷安装包 → 写清单 → 更新别名 → 打校验值」
  · 官网页的版本号改成从 latest.json 拉(data-ver),发版后自动跟随

二、客户端(Tauri updater 官方插件,不是自己下 exe 硬跑)
  · 新增 tauri-plugin-updater + tauri-plugin-process
  · 命令 check_update() → { available, current, version, notes, date }
         install_update() → 下载 + 校验签名 + 静默覆盖安装 + restart
  · 签名用 minisign:私钥 /root/.tauri/xiaoyin-md-updater.key(600,**不在仓库里**),
    公钥写进 tauri.conf.json;安装包被篡改会被客户端直接拒装
  · 工具条新增升级按钮(仅 Tauri 版显示);点击 → 查版本 → 有新版弹窗(带更新说明)
    → 「立即升级」显示下载进度条 → 装完自动重启
  · 版本号 2.6.1 → 2.6.2

三、已验证
  · 构建出 XiaoyinMarkdown_2.6.2_x64-setup.exe(2.21 MB)+ .sig(448B)
  · 用 cryptography 按 minisign 规则**真验了一遍签名**:Ed25519 + BLAKE2b 预哈希 → 通过
  · /update/latest.json 200 / application/json / no-store;sha256 与清单一致
  · latest-setup.exe / latest-portable.exe 均 200

注意:2.6.1 是**没有升级器**的版本,老用户要手动装一次 2.6.2;
    从 2.6.2 起才能在软件里一键升级。
This commit is contained in:
Jidekin committed 2026-11-20 10:22:26 +08:00
1 parent f53abeb0c8
commit 44335d02f2
10 files changed
+893 -27

No files matched your search

+74 -1
View File
@@ -459,6 +459,75 @@ fn migrate_apply(app: AppHandle, mode: String, docs: Vec<serde_json::Value>, kee
Ok(())
}
/* ---------------- 在线升级 ---------------- */
/// 检查结果回给前端:available=false 时 version 就是当前版本
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct UpdateStatus {
available: bool,
current: String,
version: String,
notes: String,
date: String,
}
/// 查 https://md.wangjinding.com/update/latest.json 有没有比当前新的版本。
/// 用官方 updater 插件:带 minisign 签名校验,装的是签名过的安装包,避免被中间人替换。
#[tauri::command]
async fn check_update(app: AppHandle) -> Result<UpdateStatus, String> {
use tauri_plugin_updater::UpdaterExt;
let current = app.package_info().version.to_string();
let updater = app.updater().map_err(|e| format!("初始化升级器失败:{e}"))?;
match updater.check().await {
Ok(Some(u)) => Ok(UpdateStatus {
available: true,
current,
version: u.version.clone(),
notes: u.body.clone().unwrap_or_default(),
date: u.date.map(|d| d.to_string()).unwrap_or_default(),
}),
Ok(None) => Ok(UpdateStatus {
available: false,
version: current.clone(),
current,
notes: String::new(),
date: String::new(),
}),
Err(e) => Err(format!("检查更新失败:{e}")),
}
}
/// 下载 + 校验签名 + 跑安装包覆盖安装,然后重启自己。
/// 进度通过 `update-progress` 事件推给前端(chunk 为已下载字节,total 可能为 null)。
#[tauri::command]
async fn install_update(app: AppHandle) -> Result<(), String> {
use tauri_plugin_updater::UpdaterExt;
let updater = app.updater().map_err(|e| format!("初始化升级器失败:{e}"))?;
let found = updater.check().await.map_err(|e| format!("检查更新失败:{e}"))?;
let Some(update) = found else {
return Err("已经是最新版本".into());
};
let handle = app.clone();
let mut downloaded: u64 = 0;
update
.download_and_install(
move |chunk: usize, total: Option<u64>| {
downloaded += chunk as u64;
let _ = handle.emit(
"update-progress",
serde_json::json!({ "downloaded": downloaded, "total": total }),
);
},
|| {},
)
.await
.map_err(|e| format!("下载或安装失败:{e}"))?;
// 安装包已静默跑完并覆盖了程序,重启到新版本
let _ = app.emit("update-restarting", serde_json::json!({}));
app.restart();
}
/* ---------------- 入口 ---------------- */
fn main() {
@@ -491,6 +560,8 @@ fn main() {
}
}
}))
.plugin(tauri_plugin_updater::Builder::new().build())
.plugin(tauri_plugin_process::init())
.invoke_handler(tauri::generate_handler![
read_text_file,
write_text_file,
@@ -507,7 +578,9 @@ fn main() {
open_new_window,
force_close,
get_window_count,
migrate_apply
migrate_apply,
check_update,
install_update
])
.on_window_event(|window, event| {
if let WindowEvent::CloseRequested { api, .. } = event {