Skip to content

Commit

Permalink
精简模式,随机地址获取IP
Browse files Browse the repository at this point in the history
更新来自原作者:
添加推送内容精简,
添加随机地址获取IP,避免连接频繁等原因导致获取失败
  • Loading branch information
zzsj0928 authored Jul 27, 2021
2 parents 949ebe2 + a794c55 commit f198b49
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=luci-app-pushbot
PKG_VERSION:=3.10
PKG_VERSION:=3.12
PKG_RELEASE:=9

include $(INCLUDE_DIR)/package.mk
Expand Down
32 changes: 26 additions & 6 deletions luasrc/model/cbi/pushbot/setting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ a=s:taboption("basic", Flag,"pushbot_enable",translate("启用"))
a.default=0
a.rmempty = true

--精简模式
a=s:taboption("basic", Flag,"lite_enable",translate("精简模式"))
a.default=0
a.rmempty = true

a= s:taboption("basic", Flag, "content_current_device", "精简当前设备列表")
a.default = 0
a.rmempty = true
a:depends({lite_enable="1"})

a= s:taboption("basic", Flag, "content_nowtime", "精简当前时间")
a.default = 0
a.rmempty = true
a:depends({lite_enable="1"})

a= s:taboption("basic", Flag, "content_content", "只推送标题")
a.default = 0
a.rmempty = true
a:depends({lite_enable="1"})

a=s:taboption("basic", ListValue,"send_we",translate("推送模式"))
a.default=""
a.rmempty = true
Expand Down Expand Up @@ -149,11 +169,11 @@ for _, iface in ipairs(ifaces) do
end
a.description = translate("<br/>一般选择 wan 接口,多拨环境请自行选择")

a= s:taboption("content", Value, "ipv4_URL", "URL 地址")
a= s:taboption("content", DynamicList, "ipv4_URL", "URL 地址")
a.rmempty = true
a.default = "members.3322.org/dyndns/getip"

a:depends({pushbot_ipv4="2"})
a.description = translate("<br/>会因服务器稳定性、连接频繁等原因导致获取失败<br/>如接口可以正常获取 IP,不推荐使用")
a.description = translate("<br/>会因服务器稳定性、连接频繁等原因导致获取失败<br/>从以上列表中随机一个地址,留空使用默认地址")

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

a= s:taboption("content", Value, "ipv6_URL", "URL 地址")
a= s:taboption("content", DynamicList, "ipv6_URL", "URL 地址")
a.rmempty = true
a.default = "v6.ip.zxinc.org/getip"

a:depends({pushbot_ipv6="2"})
a.description = translate("<br/>会因服务器稳定性、连接频繁等原因导致获取失败<br/>如接口可以正常获取 IP,不推荐使用")
a.description = translate("<br/>会因服务器稳定性、连接频繁等原因导致获取失败<br/>从以上列表中随机一个地址,留空使用默认地址")

