forked from snxraven/p2ns
Cleanup - Bug Fixes
This commit is contained in:
@@ -5,6 +5,7 @@ const ca = require('../../../security/certificate_authority');
|
||||
const { createInterfaceForDomain } = require('../../../networking/virtual_interfaces');
|
||||
const { logDebug, logError } = require('../../../infrastructure/logger');
|
||||
const { broadcast } = require('../websocket');
|
||||
const { readJsonBody, sendError } = require('../../../infrastructure/http-utils');
|
||||
|
||||
const certsDir = process.env.CERTS_DIR || './certs';
|
||||
|
||||
@@ -75,78 +76,63 @@ async function handleCertsRoutes(req, res) {
|
||||
}
|
||||
|
||||
if (method === 'POST' && urlPath === '/api/generate-cert') {
|
||||
let body = '';
|
||||
req.on('data', chunk => { body += chunk; });
|
||||
req.on('end', async () => {
|
||||
try {
|
||||
const data = JSON.parse(body);
|
||||
|
||||
if (!state.domainToIPMap.has(data.domain)) {
|
||||
await createInterfaceForDomain(data.domain);
|
||||
logDebug('Admin', `Assigned IP to ${data.domain}: ${state.domainToIPMap.get(data.domain)}`);
|
||||
}
|
||||
|
||||
const ip = state.domainToIPMap.get(data.domain);
|
||||
ca.getOrCreateDomainCert(data.domain, ip);
|
||||
|
||||
broadcast({ type: 'update-certs' });
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('OK');
|
||||
} catch (err) {
|
||||
logError('Admin', `Failed to generate cert: ${err.message}`);
|
||||
res.writeHead(500);
|
||||
res.end(err.message);
|
||||
try {
|
||||
const data = await readJsonBody(req);
|
||||
|
||||
if (!state.domainToIPMap.has(data.domain)) {
|
||||
await createInterfaceForDomain(data.domain);
|
||||
logDebug('Admin', `Assigned IP to ${data.domain}: ${state.domainToIPMap.get(data.domain)}`);
|
||||
}
|
||||
});
|
||||
|
||||
const ip = state.domainToIPMap.get(data.domain);
|
||||
ca.getOrCreateDomainCert(data.domain, ip);
|
||||
|
||||
broadcast({ type: 'update-certs' });
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('OK');
|
||||
} catch (err) {
|
||||
logError('Admin', `Failed to generate cert: ${err.message}`);
|
||||
sendError(res, err, 500);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (method === 'POST' && urlPath === '/api/delete-cert') {
|
||||
let body = '';
|
||||
req.on('data', chunk => { body += chunk; });
|
||||
req.on('end', async () => {
|
||||
try {
|
||||
const data = JSON.parse(body);
|
||||
const domainDir = pathModule.join(certsDir, data.domain);
|
||||
await fs.rm(domainDir, { recursive: true, force: true });
|
||||
broadcast({ type: 'update-certs' });
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('OK');
|
||||
} catch (err) {
|
||||
logError('Admin', `Failed to delete cert: ${err.message}`);
|
||||
res.writeHead(500);
|
||||
res.end(err.message);
|
||||
}
|
||||
});
|
||||
try {
|
||||
const data = await readJsonBody(req);
|
||||
const domainDir = pathModule.join(certsDir, data.domain);
|
||||
await fs.rm(domainDir, { recursive: true, force: true });
|
||||
broadcast({ type: 'update-certs' });
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('OK');
|
||||
} catch (err) {
|
||||
logError('Admin', `Failed to delete cert: ${err.message}`);
|
||||
sendError(res, err, 500);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (method === 'POST' && urlPath === '/api/regenerate-cert') {
|
||||
let body = '';
|
||||
req.on('data', chunk => { body += chunk; });
|
||||
req.on('end', async () => {
|
||||
try {
|
||||
const data = JSON.parse(body);
|
||||
const domainDir = pathModule.join(certsDir, data.domain);
|
||||
await fs.rm(domainDir, { recursive: true, force: true });
|
||||
|
||||
if (!state.domainToIPMap.has(data.domain)) {
|
||||
await createInterfaceForDomain(data.domain);
|
||||
logDebug('Admin', `Assigned IP to ${data.domain}: ${state.domainToIPMap.get(data.domain)}`);
|
||||
}
|
||||
|
||||
const ip = state.domainToIPMap.get(data.domain);
|
||||
ca.getOrCreateDomainCert(data.domain, ip);
|
||||
|
||||
broadcast({ type: 'update-certs' });
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('OK');
|
||||
} catch (err) {
|
||||
logError('Admin', `Failed to regenerate cert: ${err.message}`);
|
||||
res.writeHead(500);
|
||||
res.end(err.message);
|
||||
try {
|
||||
const data = await readJsonBody(req);
|
||||
const domainDir = pathModule.join(certsDir, data.domain);
|
||||
await fs.rm(domainDir, { recursive: true, force: true });
|
||||
|
||||
if (!state.domainToIPMap.has(data.domain)) {
|
||||
await createInterfaceForDomain(data.domain);
|
||||
logDebug('Admin', `Assigned IP to ${data.domain}: ${state.domainToIPMap.get(data.domain)}`);
|
||||
}
|
||||
});
|
||||
|
||||
const ip = state.domainToIPMap.get(data.domain);
|
||||
ca.getOrCreateDomainCert(data.domain, ip);
|
||||
|
||||
broadcast({ type: 'update-certs' });
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('OK');
|
||||
} catch (err) {
|
||||
logError('Admin', `Failed to regenerate cert: ${err.message}`);
|
||||
sendError(res, err, 500);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,9 @@ async function handleConsensusRoutes(req, res) {
|
||||
} catch (err) {
|
||||
logError('Consensus', `Failed to get consensus metrics: ${err.message}`);
|
||||
trackRequest('/api/consensus/metrics', false);
|
||||
createErrorResponse(res, 500, 'Failed to get consensus metrics', err.message);
|
||||
const errorResponse = createErrorResponse(err, 500);
|
||||
res.writeHead(errorResponse.statusCode, errorResponse.headers);
|
||||
res.end(errorResponse.body);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -46,7 +48,9 @@ async function handleConsensusRoutes(req, res) {
|
||||
} catch (err) {
|
||||
logError('Consensus', `Failed to get consensus state: ${err.message}`);
|
||||
trackRequest('/api/consensus/:domain', false);
|
||||
createErrorResponse(res, 500, 'Failed to get consensus state', err.message);
|
||||
const errorResponse = createErrorResponse(err, 500);
|
||||
res.writeHead(errorResponse.statusCode, errorResponse.headers);
|
||||
res.end(errorResponse.body);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -73,7 +77,9 @@ async function handleConsensusRoutes(req, res) {
|
||||
} catch (err) {
|
||||
logError('Consensus', `Failed to recalculate consensus: ${err.message}`);
|
||||
trackRequest('/api/consensus/recalculate', false);
|
||||
createErrorResponse(res, 500, 'Failed to recalculate consensus', err.message);
|
||||
const errorResponse = createErrorResponse(err, 500);
|
||||
res.writeHead(errorResponse.statusCode, errorResponse.headers);
|
||||
res.end(errorResponse.body);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -109,7 +115,9 @@ async function handleConsensusRoutes(req, res) {
|
||||
} catch (err) {
|
||||
logError('Consensus', `Failed to recalculate consensus for domain: ${err.message}`);
|
||||
trackRequest('/api/consensus/recalculate/:domain', false);
|
||||
createErrorResponse(res, 500, 'Failed to recalculate consensus for domain', err.message);
|
||||
const errorResponse = createErrorResponse(err, 500);
|
||||
res.writeHead(errorResponse.statusCode, errorResponse.headers);
|
||||
res.end(errorResponse.body);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ const pathModule = require('path');
|
||||
const { logDebug, logError } = require('../../../infrastructure/logger');
|
||||
|
||||
const ADMIN_FRONTEND_DIR = pathModule.join(__dirname, '..', '..', 'admin-frontend');
|
||||
const BROWSER_UTILS_PATH = pathModule.join(__dirname, '..', '..', '..', 'shared', 'browser-utils.js');
|
||||
const TAILWIND_CSS = pathModule.join(__dirname, '..', '..', '..', 'css', 'tailwind.css');
|
||||
|
||||
const MIME_TYPES = {
|
||||
@@ -37,6 +38,18 @@ async function serveAdminFrontendAsset(urlPath, res) {
|
||||
}
|
||||
|
||||
const relativePath = urlPath.replace(/^\//, '');
|
||||
if (relativePath === 'utils.js') {
|
||||
try {
|
||||
await serveFile(BROWSER_UTILS_PATH, res);
|
||||
return true;
|
||||
} catch (err) {
|
||||
logError('Admin', `Failed to serve utils.js: ${err.message}`);
|
||||
res.writeHead(500, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||||
res.end('Failed to load asset');
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
const filePath = pathModule.join(ADMIN_FRONTEND_DIR, relativePath);
|
||||
if (!isPathInsideDir(filePath, ADMIN_FRONTEND_DIR)) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user