diff --git a/advanced/Scripts/speedtestmod/mod.sh b/advanced/Scripts/speedtestmod/mod.sh index 49d758abbc..2dc4299785 100755 --- a/advanced/Scripts/speedtestmod/mod.sh +++ b/advanced/Scripts/speedtestmod/mod.sh @@ -1,9 +1,11 @@ #!/bin/bash admin_dir=/var/www/html -curr_wp=/opt/pihole/webpage.sh +opt_dir=/opt/pihole +etc_dir=/etc/pihole +curr_wp=$opt_dir/webpage.sh last_wp=$curr_wp.old org_wp=$curr_wp.org -curr_db=/etc/pihole/speedtest.db +curr_db=$etc_dir/speedtest.db last_db=$curr_db.old db_table="speedtest" @@ -13,7 +15,7 @@ help() { echo "up - update Pi-hole (along with the Mod)" echo "un - remove the mod (including all backups)" echo "db - flush database (restore for a short while after)" - echo "If no option is specified, the Mod will be (re)installed." + echo "If no option is specified, the latest version of the Mod will be (re)installed." } setTags() { @@ -71,7 +73,11 @@ download() { setTags "$dest" "${src:-}" "$branch" git reset --hard origin/"$branch" git checkout -B "$branch" - git branch -u origin/"$branch" + if git rev-parse --verify "$branch" >/dev/null 2>&1; then + git branch -u "origin/$branch" "$branch" + else + git checkout --track "origin/$branch" + fi fi if [ "$(git rev-parse HEAD)" != "$(git rev-parse $latestTag)" ]; then @@ -95,22 +101,22 @@ manageHistory() { if [ -f $curr_db ] && ! isEmpty $curr_db; then echo "Flushing Database..." mv -f $curr_db $last_db - if [ -f /etc/pihole/last_speedtest ]; then - mv -f /etc/pihole/last_speedtest /etc/pihole/last_speedtest.old + 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/pihole/speedtest.log + rm -f $etc_dir/speedtest.log fi elif [ -f $last_db ]; then echo "Restoring Database..." mv -f $last_db $curr_db - if [ -f /etc/pihole/last_speedtest.old ]; then - mv -f /etc/pihole/last_speedtest.old /etc/pihole/last_speedtest + 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/pihole/speedtest.log + cp -af /var/log/pihole/speedtest.log $etc_dir/speedtest.log fi fi fi @@ -155,39 +161,7 @@ install() { $PKG_MANAGER install -y "${missingPkgs[@]}" fi - if notInstalled speedtest && notInstalled speedtest-cli; 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 - fi - fi - $PKG_MANAGER install -y speedtest - fi - - download /opt mod_pihole https://github.com/arevindh/pi-hole + download $etc_dir mod https://github.com/arevindh/pi-hole download $admin_dir admin https://github.com/arevindh/AdminLTE web if [ -f $curr_wp ]; then if ! cat $curr_wp | grep -q SpeedTest; then @@ -197,8 +171,8 @@ install() { cp -af $curr_wp $last_wp fi fi - cp -af /opt/mod_pihole/advanced/Scripts/webpage.sh $curr_wp - cp -af /opt/mod_pihole/advanced/Scripts/speedtestmod/. /opt/pihole/speedtestmod/ + cp -af $etc_dir/mod/advanced/Scripts/webpage.sh $curr_wp + cp -af $etc_dir/mod/advanced/Scripts/speedtestmod/. $opt_dir/speedtestmod/ chmod +x $curr_wp pihole updatechecker local pihole -a -s @@ -225,6 +199,7 @@ uninstall() { cp -af $org_wp $curr_wp chmod +x $curr_wp rm -rf /opt/mod_pihole + rm -rf $etc_dir/mod pihole updatechecker fi @@ -232,8 +207,8 @@ uninstall() { } purge() { - rm -rf "$admin_dir"*_admin - rm -rf /opt/pihole/speedtestmod + rm -rf "$admin_dir"/*_admin + rm -rf $opt_dir/speedtestmod 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 @@ -243,7 +218,7 @@ purge() { rm -f "$curr_wp".* rm -f "$curr_db".* rm -f "$curr_db"_* - rm -f /etc/pihole/last_speedtest.* + rm -f $etc_dir/last_speedtest.* if isEmpty $curr_db; then rm -f $curr_db fi @@ -284,7 +259,7 @@ abort() { pihole restartdns aborted=1 - printf "Please try again or try manually.\n\n$(date)\n" + printf "Please try again before reporting an issue.\n\n$(date)\n" } commit() { @@ -306,10 +281,10 @@ main() { sudo "$0" "$@" exit $? fi - aborted=0 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 @@ -333,6 +308,7 @@ main() { exit 0 } +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 3c8ceee445..a53ed7bdbf 100755 --- a/advanced/Scripts/speedtestmod/speedtest.sh +++ b/advanced/Scripts/speedtestmod/speedtest.sh @@ -21,13 +21,13 @@ speedtest() { 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 -s $serverid --accept-gdpr --accept-license -f json || /usr/bin/speedtest --accept-gdpr --accept-license -f json fi else if [[ -z "${serverid}" ]]; then /usr/bin/speedtest --json --share --secure else - /usr/bin/speedtest -s $serverid --json --share --secure + /usr/bin/speedtest --server $serverid --json --share --secure || /usr/bin/speedtest --json --share --secure fi fi } @@ -45,19 +45,49 @@ savetest() { 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 +' + local temp_file=$(mktemp) + local json_file="/tmp/speedtest_results" + jq "$rm_empty" "$json_file" > "$temp_file" && mv -f "$temp_file" "$json_file" + rm -f "$temp_file" mv -f /tmp/speedtest_results /var/log/pihole/speedtest.log cp -af /var/log/pihole/speedtest.log /etc/pihole/speedtest.log rm -f "$out" [ "$isp" == "No Internet" ] && exit 1 || exit 0 } -swaptest() { +isAvailable() { if [ -x "$(command -v apt-get)" ]; then - apt-get install -y $1 $2- - elif [ -x "$(command -v dnf)" ]; then - dnf install -y --allowerasing $1 + # 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 + 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 else - yum install -y --allowerasing $1 + 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 fi } @@ -75,10 +105,103 @@ notInstalled() { return 1 } +setTags() { + local path=${1:-} + local name=${2:-} + local branch="${3:-master}" + + if [ ! -z "$path" ]; then + cd "$path" + git fetch origin $branch:refs/remotes/origin/$branch -q + git fetch --tags -f -q + latestTag=$(git describe --tags $(git rev-list --tags --max-count=1)) + fi + if [ ! -z "$name" ]; then + localTag=$(pihole -v | grep "$name" | cut -d ' ' -f 6) + if [ "$localTag" == "HEAD" ]; then + localTag=$(pihole -v | grep "$name" | cut -d ' ' -f 7) + fi + fi +} + +download() { + local path=$1 + local name=$2 + local url=$3 + local src=${4:-} + local branch="${5:-master}" + local dest=$path/$name + + if [ ! -d "$dest" ]; then # replicate + cd "$path" + rm -rf "$name" + git clone --depth=1 -b "$branch" "$url" "$name" + setTags "$name" "${src:-}" "$branch" + if [ ! -z "$src" ]; then + if [[ "$localTag" == *.* ]] && [[ "$localTag" < "$latestTag" ]]; then + latestTag=$localTag + git fetch --unshallow + fi + fi + else # replace + cd "$dest" + if [ ! -z "$src" ]; then + if [ "$url" != "old" ]; then + git config --global --add safe.directory "$dest" + git remote -v | grep -q "old" || git remote rename origin old + git remote -v | grep -q "origin" && git remote remove origin + git remote add -t "$branch" origin "$url" + elif [ -d .git/refs/remotes/old ]; then + git remote remove origin + git remote rename old origin + git clean -ffdx + fi + fi + setTags "$dest" "${src:-}" "$branch" + git reset --hard origin/"$branch" + git checkout -B "$branch" + if git rev-parse --verify "$branch" >/dev/null 2>&1; then + git branch -u "origin/$branch" "$branch" + else + git checkout --track "origin/$branch" + fi + fi + + if [ "$(git rev-parse HEAD)" != "$(git rev-parse $latestTag)" ]; then + git -c advice.detachedHead=false checkout "$latestTag" + fi + cd .. +} + +librespeed() { + if notInstalled golang; then + if grep -q "Raspbian" /etc/os-release; then + if [ ! -f /etc/apt/sources.list.d/testing.list ] && ! grep -q "testing" /etc/apt/sources.list; then + echo "Adding testing repo to sources.list.d" + echo "deb http://archive.raspbian.org/raspbian/ testing main" >/etc/apt/sources.list.d/testing.list + echo "Package: *\nPin: release a=testing\nPin-Priority: 50" >/etc/apt/preferences.d/limit-testing + $PKG_MANAGER update + fi + + $PKG_MANAGER install -y -t testing golang + else + $PKG_MANAGER install -y golang + fi + fi + download /etc/pihole librespeed https://github.com/librespeed/speedtest-cli + cd librespeed + if [ -d out ]; then + rm -rf out + fi + ./build.sh + mv -f out/* /usr/bin/speedtest + chmod +x /usr/bin/speedtest +} + run() { speedtest | jq . >/tmp/speedtest_results || echo "Attempt ${2:-1} Failed!" >/tmp/speedtest_results local stop=$(date -u --rfc-3339='seconds') - if jq -e '.server.id' /tmp/speedtest_results &>/dev/null; then + if jq -e '.server' /tmp/speedtest_results &>/dev/null; then local res=$(/dev/null; then # librespeed + local res=$(/tmp/speedtest_results savetest "$start" "$stop" else - if notInstalled speedtest; then - local PKG_MANAGER=$(command -v apt-get || command -v dnf || command -v yum) + 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..." @@ -141,14 +282,23 @@ run() { 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 - swaptest speedtest speedtest-cli - else + elif ! notInstalled speedtest; then swaptest speedtest-cli speedtest + else + $PKG_MANAGER remove -y speedtest-cli + librespeed fi - run $1 $((${2:-1} + 1)) + run $1 $((${2:-0} + 1)) fi } @@ -158,6 +308,49 @@ main() { exit $? fi + 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 + fi + echo "Running Test..." run $1 # Number of attempts }