Files
verify/backend/cmd/seedtest/main.go
hermes 22570341ef feat(protect): 面板「加壳加固」页面
- pages/admin/protect/index.vue:应用选择 + 拖拽/点击上传 exe + 统计卡 + 任务表(状态轮询 2s、下载、删除)
- 路由 /admin/protect、侧边栏入口(PackageCheck)、面包屑 titleMap
- i18n zh/en 各补 nav.protect + admin.protect 38 键
- seedtest 增加面板管理员(admin/admin123)便于本地端到端验证
2026-11-21 14:56:02 +08:00

110 lines
3.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// seedtest:给本地 sqlite 实例造一套测试数据(应用 + 版本 + 真实 exe 内容文件)
// 用法: go run ./cmd/seedtest <exe路径>
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"time"
"verification-platform-backend/internal/config"
"verification-platform-backend/internal/database"
"verification-platform-backend/internal/model"
"verification-platform-backend/internal/utils/contentstore"
"golang.org/x/crypto/bcrypt"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "用法: seedtest <某个 exe 路径>")
os.Exit(2)
}
exePath := os.Args[1]
data, err := os.ReadFile(exePath)
must(err)
config.Init()
database.Init() // 内部已执行 AutoMigrate
// 1) 用户(面板登录用的 User 表,role=admin)
var admin model.User
database.DB.Where("username = ?", "admin").First(&admin)
if admin.ID == 0 {
hash, _ := bcrypt.GenerateFromPassword([]byte("admin123"), bcrypt.DefaultCost)
admin = model.User{Username: "admin", Password: string(hash), Role: "admin", Status: "active"}
must(database.DB.Create(&admin).Error)
fmt.Println("管理员: admin / admin123")
}
// 2) 应用所属用户(客户端账号表)
var user model.AppUser
database.DB.Where("username = ?", "seeduser").First(&user)
if user.ID == 0 {
user = model.AppUser{Username: "seeduser", Password: "x", Email: "seed@local", Status: "active"}
must(database.DB.Create(&user).Error)
}
// 2) 应用
var app model.Application
database.DB.Where("app_key = ?", "seedapp01").First(&app)
if app.ID == 0 {
app = model.Application{UserID: user.ID, Name: "种子应用", AppKey: "seedapp01", MaxDevices: 3, Status: "active"}
must(database.DB.Create(&app).Error)
}
// 3) 内容文件:按平台规则上传到 uploads/files/<h2>/<hash>
sum := sha256.Sum256(data)
hash := hex.EncodeToString(sum[:])
stored := contentstore.ContentFilePath(hash)
must(os.MkdirAll(filepath.Dir(stored), 0o755))
must(os.WriteFile(stored, data, 0o644))
fmt.Printf("内容文件: %s (%d 字节, hash=%.12s…)\n", stored, len(data), hash)
// 4) 版本 + 版本文件
base := filepath.Base(exePath)
var v model.Version
database.DB.Where("application_id = ? AND version = ?", app.ID, "9.9.9").First(&v)
if v.ID == 0 {
v = model.Version{
ApplicationID: app.ID,
Version: "9.9.9",
FilePath: stored,
FileSize: int64(len(data)),
FileHash: hash,
EntryFile: base,
PublishStatus: "published",
PublishedAt: ptrTime(time.Now()),
}
must(database.DB.Create(&v).Error)
} else {
database.DB.Model(&v).Updates(map[string]interface{}{
"entry_file": base, "file_path": stored, "file_hash": hash,
})
}
var vf model.VersionFile
database.DB.Where("version_id = ? AND file_name = ?", v.ID, base).First(&vf)
if vf.ID == 0 {
vf = model.VersionFile{
VersionID: v.ID, FileName: base, FilePath: contentstore.ContentFileURL(hash),
FileSize: int64(len(data)), FileHash: hash, FileType: "program", IsRequired: true,
}
must(database.DB.Create(&vf).Error)
}
fmt.Printf("种子完成: app_id=%d app_key=%s version_id=%d entry=%s\n", app.ID, app.AppKey, v.ID, base)
}
func ptrTime(t time.Time) *time.Time { return &t }
func must(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
}