This commit is contained in:
sushen339
2025-11-18 04:17:40 +08:00
parent 3c44f41dc2
commit 43c7d79e09
+19 -16
View File
@@ -143,8 +143,24 @@ ban_ip() {
# 查询新IP的国家信息 # 查询新IP的国家信息
COUNTRY_CODE=$(curl -s --max-time 2 "https://ipinfo.io/$BASE_IP/country" 2>/dev/null | tr -d '\n\r ') COUNTRY_CODE=$(curl -s --max-time 2 "https://ipinfo.io/$BASE_IP/country" 2>/dev/null | tr -d '\n\r ')
[ -n "$COUNTRY_CODE" ] && [ ${#COUNTRY_CODE} -ne 2 ] && COUNTRY_CODE="" [ -n "$COUNTRY_CODE" ] && [ ${#COUNTRY_CODE} -ne 2 ] && COUNTRY_CODE=""
fi
# 趁API可用时,为文件中没有国家信息的旧IP补充查询(最多补充3个,避免耗时过长) # 先保存新IP到文件
if [ "$SAVE_DISK" -eq 1 ]; then
# 检查是否已存在(支持带国家代码的格式)
if ! grep -qE "^$TARGET_IP(\||$)" "$PERSIST_FILE" 2>/dev/null; then
if [ -n "$COUNTRY_CODE" ]; then
echo "$TARGET_IP|$COUNTRY_CODE" >> "$PERSIST_FILE"
COUNTRY_NAME=$(get_country_name "$COUNTRY_CODE")
log "[执行封禁] IP=$TARGET_IP 国家=$COUNTRY_NAME 已封禁"
else
echo "$TARGET_IP" >> "$PERSIST_FILE"
log "[执行封禁] IP=$TARGET_IP 已封禁"
fi
fi
# 新IP保存后,趁API可用时为文件中没有国家信息的旧IP补充查询(最多补充3个)
if [ "$SHOULD_QUERY" -eq 1 ] && command -v curl >/dev/null 2>&1; then
if [ -f "$PERSIST_FILE" ] && [ -s "$PERSIST_FILE" ]; then if [ -f "$PERSIST_FILE" ] && [ -s "$PERSIST_FILE" ]; then
UPDATE_COUNT=0 UPDATE_COUNT=0
TEMP_UPDATE="/tmp/block_ip_update_$$" TEMP_UPDATE="/tmp/block_ip_update_$$"
@@ -152,8 +168,8 @@ ban_ip() {
while IFS= read -r line; do while IFS= read -r line; do
[ "$UPDATE_COUNT" -ge 3 ] && break [ "$UPDATE_COUNT" -ge 3 ] && break
# 只处理不含'|'的IPv4单IP行 # 只处理不含'|'的IPv4单IP行,且不是刚添加的IP
if ! echo "$line" | grep -q '|' && ! echo "$line" | grep -q ':' && ! echo "$line" | grep -q '/'; then if [ "$line" != "$TARGET_IP" ] && ! echo "$line" | grep -q '|' && ! echo "$line" | grep -q ':' && ! echo "$line" | grep -q '/'; then
OLD_IP="$line" OLD_IP="$line"
OLD_COUNTRY=$(curl -s --max-time 2 "https://ipinfo.io/$OLD_IP/country" 2>/dev/null | tr -d '\n\r ') OLD_COUNTRY=$(curl -s --max-time 2 "https://ipinfo.io/$OLD_IP/country" 2>/dev/null | tr -d '\n\r ')
if [ -n "$OLD_COUNTRY" ] && [ ${#OLD_COUNTRY} -eq 2 ]; then if [ -n "$OLD_COUNTRY" ] && [ ${#OLD_COUNTRY} -eq 2 ]; then
@@ -169,19 +185,6 @@ ban_ip() {
[ "$UPDATE_COUNT" -gt 0 ] && mv "$TEMP_UPDATE" "$PERSIST_FILE" || rm -f "$TEMP_UPDATE" [ "$UPDATE_COUNT" -gt 0 ] && mv "$TEMP_UPDATE" "$PERSIST_FILE" || rm -f "$TEMP_UPDATE"
fi fi
fi fi
if [ "$SAVE_DISK" -eq 1 ]; then
# 检查是否已存在(支持带国家代码的格式)
if ! grep -qE "^$TARGET_IP(\||$)" "$PERSIST_FILE" 2>/dev/null; then
if [ -n "$COUNTRY_CODE" ]; then
echo "$TARGET_IP|$COUNTRY_CODE" >> "$PERSIST_FILE"
COUNTRY_NAME=$(get_country_name "$COUNTRY_CODE")
log "[执行封禁] IP=$TARGET_IP 国家=$COUNTRY_NAME 已封禁"
else
echo "$TARGET_IP" >> "$PERSIST_FILE"
log "[执行封禁] IP=$TARGET_IP 已封禁"
fi
fi
elif [ "$SAVE_DISK" -ne 2 ]; then elif [ "$SAVE_DISK" -ne 2 ]; then
log "[执行封禁] IP=$TARGET_IP 已封禁" log "[执行封禁] IP=$TARGET_IP 已封禁"
fi fi