fix: update checker — lightweight API check with full-file fallback

Primary check uses GitHub API (Accept: application/vnd.github.sha)
for a ~40-byte response instead of downloading the full ~530KB file.
Falls back to full-file hash comparison if API is unreachable.

- Hex validation prevents false positives from proxy/CDN HTML responses
- All badge writes guarded with 2>/dev/null (no terminal noise)
- Separate connect-timeout (5s) from max-time for fast failure detection
- SHA baseline reset on install/update for clean re-establishment
This commit is contained in:
SamNet-dev
2026-02-10 22:09:40 -06:00
parent 34dc53615b
commit 8bf2f8c640

View File

@@ -8772,45 +8772,73 @@ SVCEOF
# Background update check (non-blocking) # Background update check (non-blocking)
{ {
local _utmp="/tmp/.conduit_uc_$$" local _badge="/tmp/.conduit_update_available"
local _uurl="https://raw.githubusercontent.com/SamNet-dev/conduit-manager/main/conduit.sh" local _sha_file="$INSTALL_DIR/.update_sha"
local _hashcmd="md5sum" local _done=false
command -v md5sum &>/dev/null || _hashcmd="sha256sum"
command -v $_hashcmd &>/dev/null || { rm -f "$_utmp"; exit 0; } # Method 1: Lightweight GitHub API check (~40B vs ~530KB full file)
if curl -fsSL --max-time 10 -o "$_utmp" "$_uurl" 2>/dev/null && [ -s "$_utmp" ]; then local _api_resp
local _remote_hash=$($_hashcmd "$_utmp" | cut -d' ' -f1) _api_resp=$(curl -fsSL --connect-timeout 5 --max-time 10 \
local _stored_hash="" "https://api.github.com/repos/SamNet-dev/conduit-manager/commits/main" \
[ -f "$INSTALL_DIR/.update_hash" ] && _stored_hash=$(<"$INSTALL_DIR/.update_hash") -H "Accept: application/vnd.github.sha" 2>/dev/null) || true
if [ -z "$_stored_hash" ]; then if [ -n "$_api_resp" ] && [ ${#_api_resp} -ge 40 ]; then
# No stored hash — check version to distinguish fresh vs old install local _remote_sha="${_api_resp:0:40}"
local _rv=$(grep -m1 '^VERSION=' "$_utmp" | cut -d'"' -f2) # Validate response is hex (not a JSON error or HTML from a proxy)
if [ -n "$_rv" ] && [ "$_rv" = "$VERSION" ]; then case "$_remote_sha" in *[!a-f0-9]*)
# Same version = likely fresh install — save baseline, no badge # Not a valid SHA, skip to Method 2
echo "$_remote_hash" > "$INSTALL_DIR/.update_hash" 2>/dev/null || true ;;
rm -f /tmp/.conduit_update_available *)
local _stored_sha=""
[ -f "$_sha_file" ] && _stored_sha=$(<"$_sha_file")
if [ -z "$_stored_sha" ]; then
echo "$_remote_sha" > "$_sha_file" 2>/dev/null || true
rm -f "$_badge" 2>/dev/null
elif [ "$_remote_sha" != "$_stored_sha" ]; then
echo "new" > "$_badge" 2>/dev/null
else else
# Different version = old install without hash — show badge rm -f "$_badge" 2>/dev/null
if [ -n "$_rv" ]; then
echo "v${_rv}" > /tmp/.conduit_update_available
else
echo "new" > /tmp/.conduit_update_available
fi
fi fi
rm -f "$_utmp" _done=true
exit 0 ;;
fi esac
if [ "$_remote_hash" != "$_stored_hash" ]; then fi
local _rv=$(grep -m1 '^VERSION=' "$_utmp" | cut -d'"' -f2)
if [ -n "$_rv" ] && [ "$_rv" != "$VERSION" ]; then # Method 2: Full file hash comparison (fallback if API unreachable)
echo "v${_rv}" > /tmp/.conduit_update_available if [ "$_done" != "true" ]; then
else local _utmp="/tmp/.conduit_uc_$$"
echo "new" > /tmp/.conduit_update_available local _uurl="https://raw.githubusercontent.com/SamNet-dev/conduit-manager/main/conduit.sh"
fi local _hashcmd="md5sum"
else command -v md5sum &>/dev/null || _hashcmd="sha256sum"
rm -f /tmp/.conduit_update_available command -v $_hashcmd &>/dev/null || { rm -f "$_utmp"; exit 0; }
fi if curl -fsSL --connect-timeout 5 --max-time 30 -o "$_utmp" "$_uurl" 2>/dev/null && [ -s "$_utmp" ]; then
local _remote_hash=$($_hashcmd "$_utmp" | cut -d' ' -f1)
local _stored_hash=""
[ -f "$INSTALL_DIR/.update_hash" ] && _stored_hash=$(<"$INSTALL_DIR/.update_hash")
if [ -z "$_stored_hash" ]; then
local _rv=$(grep -m1 '^VERSION=' "$_utmp" | cut -d'"' -f2)
if [ -n "$_rv" ] && [ "$_rv" = "$VERSION" ]; then
echo "$_remote_hash" > "$INSTALL_DIR/.update_hash" 2>/dev/null || true
rm -f "$_badge" 2>/dev/null
else
if [ -n "$_rv" ]; then
echo "v${_rv}" > "$_badge" 2>/dev/null
else
echo "new" > "$_badge" 2>/dev/null
fi
fi
elif [ "$_remote_hash" != "$_stored_hash" ]; then
local _rv=$(grep -m1 '^VERSION=' "$_utmp" | cut -d'"' -f2)
if [ -n "$_rv" ] && [ "$_rv" != "$VERSION" ]; then
echo "v${_rv}" > "$_badge" 2>/dev/null
else
echo "new" > "$_badge" 2>/dev/null
fi
else
rm -f "$_badge" 2>/dev/null
fi
fi
rm -f "$_utmp"
fi fi
rm -f "$_utmp"
} & } &
local redraw=true local redraw=true
@@ -11112,6 +11140,7 @@ update_conduit() {
echo -e " ${GREEN}✓ Script installed (v${new_version:-?})${NC}" echo -e " ${GREEN}✓ Script installed (v${new_version:-?})${NC}"
script_updated=true script_updated=true
rm -f /tmp/.conduit_update_available rm -f /tmp/.conduit_update_available
rm -f "$INSTALL_DIR/.update_sha" 2>/dev/null || true
else else
echo -e " ${RED}✗ Installation failed${NC}" echo -e " ${RED}✗ Installation failed${NC}"
fi fi
@@ -11204,8 +11233,9 @@ update_conduit() {
echo -e "${DIM}Note: Some changes may require restarting the menu to take effect.${NC}" echo -e "${DIM}Note: Some changes may require restarting the menu to take effect.${NC}"
fi fi
# Clear update badge # Clear update badge and SHA baseline (re-established on next menu open)
rm -f /tmp/.conduit_update_available rm -f /tmp/.conduit_update_available
rm -f "$INSTALL_DIR/.update_sha" 2>/dev/null || true
# Auto-update setup (skip in --auto mode or if crontab unavailable) # Auto-update setup (skip in --auto mode or if crontab unavailable)
if [ "$auto_mode" != "true" ] && command -v crontab &>/dev/null; then if [ "$auto_mode" != "true" ] && command -v crontab &>/dev/null; then
@@ -11327,6 +11357,8 @@ MANAGEMENT
$_hcmd "$0" 2>/dev/null | cut -d' ' -f1 > "$INSTALL_DIR/.update_hash" 2>/dev/null || true $_hcmd "$0" 2>/dev/null | cut -d' ' -f1 > "$INSTALL_DIR/.update_hash" 2>/dev/null || true
fi fi
fi fi
# Reset SHA baseline so next background check re-establishes it
rm -f "$INSTALL_DIR/.update_sha" 2>/dev/null || true
log_success "Management script installed: conduit" log_success "Management script installed: conduit"
} }