diff --git a/ap.py b/ap.py index 1e7b937..1bb5d51 100644 --- a/ap.py +++ b/ap.py @@ -4198,8 +4198,6 @@ def shutdown_wifi(iface=None): except Exception: pass subprocess.run(['ip', 'link', 'set', 'dev', wifi_if, 'nomaster'], capture_output=True, timeout=2) subprocess.run(['ip', 'link', 'set', 'dev', wifi_if, 'down'], capture_output=True, timeout=2) - if iw_bin: - subprocess.run([iw_bin, 'dev', wifi_if, 'set', 'type', 'managed'], capture_output=True, timeout=2) except Exception: pass atexit.register(shutdown_wifi) @@ -4220,10 +4218,12 @@ def ensure_nm_manages_wifi(iface=None, force=False): stop_hostapd(iface) ensure_wpa_supplicant_running() - subprocess.run(['rfkill', 'unblock', 'wifi'], capture_output=True, timeout=2) - subprocess.run(['ip', 'link', 'set', 'dev', iface, 'nomaster'], capture_output=True, timeout=2) - subprocess.run(['nmcli', 'device', 'set', iface, 'managed', 'yes'], capture_output=True, timeout=2) - subprocess.run(['nmcli', 'radio', 'wifi', 'on'], capture_output=True, timeout=2) + try: + subprocess.run(['rfkill', 'unblock', 'wifi'], capture_output=True, timeout=2) + subprocess.run(['ip', 'link', 'set', 'dev', iface, 'nomaster'], capture_output=True, timeout=2) + subprocess.run(['nmcli', 'device', 'set', iface, 'managed', 'yes'], capture_output=True, timeout=2) + subprocess.run(['nmcli', 'radio', 'wifi', 'on'], capture_output=True, timeout=2) + except Exception: pass for _ in range(5): st = command('nmcli', '-t', '-g', 'GENERAL.STATE', 'device', 'show', iface, timeout=1) if st and not st.startswith('10'): @@ -4244,32 +4244,36 @@ def start_bridge_hostapd(ssid, password, sec='wpa2', bridge_name='br0', iface=No stop_hostapd(iface) was_managed = not (command('nmcli', '-t', '-g', 'GENERAL.STATE', 'device', 'show', iface, timeout=1) or '').startswith('10') if was_managed: - subprocess.run(['nmcli', '--wait', '2', 'device', 'disconnect', iface], capture_output=True, timeout=2) - subprocess.run(['nmcli', 'device', 'set', iface, 'managed', 'no'], capture_output=True, timeout=2) + try: + subprocess.run(['nmcli', '--wait', '2', 'device', 'disconnect', iface], capture_output=True, timeout=2) + subprocess.run(['nmcli', 'device', 'set', iface, 'managed', 'no'], capture_output=True, timeout=2) + except Exception: pass for _ in range(10): st = command('nmcli', '-t', '-g', 'GENERAL.STATE', 'device', 'show', iface, timeout=1) if not st or st.startswith('10'): # 10 = unmanaged break time.sleep(0.05) # Arrêter wpa_supplicant afin qu'il ne bloque jamais les trames EAPOL de hostapd - subprocess.run(['pkill', '-x', 'wpa_supplicant'], capture_output=True, timeout=2) + try: + subprocess.run(['pkill', '-x', 'wpa_supplicant'], capture_output=True, timeout=2) + except Exception: pass for _ in range(5): if subprocess.run(['pgrep', '-x', 'wpa_supplicant'], capture_output=True, timeout=1).returncode != 0: break time.sleep(0.05) country = os.environ.get('AP_COUNTRY', 'FR').strip().upper() or 'FR' - subprocess.run(['rfkill', 'unblock', 'wifi'], capture_output=True, timeout=2) - subprocess.run(['iw', 'reg', 'set', country], capture_output=True, timeout=2) - # Cycle rapide DOWN -> managed -> UP -> DOWN pour vider l'ancien BSS/table de clés du firmware - subprocess.run(['ip', 'link', 'set', 'dev', iface, 'down'], capture_output=True, timeout=2) - subprocess.run(['ip', 'addr', 'flush', 'dev', iface], capture_output=True, timeout=2) - subprocess.run(['ip', 'link', 'set', 'dev', iface, 'nomaster'], capture_output=True, timeout=2) - subprocess.run(['iw', 'dev', iface, 'set', 'type', 'managed'], capture_output=True, timeout=2) - subprocess.run(['ip', 'link', 'set', 'dev', iface, 'up'], capture_output=True, timeout=2) - time.sleep(0.1) - subprocess.run(['ip', 'link', 'set', 'dev', iface, 'down'], capture_output=True, timeout=2) - time.sleep(0.1) + try: + subprocess.run(['rfkill', 'unblock', 'wifi'], capture_output=True, timeout=2) + subprocess.run(['iw', 'reg', 'set', country], capture_output=True, timeout=2) + except Exception: pass + + # Libération et préparation propre de l'interface pour hostapd + try: + subprocess.run(['ip', 'link', 'set', 'dev', iface, 'nomaster'], capture_output=True, timeout=2) + subprocess.run(['ip', 'addr', 'flush', 'dev', iface], capture_output=True, timeout=2) + subprocess.run(['ip', 'link', 'set', 'dev', iface, 'down'], capture_output=True, timeout=2) + except Exception: pass band = (AP_BAND or 'bg').strip().lower() hw_mode = 'a' if band == 'a' else 'g' @@ -4338,21 +4342,24 @@ def start_bridge_hostapd(ssid, password, sec='wpa2', bridge_name='br0', iface=No os.remove(ctrl_sock) except OSError: pass - r = subprocess.run( - [hostapd_bin, '-B', '-P', HOSTAPD_PID_FILE, '-f', HOSTAPD_LOG_FILE, HOSTAPD_CONF_FILE], - capture_output=True, text=True, timeout=5 - ) - if r.returncode != 0: - err = (r.stderr or r.stdout or '').strip() - if os.path.exists(HOSTAPD_LOG_FILE): - try: - log_tail = open(HOSTAPD_LOG_FILE, encoding='utf-8', errors='replace').read().strip() - if log_tail: - last_line = log_tail.splitlines()[-1] - err = last_line if not err else f"{err} | {last_line}" - except OSError: - pass - return False, err or 'hostapd a refusé de démarrer' + try: + r = subprocess.run( + [hostapd_bin, '-B', '-P', HOSTAPD_PID_FILE, '-f', HOSTAPD_LOG_FILE, HOSTAPD_CONF_FILE], + capture_output=True, text=True, timeout=5 + ) + if r.returncode != 0: + err = (r.stderr or r.stdout or '').strip() + if os.path.exists(HOSTAPD_LOG_FILE): + try: + log_tail = open(HOSTAPD_LOG_FILE, encoding='utf-8', errors='replace').read().strip() + if log_tail: + last_line = log_tail.splitlines()[-1] + err = last_line if not err else f"{err} | {last_line}" + except OSError: + pass + return False, err or 'hostapd a refusé de démarrer' + except Exception as e: + return False, f"Impossible d'exécuter hostapd: {e}" for i_attempt in range(20): time.sleep(0.1) @@ -4367,14 +4374,18 @@ def start_bridge_hostapd(ssid, password, sec='wpa2', bridge_name='br0', iface=No has_master = os.path.exists(f'/sys/class/net/{iface}/master') if 'AP-ENABLED' in log_txt: if not has_master: - subprocess.run(['ip', 'link', 'set', 'dev', iface, 'master', bridge_name], capture_output=True, timeout=1) + try: + subprocess.run(['ip', 'link', 'set', 'dev', iface, 'master', bridge_name], capture_output=True, timeout=1) + except Exception: pass disable_wifi_powersave(iface) return True, '' if i_attempt % 3 == 0: iw_out = command('iw', 'dev', iface, 'info', timeout=0.8) or '' if 'type AP' in iw_out: if not has_master: - subprocess.run(['ip', 'link', 'set', 'dev', iface, 'master', bridge_name], capture_output=True, timeout=1) + try: + subprocess.run(['ip', 'link', 'set', 'dev', iface, 'master', bridge_name], capture_output=True, timeout=1) + except Exception: pass disable_wifi_powersave(iface) return True, '' @@ -4382,7 +4393,9 @@ def start_bridge_hostapd(ssid, password, sec='wpa2', bridge_name='br0', iface=No iw_out = command('iw', 'dev', iface, 'info', timeout=0.8) 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, timeout=1) + try: + subprocess.run(['ip', 'link', 'set', 'dev', iface, 'master', bridge_name], capture_output=True, timeout=1) + except Exception: pass disable_wifi_powersave(iface) return True, '' return False, 'hostapd démarré mais interface non passée en mode AP' diff --git a/entrypoint.sh b/entrypoint.sh index c8fc65e..222b05d 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -443,9 +443,6 @@ cleanup() { fi ip link set "$IFACE" nomaster 2>/dev/null || true ip link set "$IFACE" down 2>/dev/null || true - if command -v iw >/dev/null 2>&1; then - iw dev "$IFACE" set type managed 2>/dev/null || true - fi fi # Suppression du fichier d'uptime lors de l'arrêt du conteneur rm -f "${AP_UPTIME_FILE:-/data/uptime}" /app/ap-dashboard-uptime 2>/dev/null || true diff --git a/index.html b/index.html index 0cad23b..617204a 100644 --- a/index.html +++ b/index.html @@ -6617,6 +6617,8 @@