mihomo-kit/tools/nikki-mirror.sh

104 lines
4.6 KiB
Bash
Executable File

#!/bin/sh
# Mirror an OpenWrt-nikki release into the kit repo's releases: the router installs
# nikki from there (router/mihomo-kit.sh nikki_feed), GitHub is often unreachable.
# GITEA_TOKEN=... tools/nikki-mirror.sh [v1.26.2|latest] [arch...]
# Each nikki_<arch>-openwrt-24.10.tar.gz is a signed opkg feed; it is repacked without
# mihomo-alpha and the zh translations (less RAM on the router, Packages and its
# signature unchanged) and uploaded to the release nikki-<version>, made if missing.
# No arch = all of the release. Assets already there are skipped.
# Then set NIKKI_VERSION in router/mihomo-kit.sh and push the kit.
# Needs curl, jq, tar, gzip.
set -eu
ver=${1:-latest}
[ $# -gt 0 ] && shift
: "${GITEA_TOKEN:?set GITEA_TOKEN (Settings -> Applications, scope write:repository)}"
GITEA=${GITEA:-https://git.sam-hub.net}
REPO=${REPO:-sam/mihomo-kit}
UPSTREAM=nikkinikki-org/OpenWrt-nikki
BRANCH=openwrt-24.10
cd "$(dirname "$0")"
work=$(pwd)/nikki-work
rm -rf "$work"
mkdir -p "$work"
trap 'rm -rf "$work"' EXIT
if [ "$ver" = latest ]; then
api=https://api.github.com/repos/$UPSTREAM/releases/latest
else
api=https://api.github.com/repos/$UPSTREAM/releases/tags/$ver
fi
curl -fsSL "$api" > "$work/upstream.json"
ver=$(jq -r .tag_name "$work/upstream.json")
tag=nikki-$ver
echo "upstream $ver -> $GITEA/$REPO release $tag"
# the token through a config file, not argv (ps); timeouts: an upload may stall
umask 077
printf 'header = "Authorization: token %s"\n' "$GITEA_TOKEN" > "$work/curlrc"
gitea() { curl -K "$work/curlrc" -fsS --connect-timeout 20 "$@"; }
api=$GITEA/api/v1/repos/$REPO
if ! gitea -m 60 "$api/releases/tags/$tag" > "$work/release.json" 2>/dev/null; then
jq -n --arg t "$tag" --arg v "$ver" '{tag_name: $t, name: $t, target_commitish: "main",
body: ("Зеркало https://github.com/nikkinikki-org/OpenWrt-nikki/releases/tag/" + $v +
" для установки nikki на роутер (без mihomo-alpha и переводов zh).")}' |
gitea -m 60 -X POST -H 'Content-Type: application/json' -d @- \
"$api/releases" > "$work/release.json"
echo "created release $tag"
fi
id=$(jq -r .id "$work/release.json")
have=$(jq -r '.assets[]?.name' "$work/release.json")
jq -r --arg b "$BRANCH" '.assets[] | select(.name | endswith("-" + $b + ".tar.gz")) |
"\(.name) \(.browser_download_url)"' "$work/upstream.json" > "$work/list"
if [ $# -gt 0 ]; then
for a in "$@"; do grep "^nikki_$a-$BRANCH.tar.gz " "$work/list" || echo "warning: no bundle for $a" >&2; done \
> "$work/list.sel"
mv "$work/list.sel" "$work/list"
fi
[ -s "$work/list" ] || { echo "nothing to mirror" >&2; exit 1; }
while read -r name url; do
if printf '%s\n' "$have" | grep -qx "$name"; then
echo "$name: already there"
continue
fi
echo "$name: downloading"
d=$work/b
rm -rf "$d"
mkdir -p "$d"
curl -fSL --connect-timeout 20 -m 600 --retry 3 --progress-bar "$url" | tar xzf - -C "$d"
[ -f "$d/Packages.gz" ] && [ -f "$d/Packages.sig" ] || { echo "$name: not a feed" >&2; exit 1; }
rm -f "$d"/mihomo-alpha_*.ipk "$d"/luci-i18n-nikki-zh-*.ipk
# ustar, no owner names: busybox tar on the router
(cd "$d" && tar --format ustar --owner 0 --group 0 -cf - .) | gzip -9 > "$work/$name"
size=$(wc -c < "$work/$name")
ok=
for try in 1 2 3; do
echo "$name: uploading $(du -h "$work/$name" | cut -f1) (try $try)"
# progress bar on the terminal, then the average speed
curl -K "$work/curlrc" -fS --connect-timeout 20 -m 600 --progress-bar -o /dev/null \
-w '%{time_total} %{speed_upload}\n' \
-X POST -F "attachment=@$work/$name" "$api/releases/$id/assets?name=$name" |
awk -v n="$name" '{printf "%s: sent in %ds, %.2f MB/s\n", n, $1, $2 / 1048576}' || true
# what the server has now: right size = done, anything else = remove and retry
gitea -m 60 "$api/releases/$id/assets" > "$work/assets.json" || continue
jq -r --arg n "$name" --argjson s "$size" '.[] | select(.name == $n) |
"\(.id) \(if .size == $s then "ok" else "bad" end)"' "$work/assets.json" > "$work/same"
if grep -q ' ok$' "$work/same"; then
# a stalled try may have landed too: keep one copy
grep ' ok$' "$work/same" | tail -n +2 | while read -r a _; do
gitea -m 60 -X DELETE "$api/releases/$id/assets/$a"; done
ok=1
break
fi
while read -r a _; do gitea -m 60 -X DELETE "$api/releases/$id/assets/$a"; done < "$work/same"
done
rm -f "$work/$name"
[ -n "$ok" ] || { echo "$name: upload failed 3 times, run again later" >&2; exit 1; }
done < "$work/list"
echo "done: $GITEA/$REPO/releases/tag/$tag"
grep -q "^NIKKI_VERSION=$ver\$" ../router/mihomo-kit.sh ||
echo "now set NIKKI_VERSION=$ver in router/mihomo-kit.sh and push the kit"