Files
bird_config/bird/whitelist/downmyip.sh
2026-04-14 12:16:31 +08:00

93 lines
2.5 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# downmyip - 自动从 /etc/bird/vars.conf 读取ASN并下载ROA配置
# 配置文件路径
VARS_CONF="/etc/bird/vars.conf"
BASE_URL="https://ntwkapi.noc.zhuantou.com.cn/brnet/roa/api.php?as="
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
get_asn() {
grep "^define $1_ASN" "$VARS_CONF" | awk -F'[=; ]' '{print $5}'
}
# 函数:下载配置
download_roa() {
local asn="$1"
local filename="/etc/bird/whitelist/$2"
if [ -z "$asn" ]; then
echo -e "${YELLOW}⏭️ $filename 未配置ASN跳过下载${NC}"
return
fi
echo -e "${YELLOW}📥 正在下载 $filename (ASN: $asn)${NC}"
if wget -q -O "$filename" "${BASE_URL}${asn}"; then
echo -e "${GREEN}$filename 下载成功${NC}"
else
echo -e "${RED}$filename 下载失败${NC}"
fi
}
# 函数:创建软链接
create_symlink() {
local script_path=$(realpath "$0")
local link_path="/usr/local/bin/downmyip"
if [ -L "$link_path" ] || [ -f "$link_path" ]; then
echo -e "${YELLOW}🔗 软链接已存在${NC}"
return
fi
echo -e "${YELLOW}🔗 创建全局命令downmyip${NC}"
if ln -s "$script_path" "$link_path"; then
echo -e "${GREEN}✅ 软链接创建完成!${NC}"
else
echo -e "${RED}❌ 请用sudo运行脚本以创建软链接${NC}"
fi
}
# ===================== 主逻辑 =====================
clear
echo -e "${YELLOW}===== downmyip - 自动下载ROA配置 =====${NC}"
# 1. 检查vars.conf是否存在
if [ ! -f "$VARS_CONF" ]; then
echo -e "${RED}❌ 配置文件不存在:$VARS_CONF${NC}"
exit 1
fi
# 2. 读取三个ASN
INET_ASN=$(get_asn "LOCAL")
DN42_ASN=$(get_asn "DN42")
UNET_ASN=$(get_asn "UNET")
echo -e "${GREEN}📄 从vars.conf读取ASN${NC}"
[ -n "$INET_ASN" ] && echo "公网ASN$INET_ASN" || echo "公网ASN未配置"
[ -n "$DN42_ASN" ] && echo "DN42 ASN$DN42_ASN" || echo "DN42 ASN未配置"
[ -n "$UNET_ASN" ] && echo "内网ASN$UNET_ASN" || echo "内网ASN未配置"
echo
# 3. 依次下载(不存在则跳过)
download_roa "$INET_ASN" "inet.conf"
download_roa "$DN42_ASN" "dn42.conf"
download_roa "$UNET_ASN" "unet.conf"
echo
# 4. 重载BIRD配置
echo -e "${YELLOW}🔄 执行birdc c${NC}"
if command -v birdc &>/dev/null; then
birdc c
echo -e "${GREEN}✅ BIRD重载完成${NC}"
else
echo -e "${RED}❌ 未找到birdc命令${NC}"
fi
# 5. 创建全局软链接
create_symlink
echo -e "\n${GREEN}===== 全部执行完毕 =====${NC}"