🌴 Sync 2026-03-05 23:51:42

This commit is contained in:
github-actions[bot] committed 2026-03-05 23:51:42 +08:00
1 parent 97c049ed0a
commit 0acff03804
14253 files changed
+3893239

No files matched your search

+216
View File
@@ -0,0 +1,216 @@
#!/bin/sh
# [K]2022
# https://github.com/kongfl888
P1=${1}
Lovely(){
local str=""
local DATE_S=`date +%S`
local t=`expr $DATE_S % 5`
if [ "$t" == "1" ];then
str="Hi Master, I'm your dnshelper. I'm very happy that you can come to see me."
elif [ "$t" == "2" ];then
str="Ha, what a good day today. Master."
elif [ "$t" == "3" ];then
str="ZZzz...Master?"
elif [ "$t" == "4" ];then
str="Oh, Master, nice to see you again."
else
str="Once upon a time,
there were a master and its helper who lived together under the sky.
And...The story is over. Thank you for watching.
Oh, that's a great story, isn't it? Master."
fi
echo ""
echo "$str"
echo ""
exit 0
}
Helpthis() {
DATE_HMS=`date +%H:%M:%S`
DATE_Y=`date +%Y`
DATE="$DATE_Ymd $DATE_HMS"
echo "
Hi Master, I'm your dnshelper. You can use me in the following ways.
usage: my-dnshelper [OPTION]
-h,--help display this help and exit.
It is juest a part of me. You still need a luci application.
Thank you for coming to look at me. I'm very happy.
By MY-Dnshelper on $DATE
[K] $DATE_Y
You can contact my Founder via this link: https://github.com/kongfl888
"
exit 0
}
readonly delay=10
source "/usr/share/my-dnshelper/comm"
c=0
rmaker="$sharedir/rulesmaker"
upruler="$sharedir/uprules"
guard=0
bootn=1
errnet=0
lnfix=0
[ "$S" = "1" ] && SPath=$EPath/Rules
case $P1 in
"-h"|"--help"|"-H")
Helpthis
;;
"-g"|"-G")
guard=1
;;
*)
Lovely
;;
esac
clear_log(){
local ss=5
[ -n "$1" ] || return
[ -s $1 ] || return
[ -z "$2" ] || ss=$2
local dlogsize=0
local ssize=1048576
local sss=$ss
local sst=1
echo ${sss} | grep -q "\." && sst=10 && ss=`echo ${sss}|cut -d '.' -f2|grep -oE '\d'|head -n1`
ssize=$(($ss * 1048576 / $sst))
[ -s $1 ] && dlogsize=`ls -l $1 | awk '{ print $5 }'`
if [ $dlogsize -gt $ssize ]; then
echo_log "Initialize log. Log size is set to ${ss} MB." > $1
fi
}
boot_fix(){
local bmin=0
local bday=0
[ $bootn -eq 0 ] && return
[ -f $updatelog ] && bootn=0 && return
bday=`uptime | grep -co "day"`
[ $bday -gt 0 ] && bootn=0 && return
if [ $(uptime | grep -oE "[0-9]+\s*min" 2>/dev/null|wc -l) -eq 1 ];then
bmin=`uptime | grep -oE "[0-9]+\s*min"|grep -oE "[0-9]+"`
[ -n "$bmin" ] || bmin=0
if [ $bmin -gt 8 ];then
bootn=0
else
bootn=1
debug_log "boot waiting."
[ $(ping_1p "baidu.com") -eq 0 ] && $upruler
fi
fi
}
GuardA(){
if [ "$EA" != "1" ]; then
[ -f /etc/init.d/my-dnshelper ] && /etc/init.d/my-dnshelper stop &
return
fi
[ -f /usr/bin/my-dnshelper ] || return
[ -f /etc/config/my-dnshelper ] || return
[ -f $rmaker ] || return
if [ ! -f "$helperconf" ];then
sleep 3s
[ -f "$helperconf" ] || $rmaker >> $Logfile
fi
if [ "$UDH" == "1" ];then
[ -n "$(get_ports_ps)" -a ! -s "$dohconf" ] && $rmaker -d >> $Logfile && sleep 1s && debug_log "start because doh config is none."
[ -z "$(get_ports_ps)" ] && sleep 3s
[ -z "$(get_ports_ps)" -a -n "$(get_ports_cfg)" -a -s "$dohconf" ] && $rmaker -d >> $Logfile && sleep 1s && debug_log "start because ps doh is none."
fi
[ -f $resolvconf ] || touch $resolvconf
[ -s $resolvconf -a "$UDH" == "0" -a "$(get_rautoc)" != "$resolvconf" ] && $rmaker -d >> $Logfile && sleep 1s && debug_log "start because resolv not be set."
if [ -h /tmp/resolv.conf ];then
debug_log "Helper - dnsmasq may failed."
[ $lnfix -eq 0 ] && sleep 3s && dnsmasq_rs 1 >> $Logfile
sleep 2s
if [ -h /tmp/resolv.conf ];then
if [ $(check_dnsmasq) -eq 0 ];then
rm -f /tmp/resolv.conf
echo "search $DDOMAIN" > /tmp/resolv.conf
echo "nameserver 127.0.0.1" >> /tmp/resolv.conf
if [ -s $resolvconf ];then
grep -q ":" $resolvconf && echo echo "nameserver ::1" >> /tmp/resolv.conf
elif [ "$forceip46" != "1" ];then
echo echo "nameserver ::1" >> /tmp/resolv.conf
fi
fi
let lnfix++
else
lnfix=0
fi
[ $lnfix -gt 60 ] && lnfix=0
fi
if [ "$dnscheck" == "1" -a $(ping_1p "114.114.114.114") -eq 0 -a $(ping_1p "baidu.com") -ne 0 ];then
push_log "Helper - checking network."
push_log "[Error]DNS may have some problems. Something were wrong."
push_log "Please restart my-dnshelper, or check other app, or check your network."
let errnet++
[ "$UDH" == "1" -a $errnet -eq 10 ] && $rmaker -d >> $Logfile && sleep 1s && debug_log "start to fix dns."
else
errnet=0
fi
[ "$(check_ln)" == "1" ] && $rmaker >> $Logfile && sleep 1s && debug_log "start because LNs missed."
boot_fix
if [ "$AUD" == "1" -a $bootn -eq 0 ]; then
oldt=$(date -d "`cat $updatelog 2>/dev/null`" +%s 2>/dev/null)
nowt=$(date +%s)
[ -z "$oldt" ] && oldt=$nowt
ctime=`expr $nowt - $oldt`
utime=`expr $UPD \* 3600`
[ "$UPD" == "0" ] && utime=1800
[ $ctime -gt $utime ] && $upruler && sleep 1s
fi
if [ $(test_fcode "config") -eq 1 ];then
debug_log "config changed"
$rmaker >> $Logfile && sleep 1s
write_fcodes
fi
if [ $(test_fcode "ip") -eq 1 ];then
debug_log "ip rules changed"
$rmaker -i >> $Logfile && sleep 1s
fi
if [ $(test_fcode "user") -eq 1 ];then
debug_log "user rules changed"
$rmaker "--usr" >> $Logfile && sleep 1s
fi
if [ $(test_fcode "dns") -eq 1 ];then
debug_log "dns changed"
$rmaker -d >> $Logfile && sleep 1s
fi
if [ $(test_fcode "bstrap") -eq 1 ];then
debug_log "bstrap changed"
$rmaker -d >> $Logfile && sleep 1s
fi
}
if [ "$S" != "1" ]; then
mkdir -p $RPath
[ -d $EPath/Rules ] && cp -rf $EPath/Rules/* $RPath && rm -rf $EPath/Rules
fi
[ -d $LPath ] || sleep ${delay}s
debug_log "first start"
$rmaker >> $Logfile
write_fcodes
if [ "$S" != "1" ]; then
[ -e $EPath/Rules ] && rm -rf $EPath/Rules
fi
sleep 2s
while true; do
sleep 30s
GuardA
clear_log "$dnsmasqlog" "$dnslogsize"
clear_log "$Logfile" "0.5"
done
@@ -0,0 +1,28 @@
#!/bin/sh
# [K]2022
# https://github.com/kongfl888
[ -s $1 ] || exit
[ -f /tmp/ad_check.txt ] && rm -f /tmp/ad_check.txt
grep -oE "^address=\/\S*\.\S*[0-9a-zA-Z]\/((([0-9]+\.){3}[0-9]+(#[0-9]+)?)|#)?" $1 > /tmp/ad_check.txt
R=$?
grep -oE "^server=(\/.*\/)?[0-9a-zA-Z:\.]+(#[0-9]+)?\s*" $1 >> /tmp/ad_check.txt
S=$?
grep -oE "^ipset=\/.*\/[0-9a-zA-Z_\-]+\s*" $1 >> /tmp/ad_check.txt
T=$?
grep -oE "^nameserver\s+[0-9a-fA-F:\.]+\s*" $1 >> /tmp/ad_check.txt
N=$?
grep -oE "^bogus-nxdomain=[0-9a-fA-F:\.]+\s*" $1 >> /tmp/ad_check.txt
B=$?
if [ "$N" = "0" -o "$R" = "0" -o "$S" = "0" -o "$T" = "0" -o "$B" = "0" ];then
echo "`sort -u /tmp/ad_check.txt`" > /tmp/ad_check.txt
cp -f /tmp/ad_check.txt $1
fi
if [ $(grep -E "\S+" $1 2>/dev/null |wc -l) -eq 0 ]; then
sed -i '/^$/d' $1
fi
rm -f /tmp/ad_check.txt
@@ -0,0 +1,14 @@
#!/bin/sh
# [K]2022
# https://github.com/kongfl888
source /usr/share/my-dnshelper/comm
fwok=1
ubus call luci getInitList '{"name":"firewall"}' && fwok=1 || fwok=0
if [ $fwok -eq 0 ]; then
echo_log "Starting to check firewall."
echo_log "Firewall initd script may have some problem."
echo_log "Helper doesn't have firewall function installed yet. You need to check it by yourself."
fi
@@ -0,0 +1,82 @@
#!/bin/sh
# [K]2022
# https://github.com/kongfl888
pram=$1
source /usr/share/my-dnshelper/comm
lucimake="https://cdn.jsdelivr.net/gh/kongfl888/openwrt-my-dnshelper/luci-app-my-dnshelper/Makefile"
apmake="https://cdn.jsdelivr.net/gh/kongfl888/openwrt-my-dnshelper/my-dnshelper/Makefile"
mvertmpf="/tmp/makevertmp.txt"
newluciv=""
newappv=""
nowapv=""
nowluciv=""
outstr=""
appvtxt=$RPath/appver.txt
dateday=`date +%d`
appupv=0
needc=0
nowapv=`opkg list-installed my-dnshelper|grep -oE "\d+(\.\d+)*(-\d+)*"`
nowluciv=`opkg list-installed luci-app-my-dnshelper |grep -oE "\d+(\.\d+)*(-\d+)*"`
get_ver(){
[ -n "$1" ] || return
local v=""
local v1=""
local v2=""
if [ -s $1 ];then
v1=`cat $1 | grep "PKG_VERSION"|cut -d '=' -f2 | head -n 1`
v2=`cat $1 | grep "PKG_RELEASE"|cut -d '=' -f2 | head -n 1`
fi
[ -n "$v1" ] && v="${v1}-${v2}"
[ -z "$v2" -o "$v2" == "0" ] && v="${v1}"
echo "$v"
}
che_appver(){
[ $(ping_1p "baidu.com") -ne 0 ] && [ $(ping_1p "qq.com") -ne 0 ] && return 0
check_wgetcurl
echo -n > $mvertmpf
$downloader $mvertmpf $lucimake
newluciv=$(get_ver $mvertmpf)
echo -n > $mvertmpf
$downloader $mvertmpf $apmake
newappv=$(get_ver $mvertmpf)
[ "$nowapv" == "$newappv" ] || outstr="my-dnshelper v${nowapv}(v${newappv} NEW)"
[ -n "${outstr}" ] && outstr="${outstr} |"
[ "$nowluciv" == "$newluciv" ] || outstr="${outstr} luci-app v${nowluciv}(v${newluciv} NEW)"
[ -d $RPath ] || mkdir -p $RPath
if [ -n "$outstr" ];then
echo "${outstr}#$dateday" > $appvtxt
else
echo "#$dateday" > $appvtxt
fi
appupv=1
rm -f $mvertmpf
}
[ "$pram" == "-f" ] && rm -f $appvtxt
if [ -f $appvtxt ]; then
#txtapv=`cat $appvtxt | grep -oE "my-dnshelper v\d+(\.\d+)*(-\d+)*"|grep -oE "\d+(\.\d+)*(-\d+)*"`
#txtluciv=`cat $appvtxt | grep -oE "luci-app v\d+(\.\d+)*(-\d+)*"|grep -oE "\d+(\.\d+)*(-\d+)*"`
#[ -n "$txtapv" -a "$txtapv" != "nowapv" ] && needc=1
#[ -n "$txtluciv" -a "$txtluciv" != "nowluciv" ] && needc=1
oldday=`cat $appvtxt | cut -d '#' -f2`
[ "$dateday" == "$oldday" ] || needc=1
[ $needc -eq 1 ] && rm -f $appvtxt
fi
[ -f $appvtxt ] || che_appver
cat $appvtxt | cut -d '#' -f1
@@ -0,0 +1,567 @@
#!/bin/sh
# [K]2022
# https://github.com/kongfl888
get_cfg_value(){
v=`uci -q get my-dnshelper.@my-dnshelper[0].$1 2>/dev/null`
if [ -z "$v" ]; then
v=""
[ -z "$2" ] || v=$2
fi
echo "$v"
}
DEBUG=0
[ -f /usr/share/my-dnshelper/debug ] && DEBUG=1
LOCK="/var/lock/my-dnshelper.lock"
SF=$(cd "$(dirname "$0")";pwd)
EA=`get_cfg_value "enable" "0"`
UF=`get_cfg_value "url"`
UA=`get_cfg_value "allowurl"`
UH=`get_cfg_value "hostsurl"`
GH=`get_cfg_value "my_github"`
S=`get_cfg_value "flash"`
AUD=`get_cfg_value "autoupdate"`
UPD=`get_cfg_value "time_update" "12"`
bio=`get_cfg_value "block_ios"`
bga=`get_cfg_value "block_games"`
bsh=`get_cfg_value "block_short"`
bgg=`get_cfg_value "block_google"`
UDH=`get_cfg_value "use_doh" "0"`
dcache=`get_cfg_value "dns_cache"`
hport=`get_cfg_value "doh_port"`
dnssec=`get_cfg_value "use_sec"`
usemul=`get_cfg_value "use_mul"`
usestrict=`get_cfg_value "use_strict"`
forcedoh=`get_cfg_value "force_doh"`
dohhttp=`get_cfg_value "doh_http"`
forceip46=`get_cfg_value "force_ip46" "1"`
filteraaaa=`get_cfg_value "filter_aaaa"`
dnslogon=`get_cfg_value "dnsmasq_log"`
dnslog=`get_cfg_value "dns_log"`
dnslogsize=`get_cfg_value "dnslog_size"`
dnslogpath=`get_cfg_value "dnslog_path"`
revlog=`get_cfg_value "rev_log"`
dnsdetect=`get_cfg_value "dns_detect"`
dnscheck=`get_cfg_value "dns_check"`
app_check=`get_cfg_value "app_check"`
FWI=$(uci -q get firewall.my_dnshelper.path 2>/dev/null)
DDOMAIN=$(uci -q get dhcp.@dnsmasq[0].domain)
localuse=$(uci -q get dhcp.@dnsmasq[0].localuse)
sharedir="/usr/share/my-dnshelper"
Logfile="/tmp/my-dnshelper.log"
D="date +'%Y-%m-%d %H:%M:%S'"
RPath="/tmp/my-dnshelper.d"
DEFAULT_DNSMASQ_CFGID="$(uci -q show "dhcp.@dnsmasq[0]" | awk 'NR==1 {split($0, conf, /[.=]/); print conf[2]}')"
if [ -f "/tmp/etc/dnsmasq.conf.$DEFAULT_DNSMASQ_CFGID" ]; then
DNSMASQ_CONF_DIR="$(awk -F '=' '/^conf-dir=/ {print $2}' "/tmp/etc/dnsmasq.conf.$DEFAULT_DNSMASQ_CFGID")"
if [ -n "$DNSMASQ_CONF_DIR" ]; then
DPath=${DNSMASQ_CONF_DIR%*/}
else
DPath="/tmp/dnsmasq.d"
fi
fi
EPath="/etc/my-dnshelper"
LPath="/var/etc/dnsmasq-my-dnshelper.d"
TPath="/tmp/dns_tmp"
helperconf="$DPath/dnsmasq-my-dnshelper.conf"
countf="$RPath/count.txt"
updatelog="$RPath/my-dnshelper.updated"
dnsmasqlog="/var/log/dnsmasq.log"
downloader="curl -kLfso"
TAG="_MY_DNSHELPER_RULE_"
confcode=""
ipfcode=""
usrcode=""
dnscode=""
bstrapcode=""
dhcpcode=""
hpcfcode=""
rulescf=""
rhostscf=""
usrcf=""
uhostscf=""
bgscf=""
SPath=$RPath
ifc=0
iac=0
ihc=0
[ "$S" == "1" ] && SPath=$EPath/Rules
[ -z "$FWI" ] && FWI="/var/etc/my-dnshelper.include"
[ -z "$DDOMAIN" ] && DDOMAIN="lan"
if [ -n "$dnslogpath" ]; then
if [ "$dnsmasqlog" != "$dnslogpath" ];then
dnsmasqlog="$dnslogpath"
[ -d $RPath ] || mkdir -p $RPath >/dev/null 2>&1
ln -sf $dnsmasqlog /var/log/dnsmasq.log >/dev/null 2>&1
fi
fi
if [ -z "$dnslogsize" ];then
dnslogsize="5242880"
else
echo "$dnslogsize"|grep -q -E "^[0-9]+$" || dnslogsize="5242880"
fi
[ -z "$UPD" ] && UPD="12"
dohconf="$SPath/dohcfg"
hpsvconf="$SPath/01.hpsv.conf"
bgsconf="$SPath/02.bgs.conf"
fcodeslist="/$SPath/fcodes.list"
httpsdpcfg="/etc/config/https-dns-proxy"
resolvconf="$EPath/resolv.dnsmasq.conf"
check_lock(){
local hy=0
local hp=0
ls $LOCK > /dev/null 2>&1 && hy=1
pgrep -f "rulesmaker" && hp=1
if [ $hy -eq 1 -a $hp -eq 0 ];then
rm -f $LOCK
fi
}
echo_log(){
echo "`eval $D` $*"
}
push_log(){
echo_log "$*" >> $Logfile
}
sort_file(){
[ -s ${1} ] || return
echo "`sort -u ${1} 2>/dev/null`" > ${1}
}
read_count(){
if [ -s $countf ];then
ifc=`cat $countf 2>/dev/null | cut -d ',' -f 1`
iac=`cat $countf 2>/dev/null | cut -d ',' -f 2`
ihc=`cat $countf 2>/dev/null | cut -d ',' -f 3`
[ -z "$ifc" ] && ifc=0
[ -z "$iac" ] && iac=0
[ -z "$ihc" ] && ihc=0
fi
}
get_count(){
if [ -s ${1} ]; then
local hv=0
hv=`sed -r "/^$/d" ${1} | grep -c -E ".*" 2>/dev/null`
[ -n "$hv" ] && echo "$hv" || echo "0"
else
echo "0"
fi
}
clean_wfiles(){
#clear $LPath but do not delete the dir,it's useful even if stop.
[ -d $LPath ] && rm -rf $LPath/*
[ -f $helperconf ] && rm -f $helperconf
}
clear_iprule(){
if [ -n "$(command -v nft)" ]; then
fw4 reload
nft flush set inet fw4 myipdrop 2>/dev/null
nft delete set inet fw4 myipdrop 2>/dev/null
else
iptables -w 2 -D FORWARD -m set --match-set myipdrop dst -m comment --comment "$TAG" -j DROP 2>/dev/null
iptables -w 2 -D OUTPUT -m set --match-set myipdrop dst -m comment --comment "$TAG" -j DROP 2>/dev/null
which ipset >/dev/null && ipset destroy myipdrop 2>/dev/null
fi
}
add_iprule(){
local i=""
[ -d $SPath ] || mkdir -p $SPath
if [ -n "$(command -v nft)" ]; then
nft add set inet fw4 myipdrop { type ipv4_addr \; flags interval \; }
for i in $(cat $EPath/ip.mdhp | tr -s '\n' | grep -v "^#" | grep -E "(\.((2(5[0-5]|[0-4][0-9]))|[0-1]?[0-9]{1,2})){3}");do
[ -n "$i" ] && nft add element inet fw4 myipdrop { $i };
done
nft add rule inet fw4 output ip saddr @myipdrop drop
nft add rule inet fw4 forward ip saddr @myipdrop drop
else
if [ -n "$(command -v ipset)" ]; then
ipset create myipdrop hash:net 2>/dev/null
for i in $(cat $EPath/ip.mdhp | tr -s '\n' | grep -v "^#" | grep -E "(\.((2(5[0-5]|[0-4][0-9]))|[0-1]?[0-9]{1,2})){3}");do
[ -n "$i" ] && ipset -! add myipdrop $i;
done
iptables -w -I FORWARD -m set --match-set myipdrop dst -m comment --comment "$TAG" -j DROP
iptables -w -I OUTPUT -m set --match-set myipdrop dst -m comment --comment "$TAG" -j DROP
fi
fi
}
ping_1p(){
local ok=1
ping -c 1 $1 >/dev/null 2>&1 && ok=0
echo $ok
return $ok
}
check_ipv4doh(){
echo `grep "listen_addr" $httpsdpcfg |grep "."|wc -l`
}
check_ipv6doh(){
echo `grep "listen_addr" $httpsdpcfg |grep ":"|wc -l`
}
get_ports_cfg(){
echo `grep "listen_port" $httpsdpcfg |grep -oE "\d+" |sort -u`
}
get_ports_ps(){
ps w 2>/dev/null | grep "https-dns-proxy" | grep -oE "\-p\s+\d+"|grep -oE "\d+"|sort -u
}
get_ports(){
local pt=$(get_ports_ps)
[ -z "$pt" ] && pt=$(get_ports_cfg)
echo "$pt"
}
get_nserver_cfg(){
local c=0
local nr=""
local nr1=""
local num=$(grep -c "config https-dns-proxy" $httpsdpcfg 2>/dev/null)
[ -z "$num" ] && num=0
while [ $c -lt $num ];
do
nr1="$(uci -q get https-dns-proxy.@https-dns-proxy[$c].listen_addr)"
nr="$nr1#`uci -q get https-dns-proxy.@https-dns-proxy[$c].listen_port`"
let c++
[ -n "$nr1" ] && echo "$nr"
done
}
get_dnsmasqcfg(){
local sr="/tmp/etc/dnsmasq.conf.cfg01411c"
[ -d "/var/etc" ] || return
for file in /var/etc/*; do
if [ -f $file ]; then
if [ `echo $file | grep 'dnsmasq.conf.cfg' -c` -gt 0 ]; then
sr="$file"
fi
fi
done
echo "$sr"
}
get_rautoc(){
local sr="$(uci -q get dhcp.@dnsmasq[0].resolvfile)"
if [ -z $sr ]; then
for s in `cat $(get_dnsmasqcfg)`;do
sr=`echo $s | grep -oE "^resolv-file=\S+" | cut -d "=" -f2`
[ -n "$sr" ] && break
done
fi
if [ -f "$sr" ]; then
echo "$sr"
else
if [ -f "/tmp/resolv.conf.d/resolv.conf.auto" ];then
echo "/tmp/resolv.conf.d/resolv.conf.auto"
else
echo ""
fi
fi
}
get_dhcpleasef(){
local sr="$(uci -q get dhcp.@dnsmasq[0].leasefile)"
if [ -z $sr ]; then
for s in `cat $(get_dnsmasqcfg)`;do
sr=`echo $s | grep -oE "^dhcp-leasefile=\S+" | cut -d "=" -f2`
[ -n "$sr" ] && break
done
fi
if [ -f "$sr" ]; then
echo "$sr"
else
if [ -f "/tmp/dhcp.leases" ];then
echo "/tmp/dhcp.leases"
else
echo ""
fi
fi
}
set_DPath(){
local dc=0
[ ! -d $DPath ] || mkdir -p $DPath
for s in `cat $(get_dnsmasqcfg)`;do
dc=`echo $s | grep "^conf-dir=$DPath" -c`
[ $dc -gt 0 ] && return 0
done
sed -i -r "/^conf-dir=/d" /etc/dnsmasq.conf
echo "conf-dir=$DPath" >> /etc/dnsmasq.conf
return 1
}
clear_doh(){
[ -f $httpsdpcfg ] || return 0
sed -i "/doh_backup_/d" /etc/config/dhcp
local num=$(grep -c "config https-dns-proxy" $httpsdpcfg 2>/dev/null)
[ -z "$num" ] && num=0
local c=0
while [ $c -lt $num ];
do
let c++
uci -q delete https-dns-proxy.@https-dns-proxy[-1] 2>/dev/null
[ $? -ne 0 ] && break
done
uci -q set https-dns-proxy.config.force_dns='0'
uci commit https-dns-proxy
}
chmod_files(){
local rautoc=$(get_rautoc)
[ -d $SPath ] && chmod 0774 $SPath/* >/dev/null 2>&1
[ -d $LPath ] && chmod 0777 $LPath/* >/dev/null 2>&1
[ -f $helperconf ] && chmod 0664 $helperconf >/dev/null 2>&1
[ -f $resolvconf ] && chmod 0664 $resolvconf >/dev/null 2>&1
[ -f $rautoc ] && chmod 0664 $rautoc >/dev/null 2>&1
[ -d /tmp/hosts ] && chmod +r /tmp/hosts/* >/dev/null 2>&1
[ -f $(get_dnsmasqcfg) ] && chmod +r $(get_dnsmasqcfg) >/dev/null 2>&1
[ -f $(get_dhcpleasef) ] && chmod +r $(get_dhcpleasef) >/dev/null 2>&1
[ -f /tmp/resolv.conf ] && chmod +r /tmp/resolv.conf >/dev/null 2>&1
}
check_masq53(){
local a=`netstat -nutlp 2>/dev/null | grep "dnsmasq"| grep "udp"| grep ":53\s" -c`
if [ "$a" == "0" -o -z "$a" ];then
pgrep -f "dnsmasq" >/dev/null 2>&1 && echo "1" || echo "-1"
else
echo "0"
fi
}
check_ln(){
local n=0
[ -s $SPath/rules.conf -a ! -s $LPath/rules.conf ] && n=1
[ -s $SPath/hosts.conf -a ! -s $LPath/hosts.conf ] && n=1
[ -s $SPath/user.conf -a ! -s $LPath/user.conf ] && n=1
[ -s $SPath/uhosts.conf -a ! -s $LPath/uhosts.conf ] && n=1
[ -s $hpsvconf -a ! -s $LPath/01.hpsv.conf ] && n=1
[ -s $bgsconf -a ! -s $LPath/02.bgs.conf ] && n=1
echo $n
}
file_idcode(){
local m5=0
local f1="$1"
if [ ! -f $1 ];then
echo ""
return
fi
which md5sum >/dev/null && m5=1
if [ $m5 -eq 1 ];then
echo "`md5sum $f1 2>/dev/null | awk '{print $1}'`"
else
echo "`date +%s -r $f1 2>/dev/null`"
fi
}
now_fcode(){
case $1 in
"config")
confcode=$(file_idcode "/etc/config/my-dnshelper")
echo "$confcode"
;;
"ip")
ipfcode=$(file_idcode "$EPath/ip.mdhp")
echo "$ipfcode"
;;
"user")
usrcode=$(file_idcode "$EPath/user.mdhp")
echo "$usrcode"
;;
"dns")
dnscode=$(file_idcode "$EPath/dns.mdhp")
echo "$dnscode"
;;
"bstrap")
bstrapcode=$(file_idcode "$EPath/bootstrap.mdhp")
echo "$bstrapcode"
;;
"dhcp")
dhcpcode=$(file_idcode "/etc/config/dhcp")
echo "$dhcpcode"
;;
"hpcf")
hpcfcode=$(file_idcode "$helperconf")
echo "$hpcfcode"
;;
"rulescf")
rcfcode=$(file_idcode "$SPath/rules.conf")
echo "$rcfcode"
;;
"rhostscf")
hcfcode=$(file_idcode "$SPath/hosts.conf")
echo "$hcfcode"
;;
"usrcf")
ucfcode=$(file_idcode "$SPath/user.conf")
echo "$ucfcode"
;;
"uhostscf")
uhcfcode=$(file_idcode "$SPath/uhosts.conf")
echo "$uhcfcode"
;;
"bgscf")
bgsfcode=$(file_idcode "$bgsconf")
echo "$bgsfcode"
;;
*)
echo ""
;;
esac
}
write_afcode(){
case $1 in
"config"|"ip"|"user"|"dns"|"bstrap"|"dhcp"|"hpcf"|"rulescf"|"rhostscf"|"usrcf"|"uhostscf"|"bgscf")
mkdir -p $SPath
sed -i "/$1/d" $fcodeslist 2>/dev/null
echo "$1|$(now_fcode $1)" >> $fcodeslist
return 0
;;
*)
return 1
;;
esac
}
write_cafcode(){
write_afcode "rulescf"
write_afcode "rhostscf"
write_afcode "usrcf"
write_afcode "uhostscf"
write_afcode "bgscf"
}
write_fcodes(){
echo -n > $fcodeslist
write_afcode "config"
write_afcode "ip"
write_afcode "user"
write_afcode "dns"
write_afcode "bstrap"
write_afcode "dhcp"
write_afcode "hpcf"
write_cafcode
}
get_fcode(){
[ -f $fcodeslist ] || (echo "" && return 0)
grep "$1" $fcodeslist 2>/dev/null | cut -d '|' -f2
}
test_fcode(){
local nowfc=$(now_fcode $1)
local recfc=$(get_fcode $1)
if [ "$recfc" == "$nowfc" ];then
echo "0"
return 0
else
echo "1"
return 1
fi
}
test_spcnf(){
local tsc=0
test_fcode "rulescf" >/dev/null && test_fcode "rhostscf" >/dev/null && test_fcode "usrcf" >/dev/null || tsc=1
test_fcode "uhostscf" >/dev/null && test_fcode "bgscf" >/dev/null && [ $tsc -eq 0 ] || tsc=1
echo $tsc
return $tsc
}
stop_httpsdp(){
if [ $(ps|grep "bin/https-dns-proxy"|grep -v "grep" |wc -l) -gt 0 ]; then
/etc/init.d/https-dns-proxy stop >/dev/null 2>&1
sleep 2s
fi
}
dnsmasq_rs(){
sleep 2s
[ "$localuse" == "1" ] || uci -q set dhcp.@dnsmasq[0].localuse="1" && uci commit dhcp
sleep 1s
if [ "$1" == "1" ];then
echo_log "Dnsmasq restart"
/etc/init.d/dnsmasq restart >/dev/null 2>&1
return
fi
if [ $(test_fcode "hpcf") -eq 1 ];then
echo_log "Dnsmasq restart."
/etc/init.d/dnsmasq restart >/dev/null 2>&1
write_afcode "hpcf"
elif [ $(test_spcnf) -eq 1 ];then
echo_log "Dnsmasq restart.."
/etc/init.d/dnsmasq restart >/dev/null 2>&1
write_cafcode
elif [ -d $LPath ];then
echo_log "Dnsmasq Reload."
/etc/init.d/dnsmasq reload >/dev/null 2>&1
else
echo_log "Dnsmasq restart..."
/etc/init.d/dnsmasq restart >/dev/null 2>&1
fi
}
rsc_dnsmasq(){
local svyes=0
dnsmasq_rs
}
check_dnsmasq(){
local dtest=0
local psget=0
local dg127=0
dtest=`dnsmasq --test 2>&1|grep -E "[Oo][kK]"|wc -l`
pgrep dnsmasq >/dev/null && psget=1
dg127=`netstat -nutlp | grep "dnsmasq"|grep "127.0.0.1"|wc -l`
if [ $dtest -gt 0 -a $psget -gt 0 -a $dg127 -gt 0 ];then
echo 0
return 0
else
echo 1
return 1
fi
}
check_addrtopmain(){
[ -n "$1" ] || return
[ -s $1 ] || return
local hr=0
grep -E "^\.[a-zA-Z]{2,6}\s*$" $1 && hr=1
grep -E "^(127|0)\.0\.0\.(0|1)\s+\.[a-zA-Z]{2,6}\s*$" $1 && hr=1
grep -E "^address=\/\.[a-zA-Z]{2,6}\/" $1 && hr=1
return $hr
}
check_wgetcurl(){
which curl >/dev/null && downloader="curl --retry 3 --connect-timeout 20 -m 80 -kLfso" && return
which wget-ssl >/dev/null && downloader="wget-ssl --no-check-certificate -t 3 -T 20 -q -O" && return
[ -z "$1" ] && opkg update || (echo error opkg && exit 1)
[ -z "$1" ] && (opkg remove wget wget-nossl --force-depends && opkg install wget-ssl ; check_wgetcurl 1 ; return)
[ "$1" == "1" ] && (opkg install curl ; check_wgetcurl 2 ; return)
echo error curl and wget && exit 1
}
debug_log(){
[ -n "$1" ] || return
[ $DEBUG -eq 1 ] && push_log "[D]$1"
}
@@ -0,0 +1,85 @@
#!/bin/sh
# [K]2022
# https://github.com/kongfl888
source /usr/share/my-dnshelper/comm
LOCK=/var/lock/my-dnshelperg.lock
[ -f $LOCK ] && exit 1
touch $LOCK
#EA=1
#GH=1
if [ "$EA" = "0" ]; then
rm -f $LOCK
exit 0
fi
gi=0
gl="http"
GL1=/tmp/e-g-1.txt
GL2=/tmp/e-g-2.txt
EG=/etc/my-dnshelper/github.mdhp
RAW="ping -c 2 raw.githubusercontents.com"
lf=""
downloader="curl --retry 3 --connect-timeout 20 -m 80 -kLfso"
which curl >/dev/null || downloader="wget-ssl --no-check-certificate -t 3 -T 20 -q -O"
which wget-ssl >/dev/null || exit 1
rm -f ${GL1} ${GL2}
if [ "$GH" != "1" ]; then
rm -f $LOCK
exit 9
fi
if [ $(ping_1p "baidu.com") -ne 0 ];then
sleep 5s
[ $(ping_1p "qq.com") -eq 0 ] || exit 1
fi
if [ `ping -c 2 raw.githubusercontents.com | grep "64" -c` -gt 0 ]; then
RAW="raw.githubusercontents.com"
elif [ `ping -c 2 ghproxy.com | grep "64" -c` -gt 0 ]; then
RAW="ghproxy.com/https://raw.githubusercontent.com"
else
RAW="raw.githubusercontent.com"
fi
$downloader $GL1 "https://$RAW/kongfl888/ad-rules/master/easy_github"
if [ ! -f ${GL1} ]; then
echo_log "Failed to Update EASY GITHUB Rule."
rm -f $LOCK
exit 1
fi
gi=`sed -n '1p' $GL1`
gl=`sed -n '2p' $GL1`
[ "$gi" = "2" ] && $downloader $GL1 $gl
if [ ! -f ${GL1} ]; then
echo_log "Failed to Update EASY GITHUB Rule."
rm -f $LOCK
exit 1
fi
gl=`grep "http" $GL1 | tail -n3`
for p in $gl; do
$downloader $GL2 $p
if [ "$?" = "0" -o -s "$GL2" ];then
lf=`echo $p|sed -r 's/.*(\.[0-9A-Za-z\-]+)+\///'|cut -d '/' -f 1,2 2>/dev/null`
break
fi
done
if [ -f ${GL2} ]; then
echo_log "Update EASY GITHUB Rule Successfully."
else
echo_log "Failed to Update EASY GITHUB Rule!"
exit 2
fi
awk '/^[0-9]/ {printf "address=/%s/%s\n",$2,$1};/^#/;/^$/' $GL2 | grep "address=" > $EG
sh /usr/share/my-dnshelper/checkconf $EG
sed -i "1i#$lf" $EG
rm -f ${GL1} ${GL2}
rm -f $LOCK
@@ -0,0 +1,380 @@
#!/bin/sh /etc/rc.common
# Copyright 2019-2022 Stan Grishin (stangri@melmac.ca)
# shellcheck disable=SC1091,SC3043,SC3060
# shellcheck disable=SC2034
START=80
# shellcheck disable=SC2034
USE_PROCD=1
if type extra_command 1>/dev/null 2>&1; then
extra_command 'version' 'Show version information'
else
# shellcheck disable=SC2034
EXTRA_COMMANDS='version'
fi
readonly PKG_VERSION='dev-test'
readonly packageName='https-dns-proxy'
readonly serviceName="$packageName $PKG_VERSION"
readonly sharedMemoryOutput="/dev/shm/$packageName-output"
readonly _OK_='\033[0;32m\xe2\x9c\x93\033[0m'
readonly _FAIL_='\033[0;31m\xe2\x9c\x97\033[0m'
readonly PROG=/usr/sbin/https-dns-proxy
readonly BOOTSTRAP_CF='1.1.1.1,1.0.0.1,2606:4700:4700::1111,2606:4700:4700::1001'
readonly BOOTSTRAP_GOOGLE='8.8.8.8,8.8.4.4,2001:4860:4860::8888,2001:4860:4860::8844'
readonly DEFAULT_BOOTSTRAP="${BOOTSTRAP_CF},${BOOTSTRAP_GOOGLE}"
readonly canaryDomainsMozilla='use-application-dns.net'
readonly canaryDomainsiCloud='mask.icloud.com mask-h2.icloud.com'
str_contains() { [ -n "$1" ] &&[ -n "$2" ] && [ "${1//$2}" != "$1" ]; }
is_mac_address() { expr "$1" : '[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]$' >/dev/null; }
is_ipv4() { expr "$1" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; }
is_ipv6() { ! is_mac_address "$1" && str_contains "$1" ":"; }
output() {
local msg memmsg logmsg
[ -t 1 ] && printf "%b" "$@"
msg="${1//$serviceName /service }";
if [ "$(printf "%b" "$msg" | wc -l)" -gt 0 ]; then
[ -s "$sharedMemoryOutput" ] && memmsg="$(cat "$sharedMemoryOutput")"
logmsg="$(printf "%b" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
logger -t "$packageName" "$(printf "%b" "$logmsg")"
rm -f "$sharedMemoryOutput"
else
printf "%b" "$msg" >> "$sharedMemoryOutput"
fi
}
output_ok() { output "$_OK_"; }
output_okn() { output "${_OK_}\\n"; }
output_fail() { output "$_FAIL_"; }
output_failn() { output "${_FAIL_}\\n"; }
uci_add_list_if_new() {
local PACKAGE="$1"
local CONFIG="$2"
local OPTION="$3"
local VALUE="$4"
local i
[ -n "$PACKAGE" ] && [ -n "$CONFIG" ] && [ -n "$OPTION" ] && [ -n "$VALUE" ] || return 1
for i in $(uci_get "$PACKAGE" "$CONFIG" "$OPTION"); do
[ "$i" = "$VALUE" ] && return 0
done
uci_add_list "$PACKAGE" "$CONFIG" "$OPTION" "$VALUE"
}
uci_changes() {
local PACKAGE="$1"
local CONFIG="$2"
local OPTION="$3"
/sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} changes "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
}
dnsmasq_restart() { [ -x /etc/init.d/dnsmasq ] || return 0; /etc/init.d/dnsmasq restart >/dev/null 2>&1; }
version() { echo "$PKG_VERSION"; }
xappend() { param="$param $1"; }
append_bool() {
local section="$1"
local option="$2"
local value="$3"
local default="${4:-0}"
local _loctmp
config_get_bool _loctmp "$section" "$option" "$default"
[ "$_loctmp" -ne 0 ] && xappend "$value"
}
append_parm() {
local section="$1"
local option="$2"
local switch="$3"
local default="$4"
local _loctmp
config_get _loctmp "$section" "$option" "$default"
[ -n "$_loctmp" ] && xappend "$switch $_loctmp"
}
append_counter() {
local section="$1"
local option="$2"
local switch="$3"
local default="${4:-0}"
local _loctmp i
config_get _loctmp "$section" "$option" "$default"
# shellcheck disable=SC2086,SC2154
for i in $(seq 1 $_loctmp); do
xappend '-v'
done
}
append_bootstrap() {
local section="$1"
local option="$2"
local switch="$3"
local default="$4"
local _old_ifs="$IFS"
local _loctmp _newtmp i
config_get _loctmp "$section" "$option" "$default"
[ -z "$_loctmp" ] && return 0
IFS=" ,"
for i in $_loctmp; do
if { [ "$ipv6_resolvers_only" -eq 0 ] && is_ipv4 "$i"; } || \
{ [ "$ipv6_resolvers_only" -ne 0 ] && is_ipv6 "$i"; }; then
[ -z "$_newtmp" ] && _newtmp="$i" || _newtmp="${_newtmp},${i}"
fi
done
IFS="$_old_ifs"
[ -n "$_newtmp" ] && xappend "$switch $_newtmp"
[ "$ipv6_resolvers_only" -eq 0 ] && xappend '-4'
}
start_instance() {
local cfg="$1" param listen_addr listen_port ipv6_resolvers_only p url
config_get url "$cfg" 'resolver_url'
config_get_bool ipv6_resolvers_only "$cfg" 'use_ipv6_resolvers_only' '0'
append_parm "$cfg" 'resolver_url' '-r'
append_parm "$cfg" 'listen_addr' '-a' '127.0.0.1'
append_parm "$cfg" 'listen_port' '-p' "$port"
append_parm "$cfg" 'dscp_codepoint' '-c'
append_bootstrap "$cfg" 'bootstrap_dns' '-b' "$DEFAULT_BOOTSTRAP"
append_parm "$cfg" 'user' '-u' 'nobody'
append_parm "$cfg" 'group' '-g' 'nogroup'
append_parm "$cfg" 'ca_certs_file' '-C'
append_parm "$cfg" 'polling_interval' '-i'
append_parm "$cfg" 'proxy_server' '-t'
append_parm "$cfg" 'logfile' '-l'
append_bool "$cfg" 'use_http1' '-x'
append_counter "$cfg" 'verbosity' '-v' '0'
procd_open_instance
# shellcheck disable=SC2086
procd_set_param command $PROG $param
procd_set_param stderr 1
procd_set_param stdout 1
procd_set_param respawn
procd_open_data
json_add_string url "$url"
if [ "$force_dns" -ne 0 ]; then
json_add_array firewall
for p in $force_dns_port; do
if netstat -tuln | grep 'LISTEN' | grep ":${p}" >/dev/null 2>&1 || [ "$p" = '53' ]; then
json_add_object ''
json_add_string type redirect
json_add_string target DNAT
json_add_string src lan
json_add_string proto 'tcp udp'
json_add_string src_dport "$p"
json_add_string dest_port "$p"
json_add_boolean reflection 0
json_close_object
else
json_add_object ''
json_add_string type rule
json_add_string src lan
json_add_string dest '*'
json_add_string proto 'tcp udp'
json_add_string dest_port "$p"
json_add_string target REJECT
json_close_object
fi
done
json_close_array
fi
procd_close_data
procd_close_instance
if [ "$?" ]; then
config_get listen_addr "$cfg" 'listen_addr' '127.0.0.1'
config_get listen_port "$cfg" 'listen_port' "$port"
if [ "$dnsmasq_config_update" = '*' ]; then
config_load 'dhcp'
config_foreach dnsmasq_doh_server 'dnsmasq' 'add' "${listen_addr}" "${listen_port}"
elif [ -n "$dnsmasq_config_update" ]; then
for i in $dnsmasq_config_update; do
if [ -n "$(uci_get 'dhcp' "@dnsmasq[$i]")" ]; then
dnsmasq_doh_server "@dnsmasq[$i]" 'add' "${listen_addr}" "${listen_port}"
elif [ -n "$(uci_get 'dhcp' "$i")" ]; then
dnsmasq_doh_server "${i}" 'add' "${listen_addr}" "${listen_port}"
fi
done
fi
output_ok
port="$((port+1))"
force_dns=0
else
output_fail
fi
}
start_service() {
local canaryDomains canary_domains_icloud canary_domains_mozilla
local dnsmasq_config_update force_dns force_dns_port
local port=5053
output "Starting $serviceName "
config_load "$packageName"
config_get dnsmasq_config_update 'config' 'dnsmasq_config_update' '*'
config_get_bool canary_domains_icloud 'config' 'canary_domains_icloud' '1'
config_get_bool canary_domains_mozilla 'config' 'canary_domains_mozilla' '1'
config_get_bool force_dns 'config' 'force_dns' '1'
config_get force_dns_port 'config' 'force_dns_port' '53 853'
if [ "$canary_domains_icloud" -ne 0 ]; then
canaryDomains="${canaryDomains:+$canaryDomains }${canaryDomainsiCloud}"
fi
if [ "$canary_domains_mozilla" -ne 0 ]; then
canaryDomains="${canaryDomains:+$canaryDomains }${canaryDomainsMozilla}"
fi
dhcp_backup 'create'
config_load "$packageName"
config_foreach start_instance "$packageName"
if [ -n "$(uci_changes dhcp)" ]; then
uci_commit 'dhcp'
dnsmasq_restart
fi
output "\\n"
}
stop_service() {
local canaryDomains canary_domains_icloud canary_domains_mozilla
local dnsmasq_config_update
local s=0
output "Stopping $serviceName "
config_load "$packageName"
config_get dnsmasq_config_update 'config' 'dnsmasq_config_update' '*'
config_get_bool canary_domains_icloud 'config' 'canary_domains_icloud' '1'
config_get_bool canary_domains_mozilla 'config' 'canary_domains_mozilla' '1'
if [ "$canary_domains_icloud" -ne 0 ]; then
canaryDomains="${canaryDomains:+$canaryDomains }${canaryDomainsiCloud}"
fi
if [ "$canary_domains_mozilla" -ne 0 ]; then
canaryDomains="${canaryDomains:+$canaryDomains }${canaryDomainsMozilla}"
fi
dhcp_backup 'restore'
if [ -n "$(uci_changes dhcp)" ]; then
uci_commit 'dhcp'
dnsmasq_restart || s=1
fi
# shellcheck disable=SC2015
[ "$s" -eq 0 ] && output_okn || output_failn
}
# shellcheck disable=SC1091
service_triggers() {
local wan wan6 i
local procd_trigger_wan6
config_load "$packageName"
config_get_bool procd_trigger_wan6 'config' 'procd_trigger_wan6' '0'
. /lib/functions/network.sh
network_flush_cache
network_find_wan wan
wan="${wan:-wan}"
if [ "$procd_trigger_wan6" -ne 0 ]; then
network_find_wan6 wan6
wan6="${wan6:-wan6}"
fi
for i in "$wan" "$wan6"; do
[ -n "$i" ] && procd_add_interface_trigger "interface.*" "$i" "/etc/init.d/${packageName}" restart
done
procd_add_config_trigger "config.change" "$packageName" "/etc/init.d/${packageName}" reload
}
service_started() { procd_set_config_changed firewall; }
service_stopped() { procd_set_config_changed firewall; }
dnsmasq_doh_server() {
local cfg="$1" param="$2" address="${3:-127.0.0.1}" port="$4" i
case "$param" in
add)
if [ "$force_dns" -ne 0 ]; then
for i in $canaryDomains; do
uci_add_list_if_new 'dhcp' "$cfg" 'server' "/${i}/"
done
fi
case $address in
0.0.0.0|::ffff:0.0.0.0) address='127.0.0.1';;
::) address='::1';;
esac
uci_add_list_if_new 'dhcp' "$cfg" 'server' "${address}#${port}"
;;
remove)
eval "$(ubus call service list "{ 'verbose': true, 'name': '$packageName' }" | jsonfilter -F '# ' -e 'TUPLES=@[*].instances[*].command[4,6]')"
for i in $TUPLES; do
uci_remove_list 'dhcp' "$cfg" 'server' "$i"
done
for i in $canaryDomains; do
uci_remove_list 'dhcp' "$cfg" 'server' "/${i}/"
done
;;
esac
}
dnsmasq_create_server_backup() {
local cfg="$1" i
[ -n "$(uci_get 'dhcp' "$cfg")" ] || return 1
if [ -z "$(uci_get 'dhcp' "$cfg" 'doh_backup_noresolv')" ]; then
if [ -z "$(uci_get 'dhcp' "$cfg" 'noresolv')" ]; then
uci_set 'dhcp' "$cfg" 'doh_backup_noresolv' '-1'
else
uci_set 'dhcp' "$cfg" 'doh_backup_noresolv' "$(uci_get 'dhcp' "$cfg" noresolv)"
fi
uci_set 'dhcp' "$cfg" 'noresolv' 1
fi
if [ -z "$(uci_get 'dhcp' "$cfg" 'doh_backup_server')" ]; then
if [ -z "$(uci_get 'dhcp' "$cfg" 'server')" ]; then
uci_add_list 'dhcp' "$cfg" 'doh_backup_server' ""
fi
for i in $(uci_get 'dhcp' "$cfg" 'server'); do
uci_add_list 'dhcp' "$cfg" 'doh_backup_server' "$i"
if [ "$i" = "$(echo "$i" | tr -d /\#)" ]; then
uci_remove_list 'dhcp' "$cfg" 'server' "$i"
fi
done
fi
return 0
}
dnsmasq_restore_server_backup() {
local cfg="$1" i
[ -n "$(uci_get 'dhcp' "$cfg")" ] || return 0
if [ -n "$(uci_get 'dhcp' "$cfg" 'doh_backup_noresolv')" ]; then
if [ "$(uci_get 'dhcp' "$cfg" 'doh_backup_noresolv')" = "-1" ]; then
uci_remove 'dhcp' "$cfg" 'noresolv'
else
uci_set 'dhcp' "$cfg" 'noresolv' "$(uci_get 'dhcp' "$cfg" 'doh_backup_noresolv')"
fi
uci_remove 'dhcp' "$cfg" 'doh_backup_noresolv'
fi
if uci_get 'dhcp' "$cfg" 'doh_backup_server' >/dev/null 2>&1; then
dnsmasq_doh_server "$cfg" 'remove'
for i in $(uci_get 'dhcp' "$cfg" 'doh_backup_server'); do
uci_add_list_if_new 'dhcp' "$cfg" 'server' "$i"
done
uci_remove 'dhcp' "$cfg" 'doh_backup_server'
fi
}
dhcp_backup() {
local i
config_load 'dhcp'
case "$1" in
create)
if [ "$dnsmasq_config_update" = "*" ]; then
config_foreach dnsmasq_create_server_backup 'dnsmasq'
elif [ -n "$dnsmasq_config_update" ]; then
for i in $dnsmasq_config_update; do
if [ -n "$(uci_get 'dhcp' "@dnsmasq[$i]")" ]; then
dnsmasq_create_server_backup "@dnsmasq[$i]"
elif [ -n "$(uci_get 'dhcp' "$i")" ]; then
dnsmasq_create_server_backup "$i"
fi
done
fi
;;
restore)
config_foreach dnsmasq_restore_server_backup 'dnsmasq'
;;
esac
}
reload_service() {
restart
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,77 @@
#!/bin/sh
# [K]2022
# https://github.com/kongfl888
source /usr/share/my-dnshelper/comm
nonet=0
errc=0
#debug
EA=1
[ "$EA" = "1" ] || exit 0
[ $(ping_1p "baidu.com") -ne 0 ] && [ $(ping_1p "qq.com") -ne 0 ] && nonet=1
if [ $nonet -eq 1 ];then
touch /tmp/hpruleupfail.log
errc=`grep -oE "\d+" /tmp/hpruleupfail.log | head -n 1 2>/dev/null`
[ -z "$errc" ] && errc=0
let errc++
echo $errc > /tmp/hpruleupfail.log
if [ $errc -gt 10 ];then
[ -d $RPath ] || mkdir -p $RPath
rm -f $updatelog
echo "`eval $D`" > $updatelog
push_log "FAILED to check update of rules."
fi
exit 1
else
errc=0
[ -f /tmp/hpruleupfail.log ] && rm -f /tmp/hpruleupfail.log
fi
sSname="$(basename $0)"
if [ $(pgrep -f ${sSname}|wc -l) -gt 2 ];then
sleep 5s
if [ $(pgrep -f ${sSname}|wc -l) -gt 2 ];then
exit 1
fi
fi
[ "$S" = "1" ] && SPath=$EPath/Rules
chmod 0744 $updatelog >/dev/null 2>&1
udt1=`cat $updatelog 2>/dev/null`
[ $(ls -l /usr/share/my-dnshelper/rulesmaker | grep -c "rwx" 2>/dev/null) -eq 0 ] && chmod 0774 /usr/share/my-dnshelper/*
push_log "Check Subscribe Rules Update"
echo "0" > $updatelog
#check_lock
sh /usr/share/my-dnshelper/rulesmaker -u >> $Logfile 2>/dev/null
sh /usr/share/my-dnshelper/checkver -f 2>/dev/null
udt2=`cat $updatelog 2>/dev/null`
udx1=`date -d "$udt1" +%s 2>/dev/null`
udx2=`date -d "$udt2" +%s 2>/dev/null`
[ -z "$udx1" ] && udx1=0
[ -z "$udx2" ] && udx2=0
if [ -z "$udt2" -o "$udt2" == "0" ];then
echo $udt1 > $updatelog
exit 1
elif [ $udx2 -lt $udx1 ];then
echo $udt1 > $updatelog
exit 1
fi
push_log "Reload Rules"
rsc_dnsmasq | grep -vE "^(0|1)$" >> $Logfile
exit 0
@@ -0,0 +1,179 @@
#!/bin/sh
# [K]2022
# https://github.com/kongfl888
source /usr/share/my-dnshelper/comm
Helpthis() {
DATE_HMS=`date +%H:%M:%S`
DATE_Y=`date +%Y`
DATE="$DATE_Ymd $DATE_HMS"
echo "
Hi Master, I'm your dnshelper. This one of my waiters for you.
usage: waiter [OPTION]
...
I am sorry.
He's a little shy and not ready.
...
It is juest a part of me. You still need a luci application.
Thank you for coming to look at me. I'm very happy.
By MY-Dnshelper on $DATE
[K] $DATE_Y
You can contact my Founder via this link: https://github.com/kongfl888
"
exit 0
}
Pa=${1}
crules=$SPath/rules.conf
cwhite=$SPath/white.txt
chosts=$SPath/hosts.conf
ifc=0
iac=0
ihc=0
check_bin(){
[ -f "/usr/bin/my-dnshelper" ] || return
local p=`ps|grep "/usr/bin/my-dnshelper"|sed "/grep/d"`
[ -n "$p" -a "$EA" != "1" ] && /etc/init.d/my-dnshelper stop
[ -z "$p" -a "$EA" == "1" ] && /etc/init.d/my-dnshelper start
}
clear_rule(){
[ -d $RPath ] || mkdir -p $RPath >/dev/null 2>&1
read_count
if [ "$1" == "1" ];then
echo -n > $crules
ifc=0
elif [ "$1" == "2" ];then
echo -n > $cwhite
iac=0
elif [ "$1" == "3" ];then
echo -n > $chosts
if [ "$GH" == "1" -a -s $EPath/github.mdhp ];then
touch $SPath/hosts.conf
cat $EPath/github.mdhp >> $SPath/hosts.conf
sort_file $SPath/hosts.conf
fi
ihc=0
fi
echo "$ifc,$iac,$ihc" > $countf
if [ "$(cat $updatelog 2>/dev/null)" == "0" ]; then
rm -f $updatelog
fi
if [ "$EA" == "1" ];then
push_log "Rules have changed."
push_log "Dnsmasq reload."
/etc/init.d/dnsmasq reload 2>/dev/null
echo "$?"
else
push_log "Rules have been cleared."
echo "0"
fi
}
get_log(){
if [ "$revlog" == "1" ];then
tail -800 $dnsmasqlog |sed '1!G;h;$!d' 2>/dev/null
else
tail -800 $dnsmasqlog 2>/dev/null
fi
}
get_dnsonline(){
local rc=0
if [ "$dnsdetect" == "1" ]; then
which curl >/dev/null 2>&1 || rc=1
if [ $rc -eq 1 ];then
echo ""
else
local rurl=`curl --connect-timeout 5 -m 10 -kLfs http://nstool.netease.com | grep -oE "http.*[a-zA-Z]\/"`
local txt=`curl --connect-timeout 5 -m 10 -kLfs $rurl | grep -oE "[Ii][pP].*[DNSdns]+.*\.\d+.*<br>"`
if [ -z "$txt" ];then
echo ""
else
local ip=`echo $txt|grep -oE "[0-9]+(\.[0-9]+){3}" |head -n 1`
local ds=`echo $txt|grep -oE "[0-9]+(\.[0-9]+){3}" |sed -n "2p"`
echo '<p>Detect DNS:IP - '$ip'| DNS - '$ds'</p>'
fi
fi
else
echo ""
fi
}
get_ports_ip(){
local a=`echo $(get_ports) |sed 's/ /,/g'`
[ $(check_ipv4doh) -gt 0 ] && a="$a IPv4"
[ $(check_ipv6doh) -gt 0 ] && a="$a IPv6"
echo $a
}
get_appver(){
local dmqver="Dnsmasq `dnsmasq --version | grep -oE 'v.*\d+(\.\d+)+'| grep -oE '\d+(\.\d+)*'`"
local appvr=""
[ "$app_check" == "1" ] && appvr=`sh /usr/share/my-dnshelper/checkver`
if [ -z "$appvr" ];then
echo $dmqver
else
echo "$dmqver | $appvr"
fi
}
case $Pa in
"--help")
Helpthis
;;
"-m"|"--dnsmasq")
get_appver
;;
"-d"|"--doh")
hv=`opkg list-installed https-dns-proxy 2>/dev/null`
[ -n "$hv" ] && echo "`echo $hv | sed -e 's/https-dns-proxy/core/' -e 's/ -/ version: /'`" || echo "https-dns-proxy is not installed"
;;
"-f"|"--filter")
get_count $crules
;;
"-a"|"--allow")
get_count $cwhite
;;
"-h"|"--hosts")
get_count $chosts
;;
"-u"|"--update")
sh /usr/share/my-dnshelper/uprules &
echo "0"
;;
"--port")
get_ports_ip
;;
"--xf")
clear_rule 1
;;
"--xw")
clear_rule 2
;;
"--xh")
clear_rule 3
;;
"--dns")
get_dnsonline
;;
"--dnslog")
get_log
;;
*)
Helpthis
;;
esac
check_bin
exit 0