This commit is contained in:
hailin 2025-06-13 23:14:51 +08:00
parent b2c0c1c231
commit 4ff660bc1f
2 changed files with 27 additions and 1 deletions

View File

@ -20,7 +20,13 @@ func GenerateLicenseHandler(db storage.Database) fiber.Handler {
return fiber.NewError(fiber.StatusBadRequest, "Missing machine ID")
}
// ⚠️ 此时不设置 Expiry 字段,由 Activate 阶段设置
// ✅ 检查是否已经生成过
if db.HasGenerated(req.MachineID) {
return fiber.NewError(fiber.StatusForbidden, "License already generated for this machine")
}
// ❌ 不再接收客户端传的 Expiry由后续激活阶段设置
req.Expiry = ""
payloadBytes, err := json.Marshal(req)
if err != nil {
@ -37,6 +43,9 @@ func GenerateLicenseHandler(db storage.Database) fiber.Handler {
Signature: signature,
}
// ✅ 保存生成记录(避免重复)
db.SaveGenerated(req.MachineID, licenseFile.Payload+"."+licenseFile.Signature)
return c.JSON(licenseFile)
}
}

View File

@ -17,6 +17,11 @@ func InitDB() Database {
machine_id TEXT UNIQUE,
license TEXT,
activated INTEGER
)`)
db.Exec(`CREATE TABLE IF NOT EXISTS generations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
machine_id TEXT UNIQUE,
license TEXT
)`)
return Database{db}
}
@ -32,3 +37,15 @@ func (d Database) SaveActivation(machineID string, licenseText string) {
d.db.Exec("INSERT OR REPLACE INTO activations(machine_id, license, activated) VALUES (?, ?, 1)",
machineID, licenseText)
}
func (d Database) HasGenerated(machineID string) bool {
row := d.db.QueryRow("SELECT license FROM generations WHERE machine_id = ?", machineID)
var license string
err := row.Scan(&license)
return err == nil
}
func (d Database) SaveGenerated(machineID string, licenseText string) {
d.db.Exec("INSERT OR REPLACE INTO generations(machine_id, license) VALUES (?, ?)",
machineID, licenseText)
}