Skip to content

Commit f198b49

Browse files
authored
精简模式,随机地址获取IP
更新来自原作者: 添加推送内容精简, 添加随机地址获取IP,避免连接频繁等原因导致获取失败
2 parents 949ebe2 + a794c55 commit f198b49

File tree

3 files changed

+75
-26
lines changed

3 files changed

+75
-26
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
include $(TOPDIR)/rules.mk
77

88
PKG_NAME:=luci-app-pushbot
9-
PKG_VERSION:=3.10
9+
PKG_VERSION:=3.12
1010
PKG_RELEASE:=9
1111

1212
include $(INCLUDE_DIR)/package.mk

luasrc/model/cbi/pushbot/setting.lua

+26-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ a=s:taboption("basic", Flag,"pushbot_enable",translate("启用"))
2828
a.default=0
2929
a.rmempty = true
3030

31+
--精简模式
32+
a=s:taboption("basic", Flag,"lite_enable",translate("精简模式"))
33+
a.default=0
34+
a.rmempty = true
35+
36+
a= s:taboption("basic", Flag, "content_current_device", "精简当前设备列表")
37+
a.default = 0
38+
a.rmempty = true
39+
a:depends({lite_enable="1"})
40+
41+
a= s:taboption("basic", Flag, "content_nowtime", "精简当前时间")
42+
a.default = 0
43+
a.rmempty = true
44+
a:depends({lite_enable="1"})
45+
46+
a= s:taboption("basic", Flag, "content_content", "只推送标题")
47+
a.default = 0
48+
a.rmempty = true
49+
a:depends({lite_enable="1"})
50+
3151
a=s:taboption("basic", ListValue,"send_we",translate("推送模式"))
3252
a.default=""
3353
a.rmempty = true
@@ -149,11 +169,11 @@ for _, iface in ipairs(ifaces) do
149169
end
150170
a.description = translate("<br/>一般选择 wan 接口,多拨环境请自行选择")
151171

152-
a= s:taboption("content", Value, "ipv4_URL", "URL 地址")
172+
a= s:taboption("content", DynamicList, "ipv4_URL", "URL 地址")
153173
a.rmempty = true
154-
a.default = "members.3322.org/dyndns/getip"
174+
155175
a:depends({pushbot_ipv4="2"})
156-
a.description = translate("<br/>会因服务器稳定性、连接频繁等原因导致获取失败<br/>如接口可以正常获取 IP,不推荐使用")
176+
a.description = translate("<br/>会因服务器稳定性、连接频繁等原因导致获取失败<br/>从以上列表中随机一个地址,留空使用默认地址")
157177

158178
a=s:taboption("content", ListValue,"pushbot_ipv6",translate("ipv6 变动通知"))
159179
a.rmempty = true
@@ -178,11 +198,11 @@ for _, iface in ipairs(ifaces) do
178198
end
179199
a.description = translate("<br/>一般选择 wan 接口,多拨环境请自行选择")
180200

181-
a= s:taboption("content", Value, "ipv6_URL", "URL 地址")
201+
a= s:taboption("content", DynamicList, "ipv6_URL", "URL 地址")
182202
a.rmempty = true
183-
a.default = "v6.ip.zxinc.org/getip"
203+
184204
a:depends({pushbot_ipv6="2"})
185-
a.description = translate("<br/>会因服务器稳定性、连接频繁等原因导致获取失败<br/>如接口可以正常获取 IP,不推荐使用")
205+
a.description = translate("<br/>会因服务器稳定性、连接频繁等原因导致获取失败<br/>从以上列表中随机一个地址,留空使用默认地址")
186206

187207
a=s:taboption("content", Flag,"pushbot_up",translate("设备上线通知"))
188208
a.default=1

root/usr/bin/pushbot/pushbot

