diff --git a/cn/lib/mirror.sh b/cn/lib/mirror.sh index 3108a2b7..3db2e965 100644 --- a/cn/lib/mirror.sh +++ b/cn/lib/mirror.sh @@ -24,23 +24,27 @@ cn_mirror_ids_by_region() { # Probe one mirror: prints " " cn_mirror_probe() { local url="$1" - local speed ttfb lastsync age now + local out speed=0 ttfb=0 lastsync age="unknown" now - read -r speed ttfb < <(curl -sS -o /dev/null -m 12 --connect-timeout 5 \ - -w '%{speed_download} %{time_starttransfer}\n' \ - "$url/core/os/x86_64/core.db" 2>/dev/null) || true - speed="${speed%%.*}" - [[ -z $speed ]] && speed=0 - - age="unknown" - lastsync=$(curl -sS -m 5 --connect-timeout 5 "$url/lastsync" 2>/dev/null | tr -cd '0-9') - if [[ -n $lastsync ]]; then - now=$(date +%s) - age=$((now - lastsync)) - (( age > 86400 )) && age="stale" + if out=$(curl -sSf -o /dev/null -m 12 --connect-timeout 5 \ + -w '%{speed_download} %{time_starttransfer}' \ + "$url/core/os/x86_64/core.db" 2>/dev/null); then + read -r speed ttfb <<<"$out" + speed="${speed%%.*}" fi - echo "$speed ${ttfb:-0} $age" + if lastsync=$(curl -sSf -m 5 --connect-timeout 5 "$url/lastsync" 2>/dev/null); then + lastsync=$(tr -cd '0-9' <<<"$lastsync") + if [[ -n $lastsync ]]; then + now=$(date +%s) + age=$((now - lastsync)) + if (( age > 86400 )); then + age="stale" + fi + fi + fi + + echo "$speed $ttfb $age" } # Rank ids by probe speed, excluding dead (speed 0) and stale mirrors