const https = require('https'); const fs = require('fs'); const path = require('path'); const { exec } = require('child_process'); const PORT = 8443; // Générer un certificat auto-signé si nécessaire function generateCertificate() { return new Promise((resolve, reject) => { const certPath = path.join(__dirname, 'cert.pem'); const keyPath = path.join(__dirname, 'key.pem'); // Vérifier si les certificats existent déjà if (fs.existsSync(certPath) && fs.existsSync(keyPath)) { console.log('✓ Certificats existants trouvés'); resolve({ cert: certPath, key: keyPath }); return; } console.log('Génération d\'un certificat auto-signé...'); const cmd = `openssl req -newkey rsa:2048 -new -nodes -x509 -days 365 -keyout key.pem -out cert.pem -subj "/C=FR/ST=State/L=City/O=Organization/CN=localhost"`; exec(cmd, (error) => { if (error) { console.error('Erreur lors de la génération du certificat:', error); console.log('\n⚠️ Installez OpenSSL ou créez les certificats manuellement'); reject(error); return; } console.log('✓ Certificat généré avec succès'); resolve({ cert: certPath, key: keyPath }); }); }); } async function startServer() { try { const { cert, key } = await generateCertificate(); const options = { key: fs.readFileSync(key), cert: fs.readFileSync(cert) }; const server = https.createServer(options, (req, res) => { let filePath = './html' + req.url; if (filePath === './html/') { filePath = './html/index.html'; } const extname = String(path.extname(filePath)).toLowerCase(); const mimeTypes = { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpg', '.gif': 'image/gif', '.svg': 'image/svg+xml', }; const contentType = mimeTypes[extname] || 'application/octet-stream'; fs.readFile(filePath, (error, content) => { if (error) { if (error.code === 'ENOENT') { res.writeHead(404, { 'Content-Type': 'text/html' }); res.end('