diff --git a/packages/termux-tools/build.sh b/packages/termux-tools/build.sh index f7aafc5df75d84..316a36c5d05df5 100644 --- a/packages/termux-tools/build.sh +++ b/packages/termux-tools/build.sh @@ -2,7 +2,7 @@ TERMUX_PKG_HOMEPAGE=https://termux.com/ TERMUX_PKG_DESCRIPTION="Basic system tools for Termux" TERMUX_PKG_LICENSE="GPL-3.0" TERMUX_PKG_MAINTAINER="@termux" -TERMUX_PKG_VERSION=0.115 +TERMUX_PKG_VERSION=0.116 TERMUX_PKG_SKIP_SRC_EXTRACT=true TERMUX_PKG_PLATFORM_INDEPENDENT=true TERMUX_PKG_ESSENTIAL=true diff --git a/packages/termux-tools/pkg b/packages/termux-tools/pkg index 5e7eed9a2a53f6..c3a41039991c8a 100755 --- a/packages/termux-tools/pkg +++ b/packages/termux-tools/pkg @@ -1,5 +1,7 @@ #!/bin/bash -set -e -u +set -eu + +USER_AGENT='Termux-PKG/1.0 mirror-checker (termux-tools @PACKAGE_VERSION@) Termux (@TERMUX_APP_PACKAGE@; install-prefix:@TERMUX_PREFIX@)' show_help() { local cache_size @@ -13,7 +15,7 @@ show_help() { echo ' cache.' echo echo ' clean - Remove all packages from .deb package cache.' - [ -n "$cache_size" ] && echo " Using ${cache_size} now." + [ -n "$cache_size" ] && echo " Using $cache_size now." echo echo ' files - Show all files installed by packages.' echo @@ -40,6 +42,32 @@ show_help() { exit 1 } +check_mirror() { + local mirror="${1%/}" + local timeout="${2-5}" + + timeout "$((timeout + 1))" curl \ + --head \ + --fail \ + --connect-timeout "$timeout" \ + --location \ + --user-agent "$USER_AGENT" \ + "$mirror/dists/stable/Release" >/dev/null 2>&1 +} + +hostname() { + echo "$1" | awk -F'[/:]' '{print $4}' +} + +last_modified() { + local mtime + local now + + mtime=$(date -r "$1" '+%s') + now=$(date '+%s') + echo $((mtime - now)) +} + select_mirror() { local main_repo="https://termux.org/packages" @@ -51,28 +79,23 @@ select_mirror() { mirrors[13]="https://grimler.se/termux-packages-24" local current_mirror - current_mirror=$(grep -P "^\s*deb\s+" @TERMUX_PREFIX@/etc/apt/sources.list | grep -oP 'https?://[a-z0-9/._-]+') + current_mirror=$(grep -P "^\s*deb\s+" @TERMUX_PREFIX@/etc/apt/sources.list | grep -oP 'https?://[^\s]+') # Do not update mirror if: - # * Uses .cn domain - specific to Chinese users. # * If $TERMUX_PKG_NO_MIRROR_SELECT was set. - if [ -n "${TERMUX_PKG_NO_MIRROR_SELECT-}" ] || grep -qP '.+\.cn/' <(echo "${current_mirror-x}"); then + # * Uses .cn domain - specific to Chinese users. + if [ -n "${TERMUX_PKG_NO_MIRROR_SELECT-}" ] || [[ "$(hostname "$current_mirror")" == *".cn" ]]; then return fi # Mirrors are rotated if 6 hours timeout has been passed or mirror is no longer accessible. - if [ -n "$(find @TERMUX_CACHE_DIR@/apt/pkgcache.bin -mmin -360 2>/dev/null)" ]; then - if [ -n "${current_mirror}" ]; then + local pkgcache="@TERMUX_CACHE_DIR@/apt/pkgcache.bin" + if (( $(last_modified "$pkgcache") <= 6 * 3600 )); then + if [ -n "$current_mirror" ]; then echo -n "Checking availability of current mirror: " - if timeout 6 curl \ - --head \ - --fail \ - --connect-timeout 5 \ - --location \ - --user-agent 'Termux-PKG/1.0 mirror-checker (termux-tools @PACKAGE_VERSION@) Termux (@TERMUX_APP_PACKAGE@; install-prefix:@TERMUX_PREFIX@)' \ - "${current_mirror%/}/dists/stable/Release" >/dev/null 2>&1; then - echo "ok" - return + if check_mirror "$current_mirror"; then + echo "ok" + return else echo "bad" fi @@ -84,15 +107,9 @@ select_mirror() { local w total_mirror_weight=0 for w in "${!mirrors[@]}"; do echo -n "[*] ${mirrors[$w]}: " - if timeout 6 curl \ - --head \ - --fail \ - --connect-timeout 5 \ - --location \ - --user-agent 'Termux-PKG/1.0 mirror-checker (termux-tools @PACKAGE_VERSION@) Termux (@TERMUX_APP_PACKAGE@; install-prefix:@TERMUX_PREFIX@)' \ - "${mirrors[$w]%/}/dists/stable/Release" >/dev/null 2>&1; then - echo "ok" - total_mirror_weight=$((total_mirror_weight + w)) + if check_mirror "${mirrors[$w]}"; then + echo "ok" + total_mirror_weight=$((total_mirror_weight + w)) else echo "bad" unset "mirrors[$w]" @@ -116,16 +133,17 @@ select_mirror() { done fi - if [ -n "${selected_mirror}" ]; then - echo "deb ${selected_mirror}/ stable main" > @TERMUX_PREFIX@/etc/apt/sources.list + if [ -n "$selected_mirror" ]; then + echo "deb $selected_mirror/ stable main" > @TERMUX_PREFIX@/etc/apt/sources.list else - echo "Using fallback mirror: ${main_repo}" - echo "deb ${main_repo}/ stable main" > @TERMUX_PREFIX@/etc/apt/sources.list + echo "Using fallback mirror: $main_repo" + echo "deb $main_repo/ stable main" > @TERMUX_PREFIX@/etc/apt/sources.list fi } update_apt_cache() { - if [ -z "$(find @TERMUX_CACHE_DIR@/apt/pkgcache.bin -mmin -20 2>/dev/null)" ]; then + local pkgcache="@TERMUX_CACHE_DIR@/apt/pkgcache.bin" + if (( $(last_modified "$pkgcache") > 1200 )); then apt update fi }