Files
proxy-suite-flake/windows/tools/lib.sh
T
faumarayandClaude Opus 5.5 5cf3a237e6 windows: AmneziaWG as a system-wide VPN
Each profile without asOutbound is a tunnel service of its own,
AmneziaWGTunnel$<interface>, as amneziawg-windows's client installs one:
amneziawg-go on Wintun, its routes and DNS, and for one peer that takes
everything its firewall blocking what would leave outside the tunnel.
It runs awg-tunnel.exe, amneziawg-windows built from its pinned commit
with a main of ours (its Run(), its log in a file) instead of its
tunnel.dll, beside Wintun's signed DLL (tools/build-amneziawg.sh).

The service writes each profile's .conf below its state root, registers
the tunnel (unrestricted service SID, which its firewall exempts it by),
starts the autostart ones with itself, and removes the tunnels of
profiles gone, and all of them on uninstall. proxy-ctl awg
list|on|off|log, and the app's AmneziaWG page: profiles (a system-wide
one switched on and off, its log), one added from a vpn:// link or a
.conf, or removed.

tools/lib.sh: clone_commit, and fetch_pinned, which fetches in pieces a
download that stalls (some networks cut TLS after about 16 KB, as
wintun.net's here).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 17:23:11 +03:00

91 lines
3.9 KiB
Bash

# Shared by tools/build-*.sh (sourced).
# clone_tag URL TAG DIR: a shallow clone of TAG into DIR, tried three times: a transfer that
# stalls (GitHub's sometimes do) is given up after a minute, a connection that hangs by curl's
# own timeout; a slow one is waited for. With SRC_CACHE set, a clone made is kept there and
# the next build copies it rather than fetching it again (the scripts check its commit either
# way).
clone_tag() {
local url=$1 tag=$2 dir=$3 try cached=""
if [ -n "${SRC_CACHE:-}" ]; then
cached="$SRC_CACHE/$(basename "$url")@$tag"
if [ -d "$cached/.git" ]; then
# Its files as its commit has them, whatever happened to the copy since.
rm -rf "$dir" && cp -a "$cached" "$dir" && git -C "$dir" reset -q --hard && git -C "$dir" clean -qfdx && return 0
fi
fi
for try in 1 2 3; do
rm -rf "$dir"
if git -c advice.detachedHead=false -c http.lowSpeedLimit=1000 -c http.lowSpeedTime=60 \
clone -q --depth 1 --branch "$tag" "$url" "$dir"; then
if [ -n "$cached" ]; then
mkdir -p "$SRC_CACHE" && rm -rf "$cached.tmp" && cp -a "$dir" "$cached.tmp" && mv "$cached.tmp" "$cached"
fi
return 0
fi
echo "cloning $url ($tag) failed, try $try of 3" >&2
sleep 5
done
return 1
}
# clone_commit URL COMMIT DIR: COMMIT alone, for a project without release tags; cached and
# tried again as clone_tag is.
clone_commit() {
local url=$1 commit=$2 dir=$3 try cached=""
if [ -n "${SRC_CACHE:-}" ]; then
cached="$SRC_CACHE/$(basename "$url")@$commit"
if [ -d "$cached/.git" ]; then
rm -rf "$dir" && cp -a "$cached" "$dir" && git -C "$dir" reset -q --hard && git -C "$dir" clean -qfdx && return 0
fi
fi
for try in 1 2 3; do
rm -rf "$dir" && mkdir -p "$dir"
if git -C "$dir" init -q && git -C "$dir" remote add origin "$url" &&
git -C "$dir" -c http.lowSpeedLimit=1000 -c http.lowSpeedTime=60 fetch -q --depth 1 origin "$commit" &&
git -C "$dir" -c advice.detachedHead=false checkout -q FETCH_HEAD; then
if [ -n "$cached" ]; then
mkdir -p "$SRC_CACHE" && rm -rf "$cached.tmp" && cp -a "$dir" "$cached.tmp" && mv "$cached.tmp" "$cached"
fi
return 0
fi
echo "fetching $url ($commit) failed, try $try of 3" >&2
sleep 5
done
return 1
}
# fetch_pinned URL SHA256 FILE: a download, checked by its hash; kept in SRC_CACHE.
fetch_pinned() {
local url=$1 sha=$2 out=$3 cached=""
if [ -n "${SRC_CACHE:-}" ]; then
cached="$SRC_CACHE/$sha"
if [ -f "$cached" ] && echo "$sha $cached" | sha256sum -c --quiet 2>/dev/null; then
cp "$cached" "$out" && return 0
fi
fi
# Whole, unless it stalls: some networks cut TLS to some hosts after about 16 KB (as
# zapret2's cutoff option knows). Then in ranges below that, each on a connection of its own.
if ! curl -fsSL --connect-timeout 30 --speed-limit 1 --speed-time 15 --max-time 900 --retry 2 -o "$out" "$url"; then
echo "$url stalled; fetching it in pieces" >&2
local size from=0 step=12000 tries=0
size=$(curl -fsSIL --connect-timeout 30 --max-time 60 "$url" | tr -d '\r' | awk 'tolower($1) == "content-length:" { n = $2 } END { print n }')
[[ "$size" =~ ^[0-9]+$ ]] && [ "$size" -gt 0 ] || { echo "$url: no size to fetch it in pieces by" >&2; return 1; }
: >"$out"
while [ "$from" -lt "$size" ]; do
if curl -fsSL --connect-timeout 30 --max-time 60 -r "$from-$((from + step - 1))" "$url" >>"$out.piece" 2>/dev/null &&
[ "$(stat -c %s "$out.piece")" -gt 0 ]; then
cat "$out.piece" >>"$out"
from=$(stat -c %s "$out")
tries=0
else
tries=$((tries + 1))
[ "$tries" -lt 5 ] || { rm -f "$out.piece"; echo "$url: a piece failed five times" >&2; return 1; }
fi
rm -f "$out.piece"
done
fi
echo "$sha $out" | sha256sum -c --quiet || { echo "$url: not the pinned file" >&2; return 1; }
if [ -n "$cached" ]; then mkdir -p "$SRC_CACHE" && cp "$out" "$cached"; fi
}