luci-app-mosdns: init.d: optimizing Scripts

* fix `service_triggers` restart service cache data loss
This commit is contained in:
sbwml 2023-03-25 06:39:55 +08:00
parent bd89c52b99
commit 8c968d1f34

View File

@ -2,6 +2,8 @@
# #
# Copyright (C) 2020-2022, IrineSistiana # Copyright (C) 2020-2022, IrineSistiana
# #
# Copyright (C) 2023, sbwml <admin@cooluc.com>
#
# This file is part of mosdns. # This file is part of mosdns.
# #
# mosdns is free software: you can redistribute it and/or modify # mosdns is free software: you can redistribute it and/or modify
@ -25,6 +27,10 @@ PROG=/usr/bin/mosdns
CONF=$(uci -q get mosdns.config.configfile) CONF=$(uci -q get mosdns.config.configfile)
CRON_FILE=/etc/crontabs/root CRON_FILE=/etc/crontabs/root
V2DAT_DIR=/usr/share/v2ray V2DAT_DIR=/usr/share/v2ray
DUMP_FILE=/etc/mosdns/cache.dump
DUMP_FILE_DEFAULT=/usr/share/mosdns/cache.dump
DEFAULT_CONF=/usr/share/mosdns/default.yaml
MOSDNS_SCRIPT=/usr/share/mosdns/mosdns.sh
get_config() { get_config() {
config_get enabled $1 enabled 0 config_get enabled $1 enabled 0
@ -51,25 +57,24 @@ get_config() {
init_yaml() { init_yaml() {
tmpdir=$(mktemp -d) || exit 1 tmpdir=$(mktemp -d) || exit 1
adlist=$(/usr/share/mosdns/mosdns.sh ad) adlist=$($MOSDNS_SCRIPT ad)
[ $enable_pipeline = 1 ] && enable_pipeline=true || enable_pipeline=false [ $enable_pipeline = 1 ] && enable_pipeline=true || enable_pipeline=false
[ "$custom_local_dns" -eq 1 ] && \ [ "$custom_local_dns" -eq 1 ] && \
local_dns=$(/usr/share/mosdns/mosdns.sh dns | awk '{for(i=1;i<=NF;i++)printf "%s- addr: %s\n%s bootstrap: '${bootstrap_dns}'\n%s enable_pipeline: '${enable_pipeline}'\n",s,$i,s,s}' s=' ') || \ local_dns=$($MOSDNS_SCRIPT dns | awk '{for(i=1;i<=NF;i++)printf "%s- addr: %s\n%s bootstrap: '${bootstrap_dns}'\n%s enable_pipeline: '${enable_pipeline}'\n",s,$i,s,s}' s=' ') || \
local_dns=$(/usr/share/mosdns/mosdns.sh dns | awk '{for(i=1;i<=NF;i++)printf "%s- addr: %s\n",s,$i,s}' s=' ') local_dns=$($MOSDNS_SCRIPT dns | awk '{for(i=1;i<=NF;i++)printf "%s- addr: %s\n",s,$i,s}' s=' ')
remote_dns=$(echo $remote_dns | awk '{for(i=1;i<=NF;i++)printf "%s- addr: %s\n%s bootstrap: '${bootstrap_dns}'\n%s enable_pipeline: '${enable_pipeline}'\n",s,$i,s,s}' s=' ') remote_dns=$(echo $remote_dns | awk '{for(i=1;i<=NF;i++)printf "%s- addr: %s\n%s bootstrap: '${bootstrap_dns}'\n%s enable_pipeline: '${enable_pipeline}'\n",s,$i,s,s}' s=' ')
sed "s,log_level,$log_level,g;s,log_file,$log_file,g; \ sed "s,log_level,$log_level,g;s,log_file,$log_file,g; \
s,listen_port,$listen_port,g;s,cache_size,$cache_size,g; \ s,listen_port,$listen_port,g;s,cache_size,$cache_size,g; \
s,cache_survival_time,$cache_survival_time,g; \ s,cache_survival_time,$cache_survival_time,g; \
s,minimal_ttl_custom,$minimal_ttl_custom,g; \ s,minimal_ttl_custom,$minimal_ttl_custom,g; \
s,maximum_ttl_custom,$maximum_ttl_custom,g; \ s,maximum_ttl_custom,$maximum_ttl_custom,g; \
s,adblock,$adlist,g;s,remote_dns_pipeline,$enable_pipeline,g" \ s,adblock,$adlist,g;s,remote_dns_pipeline,$enable_pipeline,g" $DEFAULT_CONF > $CONF
/usr/share/mosdns/default.yaml > $CONF
[ "$dump_file" -eq 1 ] && sed -i "/lazy_cache_ttl/a\ dump_file: /etc/mosdns/cache.dump\n dump_interval: $dump_interval" $CONF || \
\cp -a /usr/share/mosdns/cache.dump /etc/mosdns/cache.dump
echo "${local_dns}" > $tmpdir/local_dns.txt echo "${local_dns}" > $tmpdir/local_dns.txt
echo "${remote_dns}" > $tmpdir/remote_dns.txt echo "${remote_dns}" > $tmpdir/remote_dns.txt
sed -i -e '/- addr: local_dns/{r '$tmpdir/local_dns.txt -e';d};/- addr: remote_dns/{r '$tmpdir/remote_dns.txt -e';d}' \ sed -i -e '/- addr: local_dns/{r '$tmpdir/local_dns.txt -e';d};/- addr: remote_dns/{r '$tmpdir/remote_dns.txt -e';d}' $CONF
$CONF [ "$dump_file" -eq 1 ] && sed -i "/lazy_cache_ttl/a\ dump_file: $DUMP_FILE\n dump_interval: $dump_interval" $CONF
[ "$dump_file" -eq 1 ] && [ ! -f $DUMP_FILE ] && cp -a $DUMP_FILE_DEFAULT $DUMP_FILE
[ "$dump_file" -eq 0 ] && \cp -a $DUMP_FILE_DEFAULT $DUMP_FILE
rm -rf $tmpdir rm -rf $tmpdir
} }
@ -117,7 +122,7 @@ reload_service() {
setcron() { setcron() {
sed -i '/mosdns.sh/d' $CRON_FILE 2>/dev/null sed -i '/mosdns.sh/d' $CRON_FILE 2>/dev/null
[ "$geo_auto_update" -eq 1 ] && echo "0 $geo_update_day_time * * $geo_update_week_time /usr/share/mosdns/mosdns.sh geodata" >>$CRON_FILE [ "$geo_auto_update" -eq 1 ] && echo "0 $geo_update_day_time * * $geo_update_week_time $MOSDNS_SCRIPT geodata" >>$CRON_FILE
crontab $CRON_FILE crontab $CRON_FILE
} }
@ -140,8 +145,8 @@ start_service() {
[ $enabled != 1 ] && return 1 [ $enabled != 1 ] && return 1
delcron ; setcron delcron ; setcron
[ "${CONF}" = "/etc/mosdns/config.yaml" ] && init_yaml [ "${CONF}" = "/etc/mosdns/config.yaml" ] && init_yaml
:> $(/usr/share/mosdns/mosdns.sh logfile) :> $($MOSDNS_SCRIPT logfile)
v2dat_dump >> $(/usr/share/mosdns/mosdns.sh logfile) 2>&1 v2dat_dump >> $($MOSDNS_SCRIPT logfile) 2>&1
procd_open_instance mosdns procd_open_instance mosdns
procd_set_param command $PROG start -c "$CONF" procd_set_param command $PROG start -c "$CONF"
procd_set_param user root procd_set_param user root
@ -157,7 +162,7 @@ start_service() {
[ -s "/etc/mosdns/rule/adlist.txt" ] && [ -f "/etc/mosdns/rule/.ad_source" ] && url_adlist=$(cat /etc/mosdns/rule/.ad_source) || update_list=1 [ -s "/etc/mosdns/rule/adlist.txt" ] && [ -f "/etc/mosdns/rule/.ad_source" ] && url_adlist=$(cat /etc/mosdns/rule/.ad_source) || update_list=1
[ "$ad_source" != "$url_adlist" ] && update_list=1 [ "$ad_source" != "$url_adlist" ] && update_list=1
} }
[ "$update_list" -eq 1 ] && /usr/share/mosdns/mosdns.sh adlist_update &> /dev/null & [ "$update_list" -eq 1 ] && $MOSDNS_SCRIPT adlist_update &> /dev/null &
} }
stop_service() { stop_service() {