Skip to content

Commit

Permalink
add support for split for allowd-ips and installed routes, not fail i…
Browse files Browse the repository at this point in the history
…n case of existing route added with Table= usage
  • Loading branch information
Vladimir Grebenshchikov committed Nov 2, 2024
1 parent f006bff commit b4ad022
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions awg-quick
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ CONFIG_FILE=""
PROGRAM="${0##*/}"
ARGS=( "$@" )

declare -A ROUTES

cmd() {
echo "[#] $*" >&2
"$@"
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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[@]}"
Expand Down

0 comments on commit b4ad022

Please sign in to comment.