From 1e930dc88dd1880962880d234e4748d8c155e626 Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Mon, 14 Apr 2025 13:46:10 +0200 Subject: [PATCH] local ip helper --- misc/tools.func | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/misc/tools.func b/misc/tools.func index 5b35f1d..41f986f 100644 --- a/misc/tools.func +++ b/misc/tools.func @@ -647,3 +647,92 @@ fetch_and_deploy_gh_release() { $STD msg_ok "Deployed $app v$version to /opt/$app" rm -rf "$tmpdir" } + +setup_local_ip_helper() { + local BASE_DIR="/usr/local/community-scripts/ip-management" + local SCRIPT_PATH="$BASE_DIR/update_local_ip.sh" + local SERVICE_PATH="/etc/systemd/system/update-local-ip.service" + local TIMER_PATH="/etc/systemd/system/update-local-ip.timer" + + mkdir -p "$BASE_DIR" + + # Create update script + cat <<'EOF' >"$SCRIPT_PATH" +#!/bin/bash +set -e + +IP_FILE="/run/local-ip.env" +mkdir -p "$(dirname "$IP_FILE")" + +get_current_ip() { + ip route get 1 | awk '{print $7; exit}' 2>/dev/null +} + +current_ip="$(get_current_ip)" + +if [[ -z "$current_ip" ]]; then + echo "[ERROR] Could not detect local IP" >&2 + exit 1 +fi + +if [[ -f "$IP_FILE" ]]; then + source "$IP_FILE" + if [[ "$LOCAL_IP" == "$current_ip" ]]; then + exit 0 + fi +fi + +echo "LOCAL_IP=$current_ip" > "$IP_FILE" +echo "[INFO] LOCAL_IP updated to $current_ip" +EOF + + chmod +x "$SCRIPT_PATH" + + # Create systemd service + cat <"$SERVICE_PATH" +[Unit] +Description=Update LOCAL_IP file +After=network-online.target + +[Service] +Type=oneshot +ExecStart=$SCRIPT_PATH +EOF + + # Create systemd timer + cat <"$TIMER_PATH" +[Unit] +Description=Periodic LOCAL_IP update + +[Timer] +OnBootSec=15 +OnUnitActiveSec=60 +Persistent=true + +[Install] +WantedBy=timers.target +EOF + + systemctl daemon-reexec + systemctl daemon-reload + systemctl enable --now update-local-ip.timer + + msg_ok "Setup LOCAL_IP helper in $BASE_DIR with systemd integration" +} + +import_local_ip() { + local IP_FILE="/run/local-ip.env" + if [[ -f "$IP_FILE" ]]; then + source "$IP_FILE" + fi + + if [[ -z "$LOCAL_IP" ]]; then + LOCAL_IP="$(ip route get 1 | awk '{print $7; exit}' 2>/dev/null)" + if [[ -z "$LOCAL_IP" ]]; then + msg_error "Could not determine LOCAL_IP" + return 1 + fi + fi + + export LOCAL_IP +}