+48-19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function get_config(){
1010

1111
# 初始化设置信息
1212
function read_config(){
13-
get_config "pushbot_enable" "dd_webhook" "pushbot_ipv4" "ipv4_interface" "ipv4_URL" "pushbot_ipv6" "ipv6_interface" "ipv6_URL" "pushbot_up" "pushbot_down" "pushbot_sheep" "pushbot_whitelist" "pushbot_blacklist" "pushbot_interface" "starttime" "endtime" "cpuload_enable" "cpuload" "temperature_enable" "temperature" "device_name" "err_enable" "network_err_event" "err_sheep_enable" "system_time_event" "autoreboot_time" "network_restart_time" "public_ip_event" "public_ip_retry_count" "soc_code" "sleeptime" "up_timeout" "down_timeout" "device_aliases" "debuglevel" "cpuload" "temperature" "send_mode" "regular_time" "regular_time_2" "regular_time_3" "interval_time" "thread_num" "timeout_retry_count" "send_we" "we_webhook" "pp_token" "pp_channel" "pp_webhook" "pp_topic_enable" "pp_topic" "fs_webhook" "bark_srv_enable" "bark_srv" "bark_token" "err_device_aliases" "oui_dir" "oui_data" "client_usage" "client_usage_max" "client_usage_disturb" "client_usage_whitelist" "reset_regularly"
13+
get_config "pushbot_enable" "dd_webhook" "pushbot_ipv4" "ipv4_interface" "pushbot_ipv6" "ipv6_interface" "pushbot_up" "pushbot_down" "pushbot_sheep" "pushbot_whitelist" "pushbot_blacklist" "pushbot_interface" "starttime" "endtime" "cpuload_enable" "cpuload" "temperature_enable" "temperature" "device_name" "err_enable" "network_err_event" "err_sheep_enable" "system_time_event" "autoreboot_time" "network_restart_time" "public_ip_event" "public_ip_retry_count" "soc_code" "sleeptime" "up_timeout" "down_timeout" "device_aliases" "debuglevel" "cpuload" "temperature" "send_mode" "regular_time" "regular_time_2" "regular_time_3" "interval_time" "thread_num" "timeout_retry_count" "send_we" "we_webhook" "pp_token" "pp_channel" "pp_webhook" "pp_topic_enable" "pp_topic" "fs_webhook" "bark_srv_enable" "bark_srv" "bark_token" "err_device_aliases" "oui_dir" "oui_data" "client_usage" "client_usage_max" "client_usage_disturb" "client_usage_whitelist" "reset_regularly" "ipv4_urllist" "ipv6_urllist" "lite_enable" "content_current_device" "content_nowtime" "content_content"
1414
for str_version in "wrtbwmon" "iputils-arping" "curl" "iw"; do
1515
eval `echo ${str_version:0:2}"_version"`=`opkg list-installed|grep -w ^${str_version}|awk '{print $3}'` 2>/dev/null
1616
done
@@ -30,6 +30,7 @@ function read_config(){
3030
[ ! -z "$send_we" ] && [ "$send_we" -eq "2" ] && percentsym=25 && markdown_splitline="%0D%0A%0D%0A" && markdown_linefeed="%0D%0A%0D%0A" && greenfont="" && bluefont="" && fontend="" && tabletab=" " && boldstar=""
3131
[ ! -z "$send_we" ] && [ "$send_we" -eq "3" ] && titlel4="" && titlebold="" && titleboldend="" && titleblue="" && titlegreen="" && titleend="" && titlered="" && greenfont="" && bluefont="" && fontend="" && tabletab=" " && boldstar="" && markdown_splitline="\n---\n" && markdown_linefeed="\n"
3232
[ ! -z "$send_we" ] && [ "$send_we" -eq "4" ] && titlel4="" && titlebold="" && titleboldend="" && titleblue="" && titlegreen="" && titleend="" && titlered="" && greenfont="" && bluefont="" && fontend="" && tabletab=" " && boldstar="" && markdown_splitline="%0D%0A%0D%0A" && markdown_linefeed="%0D%0A%0D%0A" && percentsym=25 && [ -z "$bark_srv" ] && bark_srv="https://api.day.app"
33+
# [ ! -z "$content_content" ] && [ "$content_content" -eq "1" ] && str_title_start="" && str_title_end="" && str_splitline="" && str_linefeed="" && str_tab=""
3334
}
3435

3536
# 初始化
@@ -96,17 +97,35 @@ function getip(){
9697
[ -z "$ipv4_interface" ] && local wanIP=$(getinterfacelist|grep '\"address\"'|grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
9798
echo "$wanIP"
9899
elif [ $1 == "hostipv4" ] ;then
99-
[ -z "$ipv4_URL" ] && local ipv4_URL="members.3322.org/dyndns/getip"
100-
[ ! -z "$ipv4_interface" ] && local hostIP=$(curl -k -s -4 --interface ${ipv4_interface} ${ipv4_URL}) || local hostIP=$(curl -k -s -4 ${ipv4_URL})
101-
echo "$hostIP"
100+
function get_hostipv4()
101+
{
102+
[ -z "$ipv4_urllist" ] && local ipv4_urllist="www.cip.cc myip.ipip.net ipv4.ddnspod.com ifcfg.cn speed.neu.edu.cn/getIP.php ddns.oray.com/checkip www.net.cn/static/customercare/yourip.asp"
103+
local url_number=`expr $(echo "$ipv4_urllist"|grep -o ' '|wc -l) + 1`
104+
local ipv4_URL=`echo "$ipv4_urllist"|awk -v i=$(rand 1 $url_number) '{print $i}'`
105+
[ ! -z "$ipv4_interface" ] && local hostIP=$(curl -k -s -4 --interface ${ipv4_interface} -m 5 ${ipv4_URL}) || local hostIP=$(curl -k -s -4 -m 5 ${ipv4_URL})
106+
echo $hostIP|grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'|head -n1
107+
}
108+
local hostIP=`get_hostipv4`
109+
[ -z "$hostIP" ] && local hostIP=`get_hostipv4`
110+
[ -z "$hostIP" ] && local hostIP=`get_hostipv4`
111+
echo $hostIP
102112
elif [ $1 == "wanipv6" ] ;then
103113
[ ! -z "$ipv6_interface" ] && local wanIPv6=$(ip addr show ${ipv6_interface}|grep -v deprecated|grep -A1 'inet6 [^f:]'|sed -nr ':a;N;s#^ +inet6 ([a-f0-9:]+)/.+? scope global .*? valid_lft ([0-9]+sec) .*#\2 \1#p;ta'|sort -nr|head -n1|awk '{print $2}')
104114
[ -z "$ipv6_interface" ] && local wanIPv6=$(ip addr show|grep -v deprecated|grep -A1 'inet6 [^f:]'|sed -nr ':a;N;s#^ +inet6 ([a-f0-9:]+)/.+? scope global .*? valid_lft ([0-9]+sec) .*#\2 \1#p;ta'|sort -nr|head -n1|awk '{print $2}')
105115
echo "$wanIPv6"
106116
elif [ $1 == "hostipv6" ] ;then
107-
[ -z "$ipv6_URL" ] && local ipv6_URL="v6.ip.zxinc.org/getip"
108-
[ ! -z "$ipv6_interface" ] && local hostIPv6=$(curl -k -s -6 --interface ${ipv6_interface} ${ipv6_URL}) || local hostIPv6=$(curl -k -s -6 ${ipv6_URL})
109-
echo "$hostIPv6"
117+
function get_hostipv6()
118+
{
119+
[ -z "$ipv6_urllist" ] && local ipv6_urllist="ip.sb ipv6.ddnspod.com api-ipv6.ip.sb/ip speed.neu6.edu.cn/getIP.php v6.myip.la/json"
120+
local urlv6_number=`expr $(echo "$ipv6_urllist"|grep -o ' '|wc -l) + 1`
121+
local ipv6_URL=`echo "$ipv6_urllist"|awk -v i=$(rand 1 $url_number) '{print $i}'`
122+
[ ! -z "$ipv6_interface" ] && local hostIPv6=$(curl -k -s -6 --interface ${ipv6_interface} -m 5 ${ipv6_URL}) || local hostIPv6=$(curl -k -s -6 -m 5 ${ipv6_URL})
123+
echo $hostIPv6|grep -oE '([\da-fA-F0-9]{1,4}(:{1,2})){1,15}[\da-fA-F0-9]{1,4}'
124+
}
125+
local hostIPv6=`get_hostipv6`
126+
[ -z "$hostIPv6" ] && local hostIPv6=`get_hostipv6`
127+
[ -z "$hostIPv6" ] && local hostIPv6=`get_hostipv6`
128+
echo $hostIPv6 # 重试,偷懒,有空再优化
110129
fi
111130
}
112131

@@ -321,6 +340,14 @@ function cut_str {
321340
echo $(echo "$1"|cut -c -$temp_length)"*"
322341
}
323342

343+
# 随机数
344+
function rand(){
345+
min=$1
346+
max=$(($2- $min + 1))
347+
num=$(date +%s%N)
348+
echo $(($num % $max + $min))
349+
}
350+
324351
# 在线设备列表
325352
function pushbot_first(){
326353
[ -f "${dir}ipAddress" ] && local IPLIST=`cat ${dir}ipAddress|awk '{print $1}'|grep -v "^$"|sort -u`
@@ -347,7 +374,7 @@ function pushbot_first(){
347374
# 创建计划任务
348375
function pushbot_cron(){
349376
function del_cron(){
350-
( echo `crontab -l`|grep -q "pushbot" ) && crontab -l > conf && sed -i "/pushbot/d" conf && crontab conf && rm -f conf >/dev/null 2>&1
377+
( echo `crontab -l 2>/dev/null`|grep -q "pushbot" ) && crontab -l > conf && sed -i "/pushbot/d" conf && crontab conf && rm -f conf >/dev/null 2>&1
351378
}
352379
function re_cron(){
353380
/etc/init.d/cron stop
@@ -361,17 +388,17 @@ function pushbot_cron(){
361388

362389
# 重置流量
363390
if [ ! -z "$reset_regularly" ] && [ "$reset_regularly" -eq "1" ]; then
364-
crontab -l > conf && echo -e "0 0 * * * rm /tmp/pushbot/usage.db >/dev/null 2>&1" >> conf && crontab conf && rm -f conf >/dev/null 2>&1
365-
crontab -l > conf && echo -e "0 0 * * * rm /tmp/pushbot/usage6.db >/dev/null 2>&1" >> conf && crontab conf && rm -f conf >/dev/null 2>&1
391+
crontab -l 2>/dev/null > conf && echo -e "0 0 * * * rm /tmp/pushbot/usage.db >/dev/null 2>&1" >> conf && crontab conf && rm -f conf >/dev/null 2>&1
392+
crontab -l 2>/dev/null > conf && echo -e "0 0 * * * rm /tmp/pushbot/usage6.db >/dev/null 2>&1" >> conf && crontab conf && rm -f conf >/dev/null 2>&1
366393
fi
367394
[ ! -z "$regular_time_2" ] && local regular_time_2=",${regular_time_2}"
368395
[ ! -z "$regular_time_3" ] && local regular_time_3=",${regular_time_3}"
369396
# 定时发送
370397
if [ ! -z "$regular_time" ] || [ ! -z "$regular_time_2" ] || [ ! -z "$regular_time_3" ]; then
371-
crontab -l > conf && echo -e "0 $regular_time$regular_time_2$regular_time_3 * * * /usr/bin/pushbot/pushbot send &" >> conf && crontab conf && rm -f conf >/dev/null 2>&1
398+
crontab -l 2>/dev/null > conf && echo -e "0 $regular_time$regular_time_2$regular_time_3 * * * /usr/bin/pushbot/pushbot send &" >> conf && crontab conf && rm -f conf >/dev/null 2>&1
372399
# 间隔发送
373400
elif [ ! -z "$interval_time" ]; then
374-
crontab -l > conf && echo -e "0 */$interval_time * * * /usr/bin/pushbot/pushbot send &" >> conf && crontab conf && rm -f conf >/dev/null 2>&1
401+
crontab -l 2>/dev/null > conf && echo -e "0 */$interval_time * * * /usr/bin/pushbot/pushbot send &" >> conf && crontab conf && rm -f conf >/dev/null 2>&1
375402
fi
376403
re_cron
377404
}
@@ -524,10 +551,11 @@ function unattended(){
524551

525552
# 检测网络状态
526553
function rand_geturl(){
527-
local urllist="https://www.163.com https://www.qq.com https://www.baidu.com https://www.qidian.com https://www.douban.com"
528-
local url_str=$( echo "$urllist"|awk -v i="`awk 'BEGIN{srand();sum=rand()*5+ 1;printf("%d",sum)}'`" '{print $i}' )
529554
function getcheck(){
530-
echo `curl -k -s -w "%{http_code}" ${url_str} -A "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11" -o /dev/null`
555+
local urllist="https://www.163.com https://www.qq.com https://www.baidu.com https://www.qidian.com https://www.douban.com"
556+
local url_number=`expr $(echo "$urllist"|grep -o ' '|wc -l) + 1`
557+
local url_str=`echo "$urllist"|awk -v i=$(rand 1 $url_number) '{print $i}'`
558+
echo `curl -k -s -w "%{http_code}" -m 5 ${url_str} -A "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36" -o /dev/null`
531559
}
532560
local check=`getcheck`
533561
while [ -z "$check" ] || [ "$check" -ne "200" ]; do
@@ -556,7 +584,7 @@ function rand_geturl(){
556584
elif [ "$network_err_event" -eq "2" ] ;then
557585
[ "$retry_count" -lt "3" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】正在尝试重启网络,当前第 $retry_count" >> ${logfile} && ifup wan >/dev/null 2>&1
558586
[ "$retry_count" -eq "3" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】已经重启网络2次,修复失败,请主人自行修复哦 " >> ${logfile}
559-
elif [ "$network_err_event" -eq "3" ] ;then
587+
elif [ "$network_err_event" -eq "3" ] ;then
560588
if [ "$retry_count" -eq "1" ] ;then
561589
echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】正在尝试修复网络,当前第 1 次,重启网络服务中 " >> ${logfile} && network_restart
562590
elif [ "$retry_count" -eq "2" ] ;then
@@ -610,7 +638,7 @@ function rand_geturl(){
610638
network_err_time=`expr $network_err_time - 600` && sleep 60
611639
fi
612640
enable_detection
613-
sleep 60
641+
sleep $sleeptime
614642
fi
615643
continue
616644
done
@@ -985,7 +1013,7 @@ while [ "$pushbot_enable" -eq "1" ]; do
9851013
[ ! -f "${dir}send_enable.lock" ] && down_send
9861014

9871015
# 当前设备列表
988-
[ ! -z "$content" ] && [ ! -f "${dir}send_enable.lock" ] && current_device
1016+
[ ! -z "$content" ] && [ ! -f "${dir}send_enable.lock" ] && [ -z "$content_current_device" ] || [ "$content_current_device" -eq "0" ] && current_device
9891017

9901018
# 无人值守任务
9911019
[ ! -f "${dir}send_enable.lock" ] && unattended
@@ -997,9 +1025,10 @@ while [ "$pushbot_enable" -eq "1" ]; do
9971025
[ ! -f "${dir}send_enable.lock" ] && get_client_usage
9981026

9991027
if [ ! -f "${dir}send_enable.lock" ] && [ ! -z "$title" ] && [ ! -z "$content" ]; then
1000-
nowtime=`date "+%Y-%m-%d %H:%M:%S"`
1028+
[ -z "$content_nowtime" ] || [ "$content_nowtime" -eq "0" ] && nowtime=`date "+%Y-%m-%d %H:%M:%S"`
10011029
[ ! -z "$device_name" ] && title="$device_name$title"
10021030
title=`echo "$title"|sed $'s/\ / /g'|sed $'s/\"/%22/g'|sed $'s/\#/%23/g'|sed $'s/\&/%26/g'|sed $'s/\,/%2C/g'|sed $'s/\//%2F/g'|sed $'s/\:/%3A/g'|sed $'s/\;/%3B/g'|sed $'s/\=/%3D/g'|sed $'s/\@/%40/g'`
1031+
[ ! -z "$content_content" ] && [ "$content_content" -eq "1" ] && content=""
10031032
pushbot_send="curl -s \"https://oapi.dingtalk.com/robot/send?access_token=${dd_webhook}\" -H 'Content-Type: application/json' -d '{\"msgtype\": \"markdown\",\"markdown\": {\"title\":\"${title}\",\"text\":\"${titlel4} ${titlebold}<font color=#6A65FF>${title}</font>${titleboldend}${markdown_linefeed}${nowtime}${markdown_linefeed}${content}${markdown_linefeed}${titlebold}<font color=#6A65FF>${title}</font>${titleboldend}\"}}'"
10041033
wechatent_send="curl -s \"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${we_webhook}\" -H 'Content-Type: application/json' -d '{\"msgtype\": \"markdown\",\"markdown\": {\"title\":\"${title}\",\"content\":\"${titlel4} ${titlebold}<font color=#6A65FF>${title}</font>${titleboldend}${markdown_linefeed}${nowtime}${markdown_linefeed}${content}${markdown_linefeed}${titlebold}<font color=#6A65FF>${title}</font>${titleboldend}\"}}'"
10051034
feishu_send="curl -X POST https://open.feishu.cn/open-apis/bot/v2/hook/${fs_webhook} -H 'Content-Type: application/json' -d '{\"msg_type\": \"post\",\"content\": {\"post\": {\"zh_cn\": {\"title\": \"${title}\",\"content\": [[{\"tag\": \"text\",\"text\": \"${nowtime}${markdown_linefeed}${content}\"}]]}}}}'"

0 commit comments

Comments
 (0)