30 lines
688 B
Bash
Executable File
30 lines
688 B
Bash
Executable File
#!/bin/bash
|
|
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
|