small-packages/luci-app-daed/root/etc/init.d/luci_daed
2026-06-15 01:15:26 +08:00

81 lines
2.1 KiB
Bash
Executable File

#!/bin/sh /etc/rc.common
# Copyright (C) 2023 Tianling Shen <cnsztl@immortalwrt.org>
USE_PROCD=0
START=98
CONF="daed"
PROG="/usr/bin/daed"
LOG="/var/log/daed/daed.log"
RESOLV_DAED="/tmp/resolv.conf.daed"
update_cron() {
local temp_cron="/tmp/root.cron"
if [ -f /etc/crontabs/root ]; then
grep -v '/etc/daed/daed_sub.sh' /etc/crontabs/root > "$temp_cron" 2>/dev/null
fi
: >> "$temp_cron"
local auto_update
auto_update=$(uci -q get daed.config.subscribe_auto_update)
if [ "$1" = "add" ] && [ "${auto_update:-0}" -eq 1 ]; then
local min=$((RANDOM % 60))
local hr=$(uci -q get daed.config.subscribe_update_day_time)
local wk=$(uci -q get daed.config.subscribe_update_week_time)
echo "$min ${hr:-*} * * ${wk:-*} /etc/daed/daed_sub.sh >/dev/null 2>&1" >> "$temp_cron"
fi
crontab "$temp_cron" 2>/dev/null
rm -f "$temp_cron"
}
hijack_resolv_conf() {
grep -q ' /tmp/resolv\.conf ' /proc/mounts && return 0
cat /dev/null > "$RESOLV_DAED"
if [ -f "/tmp/resolv.conf.d/resolv.conf.auto" ]; then
grep '^nameserver' "/tmp/resolv.conf.d/resolv.conf.auto" >> "$RESOLV_DAED"
fi
if [ ! -s "$RESOLV_DAED" ]; then
echo "nameserver 223.5.5.5" > "$RESOLV_DAED"
echo "nameserver 119.29.29.29" >> "$RESOLV_DAED"
fi
mount --bind "$RESOLV_DAED" /tmp/resolv.conf
mount -o remount,ro,bind /tmp/resolv.conf
}
restore_resolv_conf() {
grep -q ' /tmp/resolv\.conf ' /proc/mounts || return 0
mount -o remount,rw,bind /tmp/resolv.conf 2>/dev/null
umount /tmp/resolv.conf 2>/dev/null
rm -f "$RESOLV_DAED"
}
start_service() {
[ -f "/etc/init.d/daed" ] && grep -q "DAE_LOCATION_ASSET" "/etc/init.d/daed" || sed -i '/run/i\ procd_set_param env DAE_LOCATION_ASSET="/usr/share/v2ray"' "/etc/init.d/daed"
config_load "$CONF"
local enabled
config_get_bool enabled "config" "enabled" "0"
if [ "$enabled" -eq 0 ]; then
update_cron "remove"
restore_resolv_conf
return 1
fi
update_cron "add"
hijack_resolv_conf
}
stop_service() {
update_cron "remove"
restore_resolv_conf
}
service_triggers() {
procd_add_reload_trigger "$CONF"
}