2020-07-31 13:18:43 +02:00
|
|
|
#!/bin/bash
|
2021-04-05 00:12:06 +02:00
|
|
|
set -eu
|
|
|
|
|
|
|
|
USER_AGENT='Termux-PKG/1.0 mirror-checker (termux-tools @PACKAGE_VERSION@) Termux (@TERMUX_APP_PACKAGE@; install-prefix:@TERMUX_PREFIX@)'
|
2017-02-17 23:13:49 +01:00
|
|
|
|
|
|
|
show_help() {
|
2020-08-01 01:47:03 +02:00
|
|
|
local cache_size
|
2020-10-30 21:15:19 +01:00
|
|
|
cache_size=$(du -sh @TERMUX_CACHE_DIR@/apt/archives 2>/dev/null | cut -f1)
|
2020-08-01 01:47:03 +02:00
|
|
|
|
2017-10-01 21:42:37 +02:00
|
|
|
echo 'Usage: pkg command [arguments]'
|
2020-08-01 01:47:03 +02:00
|
|
|
echo
|
2017-02-17 23:13:49 +01:00
|
|
|
echo 'A tool for managing packages. Commands:'
|
2020-08-01 01:47:03 +02:00
|
|
|
echo
|
|
|
|
echo ' autoclean - Remove all outdated packages from .deb package'
|
|
|
|
echo ' cache.'
|
|
|
|
echo
|
|
|
|
echo ' clean - Remove all packages from .deb package cache.'
|
2021-04-05 00:12:06 +02:00
|
|
|
[ -n "$cache_size" ] && echo " Using $cache_size now."
|
2020-08-01 01:47:03 +02:00
|
|
|
echo
|
|
|
|
echo ' files <packages> - Show all files installed by packages.'
|
|
|
|
echo
|
|
|
|
echo ' install <packages> - Install specified packages.'
|
|
|
|
echo
|
|
|
|
echo ' list-all - List all packages available in repositories.'
|
|
|
|
echo
|
|
|
|
echo ' list-installed - List installed packages.'
|
|
|
|
echo
|
|
|
|
echo ' reinstall <packages> - Reinstall specified installed packages at the'
|
|
|
|
echo ' latest version.'
|
|
|
|
echo
|
|
|
|
echo ' search <query> - Search package by query, for example by name or'
|
|
|
|
echo ' description part.'
|
|
|
|
echo
|
|
|
|
echo ' show <packages> - Show basic metadata, such as dependencies.'
|
|
|
|
echo
|
|
|
|
echo ' uninstall <packages> - Uninstall specified packages. Configuration files'
|
|
|
|
echo ' will be left intact.'
|
|
|
|
echo
|
|
|
|
echo ' upgrade - Upgrade all installed packages to the latest'
|
|
|
|
echo ' version.'
|
|
|
|
echo
|
2017-02-17 23:13:49 +01:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2021-04-05 00:12:06 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-05 03:32:59 +02:00
|
|
|
hostname() {
|
|
|
|
echo "$1" | awk -F'[/:]' '{print $4}'
|
|
|
|
}
|
|
|
|
|
2021-04-05 01:51:42 +02:00
|
|
|
last_modified() {
|
|
|
|
local mtime
|
|
|
|
local now
|
|
|
|
|
|
|
|
mtime=$(date -r "$1" '+%s')
|
|
|
|
now=$(date '+%s')
|
2021-05-19 13:32:58 +02:00
|
|
|
echo $((now - mtime))
|
2021-04-05 01:51:42 +02:00
|
|
|
}
|
|
|
|
|
2020-07-31 13:18:43 +02:00
|
|
|
select_mirror() {
|
2021-06-03 00:46:49 +02:00
|
|
|
local main_repo="https://packages.termux.org/apt/termux-main"
|
2020-08-01 19:25:19 +02:00
|
|
|
|
|
|
|
declare -A mirrors
|
2021-06-20 14:54:10 +02:00
|
|
|
mirrors[35]="$main_repo"
|
2021-06-25 16:56:31 +02:00
|
|
|
mirrors[18]="https://deb.kcubeterm.me/termux-main"
|
2021-06-20 14:54:10 +02:00
|
|
|
mirrors[17]="https://grimler.se/termux-packages-24"
|
2021-06-25 16:56:31 +02:00
|
|
|
mirrors[16]="https://termux.mentality.rip/termux-main"
|
|
|
|
mirrors[14]="https://termux.librehat.com/apt/termux-main"
|
2020-07-31 13:18:43 +02:00
|
|
|
|
|
|
|
local current_mirror
|
2021-05-19 13:54:08 +02:00
|
|
|
current_mirror=$(grep -m 1 -P "^\s*deb\s+" @TERMUX_PREFIX@/etc/apt/sources.list | grep -oP 'https?://[^\s]+')
|
2020-07-31 13:18:43 +02:00
|
|
|
|
|
|
|
# Do not update mirror if:
|
|
|
|
# * If $TERMUX_PKG_NO_MIRROR_SELECT was set.
|
2021-04-05 03:32:59 +02:00
|
|
|
# * Uses .cn domain - specific to Chinese users.
|
|
|
|
if [ -n "${TERMUX_PKG_NO_MIRROR_SELECT-}" ] || [[ "$(hostname "$current_mirror")" == *".cn" ]]; then
|
2020-07-31 13:18:43 +02:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2020-08-01 19:25:19 +02:00
|
|
|
# Mirrors are rotated if 6 hours timeout has been passed or mirror is no longer accessible.
|
2021-04-05 01:51:42 +02:00
|
|
|
local pkgcache="@TERMUX_CACHE_DIR@/apt/pkgcache.bin"
|
|
|
|
if (( $(last_modified "$pkgcache") <= 6 * 3600 )); then
|
2021-04-05 00:12:06 +02:00
|
|
|
if [ -n "$current_mirror" ]; then
|
2020-08-02 00:50:34 +02:00
|
|
|
echo -n "Checking availability of current mirror: "
|
2021-04-05 00:12:06 +02:00
|
|
|
if check_mirror "$current_mirror"; then
|
|
|
|
echo "ok"
|
|
|
|
return
|
2020-08-02 00:50:34 +02:00
|
|
|
else
|
|
|
|
echo "bad"
|
|
|
|
fi
|
2020-07-31 13:18:43 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-08-01 19:25:19 +02:00
|
|
|
# Test mirror availability, remove unaccessible mirrors from list.
|
|
|
|
echo "Testing the available mirrors:"
|
|
|
|
local w total_mirror_weight=0
|
|
|
|
for w in "${!mirrors[@]}"; do
|
|
|
|
echo -n "[*] ${mirrors[$w]}: "
|
2021-04-05 00:12:06 +02:00
|
|
|
if check_mirror "${mirrors[$w]}"; then
|
|
|
|
echo "ok"
|
|
|
|
total_mirror_weight=$((total_mirror_weight + w))
|
2020-08-01 19:25:19 +02:00
|
|
|
else
|
|
|
|
echo "bad"
|
2020-08-04 00:39:12 +02:00
|
|
|
unset "mirrors[$w]"
|
2020-07-31 13:18:43 +02:00
|
|
|
fi
|
|
|
|
done
|
2020-08-01 19:25:19 +02:00
|
|
|
unset w
|
|
|
|
|
|
|
|
# Weight-based mirror selection.
|
|
|
|
local selected_mirror=""
|
|
|
|
if ((total_mirror_weight > 0)); then
|
|
|
|
local w random_weight calc_weight=0
|
|
|
|
random_weight=$((RANDOM % total_mirror_weight + 1))
|
|
|
|
for w in $(echo "${!mirrors[@]}" | tr ' ' '\n' | sort -n); do
|
|
|
|
calc_weight=$((calc_weight + w))
|
|
|
|
|
|
|
|
if ((calc_weight >= random_weight)); then
|
|
|
|
echo "Picking mirror: ${mirrors[$w]}"
|
|
|
|
selected_mirror="${mirrors[$w]}"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2020-07-31 13:18:43 +02:00
|
|
|
|
2021-04-05 00:12:06 +02:00
|
|
|
if [ -n "$selected_mirror" ]; then
|
|
|
|
echo "deb $selected_mirror/ stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
|
2020-07-31 13:18:43 +02:00
|
|
|
else
|
2021-04-05 00:12:06 +02:00
|
|
|
echo "Using fallback mirror: $main_repo"
|
|
|
|
echo "deb $main_repo/ stable main" > @TERMUX_PREFIX@/etc/apt/sources.list
|
2020-07-31 13:18:43 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
update_apt_cache() {
|
2021-05-19 15:10:51 +02:00
|
|
|
local current_host
|
|
|
|
current_host=$(sed -nE -e 's|^\s*deb\s+https?://(.+)\s+stable\s+main$|\1|p' @TERMUX_PREFIX@/etc/apt/sources.list | head -n 1)
|
|
|
|
|
|
|
|
if [ -z "$current_host" ]; then
|
|
|
|
# No primary repositories configured?
|
|
|
|
apt update
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
local metadata_file
|
|
|
|
metadata_file=$(
|
|
|
|
list_prefix=$(echo "$current_host" | sed 's|/|_|g')
|
|
|
|
arch=$(dpkg --print-architecture)
|
|
|
|
echo "@TERMUX_PREFIX@/var/lib/apt/lists/${list_prefix}_dists_stable_main_binary-${arch}_Packages" | sed 's|__|_|g'
|
|
|
|
)
|
|
|
|
|
|
|
|
if [ ! -e "@TERMUX_CACHE_DIR@/apt/pkgcache.bin" ] || [ ! -e "$metadata_file" ]; then
|
|
|
|
apt update
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2021-05-19 12:59:31 +02:00
|
|
|
local cache_modified
|
2021-05-19 15:10:51 +02:00
|
|
|
cache_modified=$(last_modified "@TERMUX_CACHE_DIR@/apt/pkgcache.bin")
|
2021-05-19 12:59:31 +02:00
|
|
|
|
2021-05-19 15:10:51 +02:00
|
|
|
local sources_modified
|
|
|
|
sources_modified=$(last_modified "@TERMUX_PREFIX@/etc/apt/sources.list")
|
2021-05-19 12:59:31 +02:00
|
|
|
|
2021-05-19 15:10:51 +02:00
|
|
|
if (( sources_modified <= cache_modified )) || (( cache_modified > 1200 )); then
|
2019-11-10 15:29:05 +01:00
|
|
|
apt update
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ $# = 0 ]; then
|
|
|
|
show_help
|
|
|
|
fi
|
|
|
|
|
2017-02-17 23:13:49 +01:00
|
|
|
CMD="$1"
|
|
|
|
shift 1
|
|
|
|
|
|
|
|
case "$CMD" in
|
2019-05-06 22:22:47 +02:00
|
|
|
f*) dpkg -L "$@";;
|
2017-03-10 15:22:44 +01:00
|
|
|
h*) show_help;;
|
2021-03-10 06:48:07 +01:00
|
|
|
sh*|inf*) apt show "$@";;
|
2020-07-31 13:18:43 +02:00
|
|
|
add|i*)
|
|
|
|
select_mirror
|
|
|
|
update_apt_cache
|
|
|
|
apt install "$@"
|
|
|
|
;;
|
2020-08-01 14:21:10 +02:00
|
|
|
autoc*) apt autoclean;;
|
2020-07-31 18:31:42 +02:00
|
|
|
cl*) apt clean;;
|
2019-05-06 22:22:47 +02:00
|
|
|
list-a*) apt list "$@";;
|
|
|
|
list-i*) apt list --installed "$@";;
|
2019-11-13 15:38:40 +01:00
|
|
|
rei*) apt install --reinstall "$@";;
|
2020-07-31 13:18:43 +02:00
|
|
|
se*)
|
|
|
|
select_mirror
|
|
|
|
update_apt_cache
|
|
|
|
apt search "$@"
|
|
|
|
;;
|
2019-11-13 15:38:40 +01:00
|
|
|
un*|rem*|rm|del*) apt remove "$@";;
|
2020-07-31 13:18:43 +02:00
|
|
|
up*)
|
|
|
|
select_mirror
|
|
|
|
apt update
|
|
|
|
apt full-upgrade "$@"
|
|
|
|
;;
|
2019-11-10 15:29:05 +01:00
|
|
|
*) echo "Unknown command: '$CMD' (run 'pkg help' for usage information)"; exit 1;;
|
2017-02-17 23:13:49 +01:00
|
|
|
esac
|