update randomize vpn script to get IP from endpoint

This commit is contained in:
lucas committed 2026-10-23 19:42:39 +02:00
1 parent 073ddf11fc
commit 7f27f187d8
1 file changed
+28 -1
+28 -1
View File
@@ -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 <conf-directory>}"
# 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