From 90e7d41c140cdb9ae80a3ca0a4210fd3df8b81a3 Mon Sep 17 00:00:00 2001 From: Krzysztof Burghardt Date: Tue, 3 Sep 2024 23:05:42 +0200 Subject: [PATCH 1/4] Add -u / --upgrade to update to latest release. --- easy-wg-quick | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/easy-wg-quick b/easy-wg-quick index e9f3d4e..73b8cdc 100755 --- a/easy-wg-quick +++ b/easy-wg-quick @@ -1,6 +1,6 @@ #!/bin/sh # easy-wg-quick - Creates WireGuard configuration for hub and peers with ease -# Copyright (C) 2019-2023 Krzysztof Burghardt +# Copyright (C) 2019-2024 Krzysztof Burghardt # # # License @@ -691,11 +691,49 @@ download_and_install_wg_quick() { } } +upgrade_easy_wg_quick() { + REPO='burghardt/easy-wg-quick' + APIURL="https://api.github.com/repos/${REPO}/releases/latest" + + APIDATA="$(curl -sL "${APIURL}" || \ + wget -qO- "${APIURL}" || \ + fetch -qo- "${APIURL}")" + TAG="$(echo "${APIDATA}" | grep -oP '"tag_name": "\K(.*?)(?=")')" + DLURL="https://raw.githubusercontent.com/${REPO}/${TAG}/easy-wg-quick" + + DLFILE=$(mktemp -qt 'wg-quick.XXXXXXXXXX') || { + echo "Failed to create temporary file." + exit 1 + } + cleanup() { + # until https://github.com/koalaman/shellcheck/issues/2660 resolved + # shellcheck disable=SC2317 + rm -f "${DLFILE}" + } + trap cleanup EXIT + + curl -sL "${DLURL}" -o "${DLFILE}" > /dev/null 2>&1 || + wget -qO "${DLFILE}" "${DLURL}" > /dev/null 2>&1 || + fetch -q -o "${DLFILE}" "${DLURL}" > /dev/null 2>&1 || { + echo "Download of ${DLURL} failed." + exit 1 + } + + echo "Installing easy-wg-quick ${TAG} to $0." + chmod +x "${DLFILE}" + mv "${DLFILE}" "$0" || { + echo "Failed to install ${DLFILE} as $0." + exit 1 + } + true +} + print_usage() { echo "Usage: $0 [client_name] - create new client with optional [client_name]" echo " $0 -i / --init - create initial configuration without any clients" echo " $0 -c / --clear - clear the configuration and start over" echo " $0 -d / --install-wg-quick - download and install wg-quick script" + echo " $0 -u / --upgrade - update $0 from latest GitHub release" exit 1 } @@ -725,6 +763,10 @@ main() { download_and_install_wg_quick exit 0 ;; + "-u" | "--upgrade") + upgrade_easy_wg_quick + exit 0 + ;; *) CONF_NAME="$1" ;; From 666616ae994fb2f8ad7c23a9639a9e7647183577 Mon Sep 17 00:00:00 2001 From: Krzysztof Burghardt Date: Tue, 3 Sep 2024 23:06:53 +0200 Subject: [PATCH 2/4] Add documentation for arguments -i, -c, -d, and -u. --- README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/README.md b/README.md index b292403..c69573e 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,39 @@ Following command will create `wgclient_client_name.conf` file. ./easy-wg-quick client_name +### Special usage + +#### -h / --help + +Displays help for all supported arguments. + +#### -i / --init + +Writes the initial configuration to text files without creating a client or +hub configuration. This option is useful if you want to change auto-detected +or default configuration values. Repeat the script with the client name to +create the final configuration. + +#### -c / --clear + +To start over, manually remove all `*.bak`, `*.conf`, `*.key`, and `*.psk` +files. If you need to remove initial configuration options as well, also +delete all `*.txt` files. This script does not remove anything. + +#### -d / --install-wg-quick + +This option will download and install the wg-quick script from the official +Wireguard GitHub mirror based on the current operating system (Linux, FreeBSD, +OpenBSD and Darwin are supported). + +If run as `root`, it will install `wg-quick` in `/usr/local/sbin`. If run as +a normal user, it will use `$HOME/.local/bin`. + +#### -u / --upgrade + +This will download the latest release of that script and replace the original +file with the downloaded version. + ### Sample output ``` From 7b78937095b63401cf5cec47ffd66ebeb8e4dff4 Mon Sep 17 00:00:00 2001 From: Krzysztof Burghardt Date: Tue, 3 Sep 2024 23:20:38 +0200 Subject: [PATCH 3/4] Add -u / --upgrade argument tests. --- tests/arguments.bats | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/arguments.bats b/tests/arguments.bats index ee9a477..e24787e 100755 --- a/tests/arguments.bats +++ b/tests/arguments.bats @@ -74,7 +74,7 @@ load teardown setup @test "run with -d parameter" { run ../easy-wg-quick -d [[ "$status" -eq 0 ]] - [[ "${#lines[@]}" -ge 0 ]] + [[ "${#lines[@]}" -ge 1 ]] if [[ "$(id -u)" -eq 0 ]]; then [[ -x /usr/local/sbin/wg-quick ]] @@ -86,7 +86,7 @@ load teardown setup @test "run with --install-wg-quick parameter" { run ../easy-wg-quick --install-wg-quick [[ "$status" -eq 0 ]] - [[ "${#lines[@]}" -ge 0 ]] + [[ "${#lines[@]}" -ge 1 ]] if [[ "$(id -u)" -eq 0 ]]; then [[ -x /usr/local/sbin/wg-quick ]] @@ -95,6 +95,22 @@ load teardown setup fi } +@test "run with -u parameter" { + cp ../easy-wg-quick ./easy-wg-quick-to-upgrade + run ./easy-wg-quick-to-upgrade -u + [[ "$status" -eq 0 ]] + [[ "${#lines[@]}" -ge 1 ]] + rm ./easy-wg-quick-to-upgrade +} + +@test "run with --upgrade parameter" { + cp ../easy-wg-quick ./easy-wg-quick-to-upgrade + run ./easy-wg-quick-to-upgrade --upgrade + [[ "$status" -eq 0 ]] + [[ "${#lines[@]}" -ge 1 ]] + rm ./easy-wg-quick-to-upgrade +} + @test "run with too many parameters" { run ../easy-wg-quick foo bar [[ "$status" -eq 1 ]] From d657a91afc67bb8bc06b373c2d6912c4ec52c6c8 Mon Sep 17 00:00:00 2001 From: Krzysztof Burghardt Date: Tue, 3 Sep 2024 23:24:39 +0200 Subject: [PATCH 4/4] Fixed code formatting reported by shfmt. --- easy-wg-quick | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/easy-wg-quick b/easy-wg-quick index 73b8cdc..7408c80 100755 --- a/easy-wg-quick +++ b/easy-wg-quick @@ -695,8 +695,8 @@ upgrade_easy_wg_quick() { REPO='burghardt/easy-wg-quick' APIURL="https://api.github.com/repos/${REPO}/releases/latest" - APIDATA="$(curl -sL "${APIURL}" || \ - wget -qO- "${APIURL}" || \ + APIDATA="$(curl -sL "${APIURL}" || + wget -qO- "${APIURL}" || fetch -qo- "${APIURL}")" TAG="$(echo "${APIDATA}" | grep -oP '"tag_name": "\K(.*?)(?=")')" DLURL="https://raw.githubusercontent.com/${REPO}/${TAG}/easy-wg-quick"