#!/bin/sh
# modem-man watchdog daemon
# Следит за WAN-соединениями модемов и восстанавливает связь по лестнице:
#   1-2) ifdown/ifup интерфейса
#   3)   сброс модема (uqmi --set-device-operating-mode reset, fallback AT+CFUN=1,1)
#   4)   переподключение USB-устройства модема (sysfs authorized 0/1)
#   5)   перезагрузка роутера (с ограничениями по uptime и интервалу)
# Связь проверяется HTTP-пробами через интерфейс модема (несколько адресов),
# свежим handshake WireGuard (режим «белых списков», когда ICMP/публичные
# адреса режутся) и, в последнюю очередь, ping.
# Запускается через procd, читает /etc/config/modem-man.

. /lib/functions.sh

MM_RUNDIR="/var/run/modem-man"
MM_CONFIG="modem-man"
MM_TAG="modem-man"
MM_REBOOT_MARK="/etc/modem-man.lastreboot"
DEFAULT_PROBE_URLS="https://enroll.uavpcb.ru/health http://connectivitycheck.gstatic.com/generate_204 http://detectportal.firefox.com/success.txt"

log() { logger -t "$MM_TAG" "$*"; }

mkdir -p "$MM_RUNDIR"

# ---------------------------------------------------------------- статус
save_status() {
	# save_status cfg iface ifname type up message level fails last_ok last_action last_action_ts probe
	local cfg="$1" iface="$2" ifname="$3" type="$4" up="$5" msg="$6" level="$7" fails="$8" last_ok="$9"
	shift 9
	local last_action="$1" last_action_ts="$2" probe="$3"
	local ts
	ts="$(date +%s)"
	cat > "$MM_RUNDIR/${cfg}.json" << EOJ
{
	"config": "$cfg",
	"interface": "$iface",
	"ifname": "$ifname",
	"type": "$type",
	"up": $up,
	"message": "$msg",
	"timestamp": $ts,
	"level": $level,
	"fails": $fails,
	"last_ok": ${last_ok:-0},
	"last_action": "$last_action",
	"last_action_ts": ${last_action_ts:-0},
	"probe": "$probe"
}
EOJ
}

get_real_ifname() {
	ubus call network.interface."$1" status 2>/dev/null \
		| jsonfilter -e "@['l3_device']" 2>/dev/null | head -1
}

paused() {
	# Паузы: смена SIM (rc-shell/simswitch/eSIM), ручная пауза, hotplug
	[ -f /var/run/rc-shell/sim_checking ] && return 0
	[ -f "$MM_RUNDIR/pause" ] && return 0
	return 1
}

# ---------------------------------------------------------------- проверки
# PROBE_RESULT — чем подтверждена связь: url / wg / ping
PROBE_RESULT=""

check_http() {
	local ifname="$1" timeout="$2" urls="$3" u code
	command -v curl >/dev/null 2>&1 || return 1
	for u in $urls; do
		code=$(curl -s -o /dev/null -m "$timeout" --interface "$ifname" -w '%{http_code}' "$u" 2>/dev/null)
		case "$code" in
			2??|3??) PROBE_RESULT="$u"; return 0 ;;
		esac
	done
	return 1
}

check_wg() {
	local wgif="$1" fresh="$2" now hs age best=999999
	[ -n "$wgif" ] || return 1
	command -v wg >/dev/null 2>&1 || return 1
	now=$(date +%s)
	for hs in $(wg show "$wgif" latest-handshakes 2>/dev/null | awk '{print $2}'); do
		[ "$hs" -gt 0 ] || continue
		age=$((now - hs))
		[ "$age" -lt "$best" ] && best=$age
	done
	[ "$best" -lt "$fresh" ] && { PROBE_RESULT="wg:${wgif}:${best}s"; return 0; }
	return 1
}

check_ping() {
	local hosts="$1" ifname="$2" h
	for h in $hosts; do
		ping -c 2 -W 4 -I "$ifname" "$h" > /dev/null 2>&1 && { PROBE_RESULT="ping:$h"; return 0; }
	done
	return 1
}

connectivity_ok() {
	# connectivity_ok ifname timeout urls wgif wg_fresh ping_hosts
	PROBE_RESULT=""
	check_http "$1" "$2" "$3" && return 0
	check_wg "$4" "$5" && return 0
	check_ping "$6" "$1" && return 0
	return 1
}

# ---------------------------------------------------------------- действия
reconnect_iface() {
	local netifd_iface="$1" cfg="$2"
	log "[$cfg] action: reconnect $netifd_iface"
	ubus call "network.interface.$netifd_iface" notify_proto '{"action":5,"available":true}' 2>/dev/null
	ifdown "$netifd_iface" 2>/dev/null
	sleep 3
	ifup "$netifd_iface" 2>/dev/null
}

