105 lines
2.6 KiB
Bash
Executable File
105 lines
2.6 KiB
Bash
Executable File
#!/bin/sh /etc/rc.common
|
|
# Copyright (C) 2020 IrineSistiana
|
|
# shellcheck disable=SC2034
|
|
|
|
START=91
|
|
USE_PROCD=1
|
|
|
|
##### ONLY CHANGE THIS BLOCK ######
|
|
PROG=/usr/bin/mosdns # where is mosdns
|
|
RES_DIR=/etc/mosdns/ # resource dir / working dir / the dir where you store ip/domain lists
|
|
CONF=$(uci -q get mosdns.mosdns.configfile) # where is the config file, it can be a relative path to $RES_DIR
|
|
CRON_FILE=/etc/crontabs/root
|
|
##### ONLY CHANGE THIS BLOCK ######
|
|
|
|
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#5335'
|
|
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 2s
|
|
echo "MosDNS is restarted!"
|
|
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() {
|
|
|
|
# Reading config
|
|
inital_conf
|
|
[ $enabled != 1 ] && return 1
|
|
delcron
|
|
setcron
|
|
procd_open_instance mosdns
|
|
procd_set_param command $PROG -dir $RES_DIR -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
|
|
echo "MosDNS turn on"
|
|
}
|
|
|
|
stop_service() {
|
|
|
|
pgrep -f /usr/bin/mosdns | xargs kill -9
|
|
echo "MosDNS turn off"
|
|
|
|
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
|
|
|
|
}
|