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.
 
 
 
 

83 lines
3.0 KiB

#!/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"""
script_dir = os.path.dirname(os.path.abspath(__file__))
cert_file = os.path.join(script_dir, 'cert.pem')
key_file = os.path.join(script_dir, 'key.pem')
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:
# Les certificats sont à la racine du projet
cert_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'cert.pem')
key_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'key.pem')
# Générer si nécessaire
if not os.path.exists(cert_file) or not os.path.exists(key_file):
generate_certificate()
else:
print('✓ Certificats existants trouvés')
# Changer vers le répertoire html
script_dir = os.path.dirname(os.path.abspath(__file__))
html_dir = os.path.join(script_dir, 'html')
os.chdir(html_dir)
# 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()