modem_reset() {
	# modem_reset cfg netifd_iface
	local cfg="$1" netifd_iface="$2" dev ok=0
	dev=$(uci -q get "network.${netifd_iface}.device")
	touch "$MM_RUNDIR/pause"
	ifdown "$netifd_iface" 2>/dev/null
	if [ -n "$dev" ] && [ -c "$dev" ] && command -v uqmi >/dev/null 2>&1; then
		log "[$cfg] action: modem reset via uqmi ($dev)"
		uqmi -s -d "$dev" -t 5000 --set-device-operating-mode reset >/dev/null 2>&1 && ok=1
	fi
	if [ "$ok" = 0 ]; then
		log "[$cfg] action: modem reset via AT+CFUN=1,1"
		ubus -t 15 call mainmodem at_raw '{"command":"AT+CFUN=1,1","timeout":10}' >/dev/null 2>&1 && ok=1
	fi
	[ "$ok" = 1 ] || log "[$cfg] modem reset: no method succeeded"
	sleep 45
	rm -f "$MM_RUNDIR/pause"
	ubus call "network.interface.$netifd_iface" notify_proto '{"action":5,"available":true}' 2>/dev/null
	ifup "$netifd_iface" 2>/dev/null
}

usb_rebind() {
	# usb_rebind cfg netifd_iface ifname
	local cfg="$1" netifd_iface="$2" ifname="$3" usbdev=""
	if [ -n "$ifname" ] && [ -e "/sys/class/net/$ifname/device" ]; then
		usbdev=$(readlink -f "/sys/class/net/$ifname/device/..")
	fi
	if [ -z "$usbdev" ] || [ ! -f "$usbdev/authorized" ]; then
		# интерфейса нет — ищем устройство Quectel по VID
		local d
		for d in /sys/bus/usb/devices/*; do
			[ -f "$d/idVendor" ] && [ "$(cat "$d/idVendor" 2>/dev/null)" = "2c7c" ] && { usbdev="$d"; break; }
		done
	fi
	if [ -z "$usbdev" ] || [ ! -f "$usbdev/authorized" ]; then
		log "[$cfg] action: usb rebind skipped (device not found)"
		return 1
	fi
	log "[$cfg] action: usb rebind $usbdev"
	touch "$MM_RUNDIR/pause"
	ifdown "$netifd_iface" 2>/dev/null
	echo 0 > "$usbdev/authorized" 2>/dev/null
	sleep 5
	echo 1 > "$usbdev/authorized" 2>/dev/null
	sleep 40
	rm -f "$MM_RUNDIR/pause"
	ubus call "network.interface.$netifd_iface" notify_proto '{"action":5,"available":true}' 2>/dev/null
	ifup "$netifd_iface" 2>/dev/null
	return 0
}

router_reboot() {
	# router_reboot cfg min_uptime min_interval
	local cfg="$1" min_uptime="$2" min_interval="$3" up now last=0
	up=$(cut -d. -f1 /proc/uptime)
	now=$(date +%s)
	[ -f "$MM_REBOOT_MARK" ] && last=$(cat "$MM_REBOOT_MARK" 2>/dev/null)
	if [ "$up" -lt "$min_uptime" ]; then
		log "[$cfg] reboot skipped: uptime ${up}s < ${min_uptime}s"; return 1
	fi
	if [ -n "$last" ] && [ "$last" -gt 0 ] && [ $((now - last)) -lt "$min_interval" ]; then
		log "[$cfg] reboot skipped: last watchdog reboot $((now - last))s ago < ${min_interval}s"; return 1
	fi
	echo "$now" > "$MM_REBOOT_MARK"
	log "[$cfg] action: REBOOT (no connectivity after all recovery steps)"
	sync; sleep 2; reboot
	return 0
}

# ---------------------------------------------------------------- цикл
watch_one() {
	local cfg="$1"
	local enabled type netifd_iface ping_host ping_interval watchdog
	local probe_urls probe_timeout wg_iface wg_fresh fail_threshold recover_wait
	local allow_reboot reboot_min_uptime reboot_min_interval max_level

	config_get enabled       "$cfg" enabled       "0"
	config_get type          "$cfg" type          "qmi"
	config_get netifd_iface  "$cfg" netifd_iface  ""
	config_get ping_host     "$cfg" ping_host     "8.8.8.8 77.88.8.8"
	config_get ping_interval "$cfg" ping_interval "30"
	config_get watchdog      "$cfg" watchdog      "1"
	config_get probe_urls    "$cfg" probe_urls    "$DEFAULT_PROBE_URLS"
	config_get probe_timeout "$cfg" probe_timeout "8"
	config_get wg_iface      "$cfg" wg_iface      "wg0"
	config_get wg_fresh      "$cfg" wg_fresh      "180"
	config_get fail_threshold "$cfg" fail_threshold "2"
	config_get recover_wait  "$cfg" recover_wait  "90"
	config_get allow_reboot  "$cfg" allow_reboot  "0"
	config_get reboot_min_uptime "$cfg" reboot_min_uptime "900"
	config_get reboot_min_interval "$cfg" reboot_min_interval "3600"
	config_get max_level     "$cfg" max_level     "5"

	[ "$enabled" = "1" ] || return
	[ "$watchdog" = "1" ] || return
	[ -n "$netifd_iface" ] || return

	log "[$cfg] watchdog started (iface=$netifd_iface, interval=${ping_interval}s, probes: $(echo $probe_urls | wc -w) url + wg:$wg_iface + ping)"

	local fails=0 level=0 last_ok=0 last_action="" last_action_ts=0 ifname
	while true; do
		[ -f "$MM_RUNDIR/stop" ] && exit 0
		sleep "$ping_interval"
		[ -f "$MM_RUNDIR/stop" ] && exit 0

		if [ -f "$MM_RUNDIR/hotplug_${netifd_iface}" ]; then
			log "[$cfg] hotplug reconnect in progress, waiting 60s"
			rm -f "$MM_RUNDIR/hotplug_${netifd_iface}"
			fails=0
			sleep 60
			continue
		fi
		if paused; then
			fails=0
			continue
		fi

		ifname="$(get_real_ifname "$netifd_iface")"
		if [ -n "$ifname" ] && connectivity_ok "$ifname" "$probe_timeout" "$probe_urls" "$wg_iface" "$wg_fresh" "$ping_host"; then
			if [ "$fails" -gt 0 ] || [ "$level" -gt 0 ]; then
				log "[$cfg] connectivity OK via $ifname ($PROBE_RESULT) — recovered (level was $level)"
			fi
			fails=0; level=0; last_ok=$(date +%s)
			save_status "$cfg" "$netifd_iface" "$ifname" "$type" "true" "ok" 0 0 "$last_ok" "$last_action" "$last_action_ts" "$PROBE_RESULT"
			continue
		fi

		fails=$((fails + 1))
		if [ -z "$ifname" ]; then
			log "[$cfg] $netifd_iface has no l3 device (fail $fails, level $level)"
			save_status "$cfg" "$netifd_iface" "" "$type" "false" "no l3 device" "$level" "$fails" "$last_ok" "$last_action" "$last_action_ts" ""
		else
			log "[$cfg] no connectivity via $ifname (fail $fails, level $level)"
			save_status "$cfg" "$netifd_iface" "$ifname" "$type" "false" "no connectivity" "$level" "$fails" "$last_ok" "$last_action" "$last_action_ts" ""
		fi
		[ "$fails" -ge "$fail_threshold" ] || continue

		# Эскалация
		level=$((level + 1))
		[ "$level" -gt "$max_level" ] && level=3   # дальше циклим сброс модема/USB
		fails=0
		last_action_ts=$(date +%s)
		case "$level" in
			1|2) last_action="reconnect";   reconnect_iface "$netifd_iface" "$cfg" ;;
			3)   last_action="modem_reset"; modem_reset "$cfg" "$netifd_iface" ;;
			4)   last_action="usb_rebind";  usb_rebind "$cfg" "$netifd_iface" "$ifname" || { last_action="modem_reset"; modem_reset "$cfg" "$netifd_iface"; } ;;
			*)   if [ "$allow_reboot" = "1" ]; then
					last_action="reboot"
					router_reboot "$cfg" "$reboot_min_uptime" "$reboot_min_interval" || { last_action="modem_reset"; modem_reset "$cfg" "$netifd_iface"; }
				else
					last_action="modem_reset"; modem_reset "$cfg" "$netifd_iface"
				fi ;;
		esac
		save_status "$cfg" "$netifd_iface" "$ifname" "$type" "false" "recovering" "$level" 0 "$last_ok" "$last_action" "$last_action_ts" ""
		# даём proto-setup (QMI + SIM + регистрация + connect) время завершиться
		sleep "$recover_wait"
	done &
}

config_load "$MM_CONFIG"
config_foreach watch_one modem

trap 'kill $(jobs -p) 2>/dev/null; exit 0' TERM INT
wait
