Skip to content

Instantly share code, notes, and snippets.

@pstadler
Last active May 4, 2022 13:10
Show Gist options
  • Save pstadler/2eb645ae829941850db2e09628fe0d85 to your computer and use it in GitHub Desktop.
Save pstadler/2eb645ae829941850db2e09628fe0d85 to your computer and use it in GitHub Desktop.

Revisions

  1. pstadler revised this gist May 4, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions wireguard.sh
    Original file line number Diff line number Diff line change
    @@ -29,12 +29,12 @@ get_local_version () {
    }

    get_latest_version () {
    echo $releases | jq -r --arg version "wireguard-$BOARD" \
    echo $releases | jq -r --arg version "$BOARD-v2" \
    '[.[] | select(.assets | .[].browser_download_url | contains($version))][0] | .tag_name'
    }

    get_latest_download_url () {
    echo $releases | jq -r --arg version "wireguard-$BOARD" \
    echo $releases | jq -r --arg version "$BOARD-v2" \
    '[.[].assets | .[] | select(.browser_download_url | contains($version))][0] | .browser_download_url'
    }

  2. pstadler revised this gist May 4, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wireguard.sh
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ BOARD=e300 # ER4
    CMD_WRAPPER=/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper

    echo "Fetching WireGuard releases..."
    releases=$(curl -sSL https://api.github.com/repos/Lochnair/vyatta-wireguard/releases)
    releases=$(curl -sSL https://api.github.com/repos/WireGuard/wireguard-vyatta-ubnt/releases)

    is_installed () {
    dpkg -S wireguard >/dev/null 2>&1
  3. pstadler revised this gist May 4, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wireguard.sh
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ echo "Fetching WireGuard releases..."
    releases=$(curl -sSL https://api.github.com/repos/Lochnair/vyatta-wireguard/releases)

    is_installed () {
    dpkg -s wireguard >/dev/null 2>&1
    dpkg -S wireguard >/dev/null 2>&1
    }

    get_local_version () {
  4. pstadler revised this gist Mar 4, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions wireguard.sh
    Original file line number Diff line number Diff line change
    @@ -39,9 +39,9 @@ get_latest_download_url () {
    }

    install_latest_version () {
    curl -L -o "/tmp/wireguard-$BOARD-$version.deb" $(get_latest_download_url)
    dpkg -i "/tmp/wireguard-$BOARD-$version.deb"
    rm "/tmp/wireguard-$BOARD-$version.deb"
    curl -L -o "/tmp/wireguard-$BOARD.deb" $(get_latest_download_url)
    dpkg -i "/tmp/wireguard-$BOARD.deb"
    rm "/tmp/wireguard-$BOARD.deb"
    }

    uninstall () {
  5. pstadler revised this gist Mar 4, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion wireguard.sh
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,6 @@ get_latest_download_url () {
    '[.[].assets | .[] | select(.browser_download_url | contains($version))][0] | .browser_download_url'
    }


    install_latest_version () {
    curl -L -o "/tmp/wireguard-$BOARD-$version.deb" $(get_latest_download_url)
    dpkg -i "/tmp/wireguard-$BOARD-$version.deb"
  6. pstadler revised this gist Mar 4, 2019. No changes.
  7. pstadler revised this gist Mar 4, 2019. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions wireguard.sh
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    #!/bin/bash -e

    # WireGuard on EdgeRouter
    #
    # On EdgeRouter, this script should be in `/config/scripts/post-config.d/wireguard.sh`
    # to automatically install the latest version after firmware upgrades.
    # vyatta-wireguard
    #
    # Usage: `./wireguard.sh [upgrade|uninstall]`.
    #
    # If this script is called without arguments, the latest version will be fetched and installed.
    # When called without arguments, the latest version will be fetched and installed.
    #
    # To automatically install the latest version of WireGuard after Firmware upgrades,
    # this script should be placed in `/config/scripts/post-config.d/wireguard.sh`.
    #
    # Change `BOARD` to match your hardware. See: https://github.com/Lochnair/vyatta-wireguard/releases
    BOARD=e300 # ER4
  8. pstadler created this gist Mar 4, 2019.
    106 changes: 106 additions & 0 deletions wireguard.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,106 @@
    #!/bin/bash -e

    # WireGuard on EdgeRouter
    #
    # On EdgeRouter, this script should be in `/config/scripts/post-config.d/wireguard.sh`
    # to automatically install the latest version after firmware upgrades.
    #
    # Usage: `./wireguard.sh [upgrade|uninstall]`.
    #
    # If this script is called without arguments, the latest version will be fetched and installed.
    #
    # Change `BOARD` to match your hardware. See: https://github.com/Lochnair/vyatta-wireguard/releases
    BOARD=e300 # ER4
    # Don't touch the lines below

    CMD_WRAPPER=/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper

    echo "Fetching WireGuard releases..."
    releases=$(curl -sSL https://api.github.com/repos/Lochnair/vyatta-wireguard/releases)

    is_installed () {
    dpkg -s wireguard >/dev/null 2>&1
    }

    get_local_version () {
    ! is_installed && return

    dpkg-query --showformat='${Version}' --show wireguard
    }

    get_latest_version () {
    echo $releases | jq -r --arg version "wireguard-$BOARD" \
    '[.[] | select(.assets | .[].browser_download_url | contains($version))][0] | .tag_name'
    }

    get_latest_download_url () {
    echo $releases | jq -r --arg version "wireguard-$BOARD" \
    '[.[].assets | .[] | select(.browser_download_url | contains($version))][0] | .browser_download_url'
    }


    install_latest_version () {
    curl -L -o "/tmp/wireguard-$BOARD-$version.deb" $(get_latest_download_url)
    dpkg -i "/tmp/wireguard-$BOARD-$version.deb"
    rm "/tmp/wireguard-$BOARD-$version.deb"
    }

    uninstall () {
    $CMD_WRAPPER begin
    $CMD_WRAPPER set interfaces wireguard wg0 route-allowed-ips false
    $CMD_WRAPPER commit
    $CMD_WRAPPER delete interfaces wireguard
    $CMD_WRAPPER commit
    sudo rmmod wireguard
    sudo dpkg --purge wireguard
    }

    # uninstall
    if [ "$1" = "uninstall" ]; then
    ! is_installed && (echo "WireGuard is not installed."; exit 1)

    echo "Uninstalling WireGuard..."
    uninstall
    exit 0
    fi

    # upgrade
    if [ "$1" = "upgrade" ]; then
    latest_version=$(get_latest_version)
    local_version=$(get_local_version)

    if [[ "$latest_version" = "$local_version" || "${latest_version}-1" = "$local_version" ]]; then
    echo "WireGuard is up-to-date. (local: $(get_local_version); remote: $latest_version)"
    exit 0
    fi

    if ! is_installed; then
    echo "WireGuard is not installed yet. Run script without arguments to install."
    exit 1
    fi

    echo "Upgrading WireGuard from $local_version to $latest_version..."
    uninstall
    install_latest_version
    sudo modprobe wireguard
    $CMD_WRAPPER load
    $CMD_WRAPPER commit
    echo "Upgrade done."
    exit 0
    fi

    # install
    if is_installed; then
    echo "WireGuard $(get_local_version) is already installed."
    exit 1
    fi

    echo "Installing latest WireGuard version..."
    install_latest_version
    $CMD_WRAPPER begin
    $CMD_WRAPPER load
    $CMD_WRAPPER commit
    $CMD_WRAPPER end
    echo "Installation done."

    exit 0