#!/bin/sh /etc/rc.common
# shellcheck shell=dash disable=SC2034  # START/STOP/USE_PROCD are read by /etc/rc.common
# Dawnline: AmneziaWG access server — its backend (which also serves the personal portal) and the
# resolver for Home-only devices.

START=95
STOP=10
USE_PROCD=1

# A line in syslog as the backend writes them: "dawnline: [info] [service] …" at the
# matching priority, when the admin's level (settings.log_level: error|warn|info|debug) takes it.
log() {
	local lvl="$1" want rank prio
	shift
	want="$(uci -q get dawnline.settings.log_level)"
	case "$lvl" in error) rank=0 prio=err ;; warn) rank=1 prio=warning ;; debug) rank=3 prio=debug ;; *) rank=2 prio=info ;; esac
	case "$want" in error) want=0 ;; warn) want=1 ;; debug) want=3 ;; *) want=2 ;; esac
	[ "$rank" -le "$want" ] || return 0
	logger -t dawnline -p "daemon.$prio" "[$lvl] [service] $*"
}

# What the portal runs with, worked out once for whoever serves it: P_PORT (empty: no portal, said in the
# log), P_CERT, P_KEY, P_HPORT (plain HTTP for links with an IP address, or empty), P_PROCS.
portal_plan() {
	local enabled certs mem
	P_PORT='' P_CERT='' P_KEY='' P_HPORT=''
	config_get_bool enabled settings portal_enabled 0
	if [ "$enabled" != 1 ]; then
		log info "personal portal is off"
		return 0
	fi
	certs="$(/usr/bin/dawnline portal-cert 2>/dev/null)"
	P_CERT="${certs% *}"
	P_KEY="${certs#* }"
	if ! [ -f "$P_CERT" ] || ! [ -f "$P_KEY" ]; then
		log warn "personal portal not started: no certificate (${P_CERT:-none}): choose one in Settings, or save the portal settings again"
		return 0
	fi
	config_get P_PORT settings portal_port 8443
	# IP addresses in links get plain HTTP on the next port (names stay HTTPS-only).
	P_HPORT="$(/usr/bin/dawnline portal-http-port 2>/dev/null)"
	return 0
}

start_portal() {
	[ -n "$P_PORT" ] || return 0
	local until
	until="$(openssl x509 -enddate -noout -in "$P_CERT" 2>/dev/null | sed 's/^notAfter=/ until /')"
	log info "personal portal on port $P_PORT (HTTPS${P_HPORT:+, plain HTTP on $P_HPORT for links with an IP address}) · certificate $P_CERT$until · served by the backend"
}

# Plain DNS for Home-only tunnel devices (config written by `dawnline apply`).
start_resolver() {
	if ! [ -f /var/etc/dawnline-dns.conf ]; then
		log debug "no DNS for Home-only devices (nobody needs it)"
		return 0
	fi
	procd_open_instance dns
	procd_set_param command /usr/libexec/dawnline/resolver
	procd_set_param file /var/etc/dawnline-dns.conf
	# A new config kills it (the backend's apply): back within a second, not five.
	procd_set_param respawn 3600 1 0
	procd_close_instance
	log info "DNS for Home-only devices started (/var/etc/dawnline-dns.conf)"
}

# The backend (dawnlined, docs/backend.md): it looks at the tunnels, keeps the router to the settings,
# answers the pages and their stream, and serves the personal portal.
start_backend() {
	local bin=/usr/libexec/dawnline/dawnlined stream=poll built portal=''
	# No binary here (a fresh install with no network, a download that failed): fetched in the background,
	# once a boot, and the service started again with it.
	if [ ! -x "$bin" ] && [ ! -e /var/run/dawnline/backend.asked ]; then
		: > /var/run/dawnline/backend.asked
		( /usr/libexec/dawnline/fetch-backend && [ -x "$bin" ] && /etc/init.d/dawnline restart ) </dev/null >/dev/null 2>&1 &
	fi
	if ! [ -x "$bin" ] || ! "$bin" -version >/dev/null 2>&1; then
		log error "the backend is not on this router ($bin): Dawnline does not run until it is · /usr/libexec/dawnline/fetch-backend puts it in place"
		return 0
	fi
	# uhttpd holds an event stream without losing its connection only since 2026-08-03 (an older one stops
	# serving after a hundred of them): before that the pages follow by a held call.
	built="$({ apk list -I uhttpd-mod-ubus 2>/dev/null || opkg list-installed uhttpd-mod-ubus 2>/dev/null; } |
		sed -n 's/.*uhttpd-mod-ubus[- ]*\([0-9]\{4\}\)[.-]\([0-9]\{2\}\)[.-]\([0-9]\{2\}\).*/\1\2\3/p' | head -n 1)"
	[ -n "$built" ] && [ "$built" -ge 20260803 ] && stream=sse
	# The portal: its certificate is read again when it is renewed, so no restart follows a renewal.
	[ -n "$P_PORT" ] && portal="-portal-port $P_PORT -portal-cert $P_CERT -portal-key $P_KEY${P_HPORT:+ -portal-http $P_HPORT}"
	procd_open_instance backend
	# shellcheck disable=SC2086  # the portal's options are words on purpose
	procd_set_param command "$bin" -stream "$stream" $portal
	procd_set_param respawn 3600 5 0
	procd_set_param stderr 1
	procd_close_instance
	log info "backend started · the pages follow by $stream"
}

start_service() {
	config_load dawnline
	mkdir -p /var/run/dawnline
	log info "starting"
	# The apply writes its own lines: what it came to and how long it took (each step at debug).
	/usr/bin/dawnline apply >/dev/null 2>&1 || log error "applying the configuration failed: run \"dawnline apply\" to see why"

	portal_plan
	start_backend
	start_portal
	start_resolver
	log info "started"
}

# Noted for the service log: the backend's next start is a restart, not a crash or a boot.
stop_service() {
	log info "stopping"
	mkdir -p /var/run/dawnline
	date +%s > /var/run/dawnline/agent.stopped
	# An agent left by a version from before the backend did its work goes too.
	pgrep -f '^/usr/bin/ucode /usr/libexec/dawnline/agent' | xargs -r kill 2>/dev/null
}

reload_service() {
	log info "reloading: settings changed"
	/usr/bin/dawnline apply >/dev/null 2>&1 || log error "applying the configuration failed: run \"dawnline apply\" to see why"
	start
}

service_triggers() {
	procd_add_reload_trigger dawnline
	# A certificate issued for the first time changes what the portal is served with: the service is
	# planned again (a renewed one the backend reads by itself).
	procd_add_raw_trigger acme.issue 5000 /etc/init.d/dawnline reload
	procd_add_raw_trigger acme.renew 5000 /etc/init.d/dawnline reload
}
