From d67faae180c7f47c5c3b9eecf9c01772c00ee6d4 Mon Sep 17 00:00:00 2001 From: sbwml Date: Thu, 11 Aug 2022 02:06:28 +0800 Subject: [PATCH] luci-app-mosdns: cleanup shell script --- luci-app-mosdns/luasrc/controller/mosdns.lua | 4 +- .../luasrc/model/cbi/mosdns/update.lua | 2 +- luci-app-mosdns/root/etc/init.d/mosdns | 14 ++--- .../root/usr/share/mosdns/ad_domain.sh | 8 --- luci-app-mosdns/root/usr/share/mosdns/dns.sh | 18 ------ .../root/usr/share/mosdns/library.sh | 29 --------- .../root/usr/share/mosdns/log_path.sh | 9 --- .../root/usr/share/mosdns/mosdns.sh | 60 +++++++++++++++++++ .../root/usr/share/mosdns/update_geodat.sh | 18 ------ 9 files changed, 70 insertions(+), 92 deletions(-) delete mode 100755 luci-app-mosdns/root/usr/share/mosdns/ad_domain.sh delete mode 100755 luci-app-mosdns/root/usr/share/mosdns/dns.sh delete mode 100755 luci-app-mosdns/root/usr/share/mosdns/library.sh delete mode 100755 luci-app-mosdns/root/usr/share/mosdns/log_path.sh create mode 100755 luci-app-mosdns/root/usr/share/mosdns/mosdns.sh delete mode 100755 luci-app-mosdns/root/usr/share/mosdns/update_geodat.sh diff --git a/luci-app-mosdns/luasrc/controller/mosdns.lua b/luci-app-mosdns/luasrc/controller/mosdns.lua index 52f9634..2cfa899 100644 --- a/luci-app-mosdns/luasrc/controller/mosdns.lua +++ b/luci-app-mosdns/luasrc/controller/mosdns.lua @@ -26,9 +26,9 @@ function act_status() end function get_log() - luci.http.write(luci.sys.exec("cat $(/usr/share/mosdns/log_path.sh)")) + luci.http.write(luci.sys.exec("cat $(/usr/share/mosdns/mosdns.sh logfile)")) end function clear_log() - luci.sys.call("cat /dev/null > $(/usr/share/mosdns/log_path.sh)") + luci.sys.call("cat /dev/null > $(/usr/share/mosdns/mosdns.sh logfile)") end diff --git a/luci-app-mosdns/luasrc/model/cbi/mosdns/update.lua b/luci-app-mosdns/luasrc/model/cbi/mosdns/update.lua index 4f71de6..323d1f2 100644 --- a/luci-app-mosdns/luasrc/model/cbi/mosdns/update.lua +++ b/luci-app-mosdns/luasrc/model/cbi/mosdns/update.lua @@ -28,7 +28,7 @@ data_update = s:option(Button, "geo_update_database", translate("Database Update data_update.inputtitle = translate("Check And Update") data_update.inputstyle = "reload" data_update.write = function() - luci.sys.exec("/usr/share/mosdns/update_geodat.sh &> /dev/null &") + luci.sys.exec("/usr/share/mosdns/mosdns.sh geodata &> /dev/null &") end return m diff --git a/luci-app-mosdns/root/etc/init.d/mosdns b/luci-app-mosdns/root/etc/init.d/mosdns index f657952..4f8f570 100755 --- a/luci-app-mosdns/root/etc/init.d/mosdns +++ b/luci-app-mosdns/root/etc/init.d/mosdns @@ -38,11 +38,11 @@ init_yaml() { cache_survival_time=$(uci -q get mosdns.mosdns.cache_survival_time) minimal_ttl_custom=$(uci -q get mosdns.mosdns.minimal_ttl) maximum_ttl_custom=$(uci -q get mosdns.mosdns.maximum_ttl) - local_dns1=$(/usr/share/mosdns/dns.sh 0) - local_dns2=$(/usr/share/mosdns/dns.sh 1) + local_dns1=$(/usr/share/mosdns/mosdns.sh dns 0) + local_dns2=$(/usr/share/mosdns/mosdns.sh dns 1) remote_dns1=$(uci -q get mosdns.mosdns.remote_dns1) remote_dns2=$(uci -q get mosdns.mosdns.remote_dns2) - adblock=$(/usr/share/mosdns/ad_domain.sh) + adblock=$(/usr/share/mosdns/mosdns.sh ad) enable_pipeline=$(uci -q get mosdns.mosdns.enable_pipeline) [ $enable_pipeline = 0 ] && enable_pipeline=false [ $enable_pipeline = 1 ] && enable_pipeline=true @@ -98,13 +98,13 @@ reload_service() { } 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 + sed -i '/mosdns.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/mosdns.sh geodata" >>$CRON_FILE crontab $CRON_FILE } delcron() { - sed -i '/update_geodat.sh/d' $CRON_FILE 2>/dev/null + sed -i '/mosdns.sh/d' $CRON_FILE 2>/dev/null crontab $CRON_FILE } @@ -114,7 +114,7 @@ start_service() { delcron setcron [ "${CONF}" = "/etc/mosdns/config.yaml" ] && init_yaml - cat /dev/null > $(/usr/share/mosdns/log_path.sh) + cat /dev/null > $(/usr/share/mosdns/mosdns.sh logfile) procd_open_instance mosdns procd_set_param command $PROG start -c "$CONF" procd_set_param user root diff --git a/luci-app-mosdns/root/usr/share/mosdns/ad_domain.sh b/luci-app-mosdns/root/usr/share/mosdns/ad_domain.sh deleted file mode 100755 index 9a65101..0000000 --- a/luci-app-mosdns/root/usr/share/mosdns/ad_domain.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh - -adblock=$(uci -q get mosdns.mosdns.adblock) -if [ "$adblock" -eq 1 ];then - echo "provider:geosite:category-ads-all" -else - echo "full:disable-category-ads-all.null" -fi diff --git a/luci-app-mosdns/root/usr/share/mosdns/dns.sh b/luci-app-mosdns/root/usr/share/mosdns/dns.sh deleted file mode 100755 index d28c66d..0000000 --- a/luci-app-mosdns/root/usr/share/mosdns/dns.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/sh - -source /usr/share/mosdns/library.sh - -if [ "$(ifconfig | grep -c wan)" = 0 ]; then - bakdns "$1" - exit 0 -fi - -if [ $1 ]; then - if [[ "$(getdns 0)" =~ ^127\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then - getdns "$1" inactive - elif [[ "$(getdns "$1")" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then - getdns "$1" - else - bakdns "$1" - fi -fi diff --git a/luci-app-mosdns/root/usr/share/mosdns/library.sh b/luci-app-mosdns/root/usr/share/mosdns/library.sh deleted file mode 100755 index 847ed56..0000000 --- a/luci-app-mosdns/root/usr/share/mosdns/library.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/sh - -bakdns() { - if [ "$1" == "0" ]; then - echo "119.29.29.29" - elif [ "$1" == "1" ]; then - echo "114.114.114.114" - fi -} - -exist() { - command -v "$1" >/dev/null 2>&1 -} - -getdat() { - curl --connect-timeout 60 -m 900 -fSLo "$TMPDIR/$1" "https://github.cooluc.com/https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/$1" -} - -getdns() { - if [ "$2" == "inactive" ]; then - ubus call network.interface.wan status | jsonfilter -e "@['inactive']['dns-server'][$1]" - else - ubus call network.interface.wan status | jsonfilter -e "@['dns-server'][$1]" - fi -} - -pid() { - pgrep -f "$1" -} diff --git a/luci-app-mosdns/root/usr/share/mosdns/log_path.sh b/luci-app-mosdns/root/usr/share/mosdns/log_path.sh deleted file mode 100755 index db3298c..0000000 --- a/luci-app-mosdns/root/usr/share/mosdns/log_path.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/sh - -configfile=$(uci -q get mosdns.mosdns.configfile) -if [ "$configfile" = "/etc/mosdns/config.yaml" ]; then - uci -q get mosdns.mosdns.logfile -else - [ ! -f /etc/mosdns/config_custom.yaml ] && exit 1 - cat /etc/mosdns/config_custom.yaml | grep -A 3 log | grep file | awk -F ":" '{print $2}' | sed 's/\"//g;s/ //g' -fi diff --git a/luci-app-mosdns/root/usr/share/mosdns/mosdns.sh b/luci-app-mosdns/root/usr/share/mosdns/mosdns.sh new file mode 100755 index 0000000..13493b0 --- /dev/null +++ b/luci-app-mosdns/root/usr/share/mosdns/mosdns.sh @@ -0,0 +1,60 @@ +#!/bin/sh + +logfile_path() ( + configfile=$(uci -q get mosdns.mosdns.configfile) + if [ "$configfile" = "/etc/mosdns/config.yaml" ]; then + uci -q get mosdns.mosdns.logfile + else + [ ! -f /etc/mosdns/config_custom.yaml ] && exit 1 + cat /etc/mosdns/config_custom.yaml | grep -A 4 log | grep file | awk -F ":" '{print $2}' | sed 's/\"//g;s/ //g' + fi +) + +interface_dns() ( + peerdns=$(uci -q get network.wan.peerdns) + if [ "$peerdns" -eq 0 ]; then + ubus call network.interface.wan status | jsonfilter -e "@['dns-server'][$1]" + else + ubus call network.interface.wan status | jsonfilter -e "@['inactive']['dns-server'][$1]" + fi + [ $? -ne 0 ] && echo 119.29.29.29 +) + +ad_block() ( + adblock=$(uci -q get mosdns.mosdns.adblock) + if [ "$adblock" -eq 1 ]; then + echo "provider:geosite:category-ads-all" + else + echo "full:disable-category-ads-all.null" + fi +) + +geodat_update() ( + geodat_download() ( + google_status=$(curl -I -4 -m 3 -o /dev/null -s -w %{http_code} http://www.google.com/generate_204) + [ $google_status -ne "204" ] && mirror="https://github.cooluc.com/" + echo -e "\e[1;32m"$mirror"https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/$1\e[0m" + curl --connect-timeout 60 -m 900 -fSLo "$TMPDIR/$1" ""$mirror"https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/$1" + ) + TMPDIR=$(mktemp -d) || exit 1 + geodat_download geoip.dat + geodat_download geosite.dat + if [ "$(grep -o CN "$TMPDIR"/geoip.dat | wc -l)" -eq "0" ]; then + rm -rf "$TMPDIR"/geoip.dat + elif [ "$(grep -o .com "$TMPDIR"/geosite.dat | wc -l)" -lt "1000" ]; then + rm -rf "$TMPDIR"/geosite.dat + fi + cp -f "$TMPDIR"/* /usr/share/v2ray + rm -rf "$TMPDIR" +) + +if [ "$1" == "dns" ]; then + [ -z $2 ] && exit 0 + interface_dns $2 +elif [ "$1" == "ad" ]; then + ad_block +elif [ "$1" == "geodata" ]; then + geodat_update +elif [ "$1" == "logfile" ]; then + logfile_path +fi diff --git a/luci-app-mosdns/root/usr/share/mosdns/update_geodat.sh b/luci-app-mosdns/root/usr/share/mosdns/update_geodat.sh deleted file mode 100755 index 3359c1d..0000000 --- a/luci-app-mosdns/root/usr/share/mosdns/update_geodat.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/sh - -set -o pipefail -source /usr/share/mosdns/library.sh - -TMPDIR=$(mktemp -d) || exit 1 -getdat geoip.dat -getdat geosite.dat -if [ "$(grep -o CN "$TMPDIR"/geoip.dat | wc -l)" -eq "0" ]; then - rm -rf "$TMPDIR"/geoip.dat -fi -if [ "$(grep -o .com "$TMPDIR"/geosite.dat | wc -l)" -lt "1000" ]; then - rm -rf "$TMPDIR"/geosite.dat -fi -cp -rf "$TMPDIR"/* /usr/share/v2ray -rm -rf "$TMPDIR" - -exit 0