From 7f27f187d8d812a12b6270f80039ebbae422ccb4 Mon Sep 17 00:00:00 2001 From: Lucas Serverhost Date: Fri, 23 Oct 2026 19:42:38 +0200 Subject: [PATCH] update randomize vpn script to get IP from endpoint --- network/wg-randomize.sh | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/network/wg-randomize.sh b/network/wg-randomize.sh index f54e549..445e57c 100755 --- a/network/wg-randomize.sh +++ b/network/wg-randomize.sh @@ -1,2 +1,29 @@ #!/bin/bash -ln -sf "$(find /etc/wireguard -maxdepth 1 -name '*.conf' ! -name 'wg0.conf' | shuf -n1)" /etc/wireguard/wg0.conf +set -euo pipefail + +CONF_DIR="${1:?Usage: $0 }" + +# Pick a random .conf except wg0.conf +random_conf=$(find "$CONF_DIR" -maxdepth 1 -type f -name "*.conf" ! -name "wg0.conf" | shuf -n1) + +echo "Selected: $random_conf" + +# Copy to wg0.conf in current directory +cp "$random_conf" ./wg0.conf + +# Resolve hostname in Endpoint to IP +endpoint=$(grep '^Endpoint' wg0.conf | awk '{print $3}') +host=${endpoint%:*} +port=${endpoint##*:} + +ip=$(getent ahostsv4 "$host" | awk '{print $1; exit}') + +if [[ -z "$ip" ]]; then + echo "Failed to resolve $host" + exit 1 +fi + +sed -i "s|^Endpoint = .*|Endpoint = $ip:$port|" wg0.conf + +echo "Updated Endpoint:" +grep '^Endpoint' wg0.conf