diff --git a/license/service.go b/license/service.go index 266d042..f07b4a7 100644 --- a/license/service.go +++ b/license/service.go @@ -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) } } diff --git a/storage/db.go b/storage/db.go index 62e5ddc..ba1ce68 100644 --- a/storage/db.go +++ b/storage/db.go @@ -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) +}