feat: add hotspot admin panel and harden payment provisioning
build-image / build-push (push) Failing after 1m36s
build-image / build-push (push) Failing after 1m36s
This commit is contained in:
1 parent
fb3135d18f
commit
ebb2d1d792
25 files changed
+832
-686
No files matched your search
+9
-2
@@ -2,6 +2,7 @@ import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import crypto from 'node:crypto';
|
||||
import config from './config.js';
|
||||
import { validatePlans } from './security.js';
|
||||
|
||||
const DB_PATH = path.join(config.dataDir, 'db.json');
|
||||
|
||||
@@ -17,9 +18,15 @@ export function initDb() {
|
||||
fs.mkdirSync(config.dataDir, { recursive: true });
|
||||
if (fs.existsSync(DB_PATH)) {
|
||||
db = JSON.parse(fs.readFileSync(DB_PATH, 'utf8'));
|
||||
if (!db.plans?.length) db.plans = structuredClone(DEFAULT_PLANS);
|
||||
if (!Array.isArray(db.plans)) db.plans = structuredClone(DEFAULT_PLANS);
|
||||
if (!db.payments) db.payments = [];
|
||||
if (!validatePlans(db.plans) || !Array.isArray(db.payments)) throw new Error('Banco de dados inválido; restaure um backup.');
|
||||
if (!db.tempAccess) db.tempAccess = [];
|
||||
if (!Array.isArray(db.tempAccess)) throw new Error('Acessos temporários inválidos');
|
||||
// Migrate old payments to a fixed plan snapshot before plans are edited.
|
||||
for (const p of db.payments) {
|
||||
if (!p.uptime) p.uptime = db.plans.find(plan => plan.id === p.planId)?.uptime;
|
||||
}
|
||||
} else {
|
||||
db = { plans: structuredClone(DEFAULT_PLANS), payments: [], tempAccess: [] };
|
||||
saveDb();
|
||||
@@ -28,7 +35,7 @@ export function initDb() {
|
||||
|
||||
export function saveDb() {
|
||||
const tmp = DB_PATH + '.tmp';
|
||||
fs.writeFileSync(tmp, JSON.stringify(db, null, 2));
|
||||
fs.writeFileSync(tmp, JSON.stringify(db, null, 2), { mode: 0o600 });
|
||||
fs.renameSync(tmp, DB_PATH);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user