diff --git a/https/cert_abcd.cer b/https/cert_abcd.cer new file mode 100644 index 0000000..d003ca9 --- /dev/null +++ b/https/cert_abcd.cer @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE----- +MIIDGDCCAgCgAwIBAgIQIKm3TdMAErVIFuSUWmXD1TANBgkqhkiG9w0BAQsFADAU +MRIwEAYDVQQDDAlsb2NhbGhvc3QwHhcNMjIwNzI3MTMwMDUzWhcNMjMwNzI3MTMy +MDUzWjAUMRIwEAYDVQQDDAlsb2NhbGhvc3QwggEiMA0GCSqGSIb3DQEBAQUAA4IB +DwAwggEKAoIBAQC97MG8XIuUGoAskplE0qhMv5VyZ2Sgkp4vv8yLVP63VBvYPpg9 +hi6w8HTCwhBIy2ZmUrztqQ7Ye8fFaWlowDnrTp10ewAiAWvpjxSyeJE7dfP1AQcb +x3/YgrOCvQfy/5Xtcnm1SC/9VtxutcePJMwvD6S/5dpkVeCUhVpPuagEGvzStMhk +ovMxxaTcEG++N+2CiEtBpe4xxmDs0mmZhhJgO5t6roYLZzy2Q3BSJ3FNHArOOD3R +hxh0aV9c4RHQUGSZVVUfwUORGDp5XMqlJjfSsnNncBDBex4byV9SBOkQ4mxKQa/c +LG2MQ1RjLSVXSZ3QFMDgWdw+FWIapiOCoMXJAgMBAAGjZjBkMA4GA1UdDwEB/wQE +AwIFoDAdBgNVHSUEFjAUBggrBgEFBQcDAgYIKwYBBQUHAwEwFAYDVR0RBA0wC4IJ +bG9jYWxob3N0MB0GA1UdDgQWBBSju5UGtw9Kz49/xVHoT0XnTlKtkDANBgkqhkiG +9w0BAQsFAAOCAQEAlEqbYXNgVTBLedFNOZz97IxrlAckG+4FMzcXIWhY55GvqwtL +gwWGrjKqGkXLhMVDzWHPSKWYxDdU6WJcBux9yuOz//s1Pro59jRIJvcOivbpXLPI +SaFPnc/29UeEDoh22AGr/9sAhOX2CrVMmrbaaDcZ8UN/YnT1bwufKFQipK1sGSR+ +U5ISZ+pUXJ75aJFEZujucpFT1Sl6GWRWiL7u1Zacmm8jGIdZwpDIynSYK821fJMR +lVDQ9kPBBmU+Ummei/8vc+tqm2/3voekgLxIwxdpykn3L4JMtOn83+sZ2HVoS3Bz +/5+6Gc6u5+d3wLeO23q/sOARprC3ytzGc5bQRw== +-----END CERTIFICATE----- diff --git a/https/info.txt b/https/info.txt new file mode 100644 index 0000000..854358c --- /dev/null +++ b/https/info.txt @@ -0,0 +1,4 @@ +abcd + + +SET OPENSSL_CONF="C:\Program Files (x86)\OpenSSL-Win32\bin\openssl.cfg" \ No newline at end of file diff --git a/server/server.js b/server/server.js index e6653bb..e4325c7 100755 --- a/server/server.js +++ b/server/server.js @@ -1,6 +1,8 @@ import express from 'express'; +import https from 'https'; import path from 'path'; -import fs from 'fs/promises'; +import fs from 'fs'; +import fsPromises from 'fs/promises'; import { fileURLToPath } from 'url'; import process from 'process'; @@ -15,6 +17,9 @@ const publicDir = path.join(__dirname, '..', 'public'); const snapshotsDir = path.join(publicDir, 'snapshots'); const WEBCAM_URL = process.env.WEBCAM_URL || ''; const BODYTRACKER_URL = process.env.BODYTRACKER_URL || ''; +const HTTPS_KEY_PATH = process.env.HTTPS_KEY_PATH || path.join(__dirname, '..', 'https', 'localhost.key'); +const HTTPS_CERT_PATH = process.env.HTTPS_CERT_PATH || path.join(__dirname, '..', 'https', 'localhost.pem'); +const HTTPS_PASSPHRASE = process.env.HTTPS_PASSPHRASE || 'abcd'; app.use(express.static(publicDir)); @@ -23,13 +28,13 @@ app.get('/api/health', (req, res) => { }); async function findLatestSnapshotFile() { - const files = await fs.readdir(snapshotsDir); + const files = await fsPromises.readdir(snapshotsDir); const entries = await Promise.all( files .filter((name) => name.endsWith('.csv')) .map(async (name) => ({ name, - mtime: (await fs.stat(path.join(snapshotsDir, name))).mtime.valueOf() + mtime: (await fsPromises.stat(path.join(snapshotsDir, name))).mtime.valueOf() })) ); if (entries.length === 0) return null; @@ -69,15 +74,15 @@ app.get('/api/latest-snapshot', async (req, res) => { const imagePath = path.join(snapshotsDir, `${baseName}_annotated.jpg`); const imagePath2 = path.join(snapshotsDir, `${baseName}_annotated2.jpg`); - const content = await fs.readFile(csvPath, 'utf8'); - const result = { filename: latestFile, mtime: (await fs.stat(csvPath)).mtime.toISOString(), content }; + const content = await fsPromises.readFile(csvPath, 'utf8'); + const result = { filename: latestFile, mtime: (await fsPromises.stat(csvPath)).mtime.toISOString(), content }; try { - result.jsonFile = { filename: `${baseName}.json`, content: await fs.readFile(jsonPath, 'utf8') }; + result.jsonFile = { filename: `${baseName}.json`, content: await fsPromises.readFile(jsonPath, 'utf8') }; } catch {} try { - const jpg = await fs.readFile(imagePath); + const jpg = await fsPromises.readFile(imagePath); result.imageFile = { filename: path.basename(imagePath), mimeType: 'image/jpeg', @@ -86,7 +91,7 @@ app.get('/api/latest-snapshot', async (req, res) => { } catch {} try { - const jpg2 = await fs.readFile(imagePath2); + const jpg2 = await fsPromises.readFile(imagePath2); result.image2 = { filename: path.basename(imagePath2), mimeType: 'image/jpeg', @@ -160,6 +165,24 @@ async function checkServiceReachability(name, url) { } } +async function createHttpsServer() { + try { + await fsPromises.access(HTTPS_KEY_PATH); + await fsPromises.access(HTTPS_CERT_PATH); + + const key = fs.readFileSync(HTTPS_KEY_PATH); + const cert = fs.readFileSync(HTTPS_CERT_PATH); + const httpsOptions = { key, cert, passphrase: HTTPS_PASSPHRASE }; + + console.log(`HTTPS-Zertifikate geladen: ${HTTPS_KEY_PATH}, ${HTTPS_CERT_PATH}`); + return https.createServer(httpsOptions, app); + } catch (err) { + console.warn('HTTPS-Zertifikate konnten nicht geladen werden:', err.message || err); + console.warn('Fallback auf HTTP. External proxy muss HTTPS terminieren.'); + return null; + } +} + async function startServer() { if (WEBCAM_URL) { await checkServiceReachability('WEBCAM_URL', new URL('/health', WEBCAM_URL).toString()); @@ -169,8 +192,12 @@ async function startServer() { await checkServiceReachability('BODYTRACKER_URL', new URL('/v1/health', BODYTRACKER_URL).toString()); } - app.listen(PORT, () => { - console.log(`appRobotHoming backend listening on port ${PORT}`); + const server = await createHttpsServer(); + const isHttps = Boolean(server); + const listenServer = server || app; + + listenServer.listen(PORT, () => { + console.log(`appRobotHoming backend listening on port ${PORT} (${isHttps ? 'HTTPS' : 'HTTP'})`); console.log(`WEBCAM_URL=${WEBCAM_URL || ''}`); console.log(`BODYTRACKER_URL=${BODYTRACKER_URL || ''}`); }); @@ -180,7 +207,7 @@ startServer().catch((err) => { console.error('Fehler beim Starten des Servers:', err); console.log('Starte trotzdem den Server weiter...'); app.listen(PORT, () => { - console.log(`appRobotHoming backend listening on port ${PORT}`); + console.log(`appRobotHoming backend listening on port ${PORT} (HTTP)`); console.log(`WEBCAM_URL=${WEBCAM_URL || ''}`); console.log(`BODYTRACKER_URL=${BODYTRACKER_URL || ''}`); });