这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/termux-tools/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 48 additions & 30 deletions packages/termux-tools/pkg
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 <packages> - Show all files installed by packages.'
echo
Expand All @@ -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"

Expand All @@ -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
Expand All @@ -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]"
Expand All @@ -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
}
Expand Down