sdv2500/progettojava
0
1import logging2import sys3import os4from aiohttp import web5 6# Aggiungi path corrente per import moduli7sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))8 9from services.hls_proxy import HLSProxy10from config import PORT11 12# Configurazione logging (già configurata in config.py ma utile per il main)13logging.basicConfig(14 level=logging.INFO,15 format='%(asctime)s - %(levelname)s - %(name)s - %(message)s'16)17 18# --- Logica di Avvio ---19def create_app():20 """Crea e configura l'applicazione aiohttp."""21 zenith = HLSProxy()22 23 app = web.Application()24 25 # Registra le route26 app.router.add_get('/', zenith.handle_root)27 app.router.add_get('/favicon.ico', zenith.handle_favicon) # ✅ Route Favicon28 29 # ✅ Route Static Files (con path assoluto e creazione automatica)30 static_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')31 if not os.path.exists(static_path):32 os.makedirs(static_path)33 app.router.add_static('/static', static_path)34 35 app.router.add_get('/builder', zenith.handle_builder)36 app.router.add_get('/info', zenith.handle_info_page)37 app.router.add_get('/api/info', zenith.handle_api_info)38 app.router.add_get('/key', zenith.handle_key_request)39 app.router.add_get('/zenith/manifest.m3u8', zenith.handle_proxy_request)40 app.router.add_get('/zenith/hls/manifest.m3u8', zenith.handle_proxy_request)41 app.router.add_get('/zenith/mpd/manifest.m3u8', zenith.handle_proxy_request)42 # ✅ NUOVO: Endpoint generico per stream (compatibilità MFP)43 app.router.add_get('/zenith/stream', zenith.handle_proxy_request)44 app.router.add_get('/extractor', zenith.handle_extractor_request)45 # ✅ NUOVO: Endpoint compatibilità MFP per estrazione46 app.router.add_get('/extractor/video', zenith.handle_extractor_request)47 48 # ✅ NUOVO: Route per segmenti con estensioni corrette per compatibilità player49 app.router.add_get('/zenith/hls/segment.ts', zenith.handle_proxy_request)50 app.router.add_get('/zenith/hls/segment.m4s', zenith.handle_proxy_request)51 app.router.add_get('/zenith/hls/segment.mp4', zenith.handle_proxy_request)52 53 app.router.add_get('/playlist', zenith.handle_playlist_request)54 app.router.add_get('/segment/{segment}', zenith.handle_ts_segment)55 app.router.add_get('/decrypt/segment.mp4', zenith.handle_decrypt_segment) # ✅ NUOVO ROUTE56 57 # Route per licenze DRM (GET e POST)58 app.router.add_get('/license', zenith.handle_license_request)59 app.router.add_post('/license', zenith.handle_license_request)60 61 # ✅ NUOVO: Endpoint per generazione URL (compatibilità MFP)62 app.router.add_post('/generate_urls', zenith.handle_generate_urls)63 64 # ✅ NUOVO: Endpoint per ottenere l'IP pubblico65 app.router.add_get('/zenith/ip', zenith.handle_proxy_ip)66 67 # Gestore OPTIONS generico per CORS68 app.router.add_route('OPTIONS', '/{tail:.*}', zenith.handle_options)69 70 async def cleanup_handler(app):71 await zenith.cleanup()72 app.on_cleanup.append(cleanup_handler)73 74 return app75 76# Crea l'istanza "privata" dell'applicazione aiohttp.77app = create_app()78 79def main():80 """Funzione principale per avviare il server."""81 # Workaround per il bug di asyncio su Windows con ConnectionResetError82 if sys.platform == 'win32':83 # Silenzia il logger di asyncio per evitare spam di ConnectionResetError84 logging.getLogger('asyncio').setLevel(logging.CRITICAL)85 86 print("🚀 Avvio HLS Zenith Server...")87 print(f"📡 Server disponibile su: http://localhost:{PORT}")88 print(f"📡 Oppure: http://server-ip:{PORT}")89 print("🔗 Endpoints:")90 print(" • / - Pagina principale")91 print(" • /builder - Interfaccia web per il builder di playlist")92 print(" • /info - Pagina con informazioni sul server")93 print(" • /zenith/manifest.m3u8?url=<URL> - Zenith principale per stream")94 print(" • /playlist?url=<definizioni> - Generatore di playlist")95 print("=" * 50)96 97 web.run_app(98 app, # Usa l'istanza aiohttp originale per il runner integrato99 host='0.0.0.0',100 port=PORT101 )102 103if __name__ == '__main__':104 main()