diff --git a/ap.py b/ap.py index e006dbf..a301967 100644 --- a/ap.py +++ b/ap.py @@ -3008,17 +3008,32 @@ def start_bridge_hostapd(ssid, password, sec='wpa2', bridge_name='br0', iface=No if not os.path.exists(f'/sys/class/net/{bridge_name}'): raise RuntimeError(f"Le pont réseau '{bridge_name}' n'existe pas sur le système.") - # 1. Arrêter toute ancienne instance hostapd et retirer le contrôle NM de la carte Wi-Fi uniquement + # 1. Arrêter toute ancienne instance hostapd et attendre que NetworkManager + wpa_supplicant libèrent complètement la carte Wi-Fi stop_hostapd(iface) + was_managed = not (command('nmcli', '-t', '-g', 'GENERAL.STATE', 'device', 'show', iface) or '').startswith('10') subprocess.run(['nmcli', 'device', 'disconnect', iface], capture_output=True) subprocess.run(['nmcli', 'device', 'set', iface, 'managed', 'no'], capture_output=True) - time.sleep(0.4) - subprocess.run(['rfkill', 'unblock', 'wifi'], capture_output=True) - subprocess.run(['ip', 'addr', 'flush', 'dev', iface], capture_output=True) - subprocess.run(['ip', 'link', 'set', 'dev', iface, 'nomaster'], capture_output=True) - subprocess.run(['ip', 'link', 'set', 'dev', iface, 'up'], capture_output=True) + for _ in range(25): + st = command('nmcli', '-t', '-g', 'GENERAL.STATE', 'device', 'show', iface) + if not st or st.startswith('10'): # 10 = unmanaged + break + time.sleep(0.2) + # Si la carte venait d'être gérée par NM, laisser wpa_supplicant finir le retrait D-Bus/nl80211 pour éviter tout conflit EAPOL au 1er démarrage + if was_managed: + time.sleep(1.0) + # S'assurer qu'aucune instance wpa_supplicant résiduelle ne garde l'interface Wi-Fi verrouillée + subprocess.run(['pkill', '-f', f'wpa_supplicant.*{iface}'], capture_output=True) + time.sleep(0.3) country = os.environ.get('AP_COUNTRY', 'FR').strip().upper() or 'FR' + subprocess.run(['rfkill', 'unblock', 'wifi'], capture_output=True) + subprocess.run(['iw', 'reg', 'set', country], capture_output=True) + # Remise à zéro propre de l'interface (DOWN -> flush -> nomaster) afin que hostapd l'initialise lui-même en mode AP + bridge + subprocess.run(['ip', 'link', 'set', 'dev', iface, 'down'], capture_output=True) + subprocess.run(['ip', 'addr', 'flush', 'dev', iface], capture_output=True) + subprocess.run(['ip', 'link', 'set', 'dev', iface, 'nomaster'], capture_output=True) + time.sleep(0.2) + band = (AP_BAND or 'bg').strip().lower() hw_mode = 'a' if band == 'a' else 'g' channel = str(AP_CHANNEL or '6').strip() @@ -3093,16 +3108,38 @@ def start_bridge_hostapd(ssid, password, sec='wpa2', bridge_name='br0', iface=No except OSError: pass return False, err or 'hostapd a refusé de démarrer' - for _ in range(25): + ap_ready = False + for _ in range(30): time.sleep(0.2) - if is_hostapd_running() and os.path.exists(f'/sys/class/net/{iface}/master'): - return True, '' if not is_hostapd_running(): break + iw_out = command('iw', 'dev', iface, 'info') or '' + has_master = os.path.exists(f'/sys/class/net/{iface}/master') + log_txt = '' + if os.path.exists(HOSTAPD_LOG_FILE): + try: log_txt = open(HOSTAPD_LOG_FILE, encoding='utf-8', errors='replace').read() + except OSError: pass + if 'AP-ENABLED' in log_txt and 'type AP' in iw_out and has_master: + ap_ready = True + break + if ap_ready: + # Vérification de stabilité courte (0,4s) pour s'assurer qu'aucun événement NM résiduel n'a désactivé l'AP + time.sleep(0.4) + iw_out = command('iw', 'dev', iface, 'info') or '' + log_txt = '' + if os.path.exists(HOSTAPD_LOG_FILE): + try: log_txt = open(HOSTAPD_LOG_FILE, encoding='utf-8', errors='replace').read() + except OSError: pass + if is_hostapd_running() and 'type AP' in iw_out and os.path.exists(f'/sys/class/net/{iface}/master') and 'AP-DISABLED' not in log_txt: + return True, '' + return False, 'Interface AP instable après initialisation initiale' if is_hostapd_running(): - if not os.path.exists(f'/sys/class/net/{iface}/master'): - subprocess.run(['ip', 'link', 'set', 'dev', iface, 'master', bridge_name], capture_output=True) - return True, '' + iw_out = command('iw', 'dev', iface, 'info') or '' + if 'type AP' in iw_out: + if not os.path.exists(f'/sys/class/net/{iface}/master'): + subprocess.run(['ip', 'link', 'set', 'dev', iface, 'master', bridge_name], capture_output=True) + return True, '' + return False, 'hostapd démarré mais interface non passée en mode AP' err = '' if os.path.exists(HOSTAPD_LOG_FILE): try: err = open(HOSTAPD_LOG_FILE, encoding='utf-8', errors='replace').read().strip() @@ -3111,11 +3148,19 @@ def start_bridge_hostapd(ssid, password, sec='wpa2', bridge_name='br0', iface=No ok, err = _try_launch(enable_ht=True) if not ok: - print(f"[ap-bridge] hostapd (802.11n) a échoué ({err}), nouvelle tentative sans 802.11n...", flush=True) + print(f"[ap-bridge] 1er démarrage hostapd incomplet ({err}), relance propre...", flush=True) stop_hostapd(iface) - ok, err2 = _try_launch(enable_ht=False) + subprocess.run(['ip', 'link', 'set', 'dev', iface, 'down'], capture_output=True) + time.sleep(0.5) + ok, err2 = _try_launch(enable_ht=True) if not ok: - raise RuntimeError(f"Impossible de démarrer le point d'accès en mode Pont (hostapd) : {err2 or err}") + print(f"[ap-bridge] hostapd (802.11n) a échoué ({err2}), nouvelle tentative sans 802.11n...", flush=True) + stop_hostapd(iface) + subprocess.run(['ip', 'link', 'set', 'dev', iface, 'down'], capture_output=True) + time.sleep(0.3) + ok, err3 = _try_launch(enable_ht=False) + if not ok: + raise RuntimeError(f"Impossible de démarrer le point d'accès en mode Pont (hostapd) : {err3 or err2 or err}") # Appliquer la redirection DNS du mode pont maintenant que l'AP est actif setup_bridge_dns_redirection(bridge_name, iface)