Add Wayland compatibility and improve periodic checks

This commit is contained in:
Pascal Bouquet 2025-09-18 17:54:53 +02:00
parent d7e3c4c843
commit 270764c1c4

View File

@ -4,7 +4,7 @@
# NetworkManager Captive Portal Auto-Detector # NetworkManager Captive Portal Auto-Detector
# ============================================================================= # =============================================================================
# Erstellt von Pascal Bouquet am 16.09.2025 # Erstellt von Pascal Bouquet am 16.09.2025
# Aktualisiert am 17.09.2025 # Aktualisiert am 18.09.2025
# #
# Dieses Skript ist freie Software: Es kann frei verwendet, bearbeitet und # Dieses Skript ist freie Software: Es kann frei verwendet, bearbeitet und
# verbreitet werden. Es wird keine Garantie für die Funktionsfähigekeit übernommen. # verbreitet werden. Es wird keine Garantie für die Funktionsfähigekeit übernommen.
@ -14,9 +14,9 @@
# KONFIGURATION # KONFIGURATION
# ============================================================================= # =============================================================================
LOG_FILE="/var/log/nm-captive.log" LOG_FILE="/var/log/nm-captive.log"
LOG_LEVEL="INFO" # NONE, DEBUG, INFO, WARNING, ERROR LOG_LEVEL="ERROR" # NONE, DEBUG, INFO, WARNING, ERROR
CHECK_INTERVAL=300 # 5 Minuten in Sekunden CHECK_INTERVAL=300 # 5 Minuten in Sekunden
MAX_CHECKS=48 # Maximal 4 Stunde lang prüfen (48 * 5min) MAX_CHECKS=12 # Maximal 1 Stunde lang prüfen (12 * 5min)
# Test-URLs für Captive Portal Erkennung # Test-URLs für Captive Portal Erkennung
TEST_URLS=( TEST_URLS=(
@ -58,6 +58,75 @@ log() {
esac esac
} }
get_user_environment() {
local user=$1
local user_id=$(id -u "$user")
# Finde den Desktop-Prozess und übernehme dessen Environment
local pid=$(pgrep -u "$user" -n plasmashell || pgrep -u "$user" -n gnome-shell || pgrep -u "$user" -n kwin_wayland || echo "")
if [ -n "$pid" ] && [ -f "/proc/$pid/environ" ]; then
log "DEBUG" "Using environment from process $pid"
# Extrahiere alle benötigten Environment-Variablen
tr '\0' '\n' < "/proc/$pid/environ" | grep -E "(WAYLAND|DISPLAY|XDG|DBUS|USER|HOME|PATH)"
else
# Fallback: Basis-Environment setzen
log "DEBUG" "Using fallback environment"
echo "USER=$user"
echo "HOME=/home/$user"
echo "XDG_RUNTIME_DIR=/run/user/$user_id"
echo "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$user_id/bus"
# Versuche, den echten Display zu finden
if [ -S "/run/user/$user_id/wayland-0" ]; then
echo "WAYLAND_DISPLAY=wayland-0"
echo "XDG_SESSION_TYPE=wayland"
elif [ -S "/tmp/.X11-unix/X0" ]; then
echo "DISPLAY=:0"
else
echo "DISPLAY=:0"
fi
fi
}
get_user_session() {
local user=""
local display=""
local session_type=""
# Finde den aktuellen GUI-Benutzer
user=$(who | grep -E "(tty[0-9]|pts/)" | awk '{print $1}' | head -n1)
log "DEBUG" "who found user: $user"
if [ -n "$user" ]; then
local user_id=$(id -u "$user")
# Prüfe auf Wayland
if [ -S "/run/user/$user_id/wayland-0" ]; then
session_type="wayland"
display="wayland-0"
log "DEBUG" "Wayland socket detected"
elif [ -S "/tmp/.X11-unix/X0" ]; then
session_type="x11"
display=":0"
log "DEBUG" "X11 socket detected"
else
# Fallback basierend auf Prozessen
if pgrep -u "$user" kwin_wayland >/dev/null; then
session_type="wayland"
display="wayland-0"
log "DEBUG" "Wayland process detected"
else
session_type="x11"
display=":0"
log "DEBUG" "Assuming X11"
fi
fi
fi
echo "$user $display $session_type"
}
check_captive_portal() { check_captive_portal() {
local captive_detected=false local captive_detected=false
@ -65,7 +134,6 @@ check_captive_portal() {
log "DEBUG" "Testing URL: $url" log "DEBUG" "Testing URL: $url"
if [[ "$url" == *"apple.com"* ]]; then if [[ "$url" == *"apple.com"* ]]; then
# Apple Check: Erwartet "Success" in der Response
local response=$(curl -s -L --connect-timeout 5 "$url") local response=$(curl -s -L --connect-timeout 5 "$url")
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
log "DEBUG" "Network error for $url" log "DEBUG" "Network error for $url"
@ -78,7 +146,6 @@ check_captive_portal() {
fi fi
elif [[ "$url" == *"google"* || "$url" == *"gstatic"* ]]; then elif [[ "$url" == *"google"* || "$url" == *"gstatic"* ]]; then
# Google Check: Erwartet HTTP 204
local http_code=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 "$url") local http_code=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 "$url")
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
log "DEBUG" "Network error for $url" log "DEBUG" "Network error for $url"
@ -101,9 +168,45 @@ check_captive_portal() {
fi fi
} }
start_firefox() {
local user=$1
local display=$2
local session_type=$3
log "INFO" "Attempting to start Firefox for user: $user, display: $display, type: $session_type"
# Holle die komplette Environment des Users
local user_env=$(get_user_environment "$user")
log "DEBUG" "User environment: $user_env"
# Baue den sudo-Befehl mit der kompletten Environment
local env_vars=""
while IFS= read -r line; do
if [ -n "$line" ]; then
env_vars="$env_vars $line"
fi
done <<< "$user_env"
# Starte Firefox mit der kompletten Environment
log "DEBUG" "Starting Firefox with full user environment"
sudo -u "$user" $env_vars firefox --new-window "http://detectportal.firefox.com/canonical.html" &
local pid=$!
sleep 1
if ps -p $pid >/dev/null 2>&1; then
log "INFO" "Firefox successfully started with PID: $pid"
return 0
else
log "ERROR" "Firefox process died immediately after start"
return 1
fi
}
start_periodic_checks() { start_periodic_checks() {
local interface="$1" local interface="$1"
local user="$2" local user="$2"
local display="$3"
local session_type="$4"
local check_count=0 local check_count=0
log "INFO" "Starting periodic checks for $interface (every ${CHECK_INTERVAL}s)" log "INFO" "Starting periodic checks for $interface (every ${CHECK_INTERVAL}s)"
@ -117,13 +220,11 @@ start_periodic_checks() {
if ! check_captive_portal; then if ! check_captive_portal; then
log "INFO" "Captive portal still active - re-opening browser" log "INFO" "Captive portal still active - re-opening browser"
export DISPLAY=:0 if start_firefox "$user" "$display" "$session_type"; then
export XAUTHORITY=/home/$user/.Xauthority log "INFO" "Firefox re-opened successfully"
else
# Starte Firefox mit Firefox's eigenem Portal Detect log "ERROR" "Failed to re-open Firefox"
sudo -u $user env DISPLAY=:0 XAUTHORITY=/home/$user/.Xauthority \ fi
firefox --new-window "http://detectportal.firefox.com/canonical.html" &
log "INFO" "Firefox re-started for user $user"
else else
log "INFO" "Captive portal resolved - stopping periodic checks" log "INFO" "Captive portal resolved - stopping periodic checks"
break break
@ -145,23 +246,23 @@ if [[ "$1" == wl* ]] && [ "$2" = "up" ]; then
if ! check_captive_portal; then if ! check_captive_portal; then
log "INFO" "CAPTIVE PORTAL DETECTED! Opening browser..." log "INFO" "CAPTIVE PORTAL DETECTED! Opening browser..."
# Finde Benutzer read user display session_type <<< $(get_user_session)
local_user=$(w -hs | grep ":0" | awk '{print $1}' | head -n1)
if [ -n "$local_user" ]; then
log "INFO" "Found user: $local_user"
export DISPLAY=:0 if [ -n "$user" ] && [ -n "$display" ]; then
export XAUTHORITY=/home/$local_user/.Xauthority log "INFO" "Found user: $user, display: $display, type: $session_type"
# Starte Firefox # Starte Firefox
sudo -u $local_user env DISPLAY=:0 XAUTHORITY=/home/$local_user/.Xauthority \ if start_firefox "$user" "$display" "$session_type"; then
firefox --new-window "http://detectportal.firefox.com/canonical.html" & log "INFO" "Firefox successfully launched"
log "INFO" "Firefox started for user $local_user"
# Starte periodische Checks im Hintergrund # STARTE PERIODISCHE CHECKS IM HINTERGRUND
start_periodic_checks "$1" "$local_user" & log "INFO" "Starting periodic checks in background..."
start_periodic_checks "$1" "$user" "$display" "$session_type" &
else else
log "WARNING" "No local user found for GUI access" log "ERROR" "Failed to start Firefox"
fi
else
log "WARNING" "No local user or display found for GUI access"
fi fi
else else
log "INFO" "No captive portal detected on connection start" log "INFO" "No captive portal detected on connection start"
@ -173,4 +274,3 @@ elif [[ "$1" == wl* ]] && [ "$2" = "down" ]]; then
fi fi
log "INFO" "=== Completed ===" log "INFO" "=== Completed ==="