wg-quick: support FreeBSD/Darwin search path

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-05-23 15:08:30 +02:00
parent b818e71ba5
commit d40231c766
4 changed files with 39 additions and 16 deletions

View File

@ -2,18 +2,24 @@
# Copyright (C) 2015-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
_wg_quick_completion() {
local i a
local p i a search_paths
search_paths=( /etc/wireguard )
[[ $OSTYPE == *freebsd* || $OSTYPE == *darwin* ]] && search_paths+=( /usr/local/etc/wireguard )
if [[ $COMP_CWORD -eq 1 ]]; then
COMPREPLY+=( $(compgen -W "up down" -- "${COMP_WORDS[1]}") )
elif [[ $COMP_CWORD -eq 2 ]]; then
if [[ ${COMP_WORDS[1]} == up ]]; then
local old_glob="$(shopt -p nullglob)"
shopt -s nullglob
for i in /etc/wireguard/*.conf; do
for p in "${search_paths[@]}"; do
for i in "$p"/*.conf; do
i="${i##*/}"; i="${i%.conf}"
mapfile -t a < <(compgen -W "$i" -- "${COMP_WORDS[2]}")
COMPREPLY+=( "${a[@]}" )
done
done
eval "$old_glob"
mapfile -t a < <(compgen -f -X '!*.conf' -- "${COMP_WORDS[2]}")
COMPREPLY+=( "${a[@]}" )

View File

@ -31,7 +31,8 @@ interface without bringing the interface down.
\fICONFIG_FILE\fP is a configuration file, whose filename is the interface name
followed by `.conf'. Otherwise, \fIINTERFACE\fP is an interface name, with configuration
found at `/etc/wireguard/\fIINTERFACE\fP.conf'.
found at `/etc/wireguard/\fIINTERFACE\fP.conf', searched first, followed by distro-specific
search paths.
Generally speaking, this utility is just a simple script that wraps invocations to
.BR wg (8)

View File

@ -40,10 +40,16 @@ die() {
[[ ${BASH_VERSINFO[0]} -ge 4 ]] || die "Version mismatch: bash ${BASH_VERSINFO[0]} detected, when bash 4+ required"
CONFIG_SEARCH_PATHS=( /etc/wireguard /usr/local/etc/wireguard )
parse_options() {
local interface_section=0 line key value stripped
local interface_section=0 line key value stripped path
CONFIG_FILE="$1"
[[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]] && CONFIG_FILE="/etc/wireguard/$CONFIG_FILE.conf"
if [[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]]; then
for path in "${CONFIG_SEARCH_PATHS[@]}"; do
[[ -e $path/$CONFIG_FILE.conf ]] && { CONFIG_FILE="$path/$CONFIG_FILE.conf"; break; }
done
fi
[[ -e $CONFIG_FILE ]] || die "\`$CONFIG_FILE' does not exist"
[[ $CONFIG_FILE =~ (^|/)([a-zA-Z0-9_=+.-]{1,15})\.conf$ ]] || die "The config file must be a valid interface name, followed by .conf"
CONFIG_FILE="$(cd "${CONFIG_FILE%/*}" && pwd -P)/${CONFIG_FILE##*/}"
@ -380,9 +386,11 @@ cmd_usage() {
CONFIG_FILE is a configuration file, whose filename is the interface name
followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
configuration found at /etc/wireguard/INTERFACE.conf. It is to be readable
by wg(8)'s \`setconf' sub-command, with the exception of the following additions
to the [Interface] section, which are handled by $PROGRAM:
configuration found at:
${CONFIG_SEARCH_PATHS[@]/%//INTERFACE.conf}.
It is to be readable by wg(8)'s \`setconf' sub-command, with the exception
of the following additions to the [Interface] section, which are handled
by $PROGRAM:
- Address: may be specified one or more times and contains one or more
IP addresses (with an optional CIDR mask) to be set for the interface.

View File

@ -38,10 +38,16 @@ die() {
exit 1
}
CONFIG_SEARCH_PATHS=( /etc/wireguard /usr/local/etc/wireguard )
parse_options() {
local interface_section=0 line key value stripped
local interface_section=0 line key value stripped path
CONFIG_FILE="$1"
[[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]] && CONFIG_FILE="/etc/wireguard/$CONFIG_FILE.conf"
if [[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]]; then
for path in "${CONFIG_SEARCH_PATHS[@]}"; do
[[ -e $path/$CONFIG_FILE.conf ]] && { CONFIG_FILE="$path/$CONFIG_FILE.conf"; break; }
done
fi
[[ -e $CONFIG_FILE ]] || die "\`$CONFIG_FILE' does not exist"
[[ $CONFIG_FILE =~ (^|/)([a-zA-Z0-9_=+.-]{1,15})\.conf$ ]] || die "The config file must be a valid interface name, followed by .conf"
CONFIG_FILE="$(readlink -f "$CONFIG_FILE")"
@ -334,9 +340,11 @@ cmd_usage() {
CONFIG_FILE is a configuration file, whose filename is the interface name
followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
configuration found at /etc/wireguard/INTERFACE.conf. It is to be readable
by wg(8)'s \`setconf' sub-command, with the exception of the following additions
to the [Interface] section, which are handled by $PROGRAM:
configuration found at:
${CONFIG_SEARCH_PATHS[@]/%//INTERFACE.conf}.
It is to be readable by wg(8)'s \`setconf' sub-command, with the exception
of the following additions to the [Interface] section, which are handled
by $PROGRAM:
- Address: may be specified one or more times and contains one or more
IP addresses (with an optional CIDR mask) to be set for the interface.