server: script gen-license per generare licenze manuali da locale
- server/scripts/gen-license.mjs: usa generateLicenseKey e getPlan ufficiali, INSERT nel DB con source='manual', ritorna JSON con la chiave - server/scripts/gen-license.sh: wrapper locale che rsync-a lo script sul server e lo esegue via ssh (annual di default, oppure basic/pro/premium) Sostituisce il flusso ad-hoc di scp'are /tmp/genlic_*.mjs a ogni generazione.
This commit is contained in:
1 parent
e2ac1ca275
commit
e4bfd76a79
2 files changed
+108
No files matched your search
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env node
|
||||
// Genera una licenza nel DB delle licenze MusicTools.
|
||||
//
|
||||
// Da eseguire SUL SERVER (musictools@musictools.djluza.com) dentro ~/api/:
|
||||
// node scripts/gen-license.mjs <email> [plan]
|
||||
//
|
||||
// plan: annual (default, "full" — no limite, no scadenza)
|
||||
// basic | pro | premium (con daily_limit e period_end mensile)
|
||||
//
|
||||
// Da locale, usare lo script wrapper: server/scripts/gen-license.sh
|
||||
|
||||
import { generateLicenseKey } from "../src/license.js";
|
||||
import { getPlan } from "../src/plans.js";
|
||||
import mysql from "mysql2/promise";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const envPath = path.resolve(__dirname, "..", ".env");
|
||||
fs.readFileSync(envPath, "utf-8").split("\n").forEach((l) => {
|
||||
const m = l.match(/^([A-Z_]+)=(.*)$/);
|
||||
if (m) process.env[m[1]] = m[2];
|
||||
});
|
||||
|
||||
const [, , emailArg, planArg] = process.argv;
|
||||
if (!emailArg) {
|
||||
console.error("Uso: node scripts/gen-license.mjs <email> [plan]");
|
||||
console.error("plan: annual (default) | basic | pro | premium");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const email = emailArg.trim().toLowerCase();
|
||||
const planCode = (planArg || "annual").trim().toLowerCase();
|
||||
const plan = getPlan(planCode);
|
||||
if (!plan) {
|
||||
console.error(`Piano sconosciuto: ${planCode}`);
|
||||
console.error("Validi: annual, basic, pro, premium");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const pool = mysql.createPool({
|
||||
host: "127.0.0.1",
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
database: process.env.DB_NAME,
|
||||
});
|
||||
|
||||
const key = generateLicenseKey();
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
|
||||
const isAnnual = planCode === "annual";
|
||||
const dailyLimit = plan.dailyLimit ?? null;
|
||||
const expiresAt = isAnnual ? null : null;
|
||||
const periodEnd = isAnnual ? null : now + 30 * 24 * 3600;
|
||||
|
||||
await pool.execute(
|
||||
`INSERT INTO licenses
|
||||
(license_key, email, status, plan, daily_limit, source,
|
||||
subscription_id, current_period_end,
|
||||
expires_at, created_at, updated_at)
|
||||
VALUES (?, ?, 'active', ?, ?, 'manual', NULL, ?, ?, ?, ?)`,
|
||||
[key, email, planCode, dailyLimit, periodEnd, expiresAt, now, now],
|
||||
);
|
||||
|
||||
console.log(JSON.stringify({
|
||||
key,
|
||||
email,
|
||||
plan: planCode,
|
||||
daily_limit: dailyLimit,
|
||||
expires_at: expiresAt,
|
||||
current_period_end: periodEnd,
|
||||
}, null, 2));
|
||||
|
||||
await pool.end();
|
||||
Executable
+33
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env bash
|
||||
# Genera una licenza MusicTools da locale, eseguendo lo script sul server.
|
||||
#
|
||||
# Uso:
|
||||
# ./server/scripts/gen-license.sh <email> [plan]
|
||||
#
|
||||
# plan: annual (default, "full" — no limite, no scadenza)
|
||||
# basic | pro | premium (con daily_limit e period_end mensile)
|
||||
#
|
||||
# Richiede:
|
||||
# - ssh con chiave configurata verso musictools@musictools.djluza.com
|
||||
# - lo script server-side viene sincronizzato in automatico prima dell'esecuzione
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
EMAIL="${1:-}"
|
||||
PLAN="${2:-annual}"
|
||||
|
||||
if [ -z "$EMAIL" ]; then
|
||||
echo "Uso: $0 <email> [plan]" >&2
|
||||
echo "plan: annual (default) | basic | pro | premium" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
SSH_TARGET="musictools@musictools.djluza.com"
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
|
||||
ssh "$SSH_TARGET" 'mkdir -p ~/api/scripts'
|
||||
rsync -a --quiet \
|
||||
"$SCRIPT_DIR/gen-license.mjs" \
|
||||
"${SSH_TARGET}:api/scripts/gen-license.mjs"
|
||||
|
||||
ssh "$SSH_TARGET" "cd ~/api && node scripts/gen-license.mjs '$EMAIL' '$PLAN'"
|
||||
Reference in new issue
Block a user