#!/usr/bin/env python3 """ Serveur HTTPS simple pour Web Bluetooth API Utilise un certificat auto-signé pour localhost """ import http.server import ssl import os import subprocess import sys PORT = 8443 def generate_certificate(): """Génère un certificat auto-signé si nécessaire""" cert_file = 'cert.pem' key_file = 'key.pem' if os.path.exists(cert_file) and os.path.exists(key_file): print('✓ Certificats existants trouvés') return cert_file, key_file print('Génération d\'un certificat auto-signé...') try: cmd = [ 'openssl', 'req', '-newkey', 'rsa:2048', '-new', '-nodes', '-x509', '-days', '365', '-keyout', key_file, '-out', cert_file, '-subj', '/C=FR/ST=State/L=City/O=Organization/CN=localhost' ] subprocess.run(cmd, check=True, capture_output=True) print('✓ Certificat généré avec succès') return cert_file, key_file except (subprocess.CalledProcessError, FileNotFoundError): print('\n⚠️ Erreur: OpenSSL n\'est pas installé ou a échoué') print('Installez OpenSSL: sudo apt install openssl') sys.exit(1) def main(): try: cert_file, key_file = generate_certificate() # Changer vers le répertoire du script os.chdir(os.path.dirname(os.path.abspath(__file__))) # Créer le serveur HTTP handler = http.server.SimpleHTTPRequestHandler httpd = http.server.HTTPServer(('localhost', PORT), handler) # Configurer SSL context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) context.load_cert_chain(cert_file, key_file) httpd.socket = context.wrap_socket(httpd.socket, server_side=True) print('\n' + '=' * 60) print('🚀 Serveur HTTPS démarré avec succès!') print('=' * 60) print(f'\n📱 Ouvrez Chrome et accédez à:') print(f'\n 👉 https://localhost:{PORT}\n') print('⚠️ Vous verrez un avertissement de sécurité (certificat auto-signé)') print(' Cliquez sur "Avancé" puis "Continuer vers localhost"\n') print('🔵 Web Bluetooth sera maintenant disponible!\n') print('Appuyez sur Ctrl+C pour arrêter le serveur') print('=' * 60 + '\n') httpd.serve_forever() except KeyboardInterrupt: print('\n\n✓ Serveur arrêté') sys.exit(0) except Exception as e: print(f'\n❌ Erreur: {e}') sys.exit(1) if __name__ == '__main__': main()