You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

101 lines
3.8 KiB

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('<h1>404 - File Not Found</h1>', 'utf-8');
} else {
res.writeHead(500);
res.end('Error: ' + error.code, 'utf-8');
}
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
});
server.listen(PORT, () => {
console.log('\n' + '='.repeat(60));
console.log('🚀 Serveur HTTPS démarré avec succès!');
console.log('='.repeat(60));
console.log(`\n📱 Ouvrez Chrome et accédez à:`);
console.log(`\n 👉 https://localhost:${PORT}\n`);
console.log('⚠ Vous verrez un avertissement de sécurité (certificat auto-signé)');
console.log(' Cliquez sur "Avancé" puis "Continuer vers localhost"\n');
console.log('🔵 Web Bluetooth sera maintenant disponible!\n');
console.log('Appuyez sur Ctrl+C pour arrêter le serveur');
console.log('='.repeat(60) + '\n');
});
} catch (error) {
console.error('Impossible de démarrer le serveur:', error);
process.exit(1);
}
}
startServer();