From bc20fbc37ec1234e45f8e6816bba3cbf3ec0c47e Mon Sep 17 00:00:00 2001 From: tty228 Date: Sun, 7 Jul 2024 22:16:13 +0800 Subject: [PATCH] v3.5.3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 修复并发时文件锁失效的 bug 2. 修复带参数启动时,线程限制没有生效的 bug 3. 修复 MAC 信息表下载不成功,且日志无提示的问题 4. 优化和修复主机名等信息的获取未按预期工作的问题 5. 修复部分版本的系统,SSH登录信息获取不正确的问题 6. 局域网扫描应快速扫描更新 ARP 列表,而不是加入 IPLIST 列表,使用普通的 ping 超时检测 7. 减少一部分调试模式中的信息输出,方便排查错误 8. 稍稍调整了推送内容中,设备列表的对齐方式 9. 增加通过 NetBIOS 查询主机名和 MAC 的方式,此功能需要安装 samba*-server 或 samba*-client 10. 因 OpenWrt 版本的 jq 处理速度已经有了显著提升,改为使用 json 存储 IP 和设备信息 11. 清理多余代码和一些小的修改 --- Makefile | 2 +- root/etc/uci-defaults/luci-wechatpush | 2 - root/usr/libexec/wechatpush-call | 12 +-- root/usr/share/wechatpush/wechatpush | 145 ++++++++++++++------------ 4 files changed, 88 insertions(+), 73 deletions(-) diff --git a/Makefile b/Makefile index 467414da..46b5a50b 100755 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=luci-app-wechatpush -PKG_VERSION:=3.5.1 +PKG_VERSION:=3.5.3 PKG_RELEASE:= PKG_MAINTAINER:=tty228 diff --git a/root/etc/uci-defaults/luci-wechatpush b/root/etc/uci-defaults/luci-wechatpush index 1db8f17b..28040d7a 100755 --- a/root/etc/uci-defaults/luci-wechatpush +++ b/root/etc/uci-defaults/luci-wechatpush @@ -1,5 +1,3 @@ #!/bin/sh -dir="/tmp/wechatpush/" && mkdir -p ${dir} && mkdir -p ${dir}/client -rm -f ${dir}client/* >/dev/null 2>&1 rm -rf /tmp/luci-* >/dev/null 2>&1 diff --git a/root/usr/libexec/wechatpush-call b/root/usr/libexec/wechatpush-call index 1e56c7b7..a85bbd3b 100755 --- a/root/usr/libexec/wechatpush-call +++ b/root/usr/libexec/wechatpush-call @@ -1,8 +1,8 @@ #!/bin/sh logfile="/tmp/wechatpush/wechatpush.log" -dir="/tmp/wechatpush/" && mkdir -p "${dir}" -oui_base="${dir}oui_base.txt" +dir="/tmp/wechatpush" && mkdir -p "${dir}" +oui_base="${dir}/oui_base.txt" oui_data=$(uci get wechatpush.config.oui_data 2>/dev/null) oui_url="https://standards-oui.ieee.org/oui/oui.txt" @@ -18,16 +18,16 @@ file_date() { down_oui() { if [ -n "${oui_data}" ] && [ "${oui_data}" -ne "3" ]; then echo "$(date "+%Y-%m-%d %H:%M:%S") 【info】开始后台更新 MAC 信息文件" >>"${logfile}" - curl -fsSL --connect-timeout 15 --max-time 30 -o "${dir}oui.txt" "${oui_url}" >/dev/null 2>&1 + curl -fsSL --connect-timeout 15 --max-time 30 -o "${dir}/oui.txt" "${oui_url}" >/dev/null 2>&1 local RETVAL=$? [ ${RETVAL} -ne 0 ] && echo "$(date "+%Y-%m-%d %H:%M:%S") 【!!!】设备 MAC 信息文件下载失败,返回码为 ${RETVAL}" >>"${logfile}" && return 1 echo "$(date "+%Y-%m-%d %H:%M:%S") 【info】设备 MAC 信息文件下载成功,处理中" >>"${logfile}" if [ "${oui_data}" -eq "1" ]; then - grep -i -E ".*(base 16).*(apple|aruba|asus|autelan|belkin|bhu|buffalo|cctf|cisco|comba|datang|dell|dlink|dowell|ericsson|fast|feixun|fiberhome|fujitsu|grentech|h3c|hisense|hiwifi|honghai|honghao|hp|htc|huawei|intel|jinli|jse|lenovo|lg|liteon|malata|meizu|mercury|meru|moto|netcore|netgear|nokia|omron|oneplus|oppo|philips|router_unkown|samsung|shanzhai|sony|start_net|sunyuanda|tcl|tenda|texas|tianyu|tp-link|ubq|undefine|VMware|utstarcom|volans|xerox|xiaomi|zdc|zhongxing|smartisan).*" "${dir}oui.txt" | sed -E 's/( Electronic| Technology| Intelligence| TECHNOLOGIES| Device| Systems| TELECOMMUNICATIONS| Instruments| Electronics| Corporation| Telecommunication| Communications| Electrical| Technology| Corporate| Intelligent| Interactive| MOBILE| Solutions| Mobility| Meraki| ELECTRO| VISUAL| Limited| International| Information| LLC|Co$|Co\.|Ltd\.$|Inc\.|B\.V\.$|AB$|,).*$/ /I; s/[[:space:]]*$//; s/ +$//' >"${oui_base}" + grep -i -E ".*(base 16).*(apple|aruba|asus|autelan|belkin|bhu|buffalo|cctf|cisco|comba|datang|dell|dlink|dowell|ericsson|fast|feixun|fiberhome|fujitsu|grentech|h3c|hisense|hiwifi|honghai|honghao|hp|htc|huawei|intel|jinli|jse|lenovo|lg|liteon|malata|meizu|mercury|meru|moto|netcore|netgear|nokia|omron|oneplus|oppo|philips|router_unkown|samsung|shanzhai|sony|start_net|sunyuanda|tcl|tenda|texas|tianyu|tp-link|ubq|undefine|VMware|utstarcom|volans|xerox|xiaomi|zdc|zhongxing|smartisan).*" "${dir}/oui.txt" | sed -E 's/( Electronic| Technology| Intelligence| TECHNOLOGIES| Device| Systems| TELECOMMUNICATIONS| Instruments| Electronics| Corporation| Telecommunication| Communications| Electrical| Technology| Corporate| Intelligent| Interactive| MOBILE| Solutions| Mobility| Meraki| ELECTRO| VISUAL| Limited| International| Information| LLC|Co$|Co\.|Ltd\.$|Inc\.|B\.V\.$|AB$|,).*$/ /I; s/[[:space:]]*$//; s/ +$//' >"${oui_base}" elif [ "${oui_data}" -eq "2" ]; then - grep -i "(base 16)" "${dir}oui.txt" | sed -E 's/( Electronic| Technology| Intelligence| TECHNOLOGIES| Device| Systems| TELECOMMUNICATIONS| Instruments| Electronics| Corporation| Telecommunication| Communications| Electrical| Technology| Corporate| Intelligent| Interactive| MOBILE| Solutions| Mobility| Meraki| ELECTRO| VISUAL| Limited| International| Information| LLC|Co$|Co\.|Ltd\.$|Inc\.|B\.V\.$|AB$|,).*$/ /I; s/[[:space:]]*$//; s/ +$//' >"${oui_base}" + grep -i "(base 16)" "${dir}/oui.txt" | sed -E 's/( Electronic| Technology| Intelligence| TECHNOLOGIES| Device| Systems| TELECOMMUNICATIONS| Instruments| Electronics| Corporation| Telecommunication| Communications| Electrical| Technology| Corporate| Intelligent| Interactive| MOBILE| Solutions| Mobility| Meraki| ELECTRO| VISUAL| Limited| International| Information| LLC|Co$|Co\.|Ltd\.$|Inc\.|B\.V\.$|AB$|,).*$/ /I; s/[[:space:]]*$//; s/ +$//' >"${oui_base}" fi - rm -f "${dir}oui.txt" >/dev/null 2>&1 + rm -f "${dir}/oui.txt" >/dev/null 2>&1 echo "$(date "+%Y-%m-%d %H:%M:%S") 【info】设备 MAC 信息文件处理完成" >>"${logfile}" fi } diff --git a/root/usr/share/wechatpush/wechatpush b/root/usr/share/wechatpush/wechatpush index 16fd116a..c0dc2238 100755 --- a/root/usr/share/wechatpush/wechatpush +++ b/root/usr/share/wechatpush/wechatpush @@ -117,7 +117,7 @@ log_change() { # 初始化 init() { enable_detection - [ -f ${logfile} ] && local logrow=$(grep -c "" ${logfile}) || local logrow="0" + [ -f "$logfile" ] && local logrow=$(grep -c "" "$logfile") || local logrow="0" [ "$logrow" -ne 0 ] && echo "---------------------------------------------------------------------------------------" >>${logfile} log_change "【初始化】start running..." if [ -f "/usr/share/wechatpush/errlog" ]; then @@ -147,7 +147,6 @@ init() { [ -n "$tmp_ip" ] && add_ip_white "$tmp_ip" "0" done <<<"$tmp_ip_list" set_ip_black - rm -f "${dir}/scan_info" >/dev/null 2>&1 return 0 } @@ -155,7 +154,7 @@ init() { deltemp() { unset title content gateway_iplist rm -f "${dir}/title" "${dir}/content" "${dir}/send_enable.lock" "${dir}/send.lock" "${tempjsonpath}" "${dir}/cookies.txt" >/dev/null 2>&1 - LockFile unlock + silent_run LockFile unlock [ -f "$logfile" ] && local logrow=$(grep -c "" "$logfile") || local logrow="0" [ "$logrow" -gt 500 ] && sed -i '1,100d' "$logfile" && log_change "【清理】日志超出上限,删除前 100 条" # 检查 JSON 文件格式是否有效 @@ -173,6 +172,7 @@ enable_detection() { get_config enable [ -z "$enable" ] || [ "$enable" -eq "0" ] && exit || sleep 1 done + unset i } # 获取 ip @@ -303,7 +303,7 @@ getmac() { # 某些路由器中继模式,会进行 MAC 克隆,如果是重复值,全都重置为 unknown local mac_count=$(jq -r --arg mac "$tmp_mac" '.devices[] | select(.mac == $mac) | .mac' "$devices_json" | wc -l) - [ "$mac_count" -gt 1 ] && jq -c --arg mac "$tmp_mac" --arg new_mac "unknown" '.devices |= map(if .mac == $mac then .mac = $new_mac else . end)' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" + [ "$mac_count" -gt 1 ] && jq --arg mac "$tmp_mac" --arg new_mac "unknown" '.devices |= map(if .mac == $mac then .mac = $new_mac else . end)' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" # 已保存的 MAC tmp_mac=$(jq -r --arg ip "$ip" '.devices[] | select(.ip == $ip) | .mac' "$devices_json") @@ -389,10 +389,9 @@ check_http_access() { return 1 } -# 获取文件最后修改时间和行数 +# 获取文件最后修改时间(距离现在过去了多少秒) file_date() { local file_dir="$1" - local filerow=$(grep -c "" "${file_dir}" 2>/dev/null || echo "0") local datetime=$(date -r "${file_dir}" +%s 2>/dev/null || echo "0") expr $(date +%s) - ${datetime} } @@ -424,35 +423,47 @@ getgateway() { # 扫描范围内 IP scanlocalip() { [ -z "$scan_ip_range" ] && return - last_scan_ip_time=$(file_date "${dir}/scan_info") - - if [ ! -f "${dir}/scan_info" ]; then - start_ip=$(echo "$scan_ip_range" | cut -d "-" -f 1) - end_ip=$(echo "$scan_ip_range" | cut -d "-" -f 2) - - i=$(echo "$start_ip" | awk -F '.' '{print $NF}') - end_i=$(echo "$end_ip" | awk -F '.' '{print $NF}') - while [ "$i" -le "$end_i" ]; do - if ping -c 1 "${start_ip%.*}.$i" >/dev/null 2>&1 & then - echo "${start_ip%.*}.$i" >>"${dir}/scan_info" - fi - i=$((i + 1)) + [ -z "$last_scan_ip_time" ] && last_scan_ip_time=0 + + local current_time=$(date +%s) + local elapsed_time=$((current_time - last_scan_ip_time)) + + # 判断是否需要重新扫描 + if [ "$elapsed_time" -ge "$device_info_helper_sleeptime" ]; then + local start_ip=$(echo "$scan_ip_range" | cut -d "-" -f 1) + local end_ip=$(echo "$scan_ip_range" | cut -d "-" -f 2) + + local start_num=$(echo "$start_ip" | awk -F '.' '{print $NF}') + local end_num=$(echo "$end_ip" | awk -F '.' '{print $NF}') + local base_ip=$(echo "$start_ip" | awk -F '.' '{print $1"."$2"."$3}') + + for i in $(seq "$start_num" "$end_num"); do + # 获取一个令牌 + read -u 5 + { + ping -c 1 "${base_ip}.${i}" >/dev/null 2>&1 + # 释放令牌 + echo >&5 + } & done - fi + wait - [ "$last_scan_ip_time" -ge "$device_info_helper_sleeptime" ] && cat "${dir}/scan_info" + last_scan_ip_time="$current_time" + fi + unset i } # 查询设备接口 getinterface() { - local mac=${1} + local ip=${1} + local mac=${2} local interface [ -z "$mac" ] && return [ "$mac" == "unknown" ] && return # 从已保存的地址中获取接口 - interface=$(jq -r --arg mac "$mac" '.devices[] | select(.mac == $mac) | .interface' "$devices_json") + interface=$(jq -r --arg ip "$ip" --arg mac "$mac" '.devices[] | select(.ip == $ip and .mac == $mac) | .interface' "$devices_json") process_and_check "$interface" && return # 如果定义了 WLAN 接口列表,则查询每个接口 @@ -475,17 +486,18 @@ getping() { local timeout="$3" local retry_count="$4" - local interface=$(getinterface "$mac") + local interface=$(getinterface "ip" "$mac") [ "$iw_version" ] && [ "$interface" ] && wlan_online=$(iw dev ${interface} station dump 2>/dev/null | grep -i -w "$mac" | grep Station) >/dev/null 2>&1 [ "$wlan_online" ] && return 0 interface=$(cat /proc/net/arp | grep -w "$ip" | awk '{print $6}' | grep -v "^$" | sort -u | head -n1) for i in $(seq 1 "$retry_count"); do - # arping 应使用 br-lan,有 WiFi 的设备接口为 wlan*,重新获取 + # arping 应使用 br-lan 等接口,有 WiFi 的设备接口为 wlan*,重新获取 [ -n "$interface" ] && ip_ms=$(arping -I ${interface} -c 20 -f -w "$timeout" "$ip") 2>/dev/null (! echo "$ip_ms" | grep -q "ms") && ip_ms=$(ping -c 5 -w "$timeout" "$ip" | grep -v '100% packet loss') 2>/dev/null (! echo "$ip_ms" | grep -q "ms") && sleep 1 done + unset i echo "$ip_ms" | grep -q "ms" } @@ -675,6 +687,8 @@ cut_str() { max_column=$i fi done + unset i + local text=$(echo -n "$1" | cut -d ' ' -f $max_column) if [ $(length_str "$text") -le "$2" ]; then echo "$text" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' @@ -704,15 +718,17 @@ rand() { # 在线设备列表 first() { - local IPLIST=$(jq -r '.devices[] | select(.status == "online") | .ip' "$devices_json" | sort -u) + local IPLIST=$(jq -r '.devices[] | select(.status == "online" or .status == "unknown") | .ip' "$devices_json" | sort -u) getgateway - scan_iplist=$(scanlocalip) + silent_run scanlocalip for ip in $IPLIST; do [ -n "$passive_mode" ] && [ "$passive_mode" -eq "1" ] && break + # 获取一个令牌 read -u 5 { down "$ip" - echo "" >&5 + # 释放令牌 + echo >&5 } & done wait @@ -721,13 +737,15 @@ first() { [ -n "$IP_INFO" ] && local SUBNET=$(echo $IP_INFO | cut -d'/' -f1 | cut -d'.' -f1-3) [ -n "$SUBNET" ] && local IPLIST=$(cat /proc/net/arp | grep "0x2\|0x6" | awk '{print $1}' | grep -v "^169.254." | grep -v "^$" | sort -u | grep -oE "${SUBNET}\.[0-9]{1,3}") || local IPLIST=$(cat /proc/net/arp | grep "0x2\|0x6" | awk '{print $1}' | grep -v "^169.254." | grep -v "^$" | sort -u | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}') - local IPLIST=$(echo -e "${IPLIST}\n${gateway_iplist}\n${scan_iplist}\n${always_check_ip_list}" | grep -v "^$" | sort -u) + local IPLIST=$(echo -e "${IPLIST}\n${gateway_iplist}\n${always_check_ip_list}" | grep -v "^$" | sort -u) for ip in $IPLIST; do [ -n "$passive_mode" ] && [ "$passive_mode" -eq "1" ] && break + # 获取一个令牌 read -u 5 { up "$ip" - echo "" >&5 + # 释放令牌 + echo >&5 } & done wait @@ -838,7 +856,7 @@ blackwhitelist() { # 仅通知列表内设备 [ -n "$up_down_push_blacklist" ] && (! echo "$up_down_push_blacklist" | grep -q -i -w "$mac") && return 1 # 仅通知接口 - [ -n "$up_down_push_interface" ] && (! echo $(getinterface "$mac") | grep -q -i -w $up_down_push_interface) && return 1 + [ -n "$up_down_push_interface" ] && (! echo $(getinterface "" "$mac") | grep -q -i -w $up_down_push_interface) && return 1 for check_mac in $mark_mac_list; do # 设备在线时免打扰 @@ -917,12 +935,12 @@ check_connect() { is_online() { local urls=(http://connect.rom.miui.com/generate_204 http://wifi.vivo.com.cn/generate_204 http://connectivitycheck.platform.hicloud.com/generate_204 http://www.apple.com/library/test/success.html) local shuffled_urls=($(for i in "${urls[@]}"; do echo "$i"; done | awk 'BEGIN {srand()} {print rand(), $0}' | sort -n | cut -d' ' -f2-)) - for url in "${shuffled_urls[@]}"; do local status_code status_code=$(curl -o /dev/null -sI -w "%{http_code}" "$url") [[ "$status_code" -eq 204 || "$status_code" -eq 200 ]] && return 0 done + unset i url return 1 } @@ -984,7 +1002,7 @@ ip_changes() { local last_IPv4=$(jq -r '.address[] | select(.IPv4 != null) | .IPv4' "$devices_json") local last_IPv6=$(jq -r '.address[] | select(.IPv6 != null) | .IPv6' "$devices_json") - LockFile lock + silent_run LockFile lock if [ -z "$last_IPv4" ] && [ -z "$last_IPv6" ]; then log_change "${disturb_text}路由器已经重启!" @@ -996,7 +1014,7 @@ ip_changes() { [ -z "$title" ] && title="IP 地址变化" [ -z "$content" ] && content="${content}${str_splitline}${str_title_start} IP 地址变化${str_title_end}" content="${content}${str_linefeed}${str_tab}当前 IP: ${IPv4}" - jq -c --arg IPv4 "$IPv4" '.address[0].IPv4 = $IPv4' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" + jq --arg IPv4 "$IPv4" '.address[0].IPv4 = $IPv4' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" fi if [ "$get_ipv6_mode" -ne "0" ] && [ -n "$IPv6" ] && [ "$IPv6" != "$last_IPv6" ]; then @@ -1004,7 +1022,7 @@ ip_changes() { [ -z "$title" ] && title="IPv6 地址变化" [ -z "$content" ] && content="${content}${str_splitline}${str_title_start} IPv6 地址变化${str_title_end}" content="${content}${str_linefeed}${str_tab}当前 IPv6:${IPv6}" - jq -c --arg IPv6 "$IPv6" '.address[0].IPv6 = $IPv6' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" + jq --arg IPv6 "$IPv6" '.address[0].IPv6 = $IPv6' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" fi # IP 变化,悄咪咪的重启 zerotier @@ -1014,7 +1032,7 @@ ip_changes() { /etc/init.d/zerotier restart >/dev/null 2>&1 fi fi - LockFile unlock + silent_run LockFile unlock } # 检测设备上线 @@ -1029,7 +1047,7 @@ up() { # 连通 if [ "$ping_online" -eq "0" ]; then - LockFile lock + silent_run LockFile lock local time_up=$(date +%s) # 如果 IP 不存在,或者 http_access 键值为空,检查 http_access 参数 @@ -1038,19 +1056,15 @@ up() { # 如果 json 文件中能找到相同的 MAC,恢复数据 if [ "$mac" != "unknown" ] && jq -e --arg mac "$mac" '.devices[] | select(.mac == $mac) | .mac' "$devices_json" >/dev/null; then - # 如果是离线设备,json 文件中依然有信息,重置 IP、在线时间、流量 - if jq -e --arg mac "$mac" '.devices[] | select(.mac == $mac and .status == "offline") | .mac' "$devices_json" >/dev/null; then - jq -c --arg ip "$ip" --arg mac "$mac" --arg time_up "$time_up" '.devices |= map(if .mac == $mac then .ip = $ip | .uptime = $time_up | .usage = "" else . end)' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" - fi - # 离线、待确认离线设备,修改在线状态为 online - jq -c --arg ip "$ip" '.devices |= map(if .ip == $ip then .status = "online" else . end)' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" + # 如果是待检测离线设备,json 文件中依然有信息,重置 IP、在线时间、流量,修改在线状态为 online + jq --arg ip "$ip" --arg mac "$mac" --arg time_up "$time_up" '.devices |= map(if .mac == $mac then .ip = $ip | .uptime = $time_up | .usage = "" | .status = "online" else . end)' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" # up else usage down ${ip} # 删除当前 IP 对应的键值,重新写入 - jq -c --arg ip "$ip" 'del(.devices[] | select(.ip == $ip))' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" + jq --arg ip "$ip" 'del(.devices[] | select(.ip == $ip))' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" local name=$(getname "$ip" "$mac") - local interface=$(getinterface "$mac") + local interface=$(getinterface "ip" "$mac") local http_access=$(check_http_access "$ip") new_device='{ "name": "'"${name}"'", @@ -1062,12 +1076,12 @@ up() { "http_access": "'"${http_access}"'", "status": "online" }' - jq -c --argjson newdevice "$new_device" '.devices += [$newdevice]' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" + jq --argjson newdevice "$new_device" '.devices += [$newdevice]' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" blackwhitelist ${mac} local ip_blackwhite=$? - [ -f "${dir}/send_enable.lock" ] || [ -z "$notification_online" ] || [ -z "$ip_blackwhite" ] && LockFile unlock && return - [ -z "$ip_blackwhite" ] || [ "$ip_blackwhite" -ne "0" ] && LockFile unlock && return + [ -f "${dir}/send_enable.lock" ] || [ -z "$notification_online" ] || [ -z "$ip_blackwhite" ] && silent_run LockFile unlock && return + [ -z "$ip_blackwhite" ] || [ "$ip_blackwhite" -ne "0" ] && silent_run LockFile unlock && return [ -f "${dir}/title" ] && local title=$(cat "${dir}/title") [ -f "${dir}/content" ] && local content=$(cat "${dir}/content") @@ -1095,7 +1109,7 @@ up() { [ -n "$content" ] && echo -n "$content" >>"${dir}/content" fi fi - LockFile unlock + silent_run LockFile unlock } # 检测设备离线 @@ -1109,15 +1123,19 @@ down() { local ip_blackwhite=$? && [ "$ip_blackwhite" -ne "0" ] && tmp_timeout=10 && tmp_retry_count=2 getping "$ip" "$mac" "$tmp_timeout" "$tmp_retry_count" local ping_online=$? - LockFile lock + silent_run LockFile lock # 离线,置入待验证区 if [ "$ping_online" -eq "1" ]; then # 修改为 unknown - jq -c --arg ip "$ip" '.devices |= map(if .ip == $ip then .status = "unknown" else . end)' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" + if jq -e --arg ip "$ip" '.devices[] | select(.ip == $ip and .status == "online") | .ip' "$devices_json" >/dev/null; then + jq --arg ip "$ip" '.devices |= map(if .ip == $ip then .status = "unknown" else . end)' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" + elif jq -e --arg ip "$ip" '.devices[] | select(.ip == $ip and .status == "unknown") | .ip' "$devices_json" >/dev/null; then + jq --arg ip "$ip" '.devices |= map(if .ip == $ip then .status = "offline" else . end)' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" + fi # 更新主机名或 MAC else local name=$(getname "$ip" "$mac") - local interface=$(getinterface "$mac") + local interface=$(getinterface "ip" "$mac") # 检查是否有变化 local current_mac=$(jq -r --arg ip "$ip" '.devices[] | select(.ip == $ip) | .mac' "$devices_json") @@ -1125,7 +1143,7 @@ down() { local current_interface=$(jq -r --arg ip "$ip" '.devices[] | select(.ip == $ip) | .interface' "$devices_json") if [[ "$current_mac" != "$mac" || "$current_name" != "$name" || "$current_interface" != "$interface" ]]; then - jq -c --arg ip "$ip" --arg new_mac "$mac" --arg new_name "$name" --arg new_interface "$interface" ' + jq --arg ip "$ip" --arg new_mac "$mac" --arg new_name "$name" --arg new_interface "$interface" ' .devices |= map( if .ip == $ip then .mac = $new_mac | @@ -1138,20 +1156,16 @@ down() { fi fi - LockFile unlock + silent_run LockFile unlock } # 设备离线通知 down_send() { - # 如果待验证区没有设备,退出 - [ "$(jq '.devices | map(select(.status == "unknown")) | length' "$devices_json")" -eq 0 ] && return + # 如果没有离线设备,退出 + [ "$(jq '.devices | map(select(.status == "offline")) | length' "$devices_json")" -eq 0 ] && return - local IPLIST=$(jq -r '.devices[] | select(.status == "unknown") | .ip' "$devices_json" | sort -u) + local IPLIST=$(jq -r '.devices[] | select(.status == "offline") | .ip' "$devices_json" | sort -u) for ip in $IPLIST; do - LockFile lock - jq -c --arg ip "$ip" '.devices |= map(if .ip == $ip then .status = "offline" else . end)' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" - LockFile unlock - local mac=$(getmac "$ip") blackwhitelist "$mac" local ip_blackwhite=$? @@ -1185,6 +1199,9 @@ down_send() { log_change "${disturb_text}设备 ${name} ${ip} 断开连接 " done + silent_run LockFile lock + jq 'del(.devices[] | select(.status == "offline"))' "$devices_json" >"$tmp_devices_json" && mv "$tmp_devices_json" "$devices_json" + silent_run LockFile unlock } # 当前设备列表 @@ -1438,6 +1455,7 @@ get_disk() { [ -n "$disk_err" ] && echo -e -n "${str_linefeed}${str_tab}${str_title_start}硬盘存在错误,请及时备份数据!!!${str_title_end}" >>"$output_dir/get_disk" fi done + unset i } # 查询 IP 归属地 @@ -1846,7 +1864,7 @@ diy_send() { local type=$(jq -r '.type' ${3}) && local type=$(eval echo ${type}) local data=$(jq -r '.data' ${3}) && local data=$(eval echo ${data}) local content_type=$(jq -r '.content_type' ${3}) - ! jq -c "$type" ${3} >${tempjsonpath} && log_change "【!!!】type:{ } 字段转义变量后格式错误,请检查 type:{ } 字段内是否有特殊符号未转义或语法错误" && return 1 + ! jq "$type" ${3} >${tempjsonpath} && log_change "【!!!】type:{ } 字段转义变量后格式错误,请检查 type:{ } 字段内是否有特殊符号未转义或语法错误" && return 1 [ -n "$proxy_address" ] && local proxy_cmd="-x $proxy_address" curl $proxy_cmd --connect-timeout 30 -m 60 --retry 1 -X POST -H "$content_type" -d "$data" "${diyurl}" @@ -1860,7 +1878,7 @@ diy_send() { log_change "【debug】json 文件已保存至:${dir}/debug_send_json" log_change "【debug】推送内容预览文件保存至:${dir}/debug_send_content" log_change "【debug】如果收不到信息,请检查 ${dir}/debug_send_data 文件,或使用下列命令手动测试返回值 (可能需要关闭日志自动刷新方便选中)" - log_change "【debug】"'curl -X POST -H "'$content_type'" -d "@'${dir}/debug_send_data'" "'${diyurl}'" ' + log_change "【debug】curl -X POST -H \"$content_type\" -d \"@${dir}debug_send_data\" \"${diyurl}\"" [ -n "$4" ] && [ $RETVAL -eq 0 ] && return 0 return 1 else @@ -1965,7 +1983,6 @@ while [ "$enable" -eq "1" ]; do usage update disturb disturb=$? - device_aliases=$(cat "$device_aliases_path" 2>/dev/null) # 网络状态与 IP 变动 if [ "$get_ipv4_mode" -ne "0" ] || [ "$get_ipv6_mode" -ne "0" ]; then