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 ``` diff --git a/easy-wg-quick b/easy-wg-quick index e9f3d4e..7408c80 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" ;; 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 ]]