From e4bfd76a793c46f9d7b8fd65d421253bf223db64 Mon Sep 17 00:00:00 2001 From: luciano Date: Mon, 13 Jul 2026 10:33:25 +0200 Subject: [PATCH] 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. --- server/scripts/gen-license.mjs | 75 ++++++++++++++++++++++++++++++++++ server/scripts/gen-license.sh | 33 +++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 server/scripts/gen-license.mjs create mode 100755 server/scripts/gen-license.sh diff --git a/server/scripts/gen-license.mjs b/server/scripts/gen-license.mjs new file mode 100644 index 0000000..2321486 --- /dev/null +++ b/server/scripts/gen-license.mjs @@ -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 [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 [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(); diff --git a/server/scripts/gen-license.sh b/server/scripts/gen-license.sh new file mode 100755 index 0000000..c7dd210 --- /dev/null +++ b/server/scripts/gen-license.sh @@ -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 [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 [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'"