pia-speedtest: use a cloudflare mirror instead of the previous complication

This commit is contained in:
Michael Moon
2022-05-21 22:41:58 +08:00
parent 2b7277e678
commit 4cc632f89d

View File

@@ -12,31 +12,37 @@ fi
source "$PIA_CONFIG" source "$PIA_CONFIG"
MIRRORCACHE="$CONFIGDIR/speedtest-cache.xml" # : ${PINGFILE:="http://cloudflaremirrors.com/archlinux/iso/latest/arch/x86_64/airootfs.sfs.sig"}
# : ${TESTFILE:="http://cloudflaremirrors.com/archlinux/iso/latest/arch/x86_64/airootfs.sfs}
function refresh { : ${PINGFILE:="https://cloudflaremirrors.com/debian/dists/stable/main/installer-amd64/current/images/MANIFEST"}
echo "Updating Cache..." : ${TESTFILE:="https://cloudflaremirrors.com/debian/dists/stable/main/installer-amd64/current/images/hd-media/boot.img.gz"}
curl https://api.gentoo.org/mirrors/distfiles.xml > "${MIRRORCACHE}.temp" || return $?
mv "${MIRRORCACHE}.temp" "$MIRRORCACHE"
}
if [ ! -e "$MIRRORCACHE" ] # MIRRORCACHE="$CONFIGDIR/speedtest-cache.xml"
then #
refresh || exit $? # function refresh {
fi # echo "Updating Cache..."
# curl https://api.gentoo.org/mirrors/distfiles.xml > "${MIRRORCACHE}.temp" || return $?
if [ -f "$CONNCACHE" ] # mv "${MIRRORCACHE}.temp" "$MIRRORCACHE"
then # }
COUNTRY="$(jq -r .country "$CONNCACHE")" #
MIRROR="$(xmllint --xpath '/mirrors/mirrorgroup[@country="'"$COUNTRY"'"]/mirror/uri[@protocol="http"]/text()' "$MIRRORCACHE" 2>/dev/null | sort -R | head -n1)" # if [ ! -e "$MIRRORCACHE" ]
[ -n "$MIRROR" ] && echo "Found endpoint-local mirror $MIRROR in $(jq -r .name "$CONNCACHE")" # then
fi # refresh || exit $?
# fi
if [ -z "$MIRROR" ] #
then # if [ -f "$CONNCACHE" ]
MIRROR="$(xmllint --xpath '/mirrors/mirrorgroup[@country!="CN"]/mirror/uri[@protocol="http"]/text()' "$MIRRORCACHE" | sort -R | head -n1)" # then
echo "Using $MIRROR" # COUNTRY="$(jq -r .country "$CONNCACHE")"
fi # MIRROR="$(xmllint --xpath '/mirrors/mirrorgroup[@country="'"$COUNTRY"'"]/mirror/uri[@protocol="http"]/text()' "$MIRRORCACHE" 2>/dev/null | sort -R | head -n1)"
# [ -n "$MIRROR" ] && echo "Found endpoint-local mirror $MIRROR in $(jq -r .name "$CONNCACHE")"
# fi
#
# if [ -z "$MIRROR" ]
# then
# MIRROR="$(xmllint --xpath '/mirrors/mirrorgroup[@country!="CN"]/mirror/uri[@protocol="http"]/text()' "$MIRRORCACHE" | sort -R | head -n1)"
# echo "Using $MIRROR"
# fi
: "${SIZE_MB:=10}" : "${SIZE_MB:=10}"
: "${TIME_S:=10}" : "${TIME_S:=10}"
@@ -45,16 +51,22 @@ fi
# echo "Checking for test file..." # echo "Checking for test file..."
PINGSTART="$(date +%s.%N)" PINGSTART="$(date +%s.%N)"
TESTFILE="$(curl -s -S -m 5 "$MIRROR"/releases/amd64/autobuilds/latest-stage3-amd64-desktop-systemd.txt | tail -n1 | cut -d\ -f1; exit ${PIPESTATUS[0]})"; RET="$?" # TESTFILE="$(curl -s -S -m 5 "$MIRROR"/releases/amd64/autobuilds/latest-stage3-amd64-desktop-systemd.txt | tail -n1 | cut -d\ -f1; exit ${PIPESTATUS[0]})"; RET="$?"
[ "$RET" -ne 0 ] && exit "$RET" curl -s -S -m 5 -r 0-1 "$PINGFILE" > /dev/null || exit $?
PINGEND="$(date +%s.%N)" PINGEND="$(date +%s.%N)"
echo "Ping: ~"$(bc <<< "($PINGEND - $PINGSTART) * 200")"ms. Testing with $MIRROR/releases/amd64/autobuilds/$TESTFILE" echo "Ping: ~"$(bc <<< "($PINGEND - $PINGSTART) * 333")"ms."
DLSTART="$(date +%s.%N)" DLSTART="$(date +%s.%N)"
DLSIZE="$(curl -m "$(bc <<< "$TIME_S + $PINGEND - $PINGSTART")" -r 0-"$SIZE" "$MIRROR"/releases/amd64/autobuilds/"$TESTFILE" | wc -c; exit ${PIPESTATUS[0]})"; RET="$?" DLSIZE="$(curl -m "$(bc <<< "$TIME_S + $PINGEND - $PINGSTART")" -r 0-"$SIZE" -Y "$(( "$SIZE" / "$TIME_S" ))" -y "$TIME_S" "$TESTFILE" | head -c "$SIZE" | wc -c; exit ${PIPESTATUS[0]})"; RET="$?"
DLEND="$(date +%s.%N)" DLEND="$(date +%s.%N)"
# ignore curl: (23) Failure writing output to destination
if [ "$RET" -eq 23 ]
then
RET=0
fi
echo "$(bc <<< "$DLSIZE / 1048576")MB in "$(bc <<< "($DLEND - $DLSTART) * 1000")"ms = "$(bc <<< "$DLSIZE / ($DLEND - $DLSTART) / 131072")"Mbit/s" echo "$(bc <<< "$DLSIZE / 1048576")MB in "$(bc <<< "($DLEND - $DLSTART) * 1000")"ms = "$(bc <<< "$DLSIZE / ($DLEND - $DLSTART) / 131072")"Mbit/s"
if [ "$DLSIZE" -lt "$SIZE" ] if [ "$DLSIZE" -lt "$SIZE" ]
@@ -62,12 +74,12 @@ then
RET=12 RET=12
fi fi
if [ "$RET" -eq 0 ] # if [ "$RET" -eq 0 ]
then # then
if find "$MIRRORCACHE" -mtime -3 -exec false {} + # if find "$MIRRORCACHE" -mtime -3 -exec false {} +
then # then
refresh # refresh
fi # fi
fi # fi
exit "$RET" exit "$RET"