40 lines
1.1 KiB
JavaScript
40 lines
1.1 KiB
JavaScript
#!/usr/bin/env node
|
|
import { createPrivateKey, sign } from "crypto";
|
|
import { readFileSync } from "fs";
|
|
import { resolve, dirname } from "path";
|
|
import { fileURLToPath } from "url";
|
|
|
|
const envPath = resolve(dirname(fileURLToPath(import.meta.url)), "../.env");
|
|
|
|
let privateKeyB64 = process.env.LICENSE_PRIVATE_KEY;
|
|
|
|
if (!privateKeyB64) {
|
|
try {
|
|
const envContents = readFileSync(envPath, "utf8");
|
|
const match = envContents.match(/^LICENSE_PRIVATE_KEY=(.+)$/m);
|
|
if (match) privateKeyB64 = match[1].trim();
|
|
} catch {}
|
|
}
|
|
|
|
if (!privateKeyB64) {
|
|
console.error("LICENSE_PRIVATE_KEY not found in env or .env file");
|
|
process.exit(1);
|
|
}
|
|
|
|
const window = Math.floor(Date.now() / 1000 / 300);
|
|
const msg = Buffer.from(String(window));
|
|
|
|
const privateKey = createPrivateKey({
|
|
key: Buffer.from(privateKeyB64, "base64"),
|
|
format: "der",
|
|
type: "pkcs8",
|
|
});
|
|
|
|
const sig = sign(null, msg, privateKey);
|
|
const key = sig.toString("base64url");
|
|
|
|
const minutesLeft = 5 - (Math.floor(Date.now() / 1000) % 300) / 60;
|
|
|
|
console.log(`\nLicense key (valid for ~${minutesLeft.toFixed(1)} more minutes):\n`);
|
|
console.log(key);
|
|
console.log();
|