From 2d78f5455be84458e7cebe140f5f7eb3bbb9b572 Mon Sep 17 00:00:00 2001 From: tty228 Date: Wed, 17 Mar 2021 20:52:55 +0800 Subject: [PATCH] luci-app-serverchan: bump to 1.81 Signed-off-by: Tianling Shen --- package/ctcgfw/luci-app-serverchan/Makefile | 2 +- .../luasrc/model/cbi/serverchan/advanced.lua | 4 +- .../luasrc/model/cbi/serverchan/setting.lua | 23 ++-- .../luasrc/view/serverchan/log.htm | 3 +- .../root/usr/bin/serverchan/serverchan | 114 ++++++++++-------- 5 files changed, 75 insertions(+), 71 deletions(-) diff --git a/package/ctcgfw/luci-app-serverchan/Makefile b/package/ctcgfw/luci-app-serverchan/Makefile index 41483837c2..620b6595ec 100644 --- a/package/ctcgfw/luci-app-serverchan/Makefile +++ b/package/ctcgfw/luci-app-serverchan/Makefile @@ -6,7 +6,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=luci-app-serverchan -PKG_VERSION:=1.80 +PKG_VERSION:=1.81 PKG_RELEASE:=8 include $(INCLUDE_DIR)/package.mk diff --git a/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/advanced.lua b/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/advanced.lua index e3111b76ca..0b273a6c5f 100644 --- a/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/advanced.lua +++ b/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/advanced.lua @@ -8,12 +8,12 @@ s = m:section(TypedSection, "serverchan", "高级设置") s.anonymous = true s.addremove = false -a=s:option(Value,"up_timeout",translate('设备上线检测超时')) +a=s:option(Value,"up_timeout",translate('设备上线检测超时(s)')) a.default = "2" a.optional=false a.datatype="uinteger" -a=s:option(Value,"down_timeout",translate('设备离线检测超时')) +a=s:option(Value,"down_timeout",translate('设备离线检测超时(s)')) a.default = "20" a.optional=false a.datatype="uinteger" diff --git a/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/setting.lua b/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/setting.lua index fe4908f598..7c198d34dc 100644 --- a/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/setting.lua +++ b/package/ctcgfw/luci-app-serverchan/luasrc/model/cbi/serverchan/setting.lua @@ -25,33 +25,23 @@ s.anonymous = true --基本设置 a=s:taboption("basic", Flag,"serverchan_enable",translate("启用")) -a.default=0 a.rmempty = true -a=s:taboption("basic", ListValue,"send_tg",translate("推送模式")) -a.default="" +a=s:taboption("basic", Value,"sckey",translate('微信推送/新旧共用'), translate("").."
旧版调用代码点击这里
调用代码获取点击这里
") a.rmempty = true -a:value("",translate("微信")) -a:value("2",translate("微信测试号版")) -a:value("1",translate("Telegram")) -a=s:taboption("basic", Value,"sckey",translate('SCKEY'), translate("Serverchan Sckey").."
调用代码获取点击这里

") +a=s:taboption("basic", Value, "tg_token", translate("TG_token"),translate("").."
获取机器人点击这里
新建机器人并拉入群组/频道,填入 token
") a.rmempty = true -a:depends("send_tg","") -a=s:taboption("basic", Value,"sctkey",translate('SCKEY'), translate("Serverchan Sckey").."
调用代码获取点击这里

") +a=s:taboption("basic", Value,"chat_id",translate('TG_chatid')) a.rmempty = true -a:depends("send_tg","2") - -a=s:taboption("basic", Value, "tgtoken", translate("tg推送链接"),translate("").."
目前公众号已停用,需要自建服务器
获取机器人点击这里
此处填入推送链接,如https://tgbot.lbyczf.com/sendMessage/:Token
") -a.rmempty = true -a:depends("send_tg","1") +a.description = translate('用 tg_token 替换下列URL中的****,并用浏览器打开
https://api.telegram.org/bot****/getUpdates
找到你的chat_id,并填入
{"chat":{"id":TG_chatid,"title":"群组名","type":"channel"}') a=s:taboption("basic", Value,"device_name",translate('本设备名称')) a.rmempty = true a.description = translate("在推送信息标题中会标识本设备名称,用于区分推送信息的来源设备") -a=s:taboption("basic", Value,"sleeptime",translate('检测时间间隔')) +a=s:taboption("basic", Value,"sleeptime",translate('检测时间间隔(s)')) a.rmempty = true a.optional = false a.default = "60" @@ -72,6 +62,9 @@ a:depends("oui_data","1") a:depends("oui_data","2") a.description = translate("懒得做自动更新了,下载到内存中,重启会重新下载
若无梯子,还是下到机身吧") +a=s:taboption("basic", Flag,"reset_regularly",translate("每天零点重置流量数据")) +a.rmempty = true + a=s:taboption("basic", Flag,"debuglevel",translate("开启日志")) a.rmempty = true diff --git a/package/ctcgfw/luci-app-serverchan/luasrc/view/serverchan/log.htm b/package/ctcgfw/luci-app-serverchan/luasrc/view/serverchan/log.htm index 0802053c28..f3328a4499 100644 --- a/package/ctcgfw/luci-app-serverchan/luasrc/view/serverchan/log.htm +++ b/package/ctcgfw/luci-app-serverchan/luasrc/view/serverchan/log.htm @@ -17,7 +17,7 @@ local dsp = require "luci.dispatcher" } XHR.poll(2, '<%=dsp.build_url("admin/services/serverchan/get_log")%>', null, function(x, data) { - if(x && x.status == 200) { + if(x && x.status == 200 && document.getElementById("checkbox1").checked == true) { var log_textarea = document.getElementById('log_textarea'); log_textarea.innerHTML = x.responseText; log_textarea.scrollTop = log_textarea.scrollHeight; @@ -27,6 +27,7 @@ local dsp = require "luci.dispatcher" //]]>
+ <%:自动刷新%>
diff --git a/package/ctcgfw/luci-app-serverchan/root/usr/bin/serverchan/serverchan b/package/ctcgfw/luci-app-serverchan/root/usr/bin/serverchan/serverchan index 99273b7be1..a0728e0317 100755 --- a/package/ctcgfw/luci-app-serverchan/root/usr/bin/serverchan/serverchan +++ b/package/ctcgfw/luci-app-serverchan/root/usr/bin/serverchan/serverchan @@ -10,7 +10,7 @@ function get_config(){ # 初始化设置信息 function read_config(){ - get_config "serverchan_enable" "sckey" "serverchan_ipv4" "ipv4_interface" "ipv4_URL" "serverchan_ipv6" "ipv6_interface" "ipv6_URL" "serverchan_up" "serverchan_down" "serverchan_sheep" "serverchan_whitelist" "serverchan_blacklist" "serverchan_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_tg" "tgtoken" "err_device_aliases" "sctkey" "oui_dir" "oui_data" "client_usage" "client_usage_max" "client_usage_disturb" "client_usage_whitelist" + get_config "serverchan_enable" "sckey" "sctkey" "tg_token" "chat_id" "serverchan_ipv4" "ipv4_interface" "ipv4_URL" "serverchan_ipv6" "ipv6_interface" "ipv6_URL" "serverchan_up" "serverchan_down" "serverchan_sheep" "serverchan_whitelist" "serverchan_blacklist" "serverchan_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" "err_device_aliases" "oui_dir" "oui_data" "client_usage" "client_usage_max" "client_usage_disturb" "client_usage_whitelist" "reset_regularly" 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 @@ -25,7 +25,9 @@ function read_config(){ [ -z "$down_timeout" ] || [ "$down_timeout" -eq "0" ] && down_timeout="20";down_timeout=`expr ${down_timeout} / 2 + 1` [ -z "$timeout_retry_count" ] && timeout_retry_count="2";[ "$timeout_retry_count" -eq "0" ] && timeout_retry_count="1" markdown_splitline="%0D%0A%0D%0A---%0D%0A%0D%0A";markdown_linefeed="%0D%0A%0D%0A";markdown_tab=" ";markdown_space=" " - [ ! -z "$send_tg" ] && [ "$send_tg" -eq "1" ] && markdown_splitline="%0D%0A%0D%0A%0D%0A%0D%0A" && markdown_tab="" + [ -z "$sckey" ] && [ ! -z "$sctkey" ] && sckey="$sctkey" && uci set serverchan.serverchan.sckey="$sctkey" && uci delete serverchan.serverchan.sctkey && uci commit serverchan + [ ! -z "$sckey" ] && [ ${sckey:0:3} == "SCU" ] && sckey_url="http://sc.ftqq.com/${sckey}.send" + [ ! -z "$sckey" ] && [ ${sckey:0:3} == "SCT" ] && sckey_url="http://sctapi.ftqq.com/${sckey}.send" } # 初始化 @@ -40,10 +42,9 @@ function serverchan_init(){ deltemp rm -f ${dir}fd1 ${dir}sheep_usage ${dir}old_sheep_usage ${dir}client_usage_aliases ${dir}old_client_usage_aliases /usr/bin/serverchan/errlog >/dev/null 2>&1 [ ! -f "/usr/sbin/wrtbwmon" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】未安装 wrtbwmon ,流量统计不可用" >> ${logfile} - for read_str in "ip_version" "cu_version"; do - [ -z `eval echo '$'$read_str` ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】依赖项 iputils-arping、curl 未安装,请确认插件是否正常运行" >> ${logfile} - done - [ -z "$sckey" ] && [ -z "$sctkey" ] && [ -z "$tgtoken" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】请填写正确的 sckey " >> ${logfile} && return 1 + [ -z "$ip_version" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法获取依赖项 iputils-arping 版本号,请确认插件是否正常运行" >> ${logfile} + [ -z "$cu_version" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法获取依赖项 curl 版本号,请确认插件是否正常运行" >> ${logfile} + [ -z "$sckey" ] && [ -z "$tg_token" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】请填写正确的 sckey " >> ${logfile} && return 1 local interfacelist=`getinterfacelist` && [ -z "$interfacelist" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【!!!】无法正确获取接口信息,请确认插件是否正常运行" >> ${logfile} return 0 } @@ -121,21 +122,21 @@ function getinterfaceuptime(){ # 查询 mac 地址 function getmac(){ ( echo "$tmp_mac"|grep -q "unknown" ) && unset tmp_mac # 为unknown时重新读取 - [ -f "${dir}ipAddress" ] && [ -z "$tmp_mac" ] && local tmp_mac=`cat ${dir}ipAddress|grep -w ${1}|awk '{print $2}'|grep -v "^$"|sort -u` - [ -f "${dir}tmp_downlist" ] && [ -z "$tmp_mac" ] && local tmp_mac=`cat ${dir}tmp_downlist|grep -w ${1}|awk '{print $2}'|grep -v "^$"|sort -u` - [ -f "/var/dhcp.leases" ] && [ -z "$tmp_mac" ] && local tmp_mac=`cat /var/dhcp.leases|grep -w ${1}|awk '{print $2}'|grep -v "^$"|sort -u` - [ -z "$tmp_mac" ] && local tmp_mac=`cat /proc/net/arp|grep "0x2\|0x6"|grep -w ${1}|awk '{print $4}'|grep -v "^$"|sort -u` + [ -f "${dir}ipAddress" ] && [ -z "$tmp_mac" ] && local tmp_mac=`cat ${dir}ipAddress|grep -w ${1}|awk '{print $2}'|grep -v "^$"|sort -u|head -n1` + [ -f "${dir}tmp_downlist" ] && [ -z "$tmp_mac" ] && local tmp_mac=`cat ${dir}tmp_downlist|grep -w ${1}|awk '{print $2}'|grep -v "^$"|sort -u|head -n1` + [ -f "/var/dhcp.leases" ] && [ -z "$tmp_mac" ] && local tmp_mac=`cat /var/dhcp.leases|grep -w ${1}|awk '{print $2}'|grep -v "^$"|sort -u|head -n1` + [ -z "$tmp_mac" ] && local tmp_mac=`cat /proc/net/arp|grep "0x2\|0x6"|grep -w ${1}|awk '{print $4}'|grep -v "^$"|sort -u|head -n1` [ -z "$tmp_mac" ] && local tmp_mac="unknown" echo "$tmp_mac" } # 查询主机名 function getname(){ - [ -z "$tmp_name" ] && local tmp_name=`echo "$device_aliases"|grep -i $2|awk '{print $2}'|grep -v "^$"|sort -u` - [ -f "${dir}ipAddress" ] && [ -z "$tmp_name" ] && local tmp_name=`cat ${dir}ipAddress|grep -w ${1}|awk '{print $3}'|grep -v "^$"|sort -u` - [ -f "${dir}tmp_downlist" ] && [ -z "$tmp_name" ] && local tmp_name=`cat ${dir}tmp_downlist|grep -w ${1}|awk '{print $3}'|grep -v "^$"|sort -u` + [ -z "$tmp_name" ] && local tmp_name=`echo "$device_aliases"|grep -i $2|awk '{print $2}'|grep -v "^$"|sort -u|head -n1` + [ -f "${dir}ipAddress" ] && [ -z "$tmp_name" ] && local tmp_name=`cat ${dir}ipAddress|grep -w ${1}|awk '{print $3}'|grep -v "^$"|sort -u|head -n1` + [ -f "${dir}tmp_downlist" ] && [ -z "$tmp_name" ] && local tmp_name=`cat ${dir}tmp_downlist|grep -w ${1}|awk '{print $3}'|grep -v "^$"|sort -u|head -n1` ( ! echo "$tmp_name"|grep -q -w "unknown\|*" ) && [ ! -z "$tmp_name" ] && echo "$tmp_name" && return || unset tmp_name # 为unknown时重新读取 - [ -f "/var/dhcp.leases" ] && [ -z "$tmp_name" ] && local tmp_name=`cat /var/dhcp.leases|grep -w ${1}|awk '{print $4}'|grep -v "^$"|sort -u` + [ -f "/var/dhcp.leases" ] && [ -z "$tmp_name" ] && local tmp_name=`cat /var/dhcp.leases|grep -w ${1}|awk '{print $4}'|grep -v "^$"|sort -u|head -n1` ( ! echo "$tmp_name"|grep -q -w "unknown\|*" ) && [ ! -z "$tmp_name" ] && echo "$tmp_name" && return || unset tmp_name # 为unknown时重新读取 [ -z "$dhcp_config" ] && dhcp_config=`uci show dhcp|grep "ip\|mac\|name"` for dhcp_config_str in "host" "domain"; do @@ -154,15 +155,15 @@ function getname(){ # 查询设备接口 function getinterface(){ - [ -f "${dir}ipAddress" ] && local ip_interface=`cat ${dir}ipAddress|grep -w ${1}|awk '{print $5}'|grep -v "^$"|sort -u` - [ -f "${dir}tmp_downlist" ] && [ -z "$ip_interface" ] && local ip_interface=`cat ${dir}tmp_downlist|grep -w ${1}|awk '{print $5}'|grep -v "^$"|sort -u` + [ -f "${dir}ipAddress" ] && local ip_interface=`cat ${dir}ipAddress|grep -w ${1}|awk '{print $5}'|grep -v "^$"|sort -u|head -n1` + [ -f "${dir}tmp_downlist" ] && [ -z "$ip_interface" ] && local ip_interface=`cat ${dir}tmp_downlist|grep -w ${1}|awk '{print $5}'|grep -v "^$"|sort -u|head -n1` if [ -z "$ip_interface" ] && [ ! -z "$wlan_interface" ]; then for interface in $wlan_interface; do local ip_interface=`iw dev $interface station dump 2>/dev/null|grep Station|grep -i -w ${1}|sed -nr 's#^.*on (.*))#\1#gp'` >/dev/null 2>&1 [ ! -z "$ip_interface" ] && echo "$ip_interface" && return done fi - [ -z "$ip_interface" ] && local ip_interface=`cat /proc/net/arp|grep "0x2\|0x6"|grep -i -w ${1}|awk '{print $6}'|grep -v "^$"|sort -u` + [ -z "$ip_interface" ] && local ip_interface=`cat /proc/net/arp|grep "0x2\|0x6"|grep -i -w ${1}|awk '{print $6}'|grep -v "^$"|sort -u|head -n1` echo "$ip_interface" } @@ -171,7 +172,7 @@ function getping(){ [ "$iw_version" ] && local wlan_online=`iw dev ${ip_interface} station dump|grep -i -w ${ip_mac}|grep Station` >/dev/null 2>&1 [ "$wlan_online" ] && return 0 for i in `seq 1 ${3}`; do - ( ! echo "$ip_ms"|grep -q "ms" ) && local ip_ms=$( arping -I `cat /proc/net/arp|grep -w ${1}|awk '{print $6}'|grep -v "^$"|sort -u` -c 20 -f -w ${2} $1 ) 2>/dev/null + ( ! echo "$ip_ms"|grep -q "ms" ) && local ip_ms=$( arping -I `cat /proc/net/arp|grep -w ${1}|awk '{print $6}'|grep -v "^$"|sort -u|head -n1` -c 20 -f -w ${2} $1 ) 2>/dev/null ( ! echo "$ip_ms"|grep -q "ms" ) && local ip_ms=`ping -c 5 -w ${2} ${1}|grep -v '100% packet loss'` 2>/dev/null ( ! echo "$ip_ms"|grep -q "ms" ) && sleep 1 done @@ -202,10 +203,11 @@ function usage(){ [ ! -z "$wr_version" ] && ( version_ge "${wr_version}" "1.2.0" ) && wrtbwmon -f ${dir}usage.db 2>/dev/null && return [ ! -z "$wr_version" ] && ( version_le "${wr_version}" "1.0.0" ) || [ -z "$wr_version" ] && wrtbwmon update ${dir}usage.db 2>/dev/null && return elif [ $1 == "get" ] ;then - [ ! -f "${dir}usage.db" ] && echo `bytes_for_humans 0` && return + [ ! -f "${dir}usage.db" ] && [ ! "$3" ] && echo `bytes_for_humans 0` && return + [ ! -f "${dir}usage.db" ] && [ "$3" ] && echo 0 && return [ -z "$total_n" ] && total_n=`cat ${dir}usage.db|head -n1|grep "total"|sed 's/,/\n/g'|awk '/total/{print NR}'` 2>/dev/null [ -z "$total_n" ] && total_n="6" - [ "$2" ] && local tmptotal=`cat ${dir}usage.db|sed 's/,,,/,0,0,/g'|sed 's/,,/,0,/g'|sed 's/,/ /g'|grep -i -w ${2}|awk "{print "'$'$total_n"}"|grep -v "^$"|sort -u` 2>/dev/null + [ "$2" ] && local tmptotal=`cat ${dir}usage.db|sed 's/,,,/,0,0,/g'|sed 's/,,/,0,/g'|sed 's/,/ /g'|grep -i -w ${2}|awk "{print "'$'$total_n"}"|grep -v "^$"|sort -u|head -n1` 2>/dev/null [ -z "$tmptotal" ] && local tmptotal="0" [ ! "$3" ] && echo `bytes_for_humans ${tmptotal}` || echo "$tmptotal" elif [ $1 == "down" ] ;then @@ -238,15 +240,15 @@ function get_client_usage(){ if [ "$((`date +%s`-$get_client_usage_time))" -ge "60" ]; then > ${dir}client_usage_aliases for mac in $MACLIST; do - ( ! cat ${dir}ipAddress|grep -q -i -w $mac|grep -v "^$"|sort -u ) && continue + ( ! cat ${dir}ipAddress|grep -q -i -w $mac|grep -v "^$"|sort -u|head -n1 ) && continue echo "$mac" `usage get ${mac} bytes` >> ${dir}client_usage_aliases - [ -f "${dir}old_client_usage_aliases" ] && get_client_usage_bytes=`cat ${dir}old_client_usage_aliases|grep -i -w $mac|awk '{print $2}'|grep -v "^$"|sort -u` || continue + [ -f "${dir}old_client_usage_aliases" ] && get_client_usage_bytes=`cat ${dir}old_client_usage_aliases|grep -i -w $mac|awk '{print $2}'|grep -v "^$"|sort -u|head -n1` || continue [ -z "$get_client_usage_bytes" ] && get_client_usage_bytes="0" if [ "$((`usage get ${mac} bytes`-$get_client_usage_bytes))" -ge "$client_usage_max" ]; then - local ip=`cat ${dir}ipAddress|grep -i -w $mac|awk '{print $1}'|grep -v "^$"|sort -u` + local ip=`cat ${dir}ipAddress|grep -i -w $mac|awk '{print $1}'|grep -v "^$"|sort -u|head -n1` local ip_name=`getname ${ip} ${mac}` local tmp_usage=$(bytes_for_humans $(expr `usage get ${mac} bytes` - ${get_client_usage_bytes})) - local time_up=`cat ${dir}ipAddress|grep -w ${ip}|awk '{print $4}'|grep -v "^$"|sort -u` + local time_up=`cat ${dir}ipAddress|grep -w ${ip}|awk '{print $4}'|grep -v "^$"|sort -u|head -n1` local ip_total=`usage get $mac` && [ ! -z "$ip_total" ] && local ip_total="${markdown_linefeed}${markdown_tab}总计流量: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_total}" local time1=`date +%s` local time1=$(time_for_humans `expr ${time1} - ${time_up}`) @@ -348,22 +350,24 @@ function serverchan_cron(){ /etc/init.d/cron stop /etc/init.d/cron start } - if [ -z "$send_mode" ] || [ -z "$serverchan_enable" ]; then - del_cron + del_cron + if [ -z "$serverchan_enable" ] || [ -z "$send_mode" -a -z "$reset_regularly" ]; then re_cron return fi + + # 重置流量 + if [ ! -z "$reset_regularly" ] && [ "$reset_regularly" -eq "1" ]; then + crontab -l > conf && echo -e "0 0 * * * rm /tmp/serverchan/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/serverchan/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 - ( echo `crontab -l`|grep "serverchan"|grep -q " $regular_time$regular_time_2$regular_time_3 " ) && return - del_cron crontab -l > conf && echo -e "0 $regular_time$regular_time_2$regular_time_3 * * * /usr/bin/serverchan/serverchan send &" >> conf && crontab conf && rm -f conf >/dev/null 2>&1 # 间隔发送 elif [ ! -z "$interval_time" ]; then - ( echo `crontab -l`|grep "serverchan"|grep -q " */$interval_time " ) && return - del_cron crontab -l > conf && echo -e "0 */$interval_time * * * /usr/bin/serverchan/serverchan send &" >> conf && crontab conf && rm -f conf >/dev/null 2>&1 fi re_cron @@ -375,8 +379,9 @@ function serverchan_disturb(){ if [ `date +%H` -ge $endtime -a $starttime -lt $endtime ] || [ `date +%H` -lt $starttime -a $starttime -lt $endtime ] || [ `date +%H` -lt $starttime -a `date +%H` -ge $endtime -a $starttime -gt $endtime ]; then unset sheep_starttime rm -f ${dir}sheep_usage ${dir}old_sheep_usage 2>/dev/null - disturb_text="【微信推送】" - [ ! -z "$send_tg" ] && [ "$send_tg" -eq "1" ] && disturb_text="【Telegram推送】" + [ ! -z "$sckey" ] && [ -z "$sckey" ] && disturb_text="【微信推送】" + [ ! -z "$tg_token" ] && [ -z "$sckey" ] && disturb_text="【Telegram推送】" + [ ! -z "$tg_token" ] && [ ! -z "$sckey" ] && disturb_text="【微信/TG推送】" return 0 else [ -z "$sheep_starttime" ] && echo "`date "+%Y-%m-%d %H:%M:%S"` 【免打扰】夜深了,该休息了" >> ${logfile} && sheep_starttime=`date +%s` @@ -402,6 +407,7 @@ function LockFile(){ LockFile lock fi [ $1 = "unlock" ] && rm -f ${dir}serverchan.lock >/dev/null 2>&1 + return } # 检测黑白名单 @@ -411,6 +417,7 @@ function blackwhitelist(){ [ ! -z "$serverchan_whitelist" ] && ( ! echo "$serverchan_whitelist"|grep -q -i -w $1) && return [ ! -z "$serverchan_blacklist" ] && ( echo "$serverchan_blacklist"|grep -q -i -w $1) && return [ ! -z "$serverchan_interface" ] && ( echo `getinterface ${1}`|grep -q -i -w $serverchan_interface ) && return + return 1 } function get_client(){ @@ -454,7 +461,7 @@ function geterrdevicealiases(){ [ -f ${dir}ipAddress ] && local logrow=$(grep -c "" ${dir}ipAddress) || local logrow="0";[ $logrow -eq "0" ] && return local MACLIST=`cat ${dir}ipAddress|awk '{print $2}'|grep -v "^$"|sort -u` for mac in $MACLIST; do - [ -z "$err_mac" ] && [ ! -z "$mac" ] && local err_mac=`echo "$err_device_aliases"|grep -i $mac|grep -v "^$"|sort -u` + [ -z "$err_mac" ] && [ ! -z "$mac" ] && local err_mac=`echo "$err_device_aliases"|grep -i $mac|grep -v "^$"|sort -u|head -n1` done # 进入免打扰时间已经超过一小时 if [ ! -z "$sheep_starttime" ] && [ "$((`date +%s`-$sheep_starttime))" -ge "3600" ]; then @@ -616,8 +623,8 @@ function ip_changes(){ [ ! -z "$serverchan_ipv6" ] && [ "$serverchan_ipv6" -eq "2" ] && local IPv6=`getip hostipv6` if [ -f ${dir}ip ]; then - local last_IPv4=$(cat "${dir}ip"|grep IPv4|awk '{print $2}'|grep -v "^$"|sort -u) - local last_IPv6=$(cat "${dir}ip"|grep IPv6|awk '{print $2}'|grep -v "^$"|sort -u) + local last_IPv4=$(cat "${dir}ip"|grep IPv4|awk '{print $2}'|grep -v "^$"|sort -u|head -n1) + local last_IPv6=$(cat "${dir}ip"|grep IPv6|awk '{print $2}'|grep -v "^$"|sort -u|head -n1) if [ ! -z "$serverchan_ipv4" ] && [ "$serverchan_ipv4" -ne "0" ] && [ ! -z "$IPv4" ] && ( ! echo ${IPv4}|grep -w -q ${last_IPv4} ); then echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}当前IP:${IPv4}" >> ${logfile} echo IPv4 $IPv4 > ${dir}ip && echo -e IPv6 $last_IPv6 >> ${dir}ip @@ -667,16 +674,18 @@ function up(){ getping ${1} ${up_timeout} "1";local ping_online=$? if [ "$ping_online" -eq "0" ]; then LockFile lock - [ -f "${dir}tmp_downlist" ] && local tmp_downip=`cat ${dir}tmp_downlist|grep -w ${1}|grep -v "^$"|sort -u` + [ -f "${dir}tmp_downlist" ] && local tmp_downip=`cat ${dir}tmp_downlist|grep -w ${1}|grep -v "^$"|sort -u|head -n1` if [ ! -z "$tmp_downip" ]; then - cat ${dir}tmp_downlist|grep -w ${1}|grep -v "^$"|sort -u >> ${dir}ipAddress + cat ${dir}tmp_downlist|grep -w ${1}|grep -v "^$"|sort -u|head -n1 >> ${dir}ipAddress sed -i "/^${1} /d" ${dir}tmp_downlist else usage down $1 local ip_name=`getname ${1} ${ip_mac}` blackwhitelist ${ip_mac};local ip_blackwhite=$? echo "$1 ${ip_mac} ${ip_name} `date +%s` ${ip_interface}" >> ${dir}ipAddress - [ -f "${dir}send_enable.lock" ] || [ -z "$serverchan_up" ] || [ "$serverchan_up" -ne "1" ] || [ -z "$ip_blackwhite" ] || [ "$ip_blackwhite" -ne 0 ] && LockFile unlock && return + [ -f "${dir}send_enable.lock" ] || [ -z "$serverchan_up" ] || [ -z "$ip_blackwhite" ] && LockFile unlock && return + [ ! -z "$serverchan_up" ] && [ "$serverchan_up" -ne "1" ] && LockFile unlock && return + [ ! -z "$ip_blackwhite" ] && [ "$ip_blackwhite" -ne "0" ] && LockFile unlock && return [ -f "${dir}title" ] && local title=`cat ${dir}title` [ -f "${dir}content" ] && local content=`cat ${dir}content` if [ -z "$title" ]; then @@ -690,7 +699,7 @@ function up(){ local content="${markdown_splitline}#### 新设备连接${markdown_linefeed}${markdown_tab}客户端名:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_name}${markdown_linefeed}${markdown_tab}客户端IP: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${1}${markdown_linefeed}${markdown_tab}客户端MAC:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_mac}${markdown_linefeed}${markdown_tab}网络接口:${markdown_space}${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_interface}" fi echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}新设备 ${ip_name} ${1} 连接了">> ${logfile} - [ ! -z "$serverchan_blacklist" ] && local title="你偷偷关注的设备上线了" + #[ ! -z "$serverchan_blacklist" ] && local title="你偷偷关注的设备上线了" [ ! -z "$title" ] && echo "$title" >${dir}title [ ! -z "$content" ] && echo -n "$content" >>${dir}content fi @@ -706,14 +715,14 @@ function down(){ getping ${1} ${down_timeout} ${timeout_retry_count};local ping_online=$? if [ "$ping_online" -eq "1" ]; then LockFile lock - [ ! -f "${dir}send_enable.lock" ] && cat ${dir}ipAddress|grep -w ${1}|grep -v "^$"|sort -u >> ${dir}tmp_downlist + [ ! -f "${dir}send_enable.lock" ] && cat ${dir}ipAddress|grep -w ${1}|grep -v "^$"|sort -u|head -n1 >> ${dir}tmp_downlist sed -i "/^${1} /d" ${dir}ipAddress LockFile unlock else - local tmp_name=`cat ${dir}ipAddress|grep -w ${1}|awk '{print $3}'|grep -v "^$"|sort -u` + local tmp_name=`cat ${dir}ipAddress|grep -w ${1}|awk '{print $3}'|grep -v "^$"|sort -u|head -n1` if [ "$ip_name" != "$tmp_name" ]; then LockFile lock - local tmp_str=$(echo "$1 ${ip_mac} ${ip_name} `cat ${dir}ipAddress|grep -w ${1}|awk '{print $4}'|grep -v "^$"|sort -u` ${ip_interface}") + local tmp_str=$(echo "$1 ${ip_mac} ${ip_name} `cat ${dir}ipAddress|grep -w ${1}|awk '{print $4}'|grep -v "^$"|sort -u|head -n1` ${ip_interface}") sed -i "/^${1} /d" ${dir}ipAddress echo "$tmp_str" >> ${dir}ipAddress LockFile unlock @@ -728,9 +737,11 @@ function down_send(){ for ip in $IPLIST; do local ip_mac=`getmac ${ip}` blackwhitelist ${ip_mac};local ip_blackwhite=$? - [ -z "$serverchan_down" ] || [ "$serverchan_down" -ne "1" ] || [ -z "$ip_blackwhite" ] || [ "$ip_blackwhite" -ne 0 ] && continue + [ -z "$serverchan_down" ] || [ -z "$ip_blackwhite" ] && continue + [ ! -z "$serverchan_down" ] && [ "$serverchan_down" -ne "1" ] && continue + [ ! -z "$ip_blackwhite" ] && [ "$ip_blackwhite" -ne "0" ] && continue local ip_name=`getname ${ip} ${ip_mac}` - local time_up=`cat ${dir}tmp_downlist|grep -w ${ip}|awk '{print $4}'|grep -v "^$"|sort -u` + local time_up=`cat ${dir}tmp_downlist|grep -w ${ip}|awk '{print $4}'|grep -v "^$"|sort -u|head -n1` local ip_total=`usage get $ip_mac` && [ ! -z "$ip_total" ] && local ip_total="${markdown_linefeed}${markdown_tab}总计流量: ${markdown_space}${markdown_space}${markdown_space}${markdown_space}${ip_total}" local time1=`date +%s` local time1=$(time_for_humans `expr ${time1} - ${time_up}`) @@ -860,7 +871,7 @@ function send(){ if [ ! -z "$router_status" ] && [ "$router_status" -eq "1" ]; then local systemload=`cat /proc/loadavg|awk '{print $1" "$2" "$3}'` local cpuload=`getcpu` - local ramload=`free -m|sed -n '2p'|awk '{print""($3/$2)*100"%"}'` + local ramload=`free -m|sed -n '2p'|awk '{printf "%.2f%%\n",($3/$2)*100}'` local systemstatustime=`cat /proc/uptime|awk -F. '{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;run_minute=($1 % 3600)/60;run_second=$1 % 60;printf("运行时间:%d天%d时%d分%d秒",run_days,run_hour,run_minute,run_second)}'`;unset run_days run_hour run_minute run_second local send_content="${send_content}${markdown_splitline}#### 系统运行状态" local send_content="${send_content}${markdown_linefeed}${markdown_tab}平均负载:${systemload}" @@ -891,7 +902,7 @@ function send(){ local IPLIST=`cat ${dir}ipAddress 2>/dev/null|awk '{print $1}'` [ -z "$IPLIST" ] && local send_content="${send_content}${markdown_splitline}#### 当前无在线设备" || local send_content="${send_content}${markdown_splitline}#### 在线设备" for ip in $IPLIST; do - local time_up=`cat ${dir}ipAddress|grep -w ${ip}|awk '{print $4}'|grep -v "^$"|sort -u` + local time_up=`cat ${dir}ipAddress|grep -w ${ip}|awk '{print $4}'|grep -v "^$"|sort -u|head -n1` local time1=`date +%s` local time1=$(time_for_humans `expr ${time1} - ${time_up}`) local ip_mac=`getmac ${ip}` @@ -906,9 +917,8 @@ function send(){ local send_title=`echo "$send_title"|sed $'s/\ /%20/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 "$send_content" ] && local send_content="${markdown_splitline}#### 我遇到了一个难题${markdown_linefeed}${markdown_tab}定时发送选项错误,你没有选择需要发送的项目,该怎么办呢${markdown_splitline}" local nowtime=`date "+%Y-%m-%d %H:%M:%S"` - [ "$send_disturb" -eq "0" ] && [ -z "$send_tg" ] && curl -s "http://sc.ftqq.com/${sckey}.send?text=${send_title}" -d "&desp=${nowtime}${markdown_linefeed}${send_content}" >/dev/null 2>&1 - [ "$send_disturb" -eq "0" ] && [ ! -z "$send_tg" ] && [ "$send_tg" -eq "2" ] && curl -s "http://sctapi.ftqq.com/${sctkey}.send?text=${send_title}" -d "desp=${nowtime}${markdown_linefeed}${send_content}" >/dev/null 2>&1 - [ "$send_disturb" -eq "0" ] && [ ! -z "$send_tg" ] && [ "$send_tg" -eq "1" ] && curl -d "text=${send_title}${markdown_linefeed}${nowtime}${markdown_linefeed}${send_content}" -X POST "${tgtoken}" >/dev/null 2>&1 + [ "$send_disturb" -eq "0" ] && [ ! -z "$sckey" ] && curl -s "${sckey_url}?text=${send_title}" -d "&desp=${nowtime}${markdown_linefeed}${send_content}" >/dev/null 2>&1 + [ "$send_disturb" -eq "0" ] && [ ! -z "$tg_token" ] && curl -s "https://api.telegram.org/bot${tg_token}/sendMessage" -d "chat_id=${chat_id}&text=${send_title}${nowtime}${send_content}" >/dev/null 2>&1 deltemp echo "`date "+%Y-%m-%d %H:%M:%S"` ${disturb_text}定时推送任务完成" >> ${logfile} } @@ -982,9 +992,9 @@ while [ "$serverchan_enable" -eq "1" ]; do nowtime=`date "+%Y-%m-%d %H:%M:%S"` [ ! -z "$device_name" ] && title="【$device_name】$title" title=`echo "$title"|sed $'s/\ /%20/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'` - [ "$disturb" -eq "0" ] && [ -z "$send_tg" ] && curl -s "http://sc.ftqq.com/${sckey}.send?text=${title}" -d "desp=${nowtime}${markdown_linefeed}${content}" >/dev/null 2>&1 - [ "$disturb" -eq "0" ] && [ ! -z "$send_tg" ] && [ "$send_tg" -eq "2" ] && curl -s "http://sctapi.ftqq.com/${sctkey}.send?text=${title}" -d "desp=${nowtime}${markdown_linefeed}${content}" >/dev/null 2>&1 - [ "$disturb" -eq "0" ] && [ ! -z "$send_tg" ] && [ "$send_tg" -eq "1" ] && curl -d "text=${title}${markdown_linefeed}${nowtime}${markdown_linefeed}${content}" -X POST "${tgtoken}" >/dev/null 2>&1 + [ "$disturb" -eq "0" ] && [ ! -z "$sckey" ] && curl -s "${sckey_url}?text=${title}" -d "desp=${nowtime}${markdown_linefeed}${content}" >/dev/null 2>&1 + [ "$disturb" -eq "0" ] && [ ! -z "$tg_token" ] && curl -s "https://api.telegram.org/bot${tg_token}/sendMessage" -d "chat_id=${chat_id}&text=${title}${nowtime}${content}" >/dev/null 2>&1 + fi while [ -f "${dir}send_enable.lock" ]; do