diff --git a/advanced/Scripts/speedtestmod/lib.sh b/advanced/Scripts/speedtestmod/lib.sh index a5d1d04086..b0c7d42f67 100755 --- a/advanced/Scripts/speedtestmod/lib.sh +++ b/advanced/Scripts/speedtestmod/lib.sh @@ -135,7 +135,7 @@ download() { # 0 if available, 1 if not ####################################### isAvailable() { - if [[ "$PKG_MANAGER" == *"apt-get"* ]]; then + if [[ "$PKG_MANAGER" == *"apt"* ]]; then # Check if there is a candidate and it is not "(none)" apt-cache policy "$1" | grep -q "Candidate:" && ! apt-cache policy "$1" | grep -q "Candidate: (none)" && return 0 || return 1 elif [[ "$PKG_MANAGER" == *"dnf"* || "$PKG_MANAGER" == *"yum"* ]]; then @@ -156,7 +156,7 @@ isAvailable() { # 0 if the package is not installed, 1 if it is ####################################### notInstalled() { - if [[ "$PKG_MANAGER" == *"apt-get"* ]]; then + if [[ "$PKG_MANAGER" == *"apt"* ]]; then dpkg -s "$1" &>/dev/null || return 0 elif [[ "$PKG_MANAGER" == *"dnf"* || "$PKG_MANAGER" == *"yum"* ]]; then rpm -q "$1" &>/dev/null || return 0 @@ -215,7 +215,7 @@ getCnf() { # 0 if the installation was successful, 1 if it was not ####################################### libreSpeed() { - echo "Installing LibreSpeed..." + echo "Installing librespeed-cli..." $PKG_MANAGER remove -y speedtest-cli speedtest >/dev/null 2>&1 if notInstalled golang; then @@ -224,12 +224,14 @@ libreSpeed() { echo "Adding testing repo to sources.list.d" echo "deb http://archive.raspbian.org/raspbian/ testing main" >/etc/apt/sources.list.d/testing.list printf "Package: *\nPin: release a=testing\nPin-Priority: 50" >/etc/apt/preferences.d/limit-testing - $PKG_MANAGER update -y + $PKG_MANAGER update -y &>/dev/null fi - $PKG_MANAGER install -y -t testing golang + isAvailable golang || $PKG_MANAGER update -y &>/dev/null + $PKG_MANAGER install -y -t testing golang >/dev/null 2>&1 else - $PKG_MANAGER install -y golang + [[ $PKG_MANAGER == *"apt"* ]] && ! isAvailable golang && $PKG_MANAGER update -y &>/dev/null || : + $PKG_MANAGER install -y golang >/dev/null 2>&1 fi fi @@ -243,7 +245,7 @@ libreSpeed() { chmod +x /usr/bin/speedtest if [[ -x /usr/bin/speedtest ]]; then - echo "Installed LibreSpeed." + echo "Installed librespeed-cli" return 0 fi @@ -275,9 +277,11 @@ swivelSpeed() { /usr/bin/yum) "$PKG_MANAGER" install -y --allowerasing "$candidate" &>/dev/null ;; esac - if ! notInstalled "$candidate"; then + if ! notInstalled "$candidate" && [[ -x /usr/bin/speedtest ]]; then printf "Installed " - /usr/bin/speedtest --version || return 1 + local version= + version=$(/usr/bin/speedtest --version) || return 1 + echo "${version%%$'\n'*}" return 0 fi @@ -298,7 +302,7 @@ ooklaSpeed() { if [[ "$PKG_MANAGER" == *"yum"* || "$PKG_MANAGER" == *"dnf"* && ! -f /etc/yum.repos.d/ookla_speedtest-cli.repo ]]; then echo "Adding speedtest source for RPM..." curl -sSLN https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh | sudo bash - elif [[ "$PKG_MANAGER" == *"apt-get"* && ! -f /etc/apt/sources.list.d/ookla_speedtest-cli.list ]]; then + elif [[ "$PKG_MANAGER" == *"apt"* && ! -f /etc/apt/sources.list.d/ookla_speedtest-cli.list ]]; then echo "Adding speedtest source for DEB..." if [[ -e /etc/os-release ]]; then # shellcheck disable=SC1091 @@ -315,10 +319,10 @@ ooklaSpeed() { fi wget -O /tmp/script.deb.sh https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh >/dev/null 2>&1 chmod +x /tmp/script.deb.sh - os=$os dist=$dist /tmp/script.deb.sh + os=$os dist=$dist /tmp/script.deb.sh >/dev/null 2>&1 rm -f /tmp/script.deb.sh else - curl -sSLN https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash + curl -sSLN https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash >/dev/null 2>&1 fi sed -i 's/g]/g allow-insecure=yes trusted=yes]/' /etc/apt/sources.list.d/ookla_speedtest-cli.list @@ -405,7 +409,7 @@ ExecStart=/usr/local/bin/pihole -a -sn [Install] WantedBy=multi-user.target EOF' - sudo bash -c 'cat > /etc/systemd/system/pihole-speedtest.timer << EOF + sudo bash -c 'cat > /etc/systemd/system/pihole-speedtest.timer << EOF [Unit] Description=Pi-hole Speedtest Timer diff --git a/advanced/Scripts/speedtestmod/mod.sh b/advanced/Scripts/speedtestmod/mod.sh index d8b9302f58..e2e5e57d0a 100755 --- a/advanced/Scripts/speedtestmod/mod.sh +++ b/advanced/Scripts/speedtestmod/mod.sh @@ -6,6 +6,10 @@ # shellcheck disable=SC2015 # +declare -r MOD_REPO="arevindh" +declare -r MOD_BRANCH="master" +declare -r CORE_BRANCH="master" +declare -r ADMIN_BRANCH="master" declare -r HTML_DIR="/var/www/html" declare -r CORE_DIR="/etc/.pihole" declare -r OPT_DIR="/opt/pihole" @@ -15,17 +19,18 @@ declare -r CURR_WP="$OPT_DIR/webpage.sh" declare -r CURR_DB="$ETC_DIR/speedtest.db" declare -r LAST_DB="$CURR_DB.old" declare -r DB_TABLE="speedtest" -declare -i aborted=0 -st_ver="" -mod_core_ver="" -mod_admin_ver="" -cleanup=true +declare cleanup +declare aborted +cleanup=$(mktemp) +aborted=$(mktemp) +echo "false" >"$cleanup" +echo "false" >"$aborted" # shellcheck disable=SC2034 SKIP_INSTALL=true # shellcheck disable=SC1091 source "$CORE_DIR/automated install/basic-install.sh" # shellcheck disable=SC1090,SC1091 -[[ -f "$OPT_DIR/speedtestmod/lib.sh" ]] && source "$OPT_DIR/speedtestmod/lib.sh" || source <(curl -sSLN https://github.com/arevindh/pi-hole/raw/master/advanced/Scripts/speedtestmod/lib.sh) +[[ -f "$OPT_DIR/speedtestmod/lib.sh" ]] && source "$OPT_DIR/speedtestmod/lib.sh" || source <(curl -sSLN https://github.com/"$MOD_REPO"/pi-hole/raw/"$CORE_BRANCH"/advanced/Scripts/speedtestmod/lib.sh) ####################################### # Display the help message @@ -41,35 +46,34 @@ help() { "The Mod Script" "Usage: sudo bash /path/to/mod.sh [options]" " or: curl -sSLN //link/to/mod.sh | sudo bash [-s -- options]" - "(Re)install the latest release of the Speedtest Mod and/or the following options:" + " or: pihole -a -sm [options]" + "(Re)install Speedtest Mod and/or the following options:" "" "Installation:" " -u, --update, up also update Pi-hole" - " -r, --reinstall keep current version of the mod, if installed" - " -t, --testing install the beta version of the mod" + " -r, --reinstall repair currently installed version of the Mod" + " -t, --testing try unstable changes" "" "Restoration:" - " -n, --uninstall, un purge the mod, keeping the speedtest package, logs, and database" + " -n, --uninstall, un purge the Mod, keeping the speedtest package, logs, and database" " -b, --backup backup Pi-hole for faster offline restore" " -o, --online force online restore of Pi-hole" "" "Standalone:" " -d, --database, db flush/restore the database if it's not/empty" - " -s, --speedtest[=] install Ookla's or the specified CLI even if another is already installed" + " -s, --speedtest[=] install Ookla's or the specified CLI immediately" " -x, --verbose show the commands being run" - " -v, --version display the version of the mod and exit" + " -v, --version display the installed version of the Mod and exit" " -h, --help display this help message and exit" "" "Examples:" - " sudo bash /opt/pihole/speedtestmod/mod.sh -d -slibre" - " sudo bash /opt/pihole/speedtestmod/mod.sh --uninstall" - " curl -sSL https://github.com/arevindh/pihole-speedtest/raw/master/mod | sudo bash" - " curl -sSLN https://github.com/arevindh/pi-hole/raw/master/advanced/Scripts/speedtestmod/mod.sh | sudo bash -s -- -bo" + " pihole -a -sm -d -slibre" + " sudo bash /opt/pihole/speedtestmod/mod.sh --update" + " curl -sSL https://github.com/$MOD_REPO/pihole-speedtest/raw/$CORE_BRANCH/mod | sudo bash" + " curl -sSLN https://github.com/$MOD_REPO/pi-hole/raw/$CORE_BRANCH/advanced/Scripts/speedtestmod/mod.sh | sudo bash -s -- -bo" ) printf "%s\n" "${help_text[@]}" - cleanup=false - exit 0 } ####################################### @@ -166,9 +170,9 @@ purge() { # The changes reverted # shellcheck disable=SC2317 ########### abort() { - if $cleanup && [[ $aborted -eq 0 ]]; then + if grep -q true "$cleanup" && grep -q false "$aborted"; then echo "Process Aborting..." - aborted=1 + echo "true" >"$aborted" if [[ -d "$CORE_DIR"/.git/refs/remotes/old ]]; then download /etc .pihole "" Pi-hole @@ -200,7 +204,7 @@ abort() { # The repositories cleaned up # shellcheck disable=SC2317 ########### commit() { - if $cleanup; then + if grep -q true "$cleanup"; then for dir in $CORE_DIR $HTML_DIR/admin; do [[ ! -d "$dir" ]] && continue || pushd "$dir" &>/dev/null || exit 1 ! git remote -v | grep -q "old" || git remote remove old @@ -223,9 +227,6 @@ commit() { # CURR_DB # LAST_DB # ETC_DIR -# st_ver -# mod_core_ver -# mod_admin_ver # cleanup # Arguments: # $@: The options for managing the installation @@ -233,10 +234,17 @@ commit() { # The installation managed ####################################### main() { + set -u + local -r short_opts=-ubortnds::vxh local -r long_opts=update,backup,online,reinstall,testing,uninstall,database,speedtest::,version,verbose,help local parsed_opts - parsed_opts=$(getopt --options ${short_opts} --longoptions ${long_opts} --name "$0" -- "$@") || help + + if ! parsed_opts=$(getopt --options ${short_opts} --longoptions ${long_opts} --name "$0" -- "$@"); then + help + return 1 + fi + eval set -- "${parsed_opts}" declare -a POSITIONAL EXTRA_ARGS @@ -252,6 +260,9 @@ main() { local select_test=false local selected_test="" local do_main=false + local st_ver="" + local mod_core_ver="" + local mod_admin_ver="" while [[ $# -gt 0 ]]; do case "$1" in @@ -282,16 +293,24 @@ main() { -d | --database) database=true ;; -s | --speedtest) select_test=true - [[ -n "$2" && ! "$2" =~ sivel|libre ]] && help || selected_test=$2 + + if [[ -n "$2" && ! "$2" =~ sivel|libre ]]; then + help + return 1 + fi + + selected_test=$2 shift ;; -v | --version) getVersion $MOD_DIR - cleanup=false - exit 0 + return 0 ;; -x | --verbose) verbose=true ;; - -h | --help) help ;; + -h | --help) + help + return 0 + ;; --) dashes=1 ;; *) [[ $dashes -eq 0 ]] && POSITIONAL+=("$1") || EXTRA_ARGS+=("$1") ;; esac @@ -306,18 +325,31 @@ main() { up) update=true ;; un) uninstall=true ;; db) database=true ;; - *) help ;; + *) + help + return 1 + ;; esac done - readonly update backup online reinstall stable uninstall database verbose cleanup select_test selected_test + echo "true" >"$cleanup" + ! $do_main && ! $database && ! $select_test && do_main=true || : + readonly update backup online reinstall stable uninstall database verbose select_test selected_test do_main + printf "%s\n\nRunning the Mod Script by @ipitio...\n" "$(date)" + ! $verbose || set -x + + if $select_test; then + case $selected_test in + sivel) swivelSpeed ;; + libre) libreSpeed ;; + *) ooklaSpeed ;; + esac + fi + + set -Eeo pipefail trap '[ "$?" -eq "0" ] && commit || abort' EXIT trap 'abort' INT TERM ERR - set -Eeuo pipefail shopt -s dotglob - printf "%s\n\nRunning the Mod Script by @ipitio...\n" "$(date)" - ! $do_main && ! $database && ! $select_test && do_main=true || : - ! $verbose || set -x if $database; then if [[ -f $CURR_DB ]] && ! isEmpty $CURR_DB; then @@ -341,16 +373,6 @@ main() { fi fi - if $select_test; then - set +Eeo pipefail # don't exit on error, not critical - case $selected_test in - sivel) swivelSpeed ;; - libre) libreSpeed ;; - *) ooklaSpeed ;; - esac - set -Eeo pipefail - fi - if $do_main; then if [[ ! -f /usr/local/bin/pihole ]]; then # https://discourse.pi-hole.net/t/pi-hole-as-part-of-a-post-installation-script/3523/15 @@ -453,7 +475,7 @@ EOF if [[ ${#missingpkgs[@]} -gt 0 ]]; then echo "Installing Missing Dependencies..." if ! $PKG_MANAGER install -y "${missingpkgs[@]}" &>/dev/null; then - [[ "$PKG_MANAGER" == *"apt-get"* ]] || exit 1 + [[ "$PKG_MANAGER" == *"apt"* ]] || exit 1 echo "And Updating Package Cache..." $PKG_MANAGER update -y &>/dev/null $PKG_MANAGER install -y "${missingpkgs[@]}" &>/dev/null @@ -466,8 +488,10 @@ EOF local -r installed_admin_ver=$(getVersion "web") if [[ "$installed_core_ver" == *.* && "$installed_admin_ver" == *.* ]]; then echo "Finding Latest Compatible Versions..." - mod_core_ver=$(git ls-remote "https://github.com/arevindh/pi-hole" | grep -q "$installed_core_ver" && git ls-remote "https://github.com/arevindh/pi-hole" | grep "$installed_core_ver" | awk '{print $2;}' | cut -d '/' -f 3 | sort -Vr | head -n1 || echo "") - mod_admin_ver=$(git ls-remote "https://github.com/arevindh/AdminLTE" | grep -q "$installed_admin_ver" && git ls-remote "https://github.com/arevindh/AdminLTE" | grep "$installed_admin_ver" | awk '{print $2;}' | cut -d '/' -f 3 | sort -Vr | head -n1 || echo "") + local -r remote_core_ver=$(git ls-remote "https://github.com/$MOD_REPO/pi-hole") + local -r remote_admin_ver=$(git ls-remote "https://github.com/$MOD_REPO/AdminLTE") + mod_core_ver=$(grep -q "$installed_core_ver" <<<"$remote_core_ver" && grep "$installed_core_ver" <<<"$remote_core_ver" | awk '{print $2;}' | cut -d '/' -f 3 | sort -Vr | head -n1 || echo "") + mod_admin_ver=$(grep -q "$installed_admin_ver" <<<"$remote_admin_ver" && grep "$installed_admin_ver" <<<"$remote_admin_ver" | awk '{print $2;}' | cut -d '/' -f 3 | sort -Vr | head -n1 || echo "") fi fi elif [[ -d /run/systemd/system ]]; then @@ -479,12 +503,12 @@ EOF if $backup; then echo "Creating Backup..." - download /etc .pihole.mod https://github.com/arevindh/pi-hole "$mod_core_ver" master $stable - download $HTML_DIR admin.mod https://github.com/arevindh/AdminLTE "$mod_admin_ver" master $stable + download /etc .pihole.mod https://github.com/"$MOD_REPO"/pi-hole "$mod_core_ver" "$CORE_BRANCH" $stable + download $HTML_DIR admin.mod https://github.com/"$MOD_REPO"/AdminLTE "$mod_admin_ver" "$ADMIN_BRANCH" $stable fi $reinstall && echo "Reinstalling Mod..." || echo "Installing Mod..." - download /etc pihole-speedtest https://github.com/arevindh/pihole-speedtest "$st_ver" master $stable + download /etc pihole-speedtest https://github.com/"$MOD_REPO"/pihole-speedtest "$st_ver" "$MOD_BRANCH" $stable [[ -f $MOD_DIR/cnf ]] || touch $MOD_DIR/cnf setCnf mod-$MOD_DIR "$(getVersion $MOD_DIR)" $MOD_DIR/cnf $reinstall local stock_tag @@ -506,11 +530,11 @@ EOF fi done - $backup || download /etc .pihole https://github.com/arevindh/pi-hole "$mod_core_ver" master $stable + $backup || download /etc .pihole https://github.com/"$MOD_REPO"/pi-hole "$mod_core_ver" "$CORE_BRANCH" $stable swapScripts \cp -af $CORE_DIR/advanced/Scripts/speedtestmod/. $OPT_DIR/speedtestmod/ pihole -a -s - $backup || download $HTML_DIR admin https://github.com/arevindh/AdminLTE "$mod_admin_ver" master $stable + $backup || download $HTML_DIR admin https://github.com/"$MOD_REPO"/AdminLTE "$mod_admin_ver" "$ADMIN_BRANCH" $stable setCnf mod-$CORE_DIR "$(getVersion $CORE_DIR)" $MOD_DIR/cnf $reinstall setCnf mod-$HTML_DIR/admin "$(getVersion $HTML_DIR/admin)" $MOD_DIR/cnf $reinstall fi @@ -531,5 +555,8 @@ fi rm -f /tmp/pimod.log touch /tmp/pimod.log main "$@" 2>&1 | tee -a /tmp/pimod.log -$cleanup && mv -f /tmp/pimod.log /var/log/pihole/mod.log || rm -f /tmp/pimod.log -exit $aborted +grep -q false "$cleanup" || mv -f /tmp/pimod.log /var/log/pihole/mod.log && rm -f /tmp/pimod.log +return_status=$(<"$aborted") +rm -f "$cleanup" +rm -f "$aborted" +[[ "$return_status" == "true" ]] && exit 1 || exit 0 diff --git a/advanced/Scripts/speedtestmod/speedtest.sh b/advanced/Scripts/speedtestmod/speedtest.sh index f766e77bc8..0e3ff08ed7 100755 --- a/advanced/Scripts/speedtestmod/speedtest.sh +++ b/advanced/Scripts/speedtestmod/speedtest.sh @@ -6,9 +6,9 @@ # shellcheck disable=SC2015 # -# shellcheck disable=SC1091 -source /opt/pihole/speedtestmod/lib.sh - +declare -r MOD_REPO="arevindh" +declare -r CORE_BRANCH="master" +declare -r OPT_DIR="/opt/pihole" declare -r OUT_FILE=/tmp/speedtest.log declare -r CREATE_TABLE="create table if not exists speedtest ( id integer primary key autoincrement, @@ -24,11 +24,14 @@ upload real, share_url text );" declare START -declare SERVER_ID START=$(date -u --rfc-3339='seconds') -SERVER_ID=$(grep 'SPEEDTEST_SERVER' "/etc/pihole/setupVars.conf" | cut -d '=' -f2) -readonly START SERVER_ID -declare -i run_status=0 +readonly START +serverid=$(grep 'SPEEDTEST_SERVER' "/etc/pihole/setupVars.conf" | cut -d '=' -f2) +run_status=$(mktemp) +database="/etc/pihole/speedtest.db" +echo "0" >"$run_status" +# shellcheck disable=SC1090,SC1091 +[[ -f "$OPT_DIR/speedtestmod/lib.sh" ]] && source "$OPT_DIR/speedtestmod/lib.sh" || source <(curl -sSLN https://github.com/"$MOD_REPO"/pi-hole/raw/"$CORE_BRANCH"/advanced/Scripts/speedtestmod/lib.sh) ####################################### # Display the help message @@ -41,14 +44,25 @@ declare -i run_status=0 ####################################### help() { local -r help_text=( - "Usage: $0 [options]" - "" + "The Test Script" + "Usage: sudo bash /path/to/speedtest.sh [options]" + " or: curl -sSLN //link/to/speedtest.sh | sudo bash [-s -- options]" + " or: pihole -a -sn [options]" "Run the speedtest" "" "Options:" + " -s, --server= Speedtest server id" + " -l, --list List all speedtest servers" + " -o, --output= Sqlite3 database (default: /etc/pihole/speedtest.db)" " -a, --attempts= Number of attempts (default: 3)" " -x, --verbose Show the commands being run" " -h, --help Display this help message" + "" + "Examples:" + " pihole -a -sn -a 1" + " sudo bash /opt/pihole/speedtestmod/speedtest.sh" + " curl -sSL https://github.com/$MOD_REPO/pihole-speedtest/raw/$CORE_BRANCH/test | sudo bash" + " curl -sSLN https://github.com/$MOD_REPO/pi-hole/raw/$CORE_BRANCH/advanced/Scripts/speedtestmod/speedtest.sh | sudo bash -s -- --verbose" ) printf "%s\n" "${help_text[@]}" @@ -58,7 +72,7 @@ help() { ####################################### # Run the speedtest # Globals: -# SERVER_ID +# serverid # Arguments: # None # Outputs: @@ -66,9 +80,9 @@ help() { ####################################### speedtest() { if /usr/bin/speedtest --version | grep -q "official"; then - [[ -n "${SERVER_ID}" ]] && /usr/bin/speedtest -s "$SERVER_ID" --accept-gdpr --accept-license -f json || /usr/bin/speedtest --accept-gdpr --accept-license -f json + [[ -n "${serverid}" ]] && /usr/bin/speedtest -s "$serverid" --accept-gdpr --accept-license -f json || /usr/bin/speedtest --accept-gdpr --accept-license -f json else - [[ -n "${SERVER_ID}" ]] && /usr/bin/speedtest --server "$SERVER_ID" --json --share --secure || /usr/bin/speedtest --json --share --secure + [[ -n "${serverid}" ]] && /usr/bin/speedtest --server "$serverid" --json --share --secure || /usr/bin/speedtest --json --share --secure fi } @@ -154,7 +168,7 @@ run() { run $1 $((${2:-0} + 1)) fi else - echo "Limit Reached!" + echo "Timeout!" fi local -r rm_empty=" @@ -167,10 +181,14 @@ run() { jq "$rm_empty" "$json_file" >"$temp_file" && mv -f "$temp_file" "$json_file" rm -f "$temp_file" chmod 644 /tmp/speedtest_results - mv -f /tmp/speedtest_results /var/log/pihole/speedtest.log - \cp -af /var/log/pihole/speedtest.log /etc/pihole/speedtest.log - sqlite3 /etc/pihole/speedtest.db "$CREATE_TABLE" - sqlite3 /etc/pihole/speedtest.db "insert into speedtest values (NULL, '${START}', '${stop}', '${isp}', '${from_ip}', '${server_name}', ${server_dist}, ${server_ping}, ${download}, ${upload}, '${share_url}');" + + if [[ -f /usr/local/bin/pihole ]]; then + mv -f /tmp/speedtest_results /var/log/pihole/speedtest.log + \cp -af /var/log/pihole/speedtest.log /etc/pihole/speedtest.log + fi + + sqlite3 "$database" "$CREATE_TABLE" + sqlite3 "$database" "insert into speedtest values (NULL, '${START}', '${stop}', '${isp}', '${from_ip}', '${server_name}', ${server_dist}, ${server_ping}, ${download}, ${upload}, '${share_url}');" [[ "$isp" == "No Internet" ]] && return 1 || return 0 } @@ -184,8 +202,8 @@ run() { # The speedtest status ####################################### main() { - local -r short_opts=-a:xh - local -r long_opts=attempts:,verbose,help + local -r short_opts=-s:lo:a:xh + local -r long_opts=server:,list,output:,attempts:,verbose,help local -r parsed_opts=$(getopt --options ${short_opts} --longoptions ${long_opts} --name "$0" -- "$@") local POSITIONAL=() local attempts="3" @@ -194,6 +212,18 @@ main() { while [[ $# -gt 0 ]]; do case "$1" in + -s | --server) + serverid="$2" + shift + ;; + -l | --list) + /usr/bin/speedtest --version | grep -q official && sudo /usr/bin/speedtest -L || /usr/bin/speedtest --secure --list 2>&1 + exit 0 + ;; + -o | --output) + database="$2" + shift + ;; -a | --attempts) attempts="$2" shift @@ -209,7 +239,7 @@ main() { [[ "$attempts" =~ ^[0-9]+$ ]] || attempts="3" ! $verbose || set -x run $attempts - run_status=$? + echo "$?" >"$run_status" } if [[ $EUID != 0 ]]; then @@ -221,4 +251,6 @@ rm -f "$OUT_FILE" touch "$OUT_FILE" main "$@" 2>&1 | tee -a "$OUT_FILE" mv -f "$OUT_FILE" /var/log/pihole/speedtest-run.log || rm -f "$OUT_FILE" -exit $run_status +exit_code=$(<"$run_status") +rm -f "$run_status" +[[ "$exit_code" -eq 1 ]] && exit 1 || exit 0 diff --git a/advanced/Scripts/webpage.sh b/advanced/Scripts/webpage.sh index 313932f584..aa4c9c6373 100755 --- a/advanced/Scripts/webpage.sh +++ b/advanced/Scripts/webpage.sh @@ -53,14 +53,9 @@ Options: -h, --help Show this help dialog -i, interface Specify dnsmasq's interface listening behavior -s, speedtest Set speedtest interval, add 0 to disable - -in (Re)install Latest Speedtest Mod and only Mod - -up [un] [db] (Re)install Latest Pi-hole and (uninstall) the Mod (and flush/restore the database) - -un [db] Uninstall Speedtest Mod without updating Pi-hole (and delete/restore the database) - -db Flush or restore the Speedtest Mod database -sd Set speedtest display range - -sn Run speedtest now - -sm Speedtest Mode (deprecated) - -sc Clear speedtest data + -sm [options] Run the Mod Script in the background (tmux a -t pimod) + -sn [options] Run the Test Script in the background (tmux a -t pimodtest) -ss Set custom server -st Set default speedtest chart type (line, bar) -l, privacylevel Set privacy level (0 = lowest, 3 = highest) @@ -591,36 +586,38 @@ SpeedtestServer() { addOrEditKeyValPair "${setupVars}" "SPEEDTEST_SERVER" "$test_server" } -RunSpeedtestNow() { +RunSpeedtestMod() { # if there is a running session, wait for it to finish - # if the session is still running after 5 minutes, kill it - while [[ $(tmux list-sessions 2>/dev/null | grep -c pimodtest) -gt 0 ]]; do + while [[ $(tmux list-sessions 2>/dev/null | grep -c pimod) -gt 0 ]]; do sleep 1 ((counter++)) if [[ $counter -gt 300 ]]; then - tmux kill-session -t pimodtest + tmux kill-session -t pimod break fi done - tmux new-session -d -s pimodtest "sudo bash $speedtestfile" + # discard indexes 0 and 1 from args + args=("${args[@]:2}") + tmux new-session -d -s pimod "sudo bash $speedtestmod ${args[*]}" } -ReinstallSpeedTest() { - tmux new-session -d -s pimod "sudo bash $speedtestmod" -} - -UpdateSpeedTest() { - tmux new-session -d -s pimod "sudo bash $speedtestmod up ${args[2]} ${args[3]}" -} +RunSpeedtestNow() { + # if the session is still running after 5 minutes, kill it + while [[ $(tmux list-sessions 2>/dev/null | grep -c pimodtest) -gt 0 ]]; do + sleep 1 + ((counter++)) -UninstallSpeedTest() { - tmux new-session -d -s pimod "sudo bash $speedtestmod un ${args[2]}" -} + if [[ $counter -gt 300 ]]; then + tmux kill-session -t pimodtest + break + fi + done -ClearSpeedtestData() { - tmux new-session -d -s pimod "sudo bash $speedtestmod db" + # discard indexes 0 and 1 from args + args=("${args[@]:2}") + tmux new-session -d -s pimodtest "sudo bash $speedtestfile ${args[*]}" } SetWebUITheme() { @@ -966,13 +963,9 @@ main() { "clearaudit" ) clearAudit;; "-l" | "privacylevel" ) SetPrivacyLevel;; "-s" | "speedtest" ) ChangeSpeedTestSchedule;; - "-in" ) ReinstallSpeedTest;; - "-up" ) UpdateSpeedTest;; - "-un" ) UninstallSpeedTest;; - "-db" ) ClearSpeedtestData;; "-sd" ) UpdateSpeedTestRange;; - "-sn" ) RunSpeedtestNow;; - "-sc" ) ClearSpeedtestData;; + "-sm" ) RunSpeedtestMod ;; + "-sn" ) RunSpeedtestNow ;; "-ss" ) SpeedtestServer;; "-st" ) UpdateSpeedTestChartType;; "addcustomdns" ) AddCustomDNSAddress;;