Compare commits

..
7 Commits
Author SHA1 Message Date
zerorigin2019 2bd2b6d1af 发布 0.2.2:全部分发产物带签名,修发布脚本读 manifest 的编码
mygo.json version -> 0.2.2;CHANGELOG 加 0.2.2 小节。publish-gitea.ps1 读 update-windows-amd64.json 时补 -Encoding UTF8(PS 5.1 默认 ANSI 解码,中文 notes 乱码导致 ConvertFrom-Json 直接失败)。AGENTS.md 补该坑。产物:setup/tar.gz/portable.zip 全部签名 Valid;端到端 0.2.1->0.2.2 走 24KB delta。
2026-11-16 11:14:46 +08:00
zerorigin2019 37827eb8c7 接入自签名代码签名:主 exe、watchdog、安装包与卸载器统一签名
mygo.json 增加 windows.signCommand -> scripts/codesign/sign-file.ps1(PowerShell Set-AuthenticodeSignature,不依赖 Windows SDK)。新增 new-cert.ps1 生成 10 年期自签名证书、trust.ps1 导入当前用户 Root + TrustedPublisher(certutil 静默导入)。公钥 certs/ZeroOrigin-CodeSign.cer 进仓库,*.pfx 进 .gitignore。AGENTS.md / 架构文档 / README 同步签名流程与三条坑。
2026-11-16 11:06:28 +08:00
zerorigin2019 2886a61d10 发布 0.2.1:更新链路验证版本 2026-11-16 10:49:47 +08:00
zerorigin2019 3d41888327 发布脚本:同时上传 install.ps1 一键安装脚本 2026-11-16 10:42:21 +08:00
zerorigin2019 b36b1ca071 发布流程:修 Release 中文乱码,补安装版与便携版分发
PowerShell 5.1 的 Invoke-RestMethod 传字符串 body 会按 ISO-8859-1 编码,Release 标题/正文的中文全变成 ?;改为 UTF-8 字节 + charset,并对已有 Release 做 PATCH。build.bat 生成便携版 zip 并把它的 SHA256 补进 SHA256SUMS,发布脚本同时上传 setup 安装包与便携版 zip(Gitea Release 附件实测接受 .exe)。新增 -MetadataOnly 只刷新文案;修 build.bat 读 mygo.json 未指定 UTF8 导致版本号取空的问题。
2026-11-16 10:40:39 +08:00
zerorigin 534b5555a7 AGENTS.md:补发布脚本的 UTF-8 BOM 坑 2026-11-16 10:33:13 +08:00
zerorigin 8891b565d1 保持发布脚本的 UTF-8 BOM(Windows PowerShell 5.1 按 ANSI 解析会乱码) 2026-11-16 10:32:47 +08:00
12 changed files with 365 additions and 30 deletions

No files matched your search

