wg-quick: allow for saving existing interface

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2017-10-31 17:51:38 +01:00
parent 225882ccc4
commit eb181e811c
2 changed files with 14 additions and 2 deletions

View file

@ -9,6 +9,8 @@ wg-quick - set up a WireGuard interface simply
.I up .I up
| |
.I down .I down
|
.I save
] [ ] [
.I CONFIG_FILE .I CONFIG_FILE
| |
@ -24,7 +26,8 @@ Use \fIup\fP to add and set up an interface, and use \fIdown\fP to tear down and
an interface. Running \fIup\fP adds a WireGuard interface, brings up the interface with the an interface. Running \fIup\fP adds a WireGuard interface, brings up the interface with the
supplied IP addresses, sets up mtu and routes, and optionally runs pre/post up scripts. Running \fIdown\fP supplied IP addresses, sets up mtu and routes, and optionally runs pre/post up scripts. Running \fIdown\fP
optionally saves the current configuration, removes the WireGuard interface, and optionally optionally saves the current configuration, removes the WireGuard interface, and optionally
runs pre/post down scripts. runs pre/post down scripts. Running \fIsave\fP saves the configuration of an existing
interface without bringing the interface down.
\fICONFIG_FILE\fP is a configuration file, whose filename is the interface name \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 followed by `.conf'. Otherwise, \fIINTERFACE\fP is an interface name, with configuration

View file

@ -213,7 +213,7 @@ execute_hooks() {
cmd_usage() { cmd_usage() {
cat >&2 <<-_EOF cat >&2 <<-_EOF
Usage: $PROGRAM [ up | down ] [ CONFIG_FILE | INTERFACE ] Usage: $PROGRAM [ up | down | save ] [ CONFIG_FILE | INTERFACE ]
CONFIG_FILE is a configuration file, whose filename is the interface name CONFIG_FILE is a configuration file, whose filename is the interface name
followed by \`.conf'. Otherwise, INTERFACE is an interface name, with followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
@ -264,6 +264,11 @@ cmd_down() {
execute_hooks "${POST_DOWN[@]}" execute_hooks "${POST_DOWN[@]}"
} }
cmd_save() {
[[ " $(wg show interfaces) " == *" $INTERFACE "* ]] || die "\`$INTERFACE' is not a WireGuard interface"
save_config
}
# ~~ function override insertion point ~~ # ~~ function override insertion point ~~
if [[ $# -eq 1 && ( $1 == --help || $1 == -h || $1 == help ) ]]; then if [[ $# -eq 1 && ( $1 == --help || $1 == -h || $1 == help ) ]]; then
@ -276,6 +281,10 @@ elif [[ $# -eq 2 && $1 == down ]]; then
auto_su auto_su
parse_options "$2" parse_options "$2"
cmd_down cmd_down
elif [[ $# -eq 2 && $1 == save ]]; then
auto_su
parse_options "$2"
cmd_save
else else
cmd_usage cmd_usage
exit 1 exit 1