diff --git a/advanced/Scripts/speedtestmod/mod.sh b/advanced/Scripts/speedtestmod/mod.sh index b59544d255..41e6e85cd9 100755 --- a/advanced/Scripts/speedtestmod/mod.sh +++ b/advanced/Scripts/speedtestmod/mod.sh @@ -1,38 +1,28 @@ #!/bin/bash -admin_dir=/var/www/html -core_dir=/etc/.pihole -opt_dir=/opt/pihole -etc_dir=/etc/pihole -curr_wp=$opt_dir/webpage.sh -curr_db=$etc_dir/speedtest.db -last_db=$curr_db.old -db_table="speedtest" - -help() { - echo "(Re)install Latest Speedtest Mod." - echo "Usage: sudo $0 [up] [un] [db]" - echo "up - update the mod (and Pi-hole if not in Docker)" - echo "un - remove the mod (and any database backups)" - echo "db - flush the database (if it's not empty, otherwise restore it)" - echo "If no option is specified, the latest version of the Mod will be (re)installed." + +getLocalVersion() { + local src=${1:-} + + if [ -z "$src" ]; then + echo "" + else + local localVersion=$(pihole -v | grep "$src" | cut -d ' ' -f 6) + [ "$localVersion" != "HEAD" ] || localVersion=$(pihole -v | grep "$src" | cut -d ' ' -f 7) + echo "$localVersion" + fi } download() { local path=$1 local name=$2 local url=$3 - local src=${4:-} + local localVersion=${4:-} local branch="${5:-master}" local dest=$path/$name + local tags=$(git ls-remote --tags "$url" | awk -F/ '{print $3}' | grep '^v[0-9]' | grep -v '\^{}' | sort -V) - if [ ! -d "$dest" ]; then - git clone --depth=1 -b "$branch" "$url" "$dest" - elif [ ! -d "$dest/.git" ]; then - mv -f "$dest" "$dest.old" - download "$@" - return - fi - + [ -d "$dest" ] && [ ! -d "$dest/.git" ] && mv -f "$dest" "$dest.old" || : + [ -d "$dest" ] || git clone --depth=1 -b "$branch" "$url" "$dest" -q cd "$dest" git config --global --add safe.directory "$dest" @@ -46,74 +36,15 @@ download() { git clean -ffdx fi - git fetch origin $branch:refs/remotes/origin/$branch -q - git reset --hard origin/"$branch" - git checkout -B "$branch" - git rev-parse --verify "$branch" >/dev/null 2>&1 && git branch -u "origin/$branch" "$branch" || git checkout --track "origin/$branch" - git tag -l | xargs git tag -d >/dev/null 2>&1 - git fetch --tags -f -q - [ "$url" == "old" ] || latestTag=$(git ls-remote --tags "$url" | awk -F/ '{print $3}' | grep -v '\^{}' | sort -V | tail -n1) - [[ "${latestTag:-}" == *.* ]] || latestTag=$(git describe --tags $(git rev-list --tags --max-count=1)) - - if [[ "$url" != *"arevindh"* ]] && [ ! -z "$src" ] && [[ "$url" != *"ipitio"* ]] && ! git remote -v | grep -q "old.*ipitio"; then - local localVersion=$(pihole -v | grep "$src" | cut -d ' ' -f 6) - [ "$localVersion" != "HEAD" ] || localVersion=$(pihole -v | grep "$src" | cut -d ' ' -f 7) - - # if the local version is less than the latest tag then use the tag before it - if [[ "$localVersion" == *.* ]] && [[ "$localVersion" < "$latestTag" ]]; then - [ "$url" == "old" ] || latestTag=$(git ls-remote --tags "$url" | awk -F/ '{print $3}' | grep -v '\^{}' | sort -V | awk -v lv="$localVersion" '$1 < lv {print $1}' | tail -n1) - [[ "$latestTag" == *.* ]] || latestTag=$(git tag -l | grep -v '\^{}' | sort -V | awk -v lv="$localVersion" '$1 < lv {print $1}' | tail -n1) - [ ! -f "$(git rev-parse --git-dir)"/shallow ] || git fetch --unshallow - fi - fi - - if [ "$branch" == "master" ] && [[ "$url" != *"ipitio"* ]] && [ "$(git rev-parse HEAD)" != "$(git rev-parse $latestTag 2>/dev/null)" ]; then - git -c advice.detachedHead=false checkout "$latestTag" - fi - + git fetch origin --depth=1 $branch:refs/remotes/origin/$branch -q + git reset --hard origin/"$branch" -q + git checkout -B "$branch" -q + [[ "$localVersion" == *.* ]] && local latestTag=$localVersion || local latestTag=$(getLocalVersion "$localVersion") + [ "$url" != "old" ] && [[ "$url" != *"arevindh"* ]] && ! git remote -v | grep -q "old.*ipitio" && [[ "$localVersion" < "$latestTag" ]] && latestTag=$(awk -v lv="$localVersion" '$1 <= lv' <<< "$tags" | tail -n1) || latestTag=$(tail -n1 <<< "$tags") + [ "$branch" == "master" ] && [[ "$url" != *"ipitio"* ]] && [ "$(git rev-parse HEAD)" != "$(git rev-parse $latestTag 2>/dev/null)" ] && git fetch origin tag $latestTag --depth=1 -q && git -c advice.detachedHead=false checkout "$latestTag" -q cd .. } -isEmpty() { - db=$1 - if [ -f $db ]; then - if ! sqlite3 "$db" "select * from $db_table limit 1;" >/dev/null 2>&1 || [ -z "$(sqlite3 "$db" "select * from $db_table limit 1;")" ]; then - return 0 - fi - fi - return 1 -} - -manageHistory() { - if [ "${1:-}" == "db" ]; then - if [ -f $curr_db ] && ! isEmpty $curr_db; then - echo "Flushing Database..." - mv -f $curr_db $last_db - - if [ -f $etc_dir/last_speedtest ]; then - mv -f $etc_dir/last_speedtest $etc_dir/last_speedtest.old - fi - - if [ -f /var/log/pihole/speedtest.log ]; then - mv -f /var/log/pihole/speedtest.log /var/log/pihole/speedtest.log.old - rm -f $etc_dir/speedtest.log - fi - elif [ -f $last_db ]; then - echo "Restoring Database..." - mv -f $last_db $curr_db - - if [ -f $etc_dir/last_speedtest.old ]; then - mv -f $etc_dir/last_speedtest.old $etc_dir/last_speedtest - fi - - if [ -f /var/log/pihole/speedtest.log.old ]; then - mv -f /var/log/pihole/speedtest.log.old /var/log/pihole/speedtest.log - cp -af /var/log/pihole/speedtest.log $etc_dir/speedtest.log - fi - fi - fi -} - notInstalled() { if [ -x "$(command -v apt-get)" ]; then dpkg -s "$1" &>/dev/null || return 0 @@ -127,179 +58,215 @@ notInstalled() { return 1 } -swapScripts() { - SKIP_INSTALL=true - set +u - source "$core_dir/automated install/basic-install.sh" - installScripts - set -u -} - -installMod() { - echo "Installing Mod..." - - if [ ! -f /usr/local/bin/pihole ]; then - echo "Installing Pi-hole..." - curl -sSL https://install.pi-hole.net | sudo bash - fi +# allow to source the above helper functions without running the whole script +if [[ "${SKIP_MOD:-}" != true ]]; then + aborted=0 + html_dir=/var/www/html + core_dir=/etc/.pihole + opt_dir=/opt/pihole + etc_dir=/etc/pihole + curr_wp=$opt_dir/webpage.sh + curr_db=$etc_dir/speedtest.db + last_db=$curr_db.old + db_table="speedtest" + st_ver=$db_table + + help() { + echo "(Re)install Latest Speedtest Mod." + echo "Usage: sudo $0 [up] [un] [db]" + echo "up - update the mod (and Pi-hole if not in Docker)" + echo "un - remove the mod (and any database backups)" + echo "db - flush the database (if it's not empty, otherwise restore it)" + echo "If no option is specified, the latest version of the Mod will be (re)installed." + } + + isEmpty() { + db=$1 + [ -f $db ] && sqlite3 "$db" "select * from $db_table limit 1;" >/dev/null 2>&1 && [ ! -z "$(sqlite3 "$db" "select * from $db_table limit 1;")" ] && return 1 || return 0 + } + + manageHistory() { + if [ "${1:-}" == "db" ]; then + if [ -f $curr_db ] && ! isEmpty $curr_db; then + echo "Flushing Database..." + mv -f $curr_db $last_db + [ ! -f $etc_dir/last_speedtest ] || mv -f $etc_dir/last_speedtest $etc_dir/last_speedtest.old + + if [ -f /var/log/pihole/speedtest.log ]; then + mv -f /var/log/pihole/speedtest.log /var/log/pihole/speedtest.log.old + rm -f $etc_dir/speedtest.log + fi + elif [ -f $last_db ]; then + echo "Restoring Database..." + mv -f $last_db $curr_db + [ ! -f $etc_dir/last_speedtest.old ] || mv -f $etc_dir/last_speedtest.old $etc_dir/last_speedtest + + if [ -f /var/log/pihole/speedtest.log.old ]; then + mv -f /var/log/pihole/speedtest.log.old /var/log/pihole/speedtest.log + cp -af /var/log/pihole/speedtest.log $etc_dir/speedtest.log + fi + fi + fi + } - local PHP_VERSION=$(php -v | head -n 1 | awk '{print $2}' | cut -d "." -f 1,2) - local PKG_MANAGER=$(command -v apt-get || command -v dnf || command -v yum) - local PKGS=(bc sqlite3 jq tmux wget "php$PHP_VERSION-sqlite3") + swapScripts() { + SKIP_INSTALL=true + set +u + source "$core_dir/automated install/basic-install.sh" + installScripts + set -u + } - local missingPkgs=() - for pkg in "${PKGS[@]}"; do - if notInstalled "$pkg"; then - missingPkgs+=("$pkg") - fi - done + installMod() { + echo "Installing Mod..." - if [ ${#missingPkgs[@]} -gt 0 ]; then - if [[ "$PKG_MANAGER" == *"apt-get"* ]]; then - apt-get update + if [ ! -f /usr/local/bin/pihole ]; then + echo "Installing Pi-hole..." + curl -sSL https://install.pi-hole.net | sudo bash fi - $PKG_MANAGER install -y "${missingPkgs[@]}" - fi - - download /etc .pihole https://github.com/arevindh/pi-hole Pi-hole - download $etc_dir speedtest https://github.com/arevindh/pihole-speedtest - download $admin_dir admin https://github.com/arevindh/AdminLTE web + local PHP_VERSION=$(php -v | head -n 1 | awk '{print $2}' | cut -d "." -f 1,2) + local PKG_MANAGER=$(command -v apt-get || command -v dnf || command -v yum) + local PKGS=(bc sqlite3 jq tmux wget "php$PHP_VERSION-sqlite3") + local missingPkgs=() - swapScripts - cp -af $core_dir/advanced/Scripts/speedtestmod/. $opt_dir/speedtestmod/ - pihole -a -s - pihole updatechecker -} + for pkg in "${PKGS[@]}"; do + notInstalled "$pkg" && missingPkgs+=("$pkg") || : + done -uninstall() { - if [ -f $curr_wp ] && cat $curr_wp | grep -q SpeedTest; then - echo "Restoring Pi-hole..." + if [ ${#missingPkgs[@]} -gt 0 ]; then + [[ "$PKG_MANAGER" != *"apt-get"* ]] || apt-get update + $PKG_MANAGER install -y "${missingPkgs[@]}" + fi - pihole -a -s -1 - download /etc .pihole https://github.com/pi-hole/pi-hole Pi-hole - download $admin_dir admin https://github.com/pi-hole/AdminLTE web + download /etc .pihole https://github.com/arevindh/pi-hole Pi-hole swapScripts - fi - - manageHistory ${1:-} -} - -purge() { - rm -rf "$admin_dir"/*_admin - rm -rf $opt_dir/speedtestmod - rm -rf $etc_dir/speedtest - rm -rf $etc_dir/mod - if [ -f /etc/systemd/system/pihole-speedtest.timer ]; then - rm -f /etc/systemd/system/pihole-speedtest.service - rm -f /etc/systemd/system/pihole-speedtest.timer - systemctl daemon-reload - fi + cp -af $core_dir/advanced/Scripts/speedtestmod/. $opt_dir/speedtestmod/ + pihole -a -s + download $html_dir admin https://github.com/arevindh/AdminLTE web + download $etc_dir speedtest https://github.com/arevindh/pihole-speedtest + pihole updatechecker + } + + uninstall() { + if [ -f $curr_wp ] && cat $curr_wp | grep -q SpeedTest; then + echo "Restoring Pi-hole..." + pihole -a -s -1 + download $html_dir admin https://github.com/pi-hole/AdminLTE web + download /etc .pihole https://github.com/pi-hole/pi-hole Pi-hole + st_ver=$(getLocalVersion "speedtest") + rm -rf $etc_dir/speedtest + swapScripts + fi - rm -f "$curr_wp".* - rm -f "$curr_db".* - rm -f "$curr_db"_* - rm -f $etc_dir/last_speedtest.* - if isEmpty $curr_db; then - rm -f $curr_db - fi + manageHistory ${1:-} + } - pihole updatechecker -} + purge() { + if [ -f /etc/systemd/system/pihole-speedtest.timer ]; then + rm -f /etc/systemd/system/pihole-speedtest.service + rm -f /etc/systemd/system/pihole-speedtest.timer + systemctl daemon-reload + fi -update() { - if [[ -d /run/systemd/system ]]; then - echo "Updating Pi-hole..." - PIHOLE_SKIP_OS_CHECK=true sudo -E pihole -up - else - echo "Systemd not found. Skipping Pi-hole update..." - fi + rm -rf "$html_dir"/*_admin + rm -rf $opt_dir/speedtestmod + rm -rf $etc_dir/mod + rm -f "$curr_wp".* + rm -f "$curr_db".* + rm -f "$curr_db"_* + rm -f $etc_dir/last_speedtest.* + isEmpty $curr_db && rm -f $curr_db || : + pihole updatechecker + } + + update() { + if [[ -d /run/systemd/system ]]; then + echo "Updating Pi-hole..." + PIHOLE_SKIP_OS_CHECK=true sudo -E pihole -up + else + echo "Systemd not found. Skipping Pi-hole update..." + fi - if [ "${1:-}" == "un" ]; then - purge - exit 0 - fi -} + if [ "${1:-}" == "un" ]; then + purge + exit 0 + fi -abort() { - echo "Process Aborting..." + installMod + } - if [ -d $admin_dir/admin/.git/refs/remotes/old ]; then - download $admin_dir admin old web - fi + abort() { + echo "Process Aborting..." - if [ -d $core_dir/.git/refs/remotes/old ]; then - download /etc .pihole old Pi-hole - source "$core_dir/automated install/basic-install.sh" - installScripts - fi + if [ -d $core_dir/.git/refs/remotes/old ]; then + download /etc .pihole old Pi-hole + swapScripts - if [ ! -f $curr_wp ] || ! cat $curr_wp | grep -q SpeedTest; then - purge - fi + if [ -d $core_dir/advanced/Scripts/speedtestmod ]; then + cp -af $core_dir/advanced/Scripts/speedtestmod/. $opt_dir/speedtestmod/ + pihole -a -s + fi + fi - if [ -f $last_db ] && [ ! -f $curr_db ]; then - mv $last_db $curr_db - fi + [ ! -d $html_dir/admin/.git/refs/remotes/old ] || download $html_dir admin old web + [ -d $etc_dir/speedtest ] && [ -d $etc_dir/speedtest/.git/refs/remotes/old ] && download $etc_dir speedtest old $st_ver || : + [ -f $curr_wp ] && cat $curr_wp | grep -q SpeedTest && purge || : + [ -f $last_db ] && [ ! -f $curr_db ] && mv $last_db $curr_db || : + aborted=1 + printf "Please try again before reporting an issue.\n\n$(date)\n" + } + + commit() { + for dir in $core_dir $html_dir/admin $etc_dir/speedtest; do + if [ -d $dir/.git/refs/remotes/old ]; then + cd $dir + git remote -v | grep -q "old" && git remote remove old || : + fi + done + printf "Done!\n\n$(date)\n" + } - pihole restartdns - aborted=1 - printf "Please try again before reporting an issue.\n\n$(date)\n" -} + main() { + printf "Thanks for using Speedtest Mod!\nScript by @ipitio\n\n$(date)\n\n" + local op=${1:-} -commit() { - cd $core_dir - git remote -v | grep -q "old" && git remote remove old - cd $admin_dir/admin - git remote -v | grep -q "old" && git remote remove old - pihole restartdns - printf "Done!\n\n$(date)\n" -} + if [ "$op" == "-h" ] || [ "$op" == "--help" ]; then + help + exit 0 + fi -main() { - printf "Thanks for using Speedtest Mod!\nScript by @ipitio\n\n$(date)\n\n" - local op=${1:-} + if [ $EUID != 0 ]; then + sudo "$0" "$@" + exit $? + fi - if [ "$op" == "-h" ] || [ "$op" == "--help" ]; then - help + set -Eeuo pipefail + trap '[ "$?" -eq "0" ] && commit || abort' EXIT + trap 'abort' INT TERM + shopt -s dotglob + + local db=$([ "$op" == "up" ] && echo "${3:-}" || [ "$op" == "un" ] && echo "${2:-}" || echo "$op") + case $op in + db) + manageHistory $db + ;; + un) + uninstall $db + purge + ;; + up) + uninstall $db + update ${2:-} + ;; + *) + uninstall $db + installMod + ;; + esac exit 0 - fi + } - if [ $EUID != 0 ]; then - sudo "$0" "$@" - exit $? - fi - - set -Eeuo pipefail - trap '[ "$?" -eq "0" ] && commit || abort' EXIT - trap 'abort' INT TERM - shopt -s dotglob - - local db=$([ "$op" == "up" ] && echo "${3:-}" || [ "$op" == "un" ] && echo "${2:-}" || echo "$op") - case $op in - db) - manageHistory $db - ;; - un) - uninstall $db - purge - ;; - up) - uninstall $db - update ${2:-} - installMod - ;; - *) - uninstall $db - installMod - ;; - esac - exit 0 -} - -# allow to source this script without running it -if [[ "${SKIP_MOD:-}" != true ]]; then - aborted=0 rm -f /tmp/pimod.log touch /tmp/pimod.log main "$@" 2>&1 | tee -a /tmp/pimod.log diff --git a/advanced/Scripts/speedtestmod/speedtest.sh b/advanced/Scripts/speedtestmod/speedtest.sh index fb2ad8d868..b52e2eaa71 100755 --- a/advanced/Scripts/speedtestmod/speedtest.sh +++ b/advanced/Scripts/speedtestmod/speedtest.sh @@ -15,23 +15,14 @@ download real, upload real, share_url text );" - SKIP_MOD=true source /opt/pihole/speedtestmod/mod.sh speedtest() { if grep -q official <<<"$(/usr/bin/speedtest --version)"; then - if [[ -z "${serverid}" ]]; then - /usr/bin/speedtest --accept-gdpr --accept-license -f json - else - /usr/bin/speedtest -s $serverid --accept-gdpr --accept-license -f json || /usr/bin/speedtest --accept-gdpr --accept-license -f json - fi + [[ ! -z "${serverid}" ]] && /usr/bin/speedtest -s $serverid --accept-gdpr --accept-license -f json || /usr/bin/speedtest --accept-gdpr --accept-license -f json else - if [[ -z "${serverid}" ]]; then - /usr/bin/speedtest --json --share --secure - else - /usr/bin/speedtest --server $serverid --json --share --secure || /usr/bin/speedtest --json --share --secure - fi + [[ ! -z "${serverid}" ]] && /usr/bin/speedtest --server $serverid --json --share --secure || /usr/bin/speedtest --json --share --secure fi } @@ -46,9 +37,6 @@ savetest() { local download=${8:-0} local upload=${9:-0} local share_url=${10:-"#"} - sqlite3 /etc/pihole/speedtest.db "$create_table" - sqlite3 /etc/pihole/speedtest.db "insert into speedtest values (NULL, '${start_time}', '${stop_time}', '${isp}', '${from_ip}', '${server}', ${server_dist}, ${server_ping}, ${download}, ${upload}, '${share_url}');" - local rm_empty=' def nonempty: . and length > 0 and (type != "object" or . != {}) and (type != "array" or any(.[]; . != "")); if type == "array" then map(walk(if type == "object" then with_entries(select(.value | nonempty)) else . end)) else walk(if type == "object" then with_entries(select(.value | nonempty)) else . end) end @@ -61,37 +49,27 @@ savetest() { mv -f /tmp/speedtest_results /var/log/pihole/speedtest.log cp -af /var/log/pihole/speedtest.log /etc/pihole/speedtest.log rm -f "$out" + sqlite3 /etc/pihole/speedtest.db "$create_table" + sqlite3 /etc/pihole/speedtest.db "insert into speedtest values (NULL, '${start_time}', '${stop_time}', '${isp}', '${from_ip}', '${server}', ${server_dist}, ${server_ping}, ${download}, ${upload}, '${share_url}');" [ "$isp" == "No Internet" ] && exit 1 || exit 0 } isAvailable() { if [ -x "$(command -v apt-get)" ]; then # Check if there is a candidate and it is not "(none)" - if apt-cache policy "$1" | grep -q "Candidate:" && ! apt-cache policy "$1" | grep -q "Candidate: (none)"; then - return 0 - fi + apt-cache policy "$1" | grep -q "Candidate:" && ! apt-cache policy "$1" | grep -q "Candidate: (none)" && return 0 || return 1 elif [ -x "$(command -v dnf)" ] || [ -x "$(command -v yum)" ]; then local PKG_MANAGER=$(command -v dnf || command -v yum) - if $PKG_MANAGER list available "$1" &>/dev/null; then - return 0 - fi + $PKG_MANAGER list available "$1" &>/dev/null && return 0 || return 1 else echo "Unsupported package manager!" exit 1 fi - - return 1 } swaptest() { if isAvailable $1; then - if [ -x "$(command -v apt-get)" ]; then - apt-get install -y $1 $2- - elif [ -x "$(command -v dnf)" ]; then - dnf install -y --allowerasing $1 - else - yum install -y --allowerasing $1 - fi + [ -x "$(command -v apt-get)" ] && apt-get install -y $1 $2- || { [ -x "$(command -v dnf)" ] && dnf install -y --allowerasing $1 || yum install -y --allowerasing $1; } fi } @@ -126,14 +104,51 @@ librespeed() { fi download /etc/pihole librespeed https://github.com/librespeed/speedtest-cli cd librespeed - if [ -d out ]; then - rm -rf out - fi + [ ! -d out ] || rm -rf out ./build.sh mv -f out/* /usr/bin/speedtest chmod +x /usr/bin/speedtest } +addSource() { + if [[ "$PKG_MANAGER" == *"yum"* || "$PKG_MANAGER" == *"dnf"* ]]; then + if [ ! -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 + fi + + yum list speedtest | grep -q "Available Packages" && $PKG_MANAGER install -y speedtest || : + elif [[ "$PKG_MANAGER" == *"apt-get"* ]]; then + if [ ! -f /etc/apt/sources.list.d/ookla_speedtest-cli.list ]; then + echo "Adding speedtest source for DEB..." + if [ -e /etc/os-release ]; then + . /etc/os-release + local base="ubuntu debian" + local os=${ID} + local dist=${VERSION_CODENAME} + if [ ! -z "${ID_LIKE-}" ] && [[ "${base//\"/}" =~ "${ID_LIKE//\"/}" ]] && [ "${os}" != "ubuntu" ]; then + os=${ID_LIKE%% *} + [ -z "${UBUNTU_CODENAME-}" ] && UBUNTU_CODENAME=$(/usr/bin/lsb_release -cs) + dist=${UBUNTU_CODENAME} + [ -z "$dist" ] && dist=${VERSION_CODENAME} + 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 + rm -f /tmp/script.deb.sh + else + curl -sSLN https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash + fi + + sed -i 's/g]/g allow-insecure=yes trusted=yes]/' /etc/apt/sources.list.d/ookla_speedtest-cli.list + apt-get update + fi + else + echo "Unsupported package manager!" + exit 1 + fi +} + run() { speedtest | jq . >/tmp/speedtest_results || echo "Attempt ${2:-1} Failed!" >/tmp/speedtest_results local stop=$(date -u --rfc-3339='seconds') @@ -151,9 +166,7 @@ run() { local from_ip=$(jq -r '.interface.externalIp' <<<"$res") local server_ping=$(jq -r '.ping.latency' <<<"$res") local share_url=$(jq -r '.result.url' <<<"$res") - if [ -z "$server_dist" ]; then - server_dist="-1" - fi + [ ! -z "$server_dist" ] || server_dist="-1" else # speedtest-cli local server_name=$(jq -r '.server.sponsor' <<<"$res") local download=$(jq -r '.download' <<<"$res" | awk '{$1=$1/1000/1000; print $1;}' | sed 's/,/./g') @@ -162,9 +175,7 @@ run() { local from_ip=$(jq -r '.client.ip' <<<"$res") local server_ping=$(jq -r '.ping' <<<"$res") local share_url=$(jq -r '.share' <<<"$res") - if [ -z "$server_dist" ]; then - server_dist=$(jq -r '.server.d' <<<"$res") - fi + [ ! -z "$server_dist" ] || server_dist=$(jq -r '.server.d' <<<"$res") fi savetest "$start" "$stop" "$isp" "$from_ip" "$server_name" "$server_dist" "$server_ping" "$download" "$upload" "$share_url" @@ -184,49 +195,9 @@ run() { savetest "$start" "$stop" else if notInstalled speedtest && notInstalled speedtest-cli; then - if [ -f /usr/bin/speedtest ]; then - rm -f /usr/bin/speedtest - fi - - if [[ "$PKG_MANAGER" == *"yum"* || "$PKG_MANAGER" == *"dnf"* ]]; then - if [ ! -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 - fi - - if yum list speedtest | grep -q "Available Packages"; then - $PKG_MANAGER install -y speedtest - fi - elif [[ "$PKG_MANAGER" == *"apt-get"* ]]; then - if [ ! -f /etc/apt/sources.list.d/ookla_speedtest-cli.list ]; then - echo "Adding speedtest source for DEB..." - if [ -e /etc/os-release ]; then - . /etc/os-release - local base="ubuntu debian" - local os=${ID} - local dist=${VERSION_CODENAME} - if [ ! -z "${ID_LIKE-}" ] && [[ "${base//\"/}" =~ "${ID_LIKE//\"/}" ]] && [ "${os}" != "ubuntu" ]; then - os=${ID_LIKE%% *} - [ -z "${UBUNTU_CODENAME-}" ] && UBUNTU_CODENAME=$(/usr/bin/lsb_release -cs) - dist=${UBUNTU_CODENAME} - [ -z "$dist" ] && dist=${VERSION_CODENAME} - 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 - rm -f /tmp/script.deb.sh - else - curl -sSLN https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash - fi - - sed -i 's/g]/g allow-insecure=yes trusted=yes]/' /etc/apt/sources.list.d/ookla_speedtest-cli.list - apt-get update - fi - - if isAvailable speedtest; then - $PKG_MANAGER install -y speedtest - fi - fi + [ ! -f /usr/bin/speedtest ] || rm -f /usr/bin/speedtest + addSource + isAvailable speedtest && $PKG_MANAGER install -y speedtest || : elif ! notInstalled speedtest; then swaptest speedtest-cli speedtest else @@ -244,51 +215,12 @@ main() { exit $? fi - if [ ! -d /etc/pihole/speedtest ]; then - download $etc_dir speedtest https://github.com/arevindh/pihole-speedtest - fi - + [ -d /etc/pihole/speedtest ] || download $etc_dir speedtest https://github.com/arevindh/pihole-speedtest PKG_MANAGER=$(command -v apt-get || command -v dnf || command -v yum) - if [ ! -f /usr/bin/speedtest ]; then - if [[ "$PKG_MANAGER" == *"yum"* || "$PKG_MANAGER" == *"dnf"* ]]; then - if [ ! -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 - fi - elif [[ "$PKG_MANAGER" == *"apt-get"* ]]; then - if [ ! -f /etc/apt/sources.list.d/ookla_speedtest-cli.list ]; then - echo "Adding speedtest source for DEB..." - if [ -e /etc/os-release ]; then - . /etc/os-release - local base="ubuntu debian" - local os=${ID} - local dist=${VERSION_CODENAME} - if [ ! -z "${ID_LIKE-}" ] && [[ "${base//\"/}" =~ "${ID_LIKE//\"/}" ]] && [ "${os}" != "ubuntu" ]; then - os=${ID_LIKE%% *} - [ -z "${UBUNTU_CODENAME-}" ] && UBUNTU_CODENAME=$(/usr/bin/lsb_release -cs) - dist=${UBUNTU_CODENAME} - [ -z "$dist" ] && dist=${VERSION_CODENAME} - 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 - rm -f /tmp/script.deb.sh - else - curl -sSLN https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash - fi - sed -i 's/g]/g allow-insecure=yes trusted=yes]/' /etc/apt/sources.list.d/ookla_speedtest-cli.list - apt-get update - fi - fi - - if isAvailable speedtest; then - $PKG_MANAGER install -y speedtest - elif isAvailable speedtest-cli; then - $PKG_MANAGER install -y speedtest-cli - else - librespeed - fi + if [ ! -f /usr/bin/speedtest ]; then + addSource + isAvailable speedtest && $PKG_MANAGER install -y speedtest || { isAvailable speedtest-cli && $PKG_MANAGER install -y speedtest-cli || librespeed; } fi echo "Running Test..." diff --git a/advanced/Scripts/webpage.sh b/advanced/Scripts/webpage.sh index 870d0e4abd..4b8e8a311b 100755 --- a/advanced/Scripts/webpage.sh +++ b/advanced/Scripts/webpage.sh @@ -623,10 +623,10 @@ schedule=\$(grep "SPEEDTESTSCHEDULE" "$setupVars" | cut -f2 -d"=") SKIP_MOD=true source /opt/pihole/speedtestmod/mod.sh -# if "pihole -v" includes "N/A" or doesn't include "speedtest", download the mod -if [[ "\$(/usr/local/bin/pihole -v)" == *"N/A"* ]] || [[ "\$(/usr/local/bin/pihole -v)" != *"speedtest"* ]]; then - download /etc/pihole speedtest https://github.com/arevindh/pihole-speedtest - pihole updatechecker +# if "pihole -v" includes "N/A" or doesn't include "speedtest", get v +if [[ "\$(/usr/local/bin/pihole -v)" == *"N/A"* ]] || [[ "\$(/usr/local/bin/pihole -v)" != *"Speedtest"* ]]; then + [ -d /etc/pihole/speedtest ] || download /etc/pihole speedtest https://github.com/arevindh/pihole-speedtest + /usr/local/bin/pihole updatechecker fi # if schedule is set and interval is "nan", set the speedtest interval to the schedule