#!/bin/sh /etc/rc.common
# /etc/init.d/shater-cron — periodic auto-updater for subscriptions & rulesets,
# plus the data-plane watchdog.
#
# A tiny procd-supervised loop that, once per tick, checks every enabled
# subscription and url-ruleset against its per-item `update_interval` and runs
#   xrayctl sub update <name>       (subscriptions)
#   xrayctl ruleset update <name>   (url rulesets)
# when the item is due, then a single `xrayctl reconcile` if anything changed
# (xrayctl hash-compares run.json and restarts the engine only on real change).
#
# RELIABILITY CONTRACT (same "железно" posture as /etc/init.d/shater):
#   * The loop body is fully INERT unless globals.enabled=1 AND the main shater
#     service is live (ACTIVE_FLAG raised by its start, cleared by its stop).
#     An admin `stop` of the main service therefore STICKS — this loop idles.
#   * Only ever invokes xrayctl verbs / the shater init — never touches the
#     data plane directly.
#   * Due-ness is tracked with epoch stamp FILES under a tmpfs run dir, so no
#     dependence on `date -r`. Stamps are lost on reboot => every item is due at
#     boot, giving a fetch-at-boot exactly as the feature catalog requires.
#     Fetches are skipped (not stamped) while the box has no default route, so
#     the boot-time fetch actually happens once WAN is up instead of silently
#     burning the attempt.
#   * A stamp is written ONLY on success; a failed fetch is retried after a
#     short backoff (RETRY_SECS) instead of waiting out the full interval.
#   * WATCHDOG: if interception is meant to be live but the engine has been
#     dead for WATCHDOG_TICKS consecutive ticks, we escalate: with
#     kill_switch=open the main service is STOPPED (tears interception down —
#     fail-open, LAN returns to plain routing); with kill_switch=closed the
#     rules stay (blocked-by-design) and we log loudly.
#   * The loop never self-exits (procd would respawn-churn an exiting body);
#     it idles on its guards instead. busybox ash only — no bashisms.

USE_PROCD=1
START=96          # order vs the main init (99) is irrelevant; loop self-guards
STOP=11

# `loop` is an internal action procd re-execs to run the periodic body.
EXTRA_COMMANDS="loop"
EXTRA_HELP="	loop	internal: run the periodic update loop (invoked by procd)"

INIT_SCRIPT=/etc/init.d/shater-cron
SHATER_INIT=/etc/init.d/shater
XRAYCTL=/usr/bin/xrayctl
RUN_JSON=/etc/xray/run.json
ACTIVE_FLAG=/var/run/shater.active
STAMP_DIR=/var/run/shater/cron
TICK=60                          # seconds between due-checks
RETRY_SECS=300                   # backoff before retrying a FAILED fetch
WATCHDOG_TICKS=5                 # consecutive dead-engine ticks before escalating
DEFAULT_SUB_INTERVAL=6h
DEFAULT_RS_INTERVAL=24h

# --- helpers ---------------------------------------------------------------

shater_enabled() {
	local en
	en=$(uci -q get shater.globals.enabled) || return 1
	[ "$en" = "1" ]
}

# The main service raised its live-flag (start) and has not stopped since.
shater_active() {
	[ -f "$ACTIVE_FLAG" ]
}

# Best-effort "do we have an uplink" check; fetching before WAN is up at boot
# would waste each item's one boot attempt.
shater_has_uplink() {
	ip route show default 2>/dev/null | grep -q '^default' && return 0
	ip -6 route show default 2>/dev/null | grep -q '^default'
}

# Stamp names embed the UCI item name; keep them to one safe path component.
shater_safe_name() {
	echo "$1" | sed 's/[^A-Za-z0-9._-]/_/g'
}

# Convert 30m|6h|24h|2d|3600 -> seconds on stdout. $2 is a fallback token used
# when the input is empty or malformed (the FALLBACK is honored, not a
# hardcoded constant).
shater_ivl_secs() {
	local v="$1" def="$2" n u
	[ -n "$v" ] || v="$def"
	n=$(echo "$v" | sed 's/[^0-9].*$//')
	u=$(echo "$v" | sed 's/^[0-9]*//')
	if [ -z "$n" ]; then
		# Malformed (no leading digits): fall back to the caller's default; if
		# that is somehow malformed too, 6h.
		v="$def"
		n=$(echo "$v" | sed 's/[^0-9].*$//')
		u=$(echo "$v" | sed 's/^[0-9]*//')
		[ -n "$n" ] || { echo 21600; return; }
	fi
	case "$u" in
		s|"") echo "$n" ;;
		m|M)  echo $(( n * 60 )) ;;
		h|H)  echo $(( n * 3600 )) ;;
		d|D)  echo $(( n * 86400 )) ;;
		*)    echo $(( n )) ;;
	esac
}

# Due if now - stamp >= interval. $1 stamp file, $2 interval seconds.
shater_due() {
	local stamp="$1" ivl="$2" now last
	now=$(date +%s)
	last=$(cat "$stamp" 2>/dev/null)
	[ -n "$last" ] || last=0
	[ $(( now - last )) -ge "$ivl" ]
}

shater_stamp() { mkdir -p "$STAMP_DIR"; date +%s > "$1"; }

# Failed fetch: pretend the last success was (interval - RETRY_SECS) ago so the
# item comes due again after a short backoff instead of a full interval — but
# never sooner than RETRY_SECS (guards tiny intervals).
shater_stamp_retry() {
	local stamp="$1" ivl="$2" back
	back=$(( ivl - RETRY_SECS ))
	[ "$back" -gt 0 ] || back=0
	mkdir -p "$STAMP_DIR"
	echo $(( $(date +%s) - back )) > "$stamp"
}

