#!/bin/sh # Install attackscout-agent from the GitHub releases, after checking its # SHA-256 against checksums.txt. Linux and macOS, amd64 and arm64. # # curl -fsSL https://raw.githubusercontent.com/AttackScout/attackscout-agent/main/install.sh | sh # # Environment: # ATTACKSCOUT_VERSION version to install, for example 0.2.3 (default: latest) # ATTACKSCOUT_INSTALL_DIR target directory (default: /usr/local/bin when # writable or with sudo, else ~/.local/bin) # # The script downloads two files from github.com and writes one binary. It # sends nothing else, and the tool itself uses no network by default. set -eu REPO="AttackScout/attackscout-agent" BIN="attackscout-agent" say() { printf '%s\n' "$*" >&2; } die() { say "error: $*"; exit 1; } fetch() { # url dest if command -v curl >/dev/null 2>&1; then curl -fsSL --retry 3 -o "$2" "$1" elif command -v wget >/dev/null 2>&1; then wget -q -O "$2" "$1" else die "curl or wget is needed" fi } latest_version() { # The /releases/latest page redirects to /releases/tag/vX.Y.Z: no API call, # no rate limit. url="https://github.com/${REPO}/releases/latest" if command -v curl >/dev/null 2>&1; then loc=$(curl -fsSLI -o /dev/null -w '%{url_effective}' "$url") else loc=$(wget -S --spider "$url" 2>&1 | sed -n 's/^ *[Ll]ocation: *//p' | tail -n 1) fi v=${loc##*/tag/} v=${v%%[[:space:]]*} if [ -z "$v" ] || [ "$v" = "$loc" ]; then die "cannot find the latest version (set ATTACKSCOUT_VERSION)" fi printf '%s' "${v#v}" } sha256() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1 elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d' ' -f1 elif command -v openssl >/dev/null 2>&1; then openssl dgst -sha256 "$1" | sed 's/^.*= *//' else die "sha256sum, shasum or openssl is needed to check the download" fi } case "$(uname -s)" in Linux) os=linux ;; Darwin) os=darwin ;; *) die "unsupported system $(uname -s): use the Windows .zip, Scoop or winget" ;; esac case "$(uname -m)" in x86_64 | amd64) arch=amd64 ;; aarch64 | arm64) arch=arm64 ;; *) die "unsupported architecture $(uname -m) (amd64 and arm64 only)" ;; esac version=${ATTACKSCOUT_VERSION:-latest} version=${version#v} [ "$version" = "latest" ] && version=$(latest_version) case "$version" in *[!0-9A-Za-z.-]*) die "invalid version $version" ;; esac archive="${BIN}_${version}_${os}_${arch}.tar.gz" base="https://github.com/${REPO}/releases/download/v${version}" tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT INT TERM say "downloading ${archive}" fetch "${base}/${archive}" "${tmp}/${archive}" || die "cannot download ${archive}: does version ${version} exist?" fetch "${base}/checksums.txt" "${tmp}/checksums.txt" || die "cannot download checksums.txt of version ${version}" want=$(awk -v f="$archive" '$2 == f { print $1 }' "${tmp}/checksums.txt") [ -n "$want" ] || die "${archive} is not listed in checksums.txt" got=$(sha256 "${tmp}/${archive}") [ "$want" = "$got" ] || die "checksum mismatch for ${archive}: expected ${want}, got ${got}" say "checksum ok" tar -xzf "${tmp}/${archive}" -C "$tmp" "$BIN" dir=${ATTACKSCOUT_INSTALL_DIR:-} sudo="" if [ -z "$dir" ]; then if [ -w /usr/local/bin ]; then dir=/usr/local/bin elif command -v sudo >/dev/null 2>&1 && [ -d /usr/local/bin ] && sudo -n true 2>/dev/null; then dir=/usr/local/bin sudo=sudo else dir="${HOME}/.local/bin" fi fi $sudo mkdir -p "$dir" $sudo install -m 0755 "${tmp}/${BIN}" "${dir}/${BIN}" say "installed ${dir}/${BIN}" case ":${PATH}:" in *":${dir}:"*) ;; *) say "add ${dir} to your PATH to run ${BIN}" ;; esac "${dir}/${BIN}" version