diff --git a/.claude/settings.local.json b/.claude/settings.local.json index d6c1757..b3c4980 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -5,7 +5,15 @@ "Bash(git config *)", "Bash(git remote *)", "Bash(git checkout *)", - "Bash(git add *)" + "Bash(git add *)", + "Bash(git commit -m 'v0.5.0 – Clownfischserver HomeIP Auto-Update: myfritz Automatik für SSH \\(5-Min Cron\\) *)", + "Bash(git push *)", + "Bash(bash -n install.sh)", + "Bash(bash -n scripts/clownfischserver-homeip-updater.sh)", + "Bash(python3 -m py_compile bot/*.py)", + "Bash(awk '{print \"Datei: \" $1 \" Größe: \" $5 \" Bytes\"}')", + "Bash(grep -oE '\\\\b\\([0-9]{1,3}\\\\.\\){3}[0-9]{1,3}\\\\b')", + "Bash(git commit -m 'Bugfix: Clownfischserver HomeIP Updater – Fehlerbehandlung & Fallbacks *)" ] } } diff --git a/install.sh b/install.sh index 0609c77..f9e8a04 100644 --- a/install.sh +++ b/install.sh @@ -168,7 +168,7 @@ step_packages() { case "$PKG_MANAGER" in apt) run_with_spinner "Paketliste aktualisieren" apt-get update -qq || fail "apt update fehlgeschlagen" - run_with_spinner "Pakete installieren" apt-get install -y -qq curl wget git ufw python3 python3-pip python3-venv sudo htop net-tools lsof rsync openssl knockd || warn "Einige Pakete konnten nicht installiert werden – siehe Log" + run_with_spinner "Pakete installieren" apt-get install -y -qq curl wget git ufw python3 python3-pip python3-venv sudo htop net-tools lsof rsync openssl knockd dnsutils || warn "Einige Pakete konnten nicht installiert werden – siehe Log" ;; dnf) run_with_spinner "Pakete aktualisieren" dnf update -y -q @@ -804,11 +804,11 @@ BOOTEOF # Cron-Job für root (da ufw sudo braucht) CRON_JOB="*/5 * * * * $INSTALL_DIR/scripts/clownfischserver-homeip-updater.sh >> /var/log/clownfischserver-homeip.log 2>&1" - # Bestehende clownfischserver Cron-Jobs entfernen - sudo crontab -l 2>/dev/null | grep -v clownfischserver-homeip-updater | sudo crontab - 2>/dev/null || true + # Bestehende clownfischserver Cron-Jobs entfernen (install.sh läuft als root) + crontab -l 2>/dev/null | grep -v clownfischserver-homeip-updater | crontab - 2>/dev/null || true # Neuen Job hinzufügen - (sudo crontab -l 2>/dev/null; echo "$CRON_JOB") | sudo crontab - >> "$LOG_FILE" 2>&1 + (crontab -l 2>/dev/null; echo "$CRON_JOB") | crontab - >> "$LOG_FILE" 2>&1 ok "Cron-Job aktiviert: alle 5 Min HomeIP prüfen" info "Log: /var/log/clownfischserver-homeip.log" diff --git a/scripts/clownfischserver-homeip-updater.sh b/scripts/clownfischserver-homeip-updater.sh index 0425413..8a9c52c 100644 --- a/scripts/clownfischserver-homeip-updater.sh +++ b/scripts/clownfischserver-homeip-updater.sh @@ -28,16 +28,28 @@ if [[ ! -f "$ENV_FILE" ]]; then exit 1 fi -# Lade myfritz-Adresse aus .env -MYFRITZ_DOMAIN=$(grep "^CLOWNFISCHSERVER_HOME_IP_DOMAIN=" "$ENV_FILE" | cut -d= -f2) +# Erstelle Log-File wenn nicht existiert +touch "$LOG_FILE" 2>/dev/null || true + +# Lade myfritz-Adresse aus .env (Zeilenumbrüche entfernen) +MYFRITZ_DOMAIN=$(grep "^CLOWNFISCHSERVER_HOME_IP_DOMAIN=" "$ENV_FILE" | cut -d= -f2 | tr -d '\r\n') if [[ -z "$MYFRITZ_DOMAIN" ]]; then log "Clownfischserver HomeIP: Deaktiviert (keine myfritz-Adresse)" exit 0 fi -# Versuche IP aufzulösen -NEW_IP=$(dig +short "$MYFRITZ_DOMAIN" A 2>/dev/null | tail -1 | grep -oE '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b') +# Versuche IP aufzulösen (mit Fallback) +if command -v dig &>/dev/null; then + NEW_IP=$(dig +short "$MYFRITZ_DOMAIN" A 2>/dev/null | tail -1 | grep -oE '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b') +elif command -v nslookup &>/dev/null; then + NEW_IP=$(nslookup "$MYFRITZ_DOMAIN" 2>/dev/null | grep -oE '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' | tail -1) +elif command -v getent &>/dev/null; then + NEW_IP=$(getent hosts "$MYFRITZ_DOMAIN" 2>/dev/null | awk '{print $1}') +else + log_error "Kein DNS-Tool verfügbar (dig/nslookup/getent)" + exit 1 +fi if [[ -z "$NEW_IP" ]]; then log_error "DNS-Auflösung fehlgeschlagen: $MYFRITZ_DOMAIN" @@ -61,10 +73,12 @@ fi # IP hat sich geändert oder ist neue Installation log "IP-Änderung erkannt! Updating ufw rules..." -# Entferne alte Regel falls vorhanden +# Entferne alte Regel falls vorhanden (mit y bestätigen für Cron) if [[ -n "$OLD_IP" ]]; then - if ufw status | grep -q "22/tcp.*ALLOW.*$OLD_IP"; then - ufw delete allow from "$OLD_IP" to any port 22 >> "$LOG_FILE" 2>&1 + # Prüfe ob Regel existiert + if ufw status numbered | grep -qE "22.*ALLOW.*FROM\s+$OLD_IP"; then + # Löschen mit 'y' autom. bestätigen + echo "y" | ufw delete allow from "$OLD_IP" to any port 22 >> "$LOG_FILE" 2>&1 log "Alte Regel entfernt: allow from $OLD_IP to port 22" fi fi