added https for client and managment

This commit is contained in:
Philipp
2026-05-21 15:11:05 +02:00
parent 0046156e58
commit e5bcce5dbb
8 changed files with 129 additions and 11 deletions
+1
View File
@@ -6,3 +6,4 @@ DATABASE_PATH="./management.sqlite"
CORS_ORIGINS="http://localhost:5173"
SESSION_COOKIE_SECURE=false
ALLOW_INSECURE_AGENT_HTTP=true
AGENT_CA_FILE=""
+55 -8
View File
@@ -1,23 +1,70 @@
import http from 'node:http';
import https from 'node:https';
import { readFileSync } from 'node:fs';
import { config } from './config.js';
const agentCa = config.agentCaFile ? readFileSync(config.agentCaFile, 'utf8') : undefined;
export async function agentRequest(node, path, options = {}) {
const response = await fetch(`${node.baseUrl}/api${path}`, {
const url = new URL(`${node.baseUrl}/api${path}`);
const data = options.body ? JSON.stringify(options.body) : '';
const response = await requestJson(url, {
method: options.method || 'GET',
headers: {
Authorization: `Bearer ${node.token}`,
'Content-Type': 'application/json',
...(data ? { 'Content-Length': Buffer.byteLength(data) } : {}),
...(options.headers || {}),
},
body: options.body ? JSON.stringify(options.body) : undefined,
signal: AbortSignal.timeout(options.timeout || 20000),
body: data,
timeout: options.timeout || 20000,
});
const text = await response.text();
const data = text ? JSON.parse(text) : null;
if (!response.ok) {
const error = new Error(data?.error || `Agent request failed with ${response.status}`);
if (response.status < 200 || response.status >= 300) {
const error = new Error(response.data?.error || `Agent request failed with ${response.status}`);
error.status = response.status;
throw error;
}
return data;
return response.data;
}
function requestJson(url, options) {
return new Promise((resolve, reject) => {
const transport = url.protocol === 'https:' ? https : http;
const request = transport.request({
protocol: url.protocol,
hostname: url.hostname,
port: url.port,
path: `${url.pathname}${url.search}`,
method: options.method,
headers: options.headers,
timeout: options.timeout,
...(url.protocol === 'https:' && agentCa ? { ca: agentCa } : {}),
}, (response) => {
let text = '';
response.setEncoding('utf8');
response.on('data', (chunk) => {
text += chunk;
});
response.on('end', () => {
try {
resolve({
status: response.statusCode || 0,
data: text ? JSON.parse(text) : null,
});
} catch (error) {
reject(error);
}
});
});
request.on('timeout', () => {
request.destroy(new Error('Agent request timed out.'));
});
request.on('error', reject);
if (options.body) request.write(options.body);
request.end();
});
}
export function publicNode(node) {
+1
View File
@@ -17,4 +17,5 @@ export const config = {
? process.env.SESSION_COOKIE_SECURE === 'true'
: process.env.NODE_ENV === 'production',
allowInsecureAgentHttp: process.env.ALLOW_INSECURE_AGENT_HTTP === 'true',
agentCaFile: process.env.AGENT_CA_FILE ? path.resolve(process.cwd(), process.env.AGENT_CA_FILE) : '',
};