#!/bin/sh . /usr/share/libubox/jshn.sh readonly state_dir='/var/run/argon-update' readonly cache_file='/var/run/argon-update/release.json' readonly sources_file='/usr/share/argon/update-sources.json' readonly repository='jerrykuku/luci-theme-argon' opkg_query() ( # OPKG locks its database even for status and comparison commands. Keep our # RPC reads from racing the worker's version checks against that native lock. prepare_state || exit 1 exec 6>"$state_dir/package-query.lock" flock -w 2 6 || exit 1 opkg "$@" ) installed_version() { current_version='' package_manager='' if command -v apk >/dev/null 2>&1; then current_version="$(apk list --installed luci-theme-argon 2>/dev/null | awk '$1 ~ /^luci-theme-argon-/ { sub(/^luci-theme-argon-/, "", $1); print $1; exit }')" [ -z "$current_version" ] || package_manager=apk fi if [ -z "$current_version" ] && command -v opkg >/dev/null 2>&1; then current_version="$(opkg_query status luci-theme-argon 2>/dev/null | awk ' /^Version: / { version=$2 } /^Status: .* installed$/ { installed=1 } END { if (installed) print version }')" [ -z "$current_version" ] || package_manager=opkg fi } valid_tag() { [ "${#1}" -le 48 ] && printf '%s\n' "$1" | grep -Eq '^v?[0-9]+(\.[0-9]+){1,3}$' } read_cache() { [ -f "$cache_file" ] && [ ! -L "$cache_file" ] && [ "$(wc -c < "$cache_file")" -le 1048576 ] || return 1 json_load_file "$cache_file" || return 1 json_get_var schema schema json_get_var cached_repository repository json_get_var tag tag json_get_var checked_at checked_at [ "$schema" = 1 ] && [ "$cached_repository" = "$repository" ] && valid_tag "$tag" && printf '%s\n' "$checked_at" | grep -Eq '^[0-9]{1,11}$' } valid_source() { [ "${#1}" -le 512 ] && printf '%s\n' "$1" | grep -Eq '^https://[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?(/[A-Za-z0-9._~-]+)+$' } load_sources() { primary_url='' fallback_url='https://jerrykuku.github.io/luci-theme-argon/updates/stable.json' if [ -f "$sources_file" ] && [ ! -L "$sources_file" ] && [ "$(wc -c < "$sources_file")" -le 4096 ] && json_load_file "$sources_file"; then json_get_var configured_primary primary json_get_var configured_fallback fallback valid_source "$configured_primary" && primary_url="$configured_primary" valid_source "$configured_fallback" && fallback_url="$configured_fallback" fi } normalize_manifest() { json_load_file "$1" || return 1 json_get_var schema schema json_get_var manifest_repository repository json_get_var tag tag [ "$schema" = 1 ] && [ "$manifest_repository" = "$repository" ] && valid_tag "$tag" || return 1 # Both formats must be complete. Never combine fields from different sources. package_manager=apk select_package || return 1 package_manager=opkg select_package || return 1 for field in ipk apk ipk_sha256 apk_sha256 ipk_size apk_size published_at notes; do json_get_var "$field" "$field" done printf '%s\n' "$published_at" | grep -Eq '^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$' || return 1 [ "${#notes}" -le 131072 ] || return 1 # Copy only the contract fields, not remote status, URLs or local timestamps. json_init json_add_int schema 1 json_add_string repository "$repository" json_add_string tag "$tag" json_add_string published_at "$published_at" json_add_string notes "$notes" json_add_string ipk "$ipk" json_add_string apk "$apk" json_add_string ipk_sha256 "$ipk_sha256" json_add_string apk_sha256 "$apk_sha256" json_add_int ipk_size "$ipk_size" json_add_int apk_size "$apk_size" json_add_int checked_at "$(date +%s)" } check_failure() { read_cache || json_init json_add_int retry_at "$retry_at" respond 1 } check_updates() { prepare_state || { fail 1; return; } # Separate from the installation lock. Concurrent tabs reuse the first check. exec 7>"$state_dir/check.lock" flock -w 15 7 || { retry_at="$(($(date +%s) + 5))"; check_failure; return; } now="$(date +%s)" if read_cache && [ "$now" -ge "$checked_at" ] && [ "$((now - checked_at))" -lt 300 ]; then respond 0 return fi retry_at=0 if [ -f "$state_dir/retry_at" ] && [ ! -L "$state_dir/retry_at" ] && [ "$(wc -c < "$state_dir/retry_at")" -le 12 ]; then retry_at="$(cat "$state_dir/retry_at")" fi if printf '%s\n' "$retry_at" | grep -Eq '^[0-9]{1,11}$' && [ "$retry_at" -gt "$now" ] && [ "$((retry_at - now))" -le 60 ]; then check_failure return fi load_sources work="$(mktemp -d "$state_dir/check.XXXXXX")" || { fail 1; return; } trap 'rm -rf "$work"' EXIT trap 'exit 1' HUP INT TERM for source_url in "$primary_url" "$fallback_url"; do [ -n "$source_url" ] || continue # The two attempts together stay below rpcd's request timeout. if (ulimit -f 2048; timeout -k 1 5 wget -T 4 -q -U 'luci-theme-argon' \ -O "$work/manifest.json" "$source_url") 2>/dev/null && [ "$(wc -c < "$work/manifest.json")" -le 1048576 ] && normalize_manifest "$work/manifest.json"; then json_dump > "$work/cache.json" [ ! -L "$cache_file" ] && mv -f "$work/cache.json" "$cache_file" 2>/dev/null || : rm -f "$state_dir/retry_at" respond 0 return fi [ "$primary_url" != "$fallback_url" ] || break done retry_at="$(($(date +%s) + 60))" printf '%s\n' "$retry_at" > "$state_dir/retry_at" check_failure } select_package() { case "$package_manager" in apk) format=apk ;; opkg) format=ipk ;; *) return 1 ;; esac json_get_var package "$format" json_get_var digest "${format}_sha256" json_get_var size "${format}_size" json_get_var tag tag valid_tag "$tag" || return 1 base="${tag#v}" case "$package_manager:$package" in apk:luci-theme-argon-"$base"-r*.apk) target_version="${package#luci-theme-argon-}" target_version="${target_version%.apk}" revision="${target_version#"$base"-r}" ;; opkg:luci-theme-argon_"$base"*_all.ipk) target_version="${package#luci-theme-argon_}" target_version="${target_version%_all.ipk}" case "$target_version" in "$base") revision=1 ;; "$base"-*) revision="${target_version#"$base"-}" ;; *) return 1 ;; esac ;; *) return 1 ;; esac printf '%s\n' "$revision" | grep -Eq '^[0-9]{1,8}$' && printf '%s\n' "$digest" | grep -Eq '^[a-f0-9]{64}$' && printf '%s\n' "$size" | grep -Eq '^[1-9][0-9]{0,7}$' && [ "$size" -gt 0 ] && [ "$size" -le 8388608 ] } is_upgrade() { # Reject unknown/custom version syntax before asking the native comparator. printf '%s\n' "$current_version" | grep -Eq '^[0-9]+(\.[0-9]+){1,3}((~|_)(alpha|beta|rc)[0-9]+|-(alpha|beta|rc)\.[0-9]+)?(-r?[0-9]+)?$' || return 1 case "$package_manager" in apk) [ "$(apk version -t "$current_version" "$target_version" 2>/dev/null)" = '<' ] ;; opkg) opkg_query compare-versions "$current_version" '<<' "$target_version" ;; *) return 1 ;; esac } prepare_state() { [ ! -L "$state_dir" ] && mkdir -p -m 700 "$state_dir" && [ ! -L "$state_dir" ] && command -v flock >/dev/null 2>&1 && command -v sha256sum >/dev/null 2>&1 && command -v timeout >/dev/null 2>&1 } add_status() { # Keep release data in the caller's namespace while loading the small job file. json_set_namespace upgrade_status previous_namespace json_init phase=idle job_error='' job_version='' if [ -f "$state_dir/status.json" ] && [ ! -L "$state_dir/status.json" ] && [ "$(wc -c < "$state_dir/status.json")" -le 4096 ] && json_load_file "$state_dir/status.json"; then json_get_var phase phase json_get_var job_error error json_get_var job_version version fi active=0 case "$phase" in starting|downloading|verifying|installing) # A kernel lock survives the initiating RPC and is released on worker exit. if command -v flock >/dev/null 2>&1; then exec 8>"$state_dir/lock" if flock -n 8; then # The worker may have finished between our snapshot and lock probe. # Read its final state while holding the lock against a new start. json_load_file "$state_dir/status.json" json_get_var phase phase json_get_var job_error error json_get_var job_version version case "$phase" in starting|downloading|verifying|installing) phase=failed; job_error=interrupted ;; esac else active=1 fi exec 8>&- else phase=failed job_error=interrupted fi ;; esac json_set_namespace "$previous_namespace" json_add_object upgrade json_add_string phase "$phase" json_add_string error "$job_error" json_add_string version "$job_version" json_add_boolean active "$active" if [ -f "$state_dir/log" ] && [ ! -L "$state_dir/log" ]; then json_add_string log "$(tail -c 8192 "$state_dir/log")" fi json_close_object } respond() { add_status # Read after the job snapshot so a completed job reports its installed version. installed_version can_upgrade=0 now="$(date +%s)" json_get_var checked_at checked_at if [ "$1" = 0 ] && printf '%s\n' "$checked_at" | grep -Eq '^[0-9]{1,11}$' && [ "$now" -ge "$checked_at" ] && [ "$((now - checked_at))" -lt 300 ] && select_package && is_upgrade && command -v flock >/dev/null 2>&1 && command -v sha256sum >/dev/null 2>&1 && command -v timeout >/dev/null 2>&1; then can_upgrade=1 fi json_add_int result "$1" json_add_string current_version "$current_version" json_add_string package_manager "$package_manager" json_add_boolean can_upgrade "$can_upgrade" json_dump } fail() { json_init respond "$1" } write_status() { json_init json_add_string phase "$1" json_add_string error "${2:-}" json_add_string version "$target_version" json_dump > "$state_dir/status.$$" && mv -f "$state_dir/status.$$" "$state_dir/status.json" } run_upgrade() { # Run a private copy, since installing the theme replaces this RPC script. work="${0%/*}" case "$work" in "$state_dir"/job.??????) ;; *) return 1 ;; esac [ -d "$work" ] && [ ! -L "$work" ] && [ -f "$work/release.json" ] || return 1 trap 'rm -rf "$work"' EXIT trap 'write_status failed interrupted; exit 1' INT TERM json_load_file "$work/release.json" && select_package || { write_status failed metadata; return; } installed_version is_upgrade || { write_status failed version; return; } # Leave room for download metadata and unpacking. The package manager also # performs its own dependency and storage checks; never force those checks. tmp_free="$(df -Pk "$state_dir" | awk 'END { print $4 }')" root_path=/ [ ! -d /overlay ] || root_path=/overlay root_free="$(df -Pk "$root_path" | awk 'END { print $4 }')" if [ "${tmp_free:-0}" -lt "$((size / 1024 + 2048))" ] || [ "${root_free:-0}" -lt "$((size * 3 / 1024 + 1024))" ]; then write_status failed space return fi write_status downloading url="https://github.com/jerrykuku/luci-theme-argon/releases/download/$tag/$package" (ulimit -f 16384; timeout -k 2 90 wget -T 20 -q -U 'luci-theme-argon' \ -O "$work/$package" "$url") 2>/dev/null || { write_status failed network; return; } write_status verifying if [ "$(wc -c < "$work/$package")" -ne "$size" ] || [ "$(sha256sum "$work/$package" | awk '{ print $1 }')" != "$digest" ]; then write_status failed checksum return fi # Recheck immediately before modifying the package database. installed_version is_upgrade || { write_status failed version; return; } write_status installing # Bound captured output without imposing a file-size limit on the installer. ( case "$package_manager" in apk) apk add --allow-untrusted "$work/$package" ;; opkg) opkg install "$work/$package" ;; esac printf '%s\n' "$?" > "$work/exit" ) 2>&1 | tail -c 8192 > "$state_dir/log" if [ "$(cat "$work/exit" 2>/dev/null)" != 0 ]; then write_status failed install return fi installed_version if [ "$current_version" != "$target_version" ]; then write_status failed install return fi write_status complete } start_upgrade() { read -r request json_load "$request" || { fail 3; return; } json_get_var requested_tag tag json_get_var requested_package package now="$(date +%s)" read_cache && [ "$now" -ge "$checked_at" ] && [ "$((now - checked_at))" -lt 300 ] && select_package && [ "$tag" = "$requested_tag" ] && [ "$package" = "$requested_package" ] || { fail 3; return; } is_upgrade || { fail 5; return; } prepare_state || { fail 1; return; } exec 9>"$state_dir/lock" flock -n 9 || { fail 4; return; } work="$(mktemp -d "$state_dir/job.XXXXXX")" || { fail 1; return; } if ! cp "$0" "$work/worker.sh" || ! json_dump > "$work/release.json"; then rm -rf "$work" fail 1 return fi : > "$state_dir/log" write_status starting # FD 9 is inherited by the worker, keeping the lock until all installation # work finishes. Detach every stdio stream so rpcd can reply immediately. (trap '' HUP; exec sh "$work/worker.sh" run "$package_manager") /dev/null 2>&1 & json_init respond 0 } umask 077 case "$1" in list) printf '%s\n' '{"get":{},"check":{},"status":{},"start":{"tag":"String","package":"String"}}' ;; run) package_manager="$2" run_upgrade ;; call) case "$2" in get|check|status|start) ;; *) exit 1 ;; esac if [ "$2" = status ]; then json_init respond 0 exit 0 fi if [ "$2" = start ]; then installed_version start_upgrade exit 0 fi if [ "$2" = get ]; then read_cache || json_init respond 0 exit 0 fi check_updates ;; esac