mirror of
https://github.com/kind-0/nsecbunkerd.git
synced 2026-08-01 07:21:42 +00:00
Introduces automatic generation of a random admin key when creating or updating the configuration file. Also adds a notifyAdminsOnBoot flag and ensures missing admin keys are added to existing configs.
77 lines
2.2 KiB
JavaScript
77 lines
2.2 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const crypto = require('crypto');
|
|
|
|
const configPath = '/app/config/nsecbunker.json';
|
|
|
|
// Generate a random admin key
|
|
function generateAdminKey() {
|
|
return crypto.randomBytes(32).toString('hex');
|
|
}
|
|
|
|
// Default config with nip46 relay
|
|
const defaultConfig = {
|
|
"nostr": {
|
|
"relays": [
|
|
"wss://nip46.stevennoack.de"
|
|
]
|
|
},
|
|
"admin": {
|
|
"npubs": [
|
|
"npub1cxa0fa6vmq5evwpgk8dg6ul99ny5e2nd3hy5fa72g598t39nxy0surzuva"
|
|
],
|
|
"adminRelays": [
|
|
"wss://nip46.stevennoack.de"
|
|
],
|
|
"key": generateAdminKey(),
|
|
"notifyAdminsOnBoot": true
|
|
},
|
|
"database": "sqlite://nsecbunker.db",
|
|
"logs": "./nsecbunker.log",
|
|
"keys": {},
|
|
"verbose": true,
|
|
"version": "0.10.5"
|
|
};
|
|
|
|
// Check if config exists
|
|
if (!fs.existsSync(configPath)) {
|
|
console.log('📝 Creating default config...');
|
|
fs.mkdirSync(path.dirname(configPath), { recursive: true });
|
|
fs.writeFileSync(configPath, JSON.stringify(defaultConfig, null, 2));
|
|
console.log('✅ Config created at', configPath);
|
|
} else {
|
|
console.log('✅ Config exists at', configPath);
|
|
|
|
// Check if admin key exists
|
|
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
|
if (!config.admin || !config.admin.key) {
|
|
console.log('🔑 Adding missing admin key...');
|
|
if (!config.admin) config.admin = {};
|
|
config.admin.key = generateAdminKey();
|
|
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
|
|
console.log('✅ Admin key added');
|
|
}
|
|
}
|
|
|
|
// Load existing config
|
|
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
|
|
|
// Add key if env vars are set and key doesn't exist
|
|
const keyName = process.env.NSECBUNKER_KEY;
|
|
const keyIv = process.env.NSECBUNKER_KEY_IV;
|
|
const keyData = process.env.NSECBUNKER_KEY_DATA;
|
|
|
|
if (keyName && keyIv && keyData && !config.keys[keyName]) {
|
|
console.log(`🔑 Adding key "${keyName}" from env vars...`);
|
|
config.keys[keyName] = {
|
|
iv: keyIv,
|
|
data: keyData
|
|
};
|
|
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
|
|
console.log(`✅ Key "${keyName}" added`);
|
|
} else if (keyName && config.keys[keyName]) {
|
|
console.log(`✅ Key "${keyName}" already exists`);
|
|
}
|
|
|
|
console.log('📋 Current keys:', Object.keys(config.keys));
|