a=s:taboption("content", Flag,"pushbot_up",translate("设备上线通知"))
a.default=1
Expand Down
67 changes: 48 additions & 19 deletions root/usr/bin/pushbot/pushbot
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function get_config(){

# 初始化设置信息
function read_config(){
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"
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"
for str_version in "wrtbwmon" "iputils-arping" "curl" "iw"; do
eval `echo ${str_version:0:2}"_version"`=`opkg list-installed|grep -w ^${str_version}|awk '{print $3}'` 2>/dev/null
done
Expand All @@ -30,6 +30,7 @@ function read_config(){
[ ! -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=""
[ ! -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"
[ ! -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"
# [ ! -z "$content_content" ] && [ "$content_content" -eq "1" ] && str_title_start="" && str_title_end="" && str_splitline="" && str_linefeed="" && str_tab=""
}

# 初始化
Expand Down Expand Up @@ -96,17 +97,35 @@ function getip(){
[ -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}')
echo "$wanIP"
elif [ $1 == "hostipv4" ] ;then
[ -z "$ipv4_URL" ] && local ipv4_URL="members.3322.org/dyndns/getip"
[ ! -z "$ipv4_interface" ] && local hostIP=$(curl -k -s -4 --interface ${ipv4_interface} ${ipv4_URL}) || local hostIP=$(curl -k -s -4 ${ipv4_URL})
echo "$hostIP"
function get_hostipv4()
{
[ -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"
local url_number=`expr $(echo "$ipv4_urllist"|grep -o ' '|wc -l) + 1`
local ipv4_URL=`echo "$ipv4_urllist"|awk -v i=$(rand 1 $url_number) '{print $i}'`
[ ! -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})
echo $hostIP|grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'|head -n1
}
local hostIP=`get_hostipv4`
[ -z "$hostIP" ] && local hostIP=`get_hostipv4`
[ -z "$hostIP" ] && local hostIP=`get_hostipv4`
echo $hostIP
elif [ $1 == "wanipv6" ] ;then
[ ! -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}')
[ -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}')
echo "$wanIPv6"
elif [ $1 == "hostipv6" ] ;then
[ -z "$ipv6_URL" ] && local ipv6_URL="v6.ip.zxinc.org/getip"
[ ! -z "$ipv6_interface" ] && local hostIPv6=$(curl -k -s -6 --interface ${ipv6_interface} ${ipv6_URL}) || local hostIPv6=$(curl -k -s -6 ${ipv6_URL})
echo "$hostIPv6"
function get_hostipv6()
{
[ -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"
local urlv6_number=`expr $(echo "$ipv6_urllist"|grep -o ' '|wc -l) + 1`
local ipv6_URL=`echo "$ipv6_urllist"|awk -v i=$(rand 1 $url_number) '{print $i}'`
[ ! -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})
echo $hostIPv6|grep -oE '([\da-fA-F0-9]{1,4}(:{1,2})){1,15}[\da-fA-F0-9]{1,4}'
}
local hostIPv6=`get_hostipv6`
[ -z "$hostIPv6" ] && local hostIPv6=`get_hostipv6`
[ -z "$hostIPv6" ] && local hostIPv6=`get_hostipv6`
echo $hostIPv6 # 重试,偷懒,有空再优化
fi
}

Expand Down Expand Up @@ -321,6 +340,14 @@ function cut_str {
echo $(echo "$1"|cut -c -$temp_length)"*"
}

# 随机数
function rand(){
min=$1
max=$(($2- $min + 1))
num=$(date +%s%N)
echo $(($num % $max + $min))
}

# 在线设备列表
function pushbot_first(){
[ -f "${dir}ipAddress" ] && local IPLIST=`cat ${dir}ipAddress|awk '{print $1}'|grep -v "^$"|sort -u`
Expand All @@ -347,7 +374,7 @@ function pushbot_first(){
# 创建计划任务
function pushbot_cron(){
function del_cron(){
( echo `crontab -l`|grep -q "pushbot" ) && crontab -l > conf && sed -i "/pushbot/d" conf && crontab conf && rm -f conf >/dev/null 2>&1
( 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
}
function re_cron(){
/etc/init.d/cron stop
Expand All @@ -361,17 +388,17 @@ function pushbot_cron(){

# 重置流量
if [ ! -z "$reset_regularly" ] && [ "$reset_regularly" -eq "1" ]; then
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
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
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
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
fi
[ ! -z "$regular_time_2" ] && local regular_time_2=",${regular_time_2}"
[ ! -z "$regular_time_3" ] && local regular_time_3=",${regular_time_3}"
# 定时发送
if [ ! -z "$regular_time" ] || [ ! -z "$regular_time_2" ] || [ ! -z "$regular_time_3" ]; then
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
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
# 间隔发送
elif [ ! -z "$interval_time" ]; then
crontab -l > conf && echo -e "0 */$interval_time * * * /usr/bin/pushbot/pushbot send &" >> conf && crontab conf && rm -f conf >/dev/null 2>&1
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
fi
re_cron
}
Expand Down Expand Up @@ -524,10 +551,11 @@ function unattended(){

# 检测网络状态
function rand_geturl(){
local urllist="https://www.163.com https://www.qq.com https://www.baidu.com https://www.qidian.com https://www.douban.com"
local url_str=$( echo "$urllist"|awk -v i="`awk 'BEGIN{srand();sum=rand()*5+ 1;printf("%d",sum)}'`" '{print $i}' )
function getcheck(){
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`
local urllist="https://www.163.com https://www.qq.com https://www.baidu.com https://www.qidian.com https://www.douban.com"
local url_number=`expr $(echo "$urllist"|grep -o ' '|wc -l) + 1`
local url_str=`echo "$urllist"|awk -v i=$(rand 1 $url_number) '{print $i}'`
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`
}
local check=`getcheck`
while [ -z "$check" ] || [ "$check" -ne "200" ]; do
Expand Down Expand Up @@ -556,7 +584,7 @@ function rand_geturl(){
elif [ "$network_err_event" -eq "2" ] ;then
[ "$retry_count" -lt "3" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】正在尝试重启网络,当前第 $retry_count" >> ${logfile} && ifup wan >/dev/null 2>&1
[ "$retry_count" -eq "3" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】已经重启网络2次,修复失败,请主人自行修复哦 " >> ${logfile}
elif [ "$network_err_event" -eq "3" ] ;then
elif [ "$network_err_event" -eq "3" ] ;then
if [ "$retry_count" -eq "1" ] ;then
echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!!】正在尝试修复网络,当前第 1 次,重启网络服务中 " >> ${logfile} && network_restart
elif [ "$retry_count" -eq "2" ] ;then
Expand Down Expand Up @@ -610,7 +638,7 @@ function rand_geturl(){
network_err_time=`expr $network_err_time - 600` && sleep 60
fi
enable_detection
sleep 60
sleep $sleeptime
fi
continue
done
Expand Down Expand Up @@ -985,7 +1013,7 @@ while [ "$pushbot_enable" -eq "1" ]; do
[ ! -f "${dir}send_enable.lock" ] && down_send

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

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

if [ ! -f "${dir}send_enable.lock" ] && [ ! -z "$title" ] && [ ! -z "$content" ]; then
nowtime=`date "+%Y-%m-%d %H:%M:%S"`
[ -z "$content_nowtime" ] || [ "$content_nowtime" -eq "0" ] && nowtime=`date "+%Y-%m-%d %H:%M:%S"`
[ ! -z "$device_name" ] && title="$device_name$title"
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'`
[ ! -z "$content_content" ] && [ "$content_content" -eq "1" ] && content=""
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}\"}}'"
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}\"}}'"
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}\"}]]}}}}'"
Expand Down

0 comments on commit f198b49

Please sign in to comment.