From b4ad022625694238e18fc20f3db0c173d9500ff0 Mon Sep 17 00:00:00 2001 From: Vladimir Grebenshchikov Date: Sat, 2 Nov 2024 03:15:05 +0100 Subject: [PATCH] add support for split for allowd-ips and installed routes, not fail in case of existing route added with Table= usage --- awg-quick | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/awg-quick b/awg-quick index 87333ac..e6d1acb 100755 --- a/awg-quick +++ b/awg-quick @@ -27,6 +27,8 @@ CONFIG_FILE="" PROGRAM="${0##*/}" ARGS=( "$@" ) +declare -A ROUTES + cmd() { echo "[#] $*" >&2 "$@" @@ -38,7 +40,7 @@ die() { } parse_options() { - local interface_section=0 line key value stripped v + local interface_section=0 line key value stripped v last_public_key CONFIG_FILE="$1" [[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]] && CONFIG_FILE="/etc/amnezia/amneziawg/$CONFIG_FILE.conf" [[ -e $CONFIG_FILE ]] || die "\`$CONFIG_FILE' does not exist" @@ -51,7 +53,7 @@ parse_options() { stripped="${line%%\#*}" key="${stripped%%=*}"; key="${key##*([[:space:]])}"; key="${key%%*([[:space:]])}" value="${stripped#*=}"; value="${value##*([[:space:]])}"; value="${value%%*([[:space:]])}" - [[ $key == "["* ]] && interface_section=0 + [[ $key == "["* ]] && interface_section=0 && last_public_key="" [[ $key == "[Interface]" ]] && interface_section=1 if [[ $interface_section -eq 1 ]]; then case "$key" in @@ -67,6 +69,12 @@ parse_options() { PostDown) POST_DOWN+=( "$value" ); continue ;; SaveConfig) read_bool SAVE_CONFIG "$value"; continue ;; esac + else + case "$key" in + PublicKey) last_public_key="$value" ;; + Routes) ROUTES["$last_public_key"]="$value"; continue ;; + DynamicRoutes) continue ;; + esac fi WG_CONFIG+="$line"$'\n' done < "$CONFIG_FILE" @@ -176,7 +184,7 @@ add_route() { [[ $TABLE != off ]] || return 0 if [[ -n $TABLE && $TABLE != auto ]]; then - cmd ip $proto route add "$1" dev "$INTERFACE" table "$TABLE" + cmd ip $proto route add "$1" dev "$INTERFACE" table "$TABLE" || true elif [[ $1 == */0 ]]; then add_default "$1" else @@ -332,6 +340,20 @@ cmd_usage() { _EOF } +get_routes() { + while read -r pub_key i; do + if [[ -v "ROUTES[$pub_key]" ]]; then + for route in ${ROUTES[$pub_key]//,/ }; do + echo "$route" + done + else + for j in $i; do + [[ $j =~ ^[0-9a-z:.]+/[0-9]+$ ]] && echo "$j" + done + fi + done < <(awg show "$INTERFACE" allowed-ips) | sort -nr -k 2 -t / +} + cmd_up() { local i [[ -z $(ip link show dev "$INTERFACE" 2>/dev/null) ]] || die "\`$INTERFACE' already exists" @@ -344,7 +366,7 @@ cmd_up() { done set_mtu_up set_dns - for i in $(while read -r _ i; do for i in $i; do [[ $i =~ ^[0-9a-z:.]+/[0-9]+$ ]] && echo "$i"; done; done < <(awg show "$INTERFACE" allowed-ips) | sort -nr -k 2 -t /); do + for i in $(get_routes); do add_route "$i" done execute_hooks "${POST_UP[@]}"