luci-app-mosdns/luci-app-mosdns/root/etc/init.d/mosdns
2022-05-01 11:52:06 +08:00

95 lines
2.3 KiB
Bash
Executable File

#!/bin/sh /etc/rc.common
# Copyright (C) 2020 IrineSistiana
# shellcheck disable=SC2034
START=91
USE_PROCD=1
PROG=/usr/bin/mosdns
CONF=$(uci -q get mosdns.mosdns.configfile)
CRON_FILE=/etc/crontabs/root
inital_conf() {
config_load "mosdns"
config_get "enabled" "mosdns" "enabled" "0"
}
service_triggers() {
procd_add_reload_trigger "mosdns"
}
restore_setting() {
sed -i "/list server/d" /etc/config/dhcp
uci set dhcp.@dnsmasq[0].noresolv='0'
uci del dhcp.@dnsmasq[0].cachesize
uci commit dhcp
}
redirect_setting() {
redirect=$(uci -q get mosdns.mosdns.redirect)
if [ "$redirect" -eq 1 ]; then
sed -i "/list server/d" /etc/config/dhcp
uci add_list dhcp.@dnsmasq[0].server="127.0.0.1#$(uci -q get mosdns.mosdns.listen_port)"
uci set dhcp.@dnsmasq[0].rebind_protection='0'
uci set dhcp.@dnsmasq[0].noresolv="1"
uci set dhcp.@dnsmasq[0].cachesize='0'
uci commit dhcp
fi
}
reload_others() {
/etc/init.d/network reload
/etc/init.d/dnsmasq reload
}
reload_service() {
stop
sleep 1
start
}
setcron() {
sed -i '/update_geodat.sh/d' $CRON_FILE 2>/dev/null
[ "$(uci -q get mosdns.mosdns.geo_auto_update)" -eq 1 ] && echo "0 $(uci -q get mosdns.mosdns.geo_update_day_time) * * $(uci -q get mosdns.mosdns.geo_update_week_time) /usr/share/mosdns/update_geodat.sh" >>$CRON_FILE
crontab $CRON_FILE
}
delcron() {
sed -i '/update_geodat.sh/d' $CRON_FILE 2>/dev/null
crontab $CRON_FILE
}
start_service() {
inital_conf
[ $enabled != 1 ] && return 1
delcron
setcron
cat /dev/null > $(uci -q get mosdns.mosdns.logfile)
procd_open_instance mosdns
procd_set_param command $PROG -c "$CONF"
procd_set_param user root
procd_set_param stdout 1
procd_set_param stderr 1
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
procd_close_instance mosdns
configfile=$(uci -q get mosdns.mosdns.configfile)
if [ "${configfile}" = "/etc/mosdns/config.yaml" ]; then
restore_setting
redirect_setting
reload_others
fi
}
stop_service() {
pgrep -f /usr/bin/mosdns | xargs kill -9
configfile=$(uci -q get mosdns.mosdns.configfile)
if [ "${configfile}" = "/etc/mosdns/config.yaml" ]; then
config_load "mosdns"
enabled=$(uci -q get mosdns.mosdns.enabled)
[ "${enabled}" = "0" ] && restore_setting
reload_others
fi
delcron
}