Files
BetterDesk/web-nodejs/services/keyService.js
T
UNITRONIX 32e29723e4 Add Node.js web console and update to v2.2.0
Introduce a new Node.js-based web console (Express + EJS + better-sqlite3) under web-nodejs/ and add installer support to choose between Node.js and the legacy Flask console. Update interactive ALL-IN-ONE installers (betterdesk.sh, betterdesk.ps1) with flags/options for --nodejs/--flask, automatic Node.js installation, migration logic, enhanced service handling and diagnostics. Bump VERSION to 2.2.0 and update README and project docs (.github/copilot-instructions.md) to reflect the new console, usage examples, and Docker/docs changes. Many new web-nodejs files and supporting middleware/services/routes/views/static assets were added to support the new console.
2026-02-17 10:59:46 +01:00

91 lines
2.0 KiB
JavaScript

/**
* BetterDesk Console - Key Service
* Reads public key and API key from filesystem
*/
const fs = require('fs');
const QRCode = require('qrcode');
const config = require('../config/config');
/**
* Read public key from file
*/
function getPublicKey() {
try {
if (fs.existsSync(config.pubKeyPath)) {
return fs.readFileSync(config.pubKeyPath, 'utf8').trim();
}
return null;
} catch (err) {
console.warn('Could not read public key:', err.message);
return null;
}
}
/**
* Get API key (masked for display)
*/
function getApiKey(masked = true) {
try {
if (fs.existsSync(config.apiKeyPath)) {
const key = fs.readFileSync(config.apiKeyPath, 'utf8').trim();
if (masked && key.length > 8) {
return key.substring(0, 4) + '****' + key.substring(key.length - 4);
}
return key;
}
return null;
} catch (err) {
console.warn('Could not read API key:', err.message);
return null;
}
}
/**
* Generate QR code for public key
*/
async function getPublicKeyQR() {
const pubKey = getPublicKey();
if (!pubKey) {
return null;
}
try {
const qrDataUrl = await QRCode.toDataURL(pubKey, {
errorCorrectionLevel: 'M',
type: 'image/png',
width: 256,
margin: 2,
color: {
dark: '#e6edf3',
light: '#0d1117'
}
});
return qrDataUrl;
} catch (err) {
console.warn('Could not generate QR code:', err.message);
return null;
}
}
/**
* Get server configuration info
*/
function getServerConfig() {
return {
publicKey: getPublicKey(),
apiKeyMasked: getApiKey(true),
hbbsApiUrl: config.hbbsApiUrl,
dbPath: config.dbPath,
pubKeyPath: config.pubKeyPath,
apiKeyPath: config.apiKeyPath
};
}
module.exports = {
getPublicKey,
getApiKey,
getPublicKeyQR,
getServerConfig
};