feat(更新链路): web_config 加 sha256(V142),/api/version/latest 回带包摘要
客户端自更新过去只验 zip 结构:桶里的包被换掉时,结构完整的恶意 zip 会直接交给 update.exe 解压执行。摘要必须由服务端给出才与下载来源无关——能替换对象的人替换不了这个接口。 - V142 加 sha256 列(可空、不回填历史行,列缺失时客户端行为不变); - /latest、公开列表、后台上传响应都回带摘要;multipart 上传按真正落盘的字节算; - 直传确认路径不重算摘要(那要把 100MB 对象拉回来),这类行留空由发布脚本回填。 盘点文档同步:V1..V142、141 个文件、后续从 V143 取号。
This commit is contained in:
+3
@@ -24,6 +24,9 @@ public class SoftwareVersionEntity {
|
||||
/** 安装包可公开访问的下载链接(MinIO path-style 直链) */
|
||||
private String fileUrl;
|
||||
|
||||
/** 安装包 sha256(hex 小写);null 表示 V142 之前的历史行,客户端对这类包不做摘要校验 */
|
||||
private String sha256;
|
||||
|
||||
/** 是否最新版:全表至多一行为 true,由 SoftwareVersionMapper#markLatestById 单语句维护 */
|
||||
private Boolean isLatest;
|
||||
|
||||
|
||||
+35
@@ -13,7 +13,10 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -102,10 +105,14 @@ public class SoftwareVersionService {
|
||||
if (entity == null) {
|
||||
result.put("version", null);
|
||||
result.put("file_url", null);
|
||||
result.put("sha256", null);
|
||||
return result;
|
||||
}
|
||||
result.put("version", entity.getVersion() == null ? "" : entity.getVersion());
|
||||
result.put("file_url", entity.getFileUrl() == null ? "" : entity.getFileUrl());
|
||||
// 空字符串 = 「服务端还没有这个包的摘要」(V142 之前的历史行)。客户端据此决定
|
||||
// 是硬校验摘要,还是退回旧的「只验 zip 结构」行为。
|
||||
result.put("sha256", entity.getSha256() == null ? "" : entity.getSha256());
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -185,6 +192,7 @@ public class SoftwareVersionService {
|
||||
SoftwareVersionEntity entity = new SoftwareVersionEntity();
|
||||
entity.setVersion(normalizedVersion);
|
||||
entity.setFileUrl(fileUrl);
|
||||
entity.setSha256(sha256Hex(tempFile));
|
||||
// 不显式写 created_at,走数据库 CURRENT_TIMESTAMP 默认值(与 Flask 行为一致)
|
||||
softwareVersionMapper.insert(entity);
|
||||
softwareVersionMapper.markLatestById(entity.getId());
|
||||
@@ -194,6 +202,7 @@ public class SoftwareVersionService {
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("version", normalizedVersion);
|
||||
result.put("file_url", fileUrl);
|
||||
result.put("sha256", entity.getSha256());
|
||||
return result;
|
||||
} catch (IOException e) {
|
||||
log.error("[software-version] 上传客户端软件版本文件处理失败 version={} filename={} err={}",
|
||||
@@ -248,6 +257,8 @@ public class SoftwareVersionService {
|
||||
SoftwareVersionEntity entity = new SoftwareVersionEntity();
|
||||
entity.setVersion(normalizedVersion);
|
||||
entity.setFileUrl(ossStorageService.getSoftwareVersionDownloadUrl(objectKey));
|
||||
// 直传确认不重算 sha256:那要把刚上传的 100MB 对象整包拉回来。这条路径落库的行摘要为空,
|
||||
// 客户端会退回「只验 zip 结构」的旧行为;要给这类包补锚,用发布脚本回填(见凭据轮换工单)。
|
||||
softwareVersionMapper.insert(entity);
|
||||
softwareVersionMapper.markLatestById(entity.getId());
|
||||
SoftwareVersionEntity saved = softwareVersionMapper.selectById(entity.getId());
|
||||
@@ -328,11 +339,35 @@ public class SoftwareVersionService {
|
||||
item.put("id", entity.getId());
|
||||
item.put("version", entity.getVersion() == null ? "" : entity.getVersion());
|
||||
item.put("file_url", entity.getFileUrl() == null ? "" : entity.getFileUrl());
|
||||
item.put("sha256", entity.getSha256() == null ? "" : entity.getSha256());
|
||||
item.put("created_at", entity.getCreatedAt() == null
|
||||
? "" : entity.getCreatedAt().format(CREATED_AT_FORMATTER));
|
||||
return item;
|
||||
}
|
||||
|
||||
/** 算安装包的 sha256(hex 小写),作为更新链路的完整性锚点写进 web_config。 */
|
||||
static String sha256Hex(File file) throws IOException {
|
||||
MessageDigest digest;
|
||||
try {
|
||||
digest = MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
// JDK 必然带 SHA-256;真缺了也不能把已上传成功的版本包静默标成"无摘要"
|
||||
throw new IOException("SHA-256 不可用", e);
|
||||
}
|
||||
byte[] buffer = new byte[1 << 20];
|
||||
try (InputStream in = Files.newInputStream(file.toPath())) {
|
||||
int read;
|
||||
while ((read = in.read(buffer)) > 0) {
|
||||
digest.update(buffer, 0, read);
|
||||
}
|
||||
}
|
||||
StringBuilder hex = new StringBuilder(digest.getDigestLength() * 2);
|
||||
for (byte b : digest.digest()) {
|
||||
hex.append(Character.forDigit((b >> 4) & 0xF, 16)).append(Character.forDigit(b & 0xF, 16));
|
||||
}
|
||||
return hex.toString();
|
||||
}
|
||||
|
||||
private long elapsedMs(long startedAt) {
|
||||
return (System.nanoTime() - startedAt) / 1_000_000L;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user