]> git.ipfire.org Git - thirdparty/wireguard-tools.git/commitdiff
wg-quick: allow specifiying multiple hooks
authorJason A. Donenfeld <Jason@zx2c4.com>
Wed, 25 Oct 2017 19:55:17 +0000 (21:55 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Tue, 31 Oct 2017 16:25:23 +0000 (17:25 +0100)
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
src/wg-quick.8
src/wg-quick.bash

index b39eff8f012a66db377b41b2c6b0d7f03ea3a8f6..2839cc9d905336648b209faf8b70d5c7653e45bc 100644 (file)
@@ -80,7 +80,8 @@ PreUp, PostUp, PreDown, PostDown \(em script snippets which will be executed by
 .BR bash (1)
 before/after setting up/tearing down the interface, most commonly used
 to configure custom DNS options or firewall rules. The special string `%i'
-is expanded to \fIINTERFACE\fP.
+is expanded to \fIINTERFACE\fP. Each one may be specified multiple times, in which case
+the commands are executed in order.
 .IP \(bu
 SaveConfig \(em if set to `true', the configuration is saved from the current state of the
 interface upon shutdown.
index 015df85967e2b73390f50268f2b2302dbf011eb8..def78af084332563eafcab32299253c06edb5cf9 100755 (executable)
@@ -15,10 +15,10 @@ INTERFACE=""
 ADDRESSES=( )
 MTU=""
 DNS=( )
-PRE_UP=""
-POST_UP=""
-PRE_DOWN=""
-POST_DOWN=""
+PRE_UP=( )
+POST_UP=( )
+PRE_DOWN=( )
+POST_DOWN=( )
 SAVE_CONFIG=0
 CONFIG_FILE=""
 PROGRAM="${0##*/}"
@@ -43,10 +43,10 @@ parse_options() {
                        Address) ADDRESSES+=( ${value//,/ } ); continue ;;
                        MTU) MTU="$value"; continue ;;
                        DNS) DNS+=( ${value//,/ } ); continue ;;
-                       PreUp) PRE_UP="$value"; continue ;;
-                       PreDown) PRE_DOWN="$value"; continue ;;
-                       PostUp) POST_UP="$value"; continue ;;
-                       PostDown) POST_DOWN="$value"; continue ;;
+                       PreUp) PRE_UP+=( "$value" ); continue ;;
+                       PreDown) PRE_DOWN+=( "$value" ); continue ;;
+                       PostUp) POST_UP+=( "$value" ); continue ;;
+                       PostDown) POST_DOWN+=( "$value" ); continue ;;
                        SaveConfig) read_bool SAVE_CONFIG "$value"; continue ;;
                        esac
                fi
@@ -197,11 +197,13 @@ save_config() {
        umask "$old_umask"
 }
 
-execute_hook() {
-       [[ -n $1 ]] || return 0
-       local hook="${1//%i/$INTERFACE}"
-       echo "[#] $hook" >&2
-       (eval "$hook")
+execute_hooks() {
+       local hook
+       for hook in "$@"; do
+               hook="${hook//%i/$INTERFACE}"
+               echo "[#] $hook" >&2
+               (eval "$hook")
+       done
 }
 
 cmd_usage() {
@@ -232,7 +234,7 @@ cmd_up() {
        local i
        [[ -z $(ip link show dev "$INTERFACE" 2>/dev/null) ]] || die "\`$INTERFACE' already exists"
        trap 'del_if; exit' INT TERM EXIT
-       execute_hook "$PRE_UP"
+       execute_hooks "${PRE_UP[@]}"
        add_if
        set_config
        for i in "${ADDRESSES[@]}"; do
@@ -244,17 +246,17 @@ cmd_up() {
        for i in $(while read -r _ i; do for i in $i; do [[ $i =~ ^[0-9a-z:.]+/[0-9]+$ ]] && echo "$i"; done; done < <(wg show "$INTERFACE" allowed-ips) | sort -nr -k 2 -t /); do
                [[ $(ip route get "$i" 2>/dev/null) == *dev\ $INTERFACE\ * ]] || add_route "$i"
        done
-       execute_hook "$POST_UP"
+       execute_hooks "${POST_UP[@]}"
        trap - INT TERM EXIT
 }
 
 cmd_down() {
        [[ " $(wg show interfaces) " == *" $INTERFACE "* ]] || die "\`$INTERFACE' is not a WireGuard interface"
-       execute_hook "$PRE_DOWN"
+       execute_hooks "${PRE_DOWN[@]}"
        [[ $SAVE_CONFIG -eq 0 ]] || save_config
        unset_dns
        del_if
-       execute_hook "$POST_DOWN"
+       execute_hooks "${POST_DOWN[@]}"
 }
 
 if [[ $# -eq 1 && ( $1 == --help || $1 == -h || $1 == help ) ]]; then