Skip to contentSkip to conversation

wg-quick: add syncconf

PATCH New Marc-Antoine Perennou 1 participant 1 message Feb 10, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wg-quick: add syncconf

Simplifies the process to reload an updated configuration, avoid subshells

---
 src/man/wg-quick.8            |  4 ++++
 src/systemd/[email protected] |  2 +-
 src/wg-quick/darwin.bash      | 13 ++++++++++++-
 src/wg-quick/freebsd.bash     | 11 ++++++++++-
 src/wg-quick/linux.bash       | 11 ++++++++++-
 src/wg-quick/openbsd.bash     | 11 ++++++++++-
 6 files changed, 47 insertions(+), 5 deletions(-)
src/man/wg-quick.8 +4 -0
index b84eb64..f52a3fe 100644
@@ -256,6 +256,10 @@ sessions:
 
 \fB    # wg syncconf wgnet0 <(wg-quick strip wgnet0)\fP
 
+You can also use the \fIsyncconf\fP command for the same purpose
+
+\fB    # wg-quick syncconf wgnet0\fP
+
 .SH SEE ALSO
 .BR wg (8),
 .BR ip (8),
src/systemd/[email protected] +1 -1
index dbdab44..cb8b3a9 100644
@@ -15,7 +15,7 @@ Type=oneshot
 RemainAfterExit=yes
 ExecStart=/usr/bin/wg-quick up %i
 ExecStop=/usr/bin/wg-quick down %i
-ExecReload=/bin/bash -c 'exec /usr/bin/wg syncconf %i <(exec /usr/bin/wg-quick strip %i)'
+ExecReload=/usr/bin/wg-quick syncconf %i
 Environment=WG_ENDPOINT_RESOLUTION_RETRIES=infinity
 
 [Install]
src/wg-quick/darwin.bash +12 -1
index cde1b54..94c669e 100755
@@ -418,7 +418,7 @@ execute_hooks() {
 
 cmd_usage() {
 	cat >&2 <<-_EOF
-	Usage: $PROGRAM [ up | down | save | strip ] [ CONFIG_FILE | INTERFACE ]
+	Usage: $PROGRAM [ up | down | save | strip | syncconf ] [ CONFIG_FILE | INTERFACE ]
 
 	  CONFIG_FILE is a configuration file, whose filename is the interface name
 	  followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
@@ -489,6 +489,13 @@ cmd_strip() {
 	echo "$WG_CONFIG"
 }
 
+cmd_syncconf() {
+	if ! get_real_interface || [[ " $(wg show interfaces) " != *" $REAL_INTERFACE "* ]]; then
+		die "\`$INTERFACE' is not a WireGuard interface"
+	fi
+	cmd wg syncconf "$REAL_INTERFACE" <(echo "$WG_CONFIG")
+}
+
 # ~~ function override insertion point ~~
 
 if [[ $# -eq 1 && ( $1 == --help || $1 == -h || $1 == help ) ]]; then
@@ -510,6 +517,10 @@ elif [[ $# -eq 2 && $1 == strip ]]; then
 	auto_su
 	parse_options "$2"
 	cmd_strip
+elif [[ $# -eq 2 && $1 == syncconf ]]; then
+	auto_su
+	parse_options "$2"
+	cmd_syncconf
 else
 	cmd_usage
 	exit 1
src/wg-quick/freebsd.bash +10 -1
index e1ee67f..9415926 100755
@@ -387,7 +387,7 @@ execute_hooks() {
 
 cmd_usage() {
 	cat >&2 <<-_EOF
-	Usage: $PROGRAM [ up | down | save | strip ] [ CONFIG_FILE | INTERFACE ]
+	Usage: $PROGRAM [ up | down | save | strip | syncconf ] [ CONFIG_FILE | INTERFACE ]
 
 	  CONFIG_FILE is a configuration file, whose filename is the interface name
 	  followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
@@ -454,6 +454,11 @@ cmd_strip() {
 	echo "$WG_CONFIG"
 }
 
+cmd_syncconf() {
+	[[ " $(wg show interfaces) " == *" $INTERFACE "* ]] || die "\`$INTERFACE' is not a WireGuard interface"
+	cmd wg syncconf "$INTERFACE" <(echo "$WG_CONFIG")
+}
+
 # ~~ function override insertion point ~~
 
 make_temp
@@ -477,6 +482,10 @@ elif [[ $# -eq 2 && $1 == strip ]]; then
 	auto_su
 	parse_options "$2"
 	cmd_strip
+elif [[ $# -eq 2 && $1 == syncconf ]]; then
+	auto_su
+	parse_options "$2"
+	cmd_syncconf
 else
 	cmd_usage
 	exit 1
src/wg-quick/linux.bash +10 -1
index e4d4c4f..83ae4a8 100755
@@ -298,7 +298,7 @@ execute_hooks() {
 
 cmd_usage() {
 	cat >&2 <<-_EOF
-	Usage: $PROGRAM [ up | down | save | strip ] [ CONFIG_FILE | INTERFACE ]
+	Usage: $PROGRAM [ up | down | save | strip | syncconf ] [ CONFIG_FILE | INTERFACE ]
 
 	  CONFIG_FILE is a configuration file, whose filename is the interface name
 	  followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
@@ -361,6 +361,11 @@ cmd_strip() {
 	echo "$WG_CONFIG"
 }
 
+cmd_syncconf() {
+	[[ " $(wg show interfaces) " == *" $INTERFACE "* ]] || die "\`$INTERFACE' is not a WireGuard interface"
+	cmd wg syncconf "$INTERFACE" <(echo "$WG_CONFIG")
+}
+
 # ~~ function override insertion point ~~
 
 if [[ $# -eq 1 && ( $1 == --help || $1 == -h || $1 == help ) ]]; then
@@ -381,6 +386,10 @@ elif [[ $# -eq 2 && $1 == strip ]]; then
 	auto_su
 	parse_options "$2"
 	cmd_strip
+elif [[ $# -eq 2 && $1 == syncconf ]]; then
+	auto_su
+	parse_options "$2"
+	cmd_syncconf
 else
 	cmd_usage
 	exit 1
src/wg-quick/openbsd.bash +10 -1
index 15550c8..6d0efa8 100755
@@ -376,7 +376,7 @@ execute_hooks() {
 
 cmd_usage() {
 	cat >&2 <<-_EOF
-	Usage: $PROGRAM [ up | down | save | strip ] [ CONFIG_FILE | INTERFACE ]
+	Usage: $PROGRAM [ up | down | save | strip | syncconf ] [ CONFIG_FILE | INTERFACE ]
 
 	  CONFIG_FILE is a configuration file, whose filename is the interface name
 	  followed by \`.conf'. Otherwise, INTERFACE is an interface name, with
@@ -441,6 +441,11 @@ cmd_strip() {
 	echo "$WG_CONFIG"
 }
 
+cmd_syncconf() {
+	get_real_interface || die "\`$INTERFACE' is not a WireGuard interface"
+	cmd wg syncconf "$REAL_INTERFACE" <(echo "$WG_CONFIG")
+}
+
 # ~~ function override insertion point ~~
 
 if [[ $# -eq 1 && ( $1 == --help || $1 == -h || $1 == help ) ]]; then
@@ -461,6 +466,10 @@ elif [[ $# -eq 2 && $1 == strip ]]; then
 	auto_su
 	parse_options "$2"
 	cmd_strip
+elif [[ $# -eq 2 && $1 == syncconf ]]; then
+	auto_su
+	parse_options "$2"
+	cmd_syncconf
 else
 	cmd_usage
 	exit 1
Signature · 1 line
2.30.0