212 lines
6.0 KiB
Go
212 lines
6.0 KiB
Go
package protect
|
||
|
||
// 把加壳产物落成一条新版本记录:客户端按「版本记录 ID 递增」判定更新,
|
||
// 原地改已发布版本的入口文件在客户端不报 manifest_hash 时永远推不下去。
|
||
|
||
import (
|
||
"crypto/sha256"
|
||
"encoding/hex"
|
||
"fmt"
|
||
"os"
|
||
"path/filepath"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
|
||
"verification-platform-backend/internal/database"
|
||
"verification-platform-backend/internal/model"
|
||
"verification-platform-backend/internal/utils/contentstore"
|
||
)
|
||
|
||
// PublishOptions 生成版本的参数
|
||
type PublishOptions struct {
|
||
Version string // 留空则按来源版本自动进位
|
||
UpdateStrategy string // optional / forced(默认 optional)
|
||
UpdateType string // full / delta(默认 full)
|
||
UpdateMethod string // auto / manual(默认 auto)
|
||
Changelog string
|
||
PublishNow bool // 建好就直接发布
|
||
}
|
||
|
||
// PublishAsVersion 把任务产物写成新版本,返回创建出的版本
|
||
func PublishAsVersion(taskID uint, opts PublishOptions) (*model.Version, error) {
|
||
var task model.ProtectTask
|
||
if err := database.DB.First(&task, taskID).Error; err != nil {
|
||
return nil, fmt.Errorf("云端加密任务不存在")
|
||
}
|
||
if task.Status != "done" || task.OutputPath == "" {
|
||
return nil, fmt.Errorf("任务尚未完成,无法生成版本")
|
||
}
|
||
data, err := os.ReadFile(task.OutputPath)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("读取产物失败: %w", err)
|
||
}
|
||
|
||
var app model.Application
|
||
if err := database.DB.First(&app, task.ApplicationID).Error; err != nil {
|
||
return nil, fmt.Errorf("应用不存在")
|
||
}
|
||
|
||
sum := sha256.Sum256(data)
|
||
hash := hex.EncodeToString(sum[:])
|
||
stored := contentstore.ContentFilePath(hash)
|
||
if stored == "" {
|
||
return nil, fmt.Errorf("内容哈希异常")
|
||
}
|
||
if err := os.MkdirAll(filepath.Dir(stored), 0o755); err != nil {
|
||
return nil, err
|
||
}
|
||
if err := os.WriteFile(stored, data, 0o644); err != nil {
|
||
return nil, fmt.Errorf("落盘产物失败: %w", err)
|
||
}
|
||
|
||
var src model.Version
|
||
hasSrc := task.VersionID > 0 && database.DB.First(&src, task.VersionID).Error == nil
|
||
|
||
versionStr := strings.TrimSpace(opts.Version)
|
||
autoBump := versionStr == ""
|
||
if autoBump {
|
||
switch {
|
||
case hasSrc:
|
||
versionStr = src.Version
|
||
default:
|
||
var latest model.Version
|
||
if database.DB.Where("application_id = ?", app.ID).Order("id DESC").First(&latest).Error == nil {
|
||
versionStr = latest.Version
|
||
} else {
|
||
versionStr = time.Now().Format("2006.01.02.1504")
|
||
}
|
||
}
|
||
// 自动进位要避开已存在的号:源版本 9.9.9 但 9.9.10 已被人占时继续往上找
|
||
for i := 0; i < 200; i++ {
|
||
next := BumpVersion(versionStr)
|
||
var used int64
|
||
database.DB.Model(&model.Version{}).
|
||
Where("application_id = ? AND version = ?", app.ID, next).Count(&used)
|
||
versionStr = next
|
||
if used == 0 {
|
||
break
|
||
}
|
||
}
|
||
}
|
||
var dup int64
|
||
database.DB.Model(&model.Version{}).
|
||
Where("application_id = ? AND version = ?", app.ID, versionStr).Count(&dup)
|
||
if dup > 0 {
|
||
return nil, fmt.Errorf("版本号 %s 已存在,请换一个", versionStr)
|
||
}
|
||
|
||
strategy := opts.UpdateStrategy
|
||
if strategy == "" {
|
||
strategy = "optional"
|
||
}
|
||
if strategy != "optional" && strategy != "forced" {
|
||
return nil, fmt.Errorf("update_strategy 只能是 optional 或 forced")
|
||
}
|
||
updateType := opts.UpdateType
|
||
if updateType == "" {
|
||
updateType = "full"
|
||
}
|
||
method := opts.UpdateMethod
|
||
if method == "" {
|
||
method = "auto"
|
||
}
|
||
entryName := filepath.Base(task.OutputName)
|
||
if entryName == "" {
|
||
entryName = "app_protected.exe"
|
||
}
|
||
|
||
srcID := task.VersionID
|
||
taskIDCopy := task.ID
|
||
v := model.Version{
|
||
ApplicationID: app.ID,
|
||
Version: versionStr,
|
||
FilePath: stored,
|
||
FileSize: int64(len(data)),
|
||
FileHash: hash,
|
||
EntryFile: entryName,
|
||
UpdateStrategy: strategy,
|
||
UpdateType: updateType,
|
||
UpdateMethod: method,
|
||
Changelog: opts.Changelog,
|
||
PublishStatus: "draft",
|
||
Protected: true,
|
||
ProtectTaskID: &taskIDCopy,
|
||
}
|
||
if srcID > 0 {
|
||
v.SourceVersionID = &srcID
|
||
}
|
||
if opts.PublishNow {
|
||
now := time.Now()
|
||
v.PublishStatus = "published"
|
||
v.PublishedAt = &now
|
||
}
|
||
if err := database.DB.Create(&v).Error; err != nil {
|
||
return nil, fmt.Errorf("创建版本失败: %w", err)
|
||
}
|
||
|
||
// 入口文件(加壳产物)
|
||
vf := model.VersionFile{
|
||
VersionID: v.ID,
|
||
FileName: entryName,
|
||
FilePath: contentstore.ContentFileURL(hash),
|
||
FileSize: int64(len(data)),
|
||
FileHash: hash,
|
||
FileType: "program",
|
||
IsRequired: true,
|
||
SortOrder: 0,
|
||
}
|
||
if err := database.DB.Create(&vf).Error; err != nil {
|
||
return nil, fmt.Errorf("写入版本文件失败: %w", err)
|
||
}
|
||
|
||
// 其余资源文件沿用来源版本(内容寻址,不重复占空间)
|
||
if hasSrc {
|
||
var srcFiles []model.VersionFile
|
||
database.DB.Where("version_id = ?", src.ID).Find(&srcFiles)
|
||
for _, f := range srcFiles {
|
||
if strings.EqualFold(filepath.Base(f.FileName), entryName) {
|
||
continue
|
||
}
|
||
if strings.EqualFold(filepath.Base(f.FileName), filepath.Base(src.EntryFile)) {
|
||
continue
|
||
}
|
||
cp := model.VersionFile{
|
||
VersionID: v.ID, FileName: f.FileName, FilePath: f.FilePath,
|
||
FileSize: f.FileSize, FileHash: f.FileHash, FileType: f.FileType,
|
||
IsRequired: f.IsRequired, SortOrder: f.SortOrder + 1,
|
||
}
|
||
database.DB.Create(&cp)
|
||
}
|
||
}
|
||
|
||
if opts.PublishNow && strategy == "forced" {
|
||
database.DB.Model(&model.Version{}).
|
||
Where("application_id = ? AND id < ? AND publish_status = ?", app.ID, v.ID, "published").
|
||
Updates(map[string]interface{}{
|
||
"publish_status": "superseded",
|
||
"superseded_by": v.ID,
|
||
"deprecated_at": time.Now(),
|
||
})
|
||
}
|
||
|
||
// 注意:不要回写 task.version_id —— 那一列表示「加壳来源版本」,
|
||
// 生成了哪个版本要靠 version.protect_task_id 反查。
|
||
return &v, nil
|
||
}
|
||
|
||
// BumpVersion 末段数字 +1:1.1.13 → 1.1.14;无数字则追加 .1
|
||
func BumpVersion(v string) string {
|
||
v = strings.TrimSpace(v)
|
||
if v == "" {
|
||
return "1.0.0"
|
||
}
|
||
parts := strings.Split(v, ".")
|
||
last := parts[len(parts)-1]
|
||
if n, err := strconv.Atoi(last); err == nil {
|
||
parts[len(parts)-1] = strconv.Itoa(n + 1)
|
||
return strings.Join(parts, ".")
|
||
}
|
||
return v + ".1"
|
||
}
|