mirror of
https://github.com/caiwx86/small-packages.git
synced 2026-10-29 17:15:13 +08:00
update 2026-10-07 18:47:42
This commit is contained in:
1 parent
617f7ca2a6
commit
bd061545ed
4832 files changed
+1693456
No files matched your search
@@ -0,0 +1,2 @@
|
||||
config autoipsetadder 'autoipsetadder'
|
||||
option enabled '0'
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
#!/bin/sh /etc/rc.common
|
||||
|
||||
USE_PROCD=1
|
||||
|
||||
START=99
|
||||
STOP=01
|
||||
CRON_FILE=/etc/crontabs/root
|
||||
CONFIGURATION=autoipsetadder
|
||||
EXTRA_COMMANDS="test_crontab"
|
||||
EXTRA_HELP="
|
||||
test_crontab"
|
||||
set_dnsmasq_log()
|
||||
{
|
||||
sed -i '/log-facility/d' /etc/dnsmasq.conf
|
||||
sed -i '/log-queries/d' /etc/dnsmasq.conf
|
||||
uci set dhcp.@dnsmasq[0].logfacility='/tmp/dnsmasq.log'
|
||||
uci delete dhcp.@dnsmasq[0].logqueries
|
||||
echo log-queries >> /etc/dnsmasq.conf
|
||||
uci commit dhcp
|
||||
/etc/init.d/dnsmasq reload
|
||||
}
|
||||
|
||||
stop_dnsmasq_log()
|
||||
{
|
||||
sed -i '/log-queries/d' /etc/dnsmasq.conf
|
||||
uci delete dhcp.@dnsmasq[0].logfacility
|
||||
uci commit dhcp
|
||||
/etc/init.d/dnsmasq reload
|
||||
}
|
||||
|
||||
reload_service()
|
||||
{
|
||||
rm -f /var/run/AdGucitest 2>/dev/null
|
||||
kill $(cat /var/run/autoipsetadder.pid)
|
||||
start
|
||||
}
|
||||
service_triggers() {
|
||||
procd_add_reload_trigger "$CONFIGURATION"
|
||||
}
|
||||
start_service() {
|
||||
# Reading config
|
||||
config_load "${CONFIGURATION}"
|
||||
mkdir -p /tmp/run/autoipsetadder
|
||||
local enabled
|
||||
config_get_bool enabled $CONFIGURATION enabled 0
|
||||
do_crontab
|
||||
if [ "$enabled" == "1" ]; then
|
||||
set_dnsmasq_log
|
||||
procd_open_instance
|
||||
procd_set_param respawn
|
||||
# pass config to script on start
|
||||
procd_set_param command sh /usr/bin/autoipsetadder/autoaddlist.sh
|
||||
procd_close_instance
|
||||
echo "autoipsetadder turn on"
|
||||
echo "enabled=$enabled"
|
||||
else
|
||||
stop_dnsmasq_log
|
||||
echo "autoipsetadder turn off"
|
||||
echo "enabled=$enabled"
|
||||
fi
|
||||
}
|
||||
|
||||
stop_service()
|
||||
{
|
||||
config_load "${CONFIGURATION}"
|
||||
stop_dnsmasq_log
|
||||
do_crontab
|
||||
kill $(cat /var/run/autoipsetadder.pid)
|
||||
echo "autoipsetadder turn off"
|
||||
echo "enabled="$enabled""
|
||||
}
|
||||
do_crontab(){
|
||||
config_get_bool enabled $CONFIGURATION enabled 0
|
||||
#config_get logfile $CONFIGURATION logfile "/tmp/addlist.log"
|
||||
#config_get dnslogfile $CONFIGURATION dnslogfile "/tmp/dnsmasq.log"
|
||||
config_get crontab $CONFIGURATION crontab ""
|
||||
cronreload=0
|
||||
findstr="echo qingkong > \$(uci get autoipsetadder.autoipsetadder.dnslogfile)"
|
||||
default="0 * * * * echo qingkong > \$(uci get autoipsetadder.autoipsetadder.dnslogfile)"
|
||||
#[ -n "$lastdnslogfile" ] && findstr="echo qingkong > $lastdnslogfile" && [ "$lastdnslogfile" != "$dnslogfile" ] && replace="${lastdnslogfile//\//\\/}/${dnslogfile//\//\\/}"
|
||||
[ "$enabled" == "0" ] || [ "${crontab//autodeldnslog/}" == "$crontab" ] && cronenable=0 || cronenable=1
|
||||
crontab_editor
|
||||
#[ "$lastdnslogfile" != "$dnslogfile" ] && uci set autoipsetadder.autoipsetadder.lastdnslogfile="$dnslogfile" && commit=1
|
||||
|
||||
findstr="/usr/bin/autoipsetadder/tailto.sh [0-9]* \$(uci get autoipsetadder.autoipsetadder.logfile)"
|
||||
default="0 0 * * * /usr/bin/autoipsetadder/tailto.sh 2000 \$(uci get autoipsetadder.autoipsetadder.logfile)"
|
||||
#[ -n "$lastlogfile" ] && findstr="/usr/bin/autoipsetadder/tailto.sh [0-9]* $lastlogfile" && [ "$lastlogfile" != "$logfile" ] && replace="${lastlogfile//\//\\/}/${logfile//\//\\/}"
|
||||
[ "$enabled" == "0" ] || [ "${crontab//autotaillog/}" == "$crontab" ] && cronenable=0 || cronenable=1
|
||||
crontab_editor
|
||||
#[ -n "$logfile" ] && [ "$lastlogfile" != "$logfile" ] && uci set autoipsetadder.autoipsetadder.lastlogfile="$logfile" && commit=1
|
||||
|
||||
[ "$cronreload" -gt 0 ] && /etc/init.d/cron restart
|
||||
#[ "$commit" -gt 0 ] && uci commit autoipsetadder
|
||||
}
|
||||
crontab_editor(){
|
||||
local testline reload
|
||||
local line="$(grep "$findstr" $CRON_FILE)"
|
||||
[ -n "$replace" ] && [ -n "$line" ] && eval testline="\${line//$replace}" && [ "$testline" != "$line" ] && line="$testline" && reload="1" && replace=""
|
||||
if [ "${line:0:1}" != "#" ]; then
|
||||
if [ $cronenable -eq 1 ]; then
|
||||
[ -z "$line" ] && line="$default" && reload="1"
|
||||
if [ -n "$reload" ]; then
|
||||
sed -i "\,$findstr,d" $CRON_FILE
|
||||
echo "$line" >> $CRON_FILE
|
||||
cronreload=$((cronreload+1))
|
||||
fi
|
||||
elif [ -n "$line" ]; then
|
||||
sed -i "\,$findstr,d" $CRON_FILE
|
||||
echo "#$line" >> $CRON_FILE
|
||||
cronreload=$((cronreload+1))
|
||||
fi
|
||||
else
|
||||
if [ $cronenable -eq 1 ]; then
|
||||
sed -i "\,$findstr,d" $CRON_FILE
|
||||
echo "${line:1}" >> $CRON_FILE
|
||||
cronreload=$((cronreload+1))
|
||||
elif [ -z "$reload" ]; then
|
||||
sed -i "\,$findstr,d" $CRON_FILE
|
||||
echo "$line" >> $CRON_FILE
|
||||
fi
|
||||
fi
|
||||
}
|
||||
test_crontab(){
|
||||
config_load "${CONFIGURATION}"
|
||||
do_crontab
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#!/bin/sh
|
||||
|
||||
uci -q batch <<-EOF >/dev/null
|
||||
delete ucitrack.@autoipsetadder[-1]
|
||||
add ucitrack autoipsetadder
|
||||
set ucitrack.@autoipsetadder[-1].init=autoipsetadder
|
||||
commit ucitrack
|
||||
EOF
|
||||
|
||||
rm -f /tmp/luci-indexcache
|
||||
exit 0
|
||||
@@ -0,0 +1,164 @@
|
||||
#!/bin/sh
|
||||
PATH="/usr/sbin:/usr/bin:/sbin:/bin"
|
||||
logfile=$(uci get autoipsetadder.autoipsetadder.logfile)
|
||||
[ -z "$logfile" ] && logfile="/tmp/addlist.log"
|
||||
dnslogfile=$(uci get autoipsetadder.autoipsetadder.dnslogfile)
|
||||
[ -z "$logfile" ] && dnslogfile="/tmp/dnsmasq.log"
|
||||
config=$(uci get autoipsetadder.autoipsetadder.config 2>/dev/null)
|
||||
[ "${config//nochina/}" == "$config" ] && nochina="0" || nochina="1"
|
||||
[ "${config//packetpass/}" == "$config" ] && packetpass="0" || packetpass="1"
|
||||
(tail -F $dnslogfile & echo $! >/var/run/autoipsetadder.pid ) | awk -v nochina="$nochina" -v packetpass="$packetpass" -F "[, ]+" '/reply/{
|
||||
ip=$8;
|
||||
if (ip=="" || ip=="127.0.0.1"|| ip=="0.0.0.0")
|
||||
{
|
||||
next;
|
||||
}
|
||||
if (index(ip,"<CNAME>")!=0)
|
||||
{
|
||||
if (cname==1)
|
||||
{
|
||||
next;
|
||||
}
|
||||
cname=1;
|
||||
domain=$6;
|
||||
#第一次cname时锁定域名,防止解析cname对其改动
|
||||
next;
|
||||
}
|
||||
#以上获得上行是否为cname,本行不是cname执行以下内容
|
||||
#lastdomain记录上次非cname的域名,与本次域名对比
|
||||
if(lastdomain!=$6){
|
||||
for (ipindex in ipcache)
|
||||
{
|
||||
delete ipcache[ipindex];
|
||||
}
|
||||
ipcount=0;
|
||||
createpid=1;
|
||||
#上行非cname,并且不是同cname解析域名的多个ip,更新域名,清理同域名免试flag
|
||||
if (cname!=1)
|
||||
{
|
||||
domain=$6;
|
||||
testall=0;
|
||||
}}
|
||||
ipcount++;
|
||||
cname=0;
|
||||
lastdomain=$6
|
||||
#去除非ipv4
|
||||
if (index(ip,".")==0)
|
||||
{
|
||||
next;
|
||||
}
|
||||
#不重复探测ip
|
||||
if (!(ip in a))
|
||||
{
|
||||
#包数>12的同域名放过
|
||||
if (passdomain==domain)
|
||||
{
|
||||
print(ip" "domain" pass by same domain ok");
|
||||
a[ip]=domain;
|
||||
next;
|
||||
}
|
||||
if (nochina==1){
|
||||
"ipset test china "ip" 2>&1"| getline ipset;
|
||||
close("ipset test china "ip" 2>&1");
|
||||
if (index(ipset,"Warning")!=0){
|
||||
print("china "ip" pass");
|
||||
a[ip]=domain;
|
||||
next;
|
||||
}}
|
||||
"ipset test gfwlist "ip" 2>&1"| getline ipset;
|
||||
close("ipset test gfwlist "ip" 2>&1");
|
||||
if (index(ipset,"Warning")!=0){
|
||||
print("gfwlist "ip" pass");
|
||||
a[ip]=domain;
|
||||
next;
|
||||
}
|
||||
|
||||
#ip压入缓存,用于未检测到443/80的缓存
|
||||
ipcache[ipcount]=ip;
|
||||
if (testall==0){
|
||||
tryhttps=0;
|
||||
tryhttp=0;
|
||||
#探测 nf_conntrack 的443/80
|
||||
while ("grep "ip" /proc/net/nf_conntrack"| getline ret > 0)
|
||||
{
|
||||
split(ret, b," +");
|
||||
split(b[11], pagnum,"=");
|
||||
#包数>12的放过
|
||||
if (packetpass==1 && pagnum[2]>12)
|
||||
{
|
||||
print("pass by packets="pagnum[2]" "ip" "domain);
|
||||
for (ipindex in ipcache)
|
||||
{
|
||||
a[ipcache[ipindex]]=domain;
|
||||
delete ipcache[ipindex];
|
||||
}
|
||||
passdomain=domain;
|
||||
close("grep "ip" /proc/net/nf_conntrack");
|
||||
ipcount--;
|
||||
next;
|
||||
}
|
||||
if (b[8]=="dst="ip)
|
||||
{
|
||||
if (b[10]=="dport=443"){
|
||||
tryhttps=1;
|
||||
break;
|
||||
}
|
||||
else if (b[10]=="dport=80"){
|
||||
tryhttp=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
close("grep "ip" /proc/net/nf_conntrack");
|
||||
}else{
|
||||
if (testall==443)
|
||||
{
|
||||
tryhttps=1
|
||||
}else{
|
||||
tryhttp=1
|
||||
}
|
||||
}
|
||||
if (tryhttps==1)
|
||||
{ if (createpid==1)
|
||||
{
|
||||
print "">"/tmp/run/autoipsetadder/"domain
|
||||
close("/tmp/run/autoipsetadder/"domain);
|
||||
print("create"domain);
|
||||
print(ip" "domain" 443"ipcount-1);
|
||||
a[ip]=domain;
|
||||
#正在使用的ip用最大延迟,最后探测,减少打断tcp的可能
|
||||
system("/usr/bin/autoipsetadder/testip.sh "ip" "domain" 443 "ipcount-1" &");
|
||||
delete ipcache[ipcount];
|
||||
createpid=0;
|
||||
}
|
||||
#未检测到443/80同域名缓存的ip进行测试,ipindex-1为测试延迟时间
|
||||
for (ipindex in ipcache){
|
||||
print(ipcache[ipindex]" "domain" 443 "ipindex-1);
|
||||
a[ipcache[ipindex]]=domain;
|
||||
system("/usr/bin/autoipsetadder/testip.sh "ipcache[ipindex]" "domain" 443 "ipindex-1" &");
|
||||
delete ipcache[ipindex];
|
||||
}
|
||||
#后续同域名ip免nf_conntrack测试
|
||||
testall=443;
|
||||
}
|
||||
else if (tryhttp==1)
|
||||
{
|
||||
if (createpid==1)
|
||||
{
|
||||
print "">"/tmp/run/autoipsetadder/"domain
|
||||
close("/tmp/run/autoipsetadder/"domain);
|
||||
print("create"domain);
|
||||
print(ip" "domain" 80 "ipcount-1);
|
||||
a[ip]=domain;
|
||||
system("/usr/bin/autoipsetadder/testip.sh "ip" "domain" 80 "ipcount-1" &");
|
||||
delete ipcache[ipcount];
|
||||
createpid=0;
|
||||
}
|
||||
for (ipindex in ipcache){
|
||||
print(ipcache[ipindex]" "domain" 80 "ipindex-1);
|
||||
a[ipcache[ipindex]]=domain;
|
||||
system("/usr/bin/autoipsetadder/testip.sh "ipcache[ipindex]" "domain" 80 "ipindex-1" &");
|
||||
delete ipcache[ipindex];
|
||||
}
|
||||
testall=80;
|
||||
}}
|
||||
}' >> $logfile
|
||||
@@ -0,0 +1,56 @@
|
||||
#!/bin/sh
|
||||
PATH="/usr/sbin:/usr/bin:/sbin:/bin"
|
||||
dlchina=$1;
|
||||
logfile=$(uci get autoipsetadder.autoipsetadder.logfile)
|
||||
[ -z "$logfile" ] && logfile="/tmp/addlist.log"
|
||||
|
||||
dnslogfile=$(uci get autoipsetadder.autoipsetadder.dnslogfile)
|
||||
[ -z "$logfile" ] && dnslogfile="/tmp/dnsmasq.log"
|
||||
|
||||
|
||||
ipset list gfwlist | awk -v dlchina="$dlchina" -v dnslogfile="$dnslogfile" -v logfile="$logfile" '{
|
||||
if (index($0,".")==0)
|
||||
{
|
||||
next;
|
||||
}
|
||||
if ($0=="127.0.0.1") {system("ipset d gfwlist 127.0.0.1");next;}
|
||||
"ipset test whitelist "$0" 2>&1"| getline ipset;
|
||||
close("ipset test whitelist "$0" 2>&1");
|
||||
if (index(ipset,"Warning")==0){
|
||||
white=0;
|
||||
}else{
|
||||
white=1;
|
||||
}
|
||||
"ipset test china "$0" 2>&1"| getline ipset;
|
||||
close("ipset test china "$0" 2>&1");
|
||||
if (index(ipset,"Warning")!=0){
|
||||
china=1;
|
||||
}
|
||||
else{
|
||||
china=0;
|
||||
}
|
||||
if (white==1)
|
||||
{
|
||||
if (china==0)
|
||||
{
|
||||
print("warning white ip not china"$0);
|
||||
ret=system("grep "$0" "logfile);
|
||||
if (ret!=0)
|
||||
{
|
||||
ret=system("grep "$0" "dnslogfile);
|
||||
}
|
||||
}
|
||||
}else if (china==1)
|
||||
{
|
||||
print("warning china ip not white"$0);
|
||||
ret=system("grep "$0" "logfile)
|
||||
if (ret!=0)
|
||||
{
|
||||
ret=system("grep "$0" "dnslogfile);
|
||||
}
|
||||
if (dlchina)
|
||||
{
|
||||
system("ipset del gfwlist "$0);
|
||||
}
|
||||
}
|
||||
}'
|
||||
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
tail -n $1 "$2" > /var/run/tailtmp
|
||||
cat /var/run/tailtmp > "$2"
|
||||
rm /var/run/tailtmp
|
||||
@@ -0,0 +1,182 @@
|
||||
#!/bin/sh
|
||||
config=$(uci get autoipsetadder.autoipsetadder.config 2>/dev/null)
|
||||
[ "${config//pingadd/}" == "$config" ] && pingadd="0" || pingadd="1"
|
||||
echo $* | awk -v pingadd="$pingadd" '{
|
||||
if ($4=="")
|
||||
{
|
||||
wait=0;
|
||||
}else
|
||||
{wait=$4;}
|
||||
system("sleep "wait);
|
||||
ERRNO="";
|
||||
pidfile="/tmp/run/autoipsetadder/"$2
|
||||
getline drop< pidfile;
|
||||
close(pidfile);
|
||||
if (ERRNO) {
|
||||
addlist=0;
|
||||
print("bypass"$1" "$2);
|
||||
next;}
|
||||
if ($3=="443")
|
||||
{
|
||||
cmd=("httping -c 1 -t 4 -l "$2" --divert-connect "$1);
|
||||
}
|
||||
else if ($3=="80")
|
||||
{
|
||||
cmd=("httping -c 1 -t 4 "$2" --divert-connect "$1);
|
||||
}
|
||||
addlist=0;
|
||||
slow=0;
|
||||
while ((cmd | getline ret) > 0)
|
||||
{
|
||||
if (addlist!=0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else if (index(ret,"short read")!=0)
|
||||
{
|
||||
if (system("httping -q -c 1 -t 4 -l "$2" --divert-connect "$1)==0)
|
||||
{
|
||||
addlist=-1;
|
||||
break;
|
||||
}else{
|
||||
print("doname rst autoaddip "$1" "$2);
|
||||
addlist=1;
|
||||
}
|
||||
}
|
||||
else if (index(ret,"timeout")!=0)
|
||||
{
|
||||
while ((cmd | getline ret) > 0)
|
||||
{
|
||||
if (index(ret,"timeout")!=0)
|
||||
{
|
||||
print("direct so slow autoaddip "$1" "$2);
|
||||
addlist=1;
|
||||
slow=1;
|
||||
}
|
||||
}
|
||||
}else if (index(ret,"SSL handshake error: (null)")!=0)
|
||||
{
|
||||
if(system("curl -m 10 --resolve "$2":443:"$1" https://"$2" -o /dev/null 2>/dev/null")==0){
|
||||
addlist=-1;
|
||||
break;
|
||||
}
|
||||
}else if (index(ret,"Connection refused")!=0){
|
||||
print("direct Connection refused autoaddip"$1" "$2);
|
||||
addlist=1;
|
||||
}
|
||||
}
|
||||
close(cmd);
|
||||
|
||||
if (addlist!=1)
|
||||
{
|
||||
if (addlist==0){
|
||||
split(ret, c,"[ /]+");
|
||||
print(c[6]);
|
||||
if (c[6]=="failed,"){
|
||||
print("can not connect autoaddip "$1" "$2);
|
||||
addlist=1;
|
||||
}
|
||||
else if (c[6]+0>10000)
|
||||
{
|
||||
print("direct ssl so slow autoaddip "$1" "$2);
|
||||
addlist=1;
|
||||
}else{
|
||||
addlist=-1;}
|
||||
}
|
||||
if (addlist==-1 && pingadd==1)
|
||||
{
|
||||
while (("ping -c 5 -q -A "$1 | getline ret) > 0)
|
||||
{
|
||||
if (index(ret,"packet loss")!=0)
|
||||
{
|
||||
split(ret, p,"[ ]+");
|
||||
if (p[4]>0 && p[4]<5)
|
||||
{
|
||||
print("ping packet loss autoaddip "$1" "$2);
|
||||
pingloss=1;
|
||||
addlist=1;
|
||||
}else{pingloss=0;}
|
||||
break;
|
||||
}
|
||||
}
|
||||
close("ping -c 5 -q "$1);
|
||||
}
|
||||
}
|
||||
ERRNO="";
|
||||
if (pingloss!=1){
|
||||
getline drop< pidfile;
|
||||
close(pidfile);
|
||||
}
|
||||
if (ERRNO) {addlist=0;next;}
|
||||
if (addlist==1){
|
||||
system("ipset add gfwlist "$1);
|
||||
while ((cmd | getline ret) > 0)
|
||||
{
|
||||
if (addlist==1)
|
||||
{
|
||||
if (index(ret,"short read")!=0)
|
||||
{
|
||||
system("ipset del gfwlist "$1);
|
||||
print("doname proxy rst autodelip "$1" "$2);
|
||||
addlist=-2;
|
||||
}
|
||||
else if (index(ret,"SSL handshake error: (null)")!=0)
|
||||
{
|
||||
if(system("curl -m 10 --resolve "$2":443:"$1" https://"$2" -o /dev/null 2>/dev/null")==0)
|
||||
{
|
||||
addlist=2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
close(cmd);
|
||||
if (addlist==1){
|
||||
split(ret, c,"[ /]+");
|
||||
print(c[6]);
|
||||
if (c[6]=="failed,")
|
||||
{
|
||||
system("ipset del gfwlist "$1);
|
||||
print("proxy can not connect autodelip "$1" "$2);
|
||||
addlist=-2;
|
||||
}else{
|
||||
addlist=2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}END{
|
||||
if (addlist==2)
|
||||
{ if (pingloss==0){
|
||||
ERRNO="";
|
||||
getline drop< pidfile;
|
||||
if (ERRNO) {
|
||||
system("ipset del gfwlist "$1);
|
||||
print("cancel add myself "$1" "$2" due to one ip success direct");
|
||||
}else{
|
||||
print $1"\n">>pidfile;
|
||||
}
|
||||
close(pidfile);}
|
||||
}else if (addlist==-1)
|
||||
{
|
||||
print($1" "$2" direct success");
|
||||
while ((getline ret< pidfile) > 0)
|
||||
{
|
||||
if (ret!=""){
|
||||
system("ipset del gfwlist "ret);
|
||||
print("cancel add someone "ret" "$2" due to me"$1" success direct");
|
||||
}
|
||||
}
|
||||
close(pidfile);
|
||||
system("rm "pidfile" 2>/dev/null");
|
||||
print($1" del "$2);
|
||||
}else if (addlist==-2)
|
||||
{
|
||||
system("sleep 10");
|
||||
while ((getline ret< pidfile) > 0)
|
||||
{
|
||||
if (ret!=""){
|
||||
system("ipset add gfwlist "$1);
|
||||
print("add "ret" "$2" due to one ip success proxy");
|
||||
break;}
|
||||
}
|
||||
close(pidfile);
|
||||
}}'
|
||||
Reference in new issue
Block a user