// Initialise/Met à jour data/config.json avec le hash scrypt du mot de passe. // Usage: node server/set-password.js [port] import crypto from 'node:crypto'; import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const DATA_DIR = path.join(__dirname, '..', 'data'); fs.mkdirSync(DATA_DIR, { recursive: true }); const CONFIG_PATH = path.join(DATA_DIR, 'config.json'); const pw = process.argv[2]; if (!pw) { console.error('Mot de passe requis.'); process.exit(1); } if (pw.length < 12) console.warn('⚠️ mot de passe court (<12) — le rate-limit de login reste la seule protection contre le brute-force.'); let config = {}; try { config = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf8')); } catch {} const salt = crypto.randomBytes(16); config.salt = salt.toString('hex'); config.passwordHash = crypto.scryptSync(pw, salt, 64).toString('hex'); config.port = Number(process.argv[3]) || config.port || 3300; fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2)); console.log('config.json mis à jour (port ' + config.port + ').');