# Walk anonymous `config subscription` / `config ruleset` sections by index and
# run any that are due. Echoes non-empty on stdout if at least one item updated.
shater_run_due() {
	local i name en ivl secs stamp changed=""

	# Subscriptions.
	i=0
	while uci -q get "shater.@subscription[$i]" >/dev/null 2>&1; do
		name=$(uci -q get "shater.@subscription[$i].name")
		en=$(uci -q get "shater.@subscription[$i].enabled")
		[ -z "$en" ] && en=1
		if [ -n "$name" ] && [ "$en" = "1" ]; then
			ivl=$(uci -q get "shater.@subscription[$i].update_interval")
			secs=$(shater_ivl_secs "$ivl" "$DEFAULT_SUB_INTERVAL")
			stamp="$STAMP_DIR/sub.$(shater_safe_name "$name")"
			if shater_due "$stamp" "$secs"; then
				if "$XRAYCTL" sub update "$name" >/dev/null 2>&1; then
					shater_stamp "$stamp"
					changed=1
				else
					logger -t shater-cron -p daemon.warn \
						"sub update '$name' failed; retrying in ${RETRY_SECS}s"
					shater_stamp_retry "$stamp" "$secs"
				fi
			fi
		fi
		i=$(( i + 1 ))
	done

	# Rulesets (only url sources auto-update; others have nothing to fetch).
	i=0
	while uci -q get "shater.@ruleset[$i]" >/dev/null 2>&1; do
		name=$(uci -q get "shater.@ruleset[$i].name")
		src=$(uci -q get "shater.@ruleset[$i].source")
		if [ -n "$name" ] && [ "$src" = "url" ]; then
			ivl=$(uci -q get "shater.@ruleset[$i].update_interval")
			secs=$(shater_ivl_secs "$ivl" "$DEFAULT_RS_INTERVAL")
			stamp="$STAMP_DIR/rs.$(shater_safe_name "$name")"
			if shater_due "$stamp" "$secs"; then
				if "$XRAYCTL" ruleset update "$name" >/dev/null 2>&1; then
					shater_stamp "$stamp"
					changed=1
				else
					logger -t shater-cron -p daemon.warn \
						"ruleset update '$name' failed; retrying in ${RETRY_SECS}s"
					shater_stamp_retry "$stamp" "$secs"
				fi
			fi
		fi
		i=$(( i + 1 ))
	done

	[ -n "$changed" ] && echo 1
}

# Watchdog body for one tick: detect "interception live, engine dead". $1 is
# the running dead-tick count; echoes the updated count.
shater_watchdog() {
	local dead="$1" ks
	if [ ! -f "$RUN_JSON" ] || pidof xray >/dev/null 2>&1; then
		echo 0
		return
	fi
	dead=$(( dead + 1 ))
	if [ "$dead" -eq "$WATCHDOG_TICKS" ]; then
		ks=$(uci -q get shater.globals.kill_switch)
		if [ "$ks" = "closed" ]; then
			# Fail-closed is a POLICY: dead engine + standing rules == traffic
			# blocked, which is what the admin asked for. Keep it, but say so.
			logger -t shater-cron -p daemon.crit \
				"xray dead for $(( dead * TICK ))s with interception live; kill_switch=closed keeps LAN blocked — fix the engine or /etc/init.d/shater stop"
		else
			# Fail-open: durably stop the stack (clears the live-flag + tears
			# interception down) so the LAN returns to plain routing.
			logger -t shater-cron -p daemon.crit \
				"xray dead for $(( dead * TICK ))s with interception live; kill_switch=open — stopping shater (fail-open, LAN back to plain routing)"
			"$SHATER_INIT" stop
			dead=0
		fi
	fi
	echo "$dead"
}

# loop: the foreground body supervised by procd. Never exits on its own — it
# idles while disabled/inactive so procd is not respawn-churned by a
# self-exiting body when the stack is off.
loop() {
	local changed sched dead=0
	mkdir -p "$STAMP_DIR"
	while :; do
		if shater_enabled && shater_active; then
			if shater_has_uplink; then
				changed=$(shater_run_due)
			else
				changed=""
			fi
			# Schedule boundary: `schedule due` prints "1" only when the set of
			# active time-scheduled rules changed since the last tick, so we
			# reconcile a few times a day at window edges — not every minute.
			sched=$("$XRAYCTL" schedule due 2>/dev/null)
			if [ -n "$changed" ] || [ -n "$sched" ]; then
				"$XRAYCTL" reconcile >/dev/null 2>&1
			fi
			dead=$(shater_watchdog "$dead")
		else
			dead=0
		fi
		sleep "$TICK"
	done
}

# --- procd lifecycle -------------------------------------------------------

start_service() {
	[ -x "$XRAYCTL" ] || return 0
	shater_enabled || return 0

	procd_open_instance shater-cron
	# Re-exec ourselves as the loop body so procd supervises a single process.
	# Invoke through the rc.common shebang (NOT `/bin/sh $INIT_SCRIPT`) so the
	# `loop` action is dispatched by rc.common and procd tracks the resulting
	# foreground PID — running `/bin/sh <script> loop` detaches the loop from
	# procd (instance shows not-running, and procd respawn-churns it).
	procd_set_param command "$INIT_SCRIPT" loop
	procd_set_param respawn 3600 10 0     # threshold timeout always-retry
	procd_set_param file /etc/config/shater # restart the loop when UCI changes
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_close_instance
}

reload_service() {
	# UCI changed: bounce the loop so new intervals/items take effect. If the
	# stack was disabled, start re-guards to a no-op.
	stop
	start
}

service_triggers() {
	procd_add_reload_trigger "shater"
}
