From 19ce650fb6fab0cbf593d02e7e35f5acd5fe6ab7 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sun, 27 May 2018 02:42:04 +0200 Subject: [PATCH] wg-quick: determine IPs when saving interface Signed-off-by: Jason A. Donenfeld --- src/wg-quick/darwin.bash | 8 ++++---- src/wg-quick/freebsd.bash | 8 +++++--- src/wg-quick/openbsd.bash | 10 +++++----- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/wg-quick/darwin.bash b/src/wg-quick/darwin.bash index 8e0a71f..18b92ea 100755 --- a/src/wg-quick/darwin.bash +++ b/src/wg-quick/darwin.bash @@ -335,12 +335,12 @@ set_config() { } save_config() { - # TODO: actually save addresses and DNS by running ifconfig and networksetup local old_umask new_config current_config address cmd new_config=$'[Interface]\n' - for address in "${ADDRESSES[@]}"; do - new_config+="Address = $address"$'\n' - done + while read -r address; do + [[ $address =~ inet6?\ ([^ ]+) ]] && new_config+="Address = ${BASH_REMATCH[1]}"$'\n' + done < <(ifconfig "$REAL_INTERFACE") + # TODO: actually determine current DNS for interface for address in "${DNS[@]}"; do new_config+="DNS = $address"$'\n' done diff --git a/src/wg-quick/freebsd.bash b/src/wg-quick/freebsd.bash index 6e94983..d9a0b03 100755 --- a/src/wg-quick/freebsd.bash +++ b/src/wg-quick/freebsd.bash @@ -309,12 +309,14 @@ set_config() { } save_config() { - # TODO: actually save addresses by running ifconfig local old_umask new_config current_config address cmd new_config=$'[Interface]\n' - for address in "${ADDRESSES[@]}"; do + { read -r _; while read -r _ _ _ address _; do new_config+="Address = $address"$'\n' - done + done } < <(netstat -I "$INTERFACE" -n -W -f inet) + { read -r _; while read -r _ _ _ address _; do + new_config+="Address = $address"$'\n' + done } < <(netstat -I "$INTERFACE" -n -W -f inet6) while read -r address; do [[ $address =~ ^nameserver\ ([a-zA-Z0-9_=+:%.-]+)$ ]] && new_config+="DNS = ${BASH_REMATCH[1]}"$'\n' done < <(resolvconf -l "$INTERFACE" 2>/dev/null) diff --git a/src/wg-quick/openbsd.bash b/src/wg-quick/openbsd.bash index d37e250..786d145 100755 --- a/src/wg-quick/openbsd.bash +++ b/src/wg-quick/openbsd.bash @@ -315,12 +315,12 @@ set_config() { } save_config() { - # TODO: actually save addresses by running ifconfig and dnses too - local old_umask new_config current_config address cmd + local old_umask new_config current_config address network cmd new_config=$'[Interface]\n' - for address in "${ADDRESSES[@]}"; do - new_config+="Address = $address"$'\n' - done + { read -r _; while read -r _ _ network address _; do + [[ $network == *Link* ]] || new_config+="Address = $address"$'\n' + done } < <(netstat -I "$REAL_INTERFACE" -n -v) + # TODO: actually determine current DNS for interface for address in "${DNS[@]}"; do new_config+="DNS = $address"$'\n' done