Simplifies the process to reload an updated configuration, avoid subshells

Signed-off-by: Marc-Antoine Perennou <[email protected]>
---
 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(-)

diff --git a/src/man/wg-quick.8 b/src/man/wg-quick.8
index b84eb64..f52a3fe 100644
--- a/src/man/wg-quick.8
+++ b/src/man/wg-quick.8
@@ -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),
diff --git a/src/systemd/[email protected] b/src/systemd/[email protected]
index dbdab44..cb8b3a9 100644
--- a/src/systemd/[email protected]
+++ b/src/systemd/[email protected]
@@ -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]
diff --git a/src/wg-quick/darwin.bash b/src/wg-quick/darwin.bash
index cde1b54..94c669e 100755
--- a/src/wg-quick/darwin.bash
+++ b/src/wg-quick/darwin.bash
@@ -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
diff --git a/src/wg-quick/freebsd.bash b/src/wg-quick/freebsd.bash
index e1ee67f..9415926 100755
--- a/src/wg-quick/freebsd.bash
+++ b/src/wg-quick/freebsd.bash
@@ -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
diff --git a/src/wg-quick/linux.bash b/src/wg-quick/linux.bash
index e4d4c4f..83ae4a8 100755
--- a/src/wg-quick/linux.bash
+++ b/src/wg-quick/linux.bash
@@ -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
diff --git a/src/wg-quick/openbsd.bash b/src/wg-quick/openbsd.bash
index 15550c8..6d0efa8 100755
--- a/src/wg-quick/openbsd.bash
+++ b/src/wg-quick/openbsd.bash
@@ -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
-- 
2.30.0

Reply via email to