diff --git a/ap.py b/ap.py index 158c34c..1e7b937 100644 --- a/ap.py +++ b/ap.py @@ -58,6 +58,10 @@ def remove_uptime_file(): atexit.register(remove_uptime_file) def _sig_handler(signum, frame): + try: + if 'shutdown_wifi' in globals(): + shutdown_wifi() + except Exception: pass try: if 'disconnect_all_vpn_profiles' in globals(): disconnect_all_vpn_profiles('SIGTERM') @@ -2045,10 +2049,10 @@ def format_speed(bps): def format_bytes(b): if not b or b <= 0: return "0 o" if b < 1024: return f"{int(b)} o" - if b < 1048576: return f"{b/1024:.1f} Ko" - if b < 1073741824: return f"{b/1048576:.2f} Mo" - if b < 1099511627776: return f"{b/1073741824:.2f} Go" - return f"{b/1099511627776:.2f} To" + if b < 1048576: return f"{b/1024:.1f}".rstrip('0').rstrip('.') + " Ko" + if b < 1073741824: return f"{b/1048576:.2f}".rstrip('0').rstrip('.') + " Mo" + if b < 1099511627776: return f"{b/1073741824:.2f}".rstrip('0').rstrip('.') + " Go" + return f"{b/1099511627776:.2f}".rstrip('0').rstrip('.') + " To" def get_top_clients(active_items=None, range_type='day'): active_map = {} @@ -4167,6 +4171,39 @@ def stop_hostapd(iface=None): except OSError: pass subprocess.run(['ip', 'link', 'set', 'dev', iface, 'nomaster'], capture_output=True, timeout=2) +def shutdown_wifi(iface=None): + """Coupe complètement le réseau Wi-Fi lors de l'arrêt du conteneur (hostapd, NM, et interface down).""" + wifi_if = iface or INTERFACE or detect_wifi_interface() + try: + stop_hostapd(wifi_if) + except Exception: pass + try: + nmcli_bin = find_bin('nmcli') + if nmcli_bin and wifi_if: + try: + name = connection_name() + if name: + subprocess.run([nmcli_bin, 'connection', 'down', name], capture_output=True, timeout=2) + except Exception: pass + subprocess.run([nmcli_bin, 'device', 'disconnect', wifi_if], capture_output=True, timeout=2) + except Exception: pass + try: + if wifi_if: + iw_bin = find_bin('iw') + if iw_bin: + try: + dump = command('iw', 'dev', wifi_if, 'station', 'dump') + for m in re.finditer(r'^Station ([0-9a-f:]{17})', dump, re.I | re.M): + subprocess.run([iw_bin, 'dev', wifi_if, 'station', 'del', m.group(1)], capture_output=True, timeout=1) + 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) + def ensure_nm_manages_wifi(iface=None, force=False): """Rend le contrôle de l'interface Wi-Fi à NetworkManager (pour le mode AP/NAT) et attend qu'elle soit prête.""" iface = iface or get_wifi_interface() diff --git a/entrypoint.sh b/entrypoint.sh index f3c6911..c8fc65e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -418,13 +418,35 @@ cleanup() { for _i in 1 2 3; do ip -4 rule del table "$tid" 2>/dev/null || break; done for _i in 1 2 3; do ip -6 rule del table "$tid" 2>/dev/null || break; done done - # Arrêt propre de hostapd (mode pont) et détachement de l'interface Wi-Fi sans toucher au pont hôte + # Arrêt complet et extinction du réseau Wi-Fi (AP/NAT et pont) + log "Extinction du réseau Wi-Fi et déconnexion des clients..." + if command -v nmcli >/dev/null 2>&1; then + [[ -n "${IFACE:-}" ]] && nmcli device disconnect "$IFACE" 2>/dev/null || true + for _con in $(nmcli -t -g NAME,DEVICE connection show --active 2>/dev/null | grep ":${IFACE:-}\$" | cut -d: -f1); do + nmcli connection down "$_con" 2>/dev/null || true + done + fi if [[ -f /run/ap-hostapd.pid ]]; then - kill "$(cat /run/ap-hostapd.pid 2>/dev/null || true)" 2>/dev/null || true + kill -15 "$(cat /run/ap-hostapd.pid 2>/dev/null || true)" 2>/dev/null || true + sleep 0.1 + kill -9 "$(cat /run/ap-hostapd.pid 2>/dev/null || true)" 2>/dev/null || true rm -f /run/ap-hostapd.pid /run/ap-hostapd.conf 2>/dev/null || true fi - pkill -f '/run/ap-hostapd.conf' 2>/dev/null || true - [[ -n "${IFACE:-}" ]] && ip link set "$IFACE" nomaster 2>/dev/null || true + pkill -9 hostapd 2>/dev/null || true + pkill -9 -f 'ap-hostapd' 2>/dev/null || true + rm -rf /run/hostapd 2>/dev/null || true + if [[ -n "${IFACE:-}" ]]; then + if command -v iw >/dev/null 2>&1; then + for _sta in $(iw dev "$IFACE" station dump 2>/dev/null | awk '$1=="Station"{print $2}'); do + iw dev "$IFACE" station del "$_sta" 2>/dev/null || true + done + 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 # Restauration uniquement d'un pont créé par APmanageur (/run/ap-created-bridge) diff --git a/index.html b/index.html index 248d2f3..0cad23b 100644 --- a/index.html +++ b/index.html @@ -6634,7 +6634,7 @@ style="background:var(--panel);padding:10px 12px;border-radius:8px;border:1px solid var(--line);margin-top:2px">