+3
View File
@@ -6,6 +6,9 @@ build/
*.exe *.exe
*.dll *.dll
# 代码签名私钥(公钥 certs/*.cer 可以提交,.pfx 绝不进仓库)
*.pfx
# 本地配置与日志(运行时生成) # 本地配置与日志(运行时生成)
config.json config.json
*.bak *.bak
+52 -9
View File
@@ -43,22 +43,40 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原
(含亮度 DDC/CI + gamma 兜底); (含亮度 DDC/CI + gamma 兜底);
`internal/assets` 托盘 PNG(go:embed);`internal/logging` slog;`internal/version` ldflags; `internal/assets` 托盘 PNG(go:embed);`internal/logging` slog;`internal/version` ldflags;
- `resources/windows-amd64/` 随 exe 复制的平台资源(build.bat 编译出 watchdog.exe); - `resources/windows-amd64/` 随 exe 复制的平台资源(build.bat 编译出 watchdog.exe);
`assets/` 图标源文件;`mygo.json` mygo 应用清单;`bin/` 产物(gitignore);`portable/` 便携版。 `assets/` 图标源文件;`mygo.json` mygo 应用清单;`bin/` 产物(gitignore);`portable/` 便携版;
- `scripts/` 发布与签名脚本:`publish-gitea.ps1`(上传 Gitea Release);`codesign/new-cert.ps1`
(生成自签名代码签名证书)、`codesign/sign-file.ps1`(mygo 的 `signCommand` 调用)、
`codesign/trust.ps1`(把公钥证书导入当前用户信任存储);`certs/` 只放公钥证书。
## 构建与验证 ## 构建与验证
- 一键构建:`build.bat`(watchdog → `go tool mygo build`(含 NSIS 安装包)→ 便携版); - 一键构建:`build.bat`(watchdog → `go tool mygo build`(含 NSIS 安装包)→ 便携版目录 + 便携版 zip);
`build.bat quick` 跳过安装包(首次生成安装包时 mygo 会下载 NSIS); `build.bat quick` 跳过安装包(首次生成安装包时 mygo 会下载 NSIS),且会清掉输出目录里的 setup exe,
要发安装版必须跑完整构建。产物是三种分发形态:安装版 `zerorigin-toolbox-setup-<ver>-amd64.exe`、
便携版 `zerorigin-toolbox-<ver>-windows-amd64-portable.zip`、自更新包
`<slug>-<ver>-windows-amd64.tar.gz` + `update-windows-amd64.json`;便携版 zip 的 SHA256 由
build.bat 追加进 `bin/SHA256SUMS`(mygo 写校验和时 zip 还不存在);
- 签名自更新:`mygo.json` 的 `updates.publicKey` 是 Ed25519 公钥,私钥在 - 签名自更新:`mygo.json` 的 `updates.publicKey` 是 Ed25519 公钥,私钥在
`%APPDATA%\mygo\update-keys\mygo-update.key`(**绝不进仓库**,用户需自行备份;`build.bat` 会读进 `%APPDATA%\mygo\update-keys\mygo-update.key`(**绝不进仓库**,用户需自行备份;`build.bat` 会读进
`MYGO_UPDATER_PRIVATE_KEY` 环境变量)。私钥缺失时 `go tool mygo build` 只构建、不产出更新包且不报错, `MYGO_UPDATER_PRIVATE_KEY` 环境变量)。私钥缺失时 `go tool mygo build` 只构建、不产出更新包且不报错,
容易误以为已经发版(构建日志里有「已加载更新签名私钥」才是有签名的构建); 容易误以为已经发版(构建日志里有「已加载更新签名私钥」才是有签名的构建);
- 代码签名(Authenticode,自签名):`mygo.json` 的 `windows.signCommand` 指向
`scripts/codesign/sign-file.ps1`,mygo 会依次签主 exe、资源里的 watchdog.exe、安装包与卸载器
(`signWindowsResources` 只签未签名的 PE,所以 setup 里的卸载器也签上了)。证书由
`scripts/codesign/new-cert.ps1` 生成(10 年,`CN=ZeroOrigin ToolBox`),公钥
`certs/ZeroOrigin-CodeSign.cer` 进仓库、私钥备份在 `%APPDATA%\ZerOriginTool\codesign\`
(**绝不进仓库**,`.gitignore` 已忽略 `*.pfx`);新机器跑 `scripts/codesign/trust.ps1` 导入
当前用户的 Root + TrustedPublisher。自签名只让**导入过根证书的机器**不再提示「未知发布者」,
对外分发无效(别人仍提示,SmartScreen 甚至更谨慎);
- 发版顺序:改 `mygo.json` 的 `version` → 在 `CHANGELOG.md` 加同名 `## <version>` 小节(缺失时 mygo 报 - 发版顺序:改 `mygo.json` 的 `version` → 在 `CHANGELOG.md` 加同名 `## <version>` 小节(缺失时 mygo 报
no notes)→ `build.bat quick` → `scripts/publish-gitea.ps1 -Owner zerorigin -Repo zerorigin-toolbox`; no notes)→ `build.bat`(要安装版必须完整构建,`quick` 会跳过 setup)→
`scripts/publish-gitea.ps1 -Owner zerorigin -Repo zerorigin-toolbox`;
- 发布脚本为什么自己写:`go tool mygo build -upload` 只支持 GitHub Release(依赖 gh CLI)或 S3 桶, - 发布脚本为什么自己写:`go tool mygo build -upload` 只支持 GitHub Release(依赖 gh CLI)或 S3 桶,
自建 Gitea 没有内置支持。脚本用 Gitea REST API 把归档、delta、SHA256SUMS 传到**固定的 `latest` 自建 Gitea 没有内置支持。脚本用 Gitea REST API 把归档、delta、SHA256SUMS、安装版 setup 与便携版 zip
Release**,同名 manifest **先删旧附件再传、并且最后传**(客户端一旦看到新 manifest,它指向的归档 传到**固定的 `latest` Release**(新用户一个页面拿全部形态),同名 manifest **先删旧附件再传、
必须已经存在);客户端只读 `<updates.url>/update-windows-amd64.json`; 并且最后传**(客户端一旦看到新 manifest,它指向的归档必须已经存在);客户端只读
`<updates.url>/update-windows-amd64.json`。改文案/修乱码用 `-MetadataOnly`,只 PATCH 标题正文、
不重传几十 MB 附件;
- 工具链:go 与 mygo 在 `C:\Users\AlwaysTrue\go\bin`(不在 PATH 时先 export); - 工具链:go 与 mygo 在 `C:\Users\AlwaysTrue\go\bin`(不在 PATH 时先 export);
- `go build ./...`、`go test ./...`(action/config/rule/trigger/platform/settings 均有单测); - `go build ./...`、`go test ./...`(action/config/rule/trigger/platform/settings 均有单测);
- `go vet ./...` 只应有 crashguard.go / win32.go 的 3 条 unsafe 警告; - `go vet ./...` 只应有 crashguard.go / win32.go 的 3 条 unsafe 警告;
@@ -195,8 +213,8 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原
- 自更新有三条硬前提,缺一条就**静默不做后台检查**:`updates.publicKey` 非空、构建时注入了 feed - 自更新有三条硬前提,缺一条就**静默不做后台检查**:`updates.publicKey` 非空、构建时注入了 feed
(`updates.github` 或 `updates.url`,二者互斥)、`!IsDev()`,并且 exe 所在目录可写(`mygo.Updater.Enabled()`); (`updates.github` 或 `updates.url`,二者互斥)、`!IsDev()`,并且 exe 所在目录可写(`mygo.Updater.Enabled()`);
- 发布源是自建 Gitea:`updates.url` = `https://git.zerorigin.cn/zerorigin/zerorigin-toolbox/releases/download/latest` - 发布源是自建 Gitea:`updates.url` = `https://git.zerorigin.cn/zerorigin/zerorigin-toolbox/releases/download/latest`
(2026-11-16 实测:TLS 正常、匿名可下载、单附件上限 100MB、`allowed_types` 含 `.gz`/`.json` 但**不含 (2026-11-16 实测:TLS 正常、匿名可下载、单附件上限 100MB;`allowed_types` 白名单只约束 issue 类附件,
`.delta`**,所以 delta 上传会被拒、脚本直接跳过)。`updates.github` 天然写死 github.com Release 附件连 `.exe` 与 `.delta` 都接受)。`updates.github` 天然写死 github.com
(`cmd/mygo/updates.go:126`),国内网络常连接超时(实测 `dial tcp 20.6.7.17:443`),不要换回去; (`cmd/mygo/updates.go:126`),国内网络常连接超时(实测 `dial tcp 20.6.7.17:443`),不要换回去;
- 固定 tag `latest` 是 URL 的一部分:**改名等于换渠道**,已发出去的 exe 不会跟过来;Release `latest` - 固定 tag `latest` 是 URL 的一部分:**改名等于换渠道**,已发出去的 exe 不会跟过来;Release `latest`
里的附件不能删,删了客户端就再也检查不到更新; 里的附件不能删,删了客户端就再也检查不到更新;
@@ -206,6 +224,31 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原
window-state.json、updater.json)自然保留;被替换的旧文件先改名成 `.<name>.old-<pid>`,由下次启动的 window-state.json、updater.json)自然保留;被替换的旧文件先改名成 `.<name>.old-<pid>`,由下次启动的
`cleanUpdateLeftovers` 清掉; `cleanUpdateLeftovers` 清掉;
- 插件窗口语言固定 `updater.Options{Language: "zh"}`(`internal/app/update.go`),不要依赖系统语言; - 插件窗口语言固定 `updater.Options{Language: "zh"}`(`internal/app/update.go`),不要依赖系统语言;
- `scripts/publish-gitea.ps1` 必须保持 **UTF-8 带 BOM**:Windows PowerShell 5.1 对无 BOM 脚本按 ANSI(GBK)
解析,中文注释会把引号配对搞乱,报 `UnexpectedToken` / `缺少右")"`。改完用
`[System.IO.File]::WriteAllText($p, $t, (New-Object System.Text.UTF8Encoding($true)))` 重写;
- Release 标题/正文的中文变成 `?`:PowerShell 5.1 的 `Invoke-RestMethod -Body <string>` 按
ISO-8859-1 编码发送。必须自己 `[System.Text.Encoding]::UTF8.GetBytes(...)` 传字节,并带
`Content-Type: application/json; charset=utf-8`。正文里既要 Markdown 反引号又要变量时用单引号
+ `-f`(双引号里 ```$` 会把 `$` 转义,正文会留下字面 `$(...)`);
- `build.bat` 读 `mygo.json` 的版本号必须 `Get-Content -Raw -Encoding UTF8`:不加会按 GBK 读中文,
`ConvertFrom-Json` 报 `Invalid object passed in, ':' or '}' expected`,版本号取空 → 便携版 zip
被命名成 `0.0.0`。zip 名对不上发布脚本的过滤器就传不上去;同理 `publish-gitea.ps1` 读
`update-<target>.json` 必须 `Get-Content -Raw -Encoding UTF8`:不加时 manifest 里的中文 notes
被按 GBK 解码,`ConvertFrom-Json` 报「传入的对象无效,应为“:”或“}”」,发布第一步就中断(0.2.2 踩到);
- Gitea 的 `allowed_types` 白名单只影响 issue 之类附件,**Release 附件实测接受 `.exe` 与 `.delta`**
(setup 安装包、增量包都直接上传成功),不需要再套一层 zip;脚本里保留「delta 失败只警告」的容错;
- 自签名证书**不能用 mygo 的 `windows.certificate` 路径**:它固定给 signtool 传
`/tr http://timestamp.digicert.com`(`cmd/mygo/winsign.go:79-82`,空字符串也会被填成该默认值),
DigiCert 不给自签名证书打时间戳,signtool 报错、整个构建失败。自签名必须走 `windows.signCommand`
且不带 `/tr`;代价是证书 2036 年到期后旧签名失效,重新发证重签即可;
- 往 `Cert:\CurrentUser\Root` 导入证书**不要用 `Import-Certificate`**:它要交互式 UI 确认,脚本里
报 `UI is not allowed in this operation`,还会卡住等对话框;用 `certutil -user -addstore Root <cer>`
(静默,见 `scripts/codesign/trust.ps1`);
- 本机没有 Windows SDK 与 signtool:签名用 PowerShell 内置的 `Set-AuthenticodeSignature`(.NET)即可,
不要为签名去装几 GB 的 SDK;签名前必须先停掉正在跑的 exe,否则报「文件正被另一个进程使用」;
- 签名会改变 exe 内容:已发布的旧包与本地新包即使版本号相同也不一致,改签名后必须重新构建、
重新发布(delta 由 mygo 重新生成,不影响更新链路);
- vet 对 crashguard.go / win32.go 的 unsafe.Pointer 警告是有意为之,不要"修复"。 - vet 对 crashguard.go / win32.go 的 unsafe.Pointer 警告是有意为之,不要"修复"。
## 风格 ## 风格
+17
View File
@@ -4,6 +4,23 @@ mygo 构建会用 `## <version>` 段落作为更新窗口里的发布说明:
`mygo.json` 的 `version` 改掉,再在下面加一段同名小节,否则 `mygo build` 会报 `mygo.json` 的 `version` 改掉,再在下面加一段同名小节,否则 `mygo build` 会报
“CHANGELOG.md has no notes for <version>”。 “CHANGELOG.md has no notes for <version>”。
## 0.2.2 - 2026-11-16
代码签名版本:所有分发产物都带 Authenticode 签名,导入过根证书的机器不再提示「无法验证发布者」。
- 主程序、watchdog、安装包与卸载器统一签名(`mygo.json` 的 `windows.signCommand`)
- 新增 `scripts/codesign/`:生成自签名证书、签名单个文件、导入信任三个脚本
- 公钥证书 `certs/ZeroOrigin-CodeSign.cer`,新机器跑 `trust.ps1` 后生效
- 程序行为与 0.2.1 一致
## 0.2.1 - 2026-11-16
更新链路验证版本:程序行为与 0.2.0 一致,主要用来验证「检查更新 → 下载 → 就地替换 → 重启」。
- 发布页同时提供安装版(setup)、便携版(zip)与自更新包,安装版与便携版都能自更新
- 修复更新说明在 Gitea 页面上显示为“?”的问题(发布脚本改用 UTF-8 字节发送 JSON)
- 附带 SHA256SUMS,下载后可核对安装包与更新包
## 0.2.0 - 2026-11-16 ## 0.2.0 - 2026-11-16
首个 mygo 单进程版本。 首个 mygo 单进程版本。
+19 -1
View File
@@ -20,7 +20,8 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原
1. 改 `mygo.json` 的 `version`; 1. 改 `mygo.json` 的 `version`;
2. 在 `CHANGELOG.md` 里加 `## <版本号>` 段(mygo 用它做更新窗口的发布说明); 2. 在 `CHANGELOG.md` 里加 `## <版本号>` 段(mygo 用它做更新窗口的发布说明);
3. `build.bat quick`(需要 `%APPDATA%\mygo\update-keys\mygo-update.key` 才会签名并产出更新包); 3. `build.bat`(完整构建才产出安装版;`quick` 会跳过 setup。需要
`%APPDATA%\mygo\update-keys\mygo-update.key` 才会签名并产出更新包);
4. 发布: 4. 发布:
```powershell ```powershell
@@ -29,3 +30,20 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原
``` ```
脚本会先传归档与 delta,最后传 manifest,并先删除同名的旧 manifest 附件。 脚本会先传归档与 delta,最后传 manifest,并先删除同名的旧 manifest 附件。
## 代码签名(自签名)
构建产物(`ZerOriginTool.exe`、`watchdog.exe`、安装包与卸载器)都带 Authenticode 签名,
用于消除本机的「发布商:未知发布者」提示:
- 公钥证书:[`certs/ZeroOrigin-CodeSign.cer`](certs/ZeroOrigin-CodeSign.cer);
- 新机器先导入信任(当前用户,无需管理员):
```powershell
powershell -NoProfile -ExecutionPolicy Bypass -File scripts\codesign\trust.ps1
```
- 私钥备份在 `%APPDATA%\ZerOriginTool\codesign\`(**不进仓库**,换机/重装前请备份)。
自签名只对**导入过这张根证书的机器**有效;对外分发仍会提示未知发布者,要彻底解决需要
CA 签发的代码签名证书。
+13
View File
@@ -48,8 +48,21 @@ if not exist portable mkdir portable
copy /y bin\windows-amd64\ZerOriginTool.exe portable\ZerOriginTool.exe >nul copy /y bin\windows-amd64\ZerOriginTool.exe portable\ZerOriginTool.exe >nul
copy /y bin\windows-amd64\watchdog.exe portable\watchdog.exe >nul copy /y bin\windows-amd64\watchdog.exe portable\watchdog.exe >nul
rem 便携版压缩包:新用户下载解压即用。自更新用的 tar.gz 也能解压,但文件名和内容(.mygo-files)
rem 是给更新器看的,不适合当分发物。
rem 注意 -Encoding UTF8:mygo.json 是 UTF-8(含中文),不加会被按 GBK 读成乱码导致 ConvertFrom-Json 失败。
for /f "tokens=*" %%v in ('powershell -NoProfile -Command "(Get-Content mygo.json -Raw -Encoding UTF8 | ConvertFrom-Json).version"') do set "APPVER=%%v"
if not defined APPVER set "APPVER=0.0.0"
powershell -NoProfile -Command "Compress-Archive -Path 'portable\ZerOriginTool.exe','portable\watchdog.exe','portable\README.txt' -DestinationPath ('bin\windows-amd64\zerorigin-toolbox-' + $env:APPVER + '-windows-amd64-portable.zip') -Force"
if errorlevel 1 echo [build] 便携版压缩包生成失败(不影响其他产物)
rem 便携版压缩包也记进 bin\SHA256SUMS(mygo 写校验和时 zip 还不存在),方便用户校验下载。
for /f "tokens=*" %%h in ('powershell -NoProfile -Command "(Get-FileHash (Join-Path 'bin\windows-amd64' ('zerorigin-toolbox-' + $env:APPVER + '-windows-amd64-portable.zip')) -Algorithm SHA256).Hash.ToLower()"') do set "ZIPHASH=%%h"
if defined ZIPHASH echo %ZIPHASH% zerorigin-toolbox-%APPVER%-windows-amd64-portable.zip>>bin\SHA256SUMS
echo [build] 完成,产物: echo [build] 完成,产物:
dir /b bin\windows-amd64\*.exe dir /b bin\windows-amd64\*.exe
dir /b bin\windows-amd64\*.zip 2>nul
echo 便携版:portable\ echo 便携版:portable\
exit /b 0 exit /b 0
Binary file not shown.
+38 -3
View File
@@ -348,14 +348,49 @@ build.bat quick :: 跳过安装包
而 config.json、window-state.json、updater.json 等便携数据不在 `.mygo-files` 里,天然保留。 而 config.json、window-state.json、updater.json 等便携数据不在 `.mygo-files` 里,天然保留。
发版顺序:改 `mygo.json` 的 `version` → `CHANGELOG.md` 加同名 `## <version>` 小节 → 发版顺序:改 `mygo.json` 的 `version` → `CHANGELOG.md` 加同名 `## <version>` 小节 →
`build.bat quick`(日志出现「已加载更新签名私钥」才算签名构建)→ `scripts/publish-gitea.ps1` `build.bat`(日志出现「已加载更新签名私钥」才算签名构建;要发安装版必须跑完整构建,`quick` 会
把 tar.gz、delta、SHA256SUMS 传到固定的 `latest` Release,manifest 最后传。 `-skip-installer` 并清掉输出目录里的 setup exe)→ `scripts/publish-gitea.ps1`
把 tar.gz、delta、SHA256SUMS、安装版 setup exe、便携版 zip 传到固定的 `latest` Release,
manifest 最后传;只改文案不重传附件用 `-MetadataOnly`。
三种分发形态挂在同一个 Release 上,新用户一个页面就能选:
安装版 `zerorigin-toolbox-setup-<version>-amd64.exe`(NSIS,装到当前用户目录、无需管理员)、
便携版 `zerorigin-toolbox-<version>-windows-amd64-portable.zip`(解压即用,配置写在 exe 同目录)、
自更新包 `zerorigin-toolbox-<version>-windows-amd64.tar.gz` + `update-windows-amd64.json`
(程序内托盘「检查更新…」自动使用,不用手动下载)。便携版 zip 的校验和由 `build.bat` 追加进
`bin/SHA256SUMS`。
### 9.2 代码签名(Authenticode)
没有代码签名时,用户双击安装包会看到「无法验证发布者 / 发布商:未知发布者」。彻底消除它需要
CA 签发的代码签名证书(OV/EV);自建/自用场景可以先用**自签名证书**,只对导入过该根证书的机器生效。
当前方案(自签名,2026-11 起):
- 证书:`scripts/codesign/new-cert.ps1` 生成 10 年期自签名代码签名证书(`CN=ZeroOrigin ToolBox`,
EKU=Code Signing),公钥 `certs/ZeroOrigin-CodeSign.cer` 进仓库,私钥导出到
`%APPDATA%\ZerOriginTool\codesign\`(`.gitignore` 忽略 `*.pfx`,**绝不进仓库**);
- 签名:`mygo.json` 的 `windows.signCommand` 指向 `scripts/codesign/sign-file.ps1`,mygo 构建时
依次签主 exe、资源里的 watchdog.exe、安装包与卸载器;
- 信任:新机器跑 `scripts/codesign/trust.ps1`,用 `certutil -user -addstore` 把公钥证书写进
当前用户的「受信任的根证书颁发机构」与「受信任的发布者」,之后本机双击不再提示未知发布者。
为什么不用 mygo 的 `windows.certificate`:那条路径固定给 signtool 传 DigiCert 时间戳参数,
自签名证书拿不到时间戳,签名直接失败;`signCommand` 走 PowerShell 内置的
`Set-AuthenticodeSignature`,不需要 Windows SDK。代价是没有 RFC 3161 时间戳,证书到期后
旧包的签名会失效——自用场景可接受,重新发证重签即可。
自签名不改变对外分发的现实:别人仍然提示「未知发布者」,SmartScreen 甚至更谨慎;要真正消除,
需要购买 OV/EV 证书(EV 通过硬件令牌或云签名服务,mygo 用 `windows.signCommand` 同样能接)。
**发布渠道**:`updates.url` 指向自建 Gitea **发布渠道**:`updates.url` 指向自建 Gitea
`https://git.zerorigin.cn/zerorigin/zerorigin-toolbox/releases/download/latest`。 `https://git.zerorigin.cn/zerorigin/zerorigin-toolbox/releases/download/latest`。
`go tool mygo build -upload` 用不上(只支持 GitHub Release 的 gh CLI 或 S3 桶),所以有发布脚本。 `go tool mygo build -upload` 用不上(只支持 GitHub Release 的 gh CLI 或 S3 桶),所以有发布脚本。
固定 tag `latest` 是 URL 的一部分,改名等于换渠道;Release 里的附件不能删,删了客户端就检查不到更新。 固定 tag `latest` 是 URL 的一部分,改名等于换渠道;Release 里的附件不能删,删了客户端就检查不到更新。
Gitea 附件白名单不含 `.delta`、单附件上限 100MB(2026-11-16 实测)。 Gitea 单附件上限 100MB;`allowed_types` 白名单只约束 issue 类附件,Release 附件实测连
`.exe` 与 `.delta` 都接受(setup 直接上传,不必再套 zip)(2026-11-16 实测)。
脚本写 Release 标题/正文必须自己编成 UTF-8 字节发送:PowerShell 5.1 的 `Invoke-RestMethod`
传字符串 body 时按 ISO-8859-1 编码,中文全变成 `?`。
`updates.github` 天然写死 github.com,国内常连接超时(`dial tcp 20.6.7.17:443`),不要换回去。 `updates.github` 天然写死 github.com,国内常连接超时(`dial tcp 20.6.7.17:443`),不要换回去。
mygo.json:`name` 决定 exe 名(ZerOriginTool.exe)、`windows.displayName` 是中文显示名、 mygo.json:`name` 决定 exe 名(ZerOriginTool.exe)、`windows.displayName` 是中文显示名、
+3 -2
View File
@@ -2,7 +2,7 @@
"name": "ZerOriginTool", "name": "ZerOriginTool",
"slug": "zerorigin-toolbox", "slug": "zerorigin-toolbox",
"identifier": "cn.zerorigin.toolbox", "identifier": "cn.zerorigin.toolbox",
"version": "0.2.0", "version": "0.2.2",
"copyright": "© 2026 零源", "copyright": "© 2026 零源",
"publisher": "零源", "publisher": "零源",
"main": "./cmd/zerorigin", "main": "./cmd/zerorigin",
@@ -14,6 +14,7 @@
}, },
"windows": { "windows": {
"displayName": "零源工具箱", "displayName": "零源工具箱",
"installDirName": "ZerOriginTool" "installDirName": "ZerOriginTool",
"signCommand": "powershell -NoProfile -ExecutionPolicy Bypass -File scripts\\codesign\\sign-file.ps1 %1"
} }
} }
+57
View File
@@ -0,0 +1,57 @@
#Requires -Version 5.1
<#
生成自签名代码签名证书(仅用于本机与受控机器,不能消除别人的「未知发布者」提示)。
产物:
certs\ZeroOrigin-CodeSign.cer 公钥证书,可提交仓库,供其他机器导入信任
%APPDATA%\ZerOriginTool\codesign\*.pfx 含私钥的备份(绝不进仓库),重装系统后用它恢复
%APPDATA%\ZerOriginTool\codesign\password.txt pfx 密码
用法:powershell -NoProfile -ExecutionPolicy Bypass -File scripts\codesign\new-cert.ps1 [-Force]
#>
[CmdletBinding()]
param(
[string]$Subject = 'CN=ZeroOrigin ToolBox, O=ZeroOrigin, C=CN',
[int]$Years = 10,
[switch]$Force
)
$ErrorActionPreference = 'Stop'
$root = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) # 仓库根
$cerDir = Join-Path $root 'certs'
$backupDir = Join-Path $env:APPDATA 'ZerOriginTool\codesign'
New-Item -ItemType Directory -Force -Path $cerDir, $backupDir | Out-Null
$existing = Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert |
Where-Object { $_.Subject -eq $Subject -and $_.HasPrivateKey }
if ($existing -and -not $Force) {
"已存在同名证书:$($existing[0].Thumbprint)($($existing[0].NotAfter.ToString('yyyy-MM-dd')) 到期);要重建请加 -Force"
exit 0
}
$cert = New-SelfSignedCertificate `
-Type CodeSigningCert `
-Subject $Subject `
-KeyUsage DigitalSignature `
-KeyAlgorithm RSA `
-KeyLength 3072 `
-HashAlgorithm SHA256 `
-CertStoreLocation Cert:\CurrentUser\My `
-NotAfter (Get-Date).AddYears($Years)
$cerPath = Join-Path $cerDir 'ZeroOrigin-CodeSign.cer'
Export-Certificate -Cert $cert -FilePath $cerPath -Force | Out-Null
$password = -join ((48..57) + (65..90) + (97..122) | Get-Random -Count 24 | ForEach-Object { [char]$_ })
$secure = ConvertTo-SecureString -String $password -AsPlainText -Force
$pfxPath = Join-Path $backupDir 'ZeroOrigin-CodeSign.pfx'
Export-PfxCertificate -Cert $cert -FilePath $pfxPath -Password $secure | Out-Null
Set-Content -Path (Join-Path $backupDir 'password.txt') -Value $password -Encoding ASCII
"证书已生成"
" 主题 : $($cert.Subject)"
" 指纹 : $($cert.Thumbprint)"
" 到期 : $($cert.NotAfter.ToString('yyyy-MM-dd'))"
" 公钥证书 : $cerPath(可提交仓库,供其他机器导入)"
" pfx 备份 : $pfxPath(含私钥,绝不进仓库)"
" pfx 密码 : $password(同目录 password.txt 也有)"
+52
View File
@@ -0,0 +1,52 @@
#Requires -Version 5.1
<#
用本机自签名证书给一个 PE 文件做 Authenticode 签名。
mygo.json 的 windows.signCommand 指向本脚本:mygo build 会给主 exe、资源里的
watchdog.exe、安装包和卸载器逐个调用它(%1 是文件路径,已带引号)。
自签名证书没有 CA 时间戳服务,所以不加 RFC 3161 时间戳:证书到期后旧签名会失效,
到那时重新发证并重签即可(自用场景够)。
用法:powershell -NoProfile -ExecutionPolicy Bypass -File scripts\codesign\sign-file.ps1 <文件>
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)][string]$Path,
[string]$Subject = 'CN=ZeroOrigin ToolBox, O=ZeroOrigin, C=CN'
)
$ErrorActionPreference = 'Stop'
if (-not (Test-Path -LiteralPath $Path)) {
"签名失败:文件不存在 $Path"
exit 1
}
$cert = Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert |
Where-Object { $_.Subject -eq $Subject -and $_.HasPrivateKey } |
Sort-Object NotAfter -Descending |
Select-Object -First 1
if (-not $cert) {
"签名失败:找不到证书「$Subject」,先运行 scripts\codesign\new-cert.ps1"
exit 1
}
$leaf = Split-Path -Leaf $Path
$existing = Get-AuthenticodeSignature -LiteralPath $Path
if ($existing.SignerCertificate) {
"已签名,跳过:$leaf"
exit 0
}
try {
$sig = Set-AuthenticodeSignature -LiteralPath $Path -Certificate $cert -HashAlgorithm SHA256
} catch {
"签名失败:$leaf — $($_.Exception.Message)"
exit 1
}
if (-not $sig.SignerCertificate) {
"签名失败:$leaf — $($sig.StatusMessage)"
exit 1
}
"已签名:$leaf($($sig.Status))"
exit 0
+50
View File
@@ -0,0 +1,50 @@
#Requires -Version 5.1
<#
把自签名代码签名证书导入当前用户的「受信任的根证书颁发机构」与「受信任的发布者」。
只影响当前用户、不需要管理员:导入后本机运行我们签名过的 exe 不再提示「未知发布者」。
换一台机器要再导一次;-Remove 可撤销。
用法:powershell -NoProfile -ExecutionPolicy Bypass -File scripts\codesign\trust.ps1 [-CerPath <证书>] [-Remove]
为什么用 certutil 而不是 Import-Certificate:后者向 Root 存储导入要求交互式 UI 确认,
在脚本里直接报 "UI is not allowed in this operation";certutil -user -addstore 是静默的。
#>
[CmdletBinding()]
param(
[string]$CerPath,
[switch]$Remove
)
$ErrorActionPreference = 'Stop'
if (-not $CerPath) {
$CerPath = Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) 'certs\ZeroOrigin-CodeSign.cer'
}
if (-not (Test-Path -LiteralPath $CerPath)) {
"找不到证书文件:$CerPath"
exit 1
}
$full = (Resolve-Path -LiteralPath $CerPath).Path
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($full)
$thumb = $cert.Thumbprint
foreach ($store in 'Root', 'TrustedPublisher') {
if ($Remove) {
$proc = Start-Process certutil -ArgumentList @('-user', '-delstore', $store, $thumb) -PassThru -NoNewWindow -Wait
} else {
$proc = Start-Process certutil -ArgumentList @('-user', '-addstore', $store, $full) -PassThru -NoNewWindow -Wait
}
if ($proc.ExitCode -ne 0) {
"操作失败:$store(certutil 退出码 $($proc.ExitCode))"
exit 1
}
}
if ($Remove) {
"已移除:$($cert.Subject)($thumb)"
} else {
"已信任:$($cert.Subject)($thumb)"
"到期:$($cert.NotAfter.ToString('yyyy-MM-dd'))"
}
exit 0
+61 -15
View File
@@ -1,4 +1,4 @@
#Requires -Version 5.1 #Requires -Version 5.1
<# <#
.SYNOPSIS .SYNOPSIS
把 mygo 构建出的更新产物发布到自建 Gitea 的一个固定 Release 里(附件目录即 updates.url)。 把 mygo 构建出的更新产物发布到自建 Gitea 的一个固定 Release 里(附件目录即 updates.url)。
@@ -43,7 +43,9 @@ param(
[Parameter(Mandatory)][string]$Repo, [Parameter(Mandatory)][string]$Repo,
[string]$Tag = "latest", [string]$Tag = "latest",
[string]$Dir = "bin/windows-amd64", [string]$Dir = "bin/windows-amd64",
[string]$Token = $env:GITEA_TOKEN [string]$Token = $env:GITEA_TOKEN,
# 只刷新 Release 的标题/正文,不重传附件:改文案、修乱码时用,避免每次重传几十 MB。
[switch]$MetadataOnly
) )
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
@@ -57,7 +59,7 @@ if (-not (Test-Path -LiteralPath $Dir)) { throw "找不到构建输出目录:$
# manifest 是唯一必须存在的产物:没有它就没有更新可发。 # manifest 是唯一必须存在的产物:没有它就没有更新可发。
$manifest = Get-ChildItem -LiteralPath $Dir -Filter "update-*-*.json" | Select-Object -First 1 $manifest = Get-ChildItem -LiteralPath $Dir -Filter "update-*-*.json" | Select-Object -First 1
if (-not $manifest) { throw "$Dir 里没有 update-<target>.json:构建时没带签名私钥?" } if (-not $manifest) { throw "$Dir 里没有 update-<target>.json:构建时没带签名私钥?" }
$m = Get-Content -LiteralPath $manifest.FullName -Raw | ConvertFrom-Json $m = Get-Content -LiteralPath $manifest.FullName -Raw -Encoding UTF8 | ConvertFrom-Json
$version = $m.version $version = $m.version
if (-not $version) { throw "$($manifest.Name) 里没有 version 字段" } if (-not $version) { throw "$($manifest.Name) 里没有 version 字段" }
$archiveName = Split-Path -Leaf ([uri]$m.url).AbsolutePath $archiveName = Split-Path -Leaf ([uri]$m.url).AbsolutePath
@@ -73,6 +75,14 @@ $sums = Join-Path $Dir "SHA256SUMS"
if (-not (Test-Path -LiteralPath $sums)) { $sums = Join-Path (Split-Path $Dir -Parent) "SHA256SUMS" } if (-not (Test-Path -LiteralPath $sums)) { $sums = Join-Path (Split-Path $Dir -Parent) "SHA256SUMS" }
if (Test-Path -LiteralPath $sums) { $uploads += Get-Item -LiteralPath $sums } if (Test-Path -LiteralPath $sums) { $uploads += Get-Item -LiteralPath $sums }
# 分发产物:安装版(setup exe)与便携版(zip)是给新用户下载的,不属于自更新链路,
# 但放在同一个 Release 里,用户在一个页面就能拿到全部形态。
$setup = Get-ChildItem -LiteralPath $Dir -Filter "*-setup-*.exe" -ErrorAction SilentlyContinue | Select-Object -First 1
$portable = Get-ChildItem -LiteralPath $Dir -Filter "*-portable*.zip" -ErrorAction SilentlyContinue | Select-Object -First 1
# install.ps1 由 mygo 生成(完整构建才有):一键安装脚本,正文里也给出它的用法。
$install = Get-ChildItem -LiteralPath $Dir -Filter "install.ps1" -ErrorAction SilentlyContinue | Select-Object -First 1
$dist = @($setup, $portable, $install) | Where-Object { $_ }
function Get-Release { function Get-Release {
$code = & curl.exe -sS -o NUL -w "%{http_code}" -H "Authorization: token $Token" "$api/releases/tags/$Tag" $code = & curl.exe -sS -o NUL -w "%{http_code}" -H "Authorization: token $Token" "$api/releases/tags/$Tag"
if ($code -eq "200") { if ($code -eq "200") {
@@ -82,21 +92,50 @@ function Get-Release {
return $null return $null
} }
# Release 标题与正文:标题带版本号,正文说清三种包怎么用。
$releaseName = "最新版本 $version"
$bodyLines = @("零源工具箱 $version", "", "下载:")
# 单引号字符串里的反引号是字面量,配合 -f 才能既保留 Markdown 代码格式又展开变量
# (双引号里用 `$ 会把 $ 转义掉,正文里就留下 $(...) 原文)。
if ($setup) {
$bodyLines += ('- 安装版 `{0}`:运行安装程序,装到当前用户目录(无需管理员),自动创建开始菜单快捷方式。' -f $setup.Name)
}
if ($portable) {
$bodyLines += ('- 便携版 `{0}`:解压到任意目录,双击 ZerOriginTool.exe 运行,配置写在 exe 同目录。' -f $portable.Name)
}
$bodyLines += ('- 自更新包 `{0}` + `{1}`:程序内托盘菜单「检查更新…」自动使用,无需手动下载。' -f $archiveName, $manifest.Name)
if ($install) {
$bodyLines += ('- 一键安装(当前用户,PowerShell):`irm {0}/{1}/{2}/releases/download/{3}/install.ps1 | iex`' -f $BaseUrl, $Owner, $Repo, $Tag)
}
$bodyLines += @("", "本 Release 的附件目录就是客户端检查更新用的 updates.url,请不要删除或改名其中的附件。")
$releaseBody = $bodyLines -join "`n"
# PowerShell 5.1 的 Invoke-RestMethod 传字符串 body 时按 ISO-8859-1 编码,中文会变成 "?";
# 必须自己编成 UTF-8 字节,并在 Content-Type 里声明 charset。
function Send-Json([string]$uri, [string]$method, $payload) {
$bytes = [System.Text.Encoding]::UTF8.GetBytes(($payload | ConvertTo-Json))
$headers = @{ Authorization = "token $Token"; "Content-Type" = "application/json; charset=utf-8" }
return Invoke-RestMethod -Uri $uri -Method $method -Headers $headers -Body $bytes
}
$release = Get-Release $release = Get-Release
if (-not $release) { if (-not $release) {
Write-Host "创建 Release $Tag …" Write-Host "创建 Release $Tag …"
$body = @{ $release = Send-Json "$api/releases" "Post" @{
tag_name = $Tag tag_name = $Tag
target_commitish = "main" target_commitish = "main"
name = "自更新分发($Tag)" name = $releaseName
body = "这个 Release 的附件目录就是客户端检查更新用的 updates.url;请不要删除或改名其中的附件。" body = $releaseBody
draft = $false draft = $false
prerelease = $false prerelease = $false
} | ConvertTo-Json }
$postHeaders = @{ Authorization = "token $Token"; "Content-Type" = "application/json" } } else {
$release = Invoke-RestMethod -Uri "$api/releases" -Method Post -Headers $postHeaders -Body $body if ($release.draft -or $release.prerelease) {
} elseif ($release.draft -or $release.prerelease) { throw "Release $Tag 是 draft/prerelease,匿名客户端下载不到;先在网页上发布它"
throw "Release $Tag 是 draft/prerelease,匿名客户端下载不到;先在网页上发布它" }
# 已存在的 Release 也要刷新标题/正文:新版本发布后版本号变了,旧脚本留下的乱码也在这里修掉。
Send-Json "$api/releases/$($release.id)" "Patch" @{ name = $releaseName; body = $releaseBody } | Out-Null
Write-Host "已更新 Release 标题与说明:$releaseName"
} }
# 同名旧附件必须先删,否则客户端可能下到旧 manifest。 # 同名旧附件必须先删,否则客户端可能下到旧 manifest。
@@ -119,20 +158,27 @@ function Send-Asset([System.IO.FileInfo]$file) {
Write-Warning " 跳过 $($file.Name)(服务器拒绝该附件类型):$out" Write-Warning " 跳过 $($file.Name)(服务器拒绝该附件类型):$out"
return return
} }
throw "上传 $($file.Name) 失败:$out" throw "上传 $($file.Name) 失败:$out(服务器附件类型白名单见 /api/v1/settings/attachment 的 allowed_types)"
} }
Write-Host (" 已上传 {0}({1:N1} MB)" -f $file.Name, ($file.Length / 1MB)) Write-Host (" 已上传 {0}({1:N1} MB)" -f $file.Name, ($file.Length / 1MB))
} }
foreach ($f in $uploads) { Send-Asset $f } if ($MetadataOnly) {
# manifest 最后传:它一旦被看到,就必须能下到上面的归档。 Write-Host "(-MetadataOnly:跳过附件上传)"
Send-Asset $manifest } else {
foreach ($f in $uploads) { Send-Asset $f }
foreach ($f in $dist) { Send-Asset $f }
# manifest 最后传:它一旦被看到,就必须能下到上面的归档。
Send-Asset $manifest
}
$base = "$BaseUrl/$Owner/$Repo/releases/download/$Tag" $base = "$BaseUrl/$Owner/$Repo/releases/download/$Tag"
Write-Host "" Write-Host ""
Write-Host "发布完成:$version" Write-Host "发布完成:$version"
Write-Host " manifest: $base/$($manifest.Name)" Write-Host " manifest: $base/$($manifest.Name)"
Write-Host " 归档 : $base/$archiveName" Write-Host " 归档 : $base/$archiveName"
if ($setup) { Write-Host " 安装版 : $base/$($setup.Name)" }
if ($portable) { Write-Host " 便携版 : $base/$($portable.Name)" }
Write-Host "" Write-Host ""
Write-Host "在另一台机器上验证(不要带令牌,客户端是匿名下载):" Write-Host "在另一台机器上验证(不要带令牌,客户端是匿名下载):"
Write-Host " curl.exe -sS $base/$($manifest.Name)" Write-Host " curl.exe -sS $base/$($manifest.Name)"