Files
dock/DNS
2025-10-28 15:49:46 +08:00

90 lines
2.8 KiB
Bash
Raw Permalink 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
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m'
echo -e "${PURPLE}"
echo "=================================================="
echo " 🌈 智能DNS推荐系统"
echo "=================================================="
echo -e "${NC}"
# 测试并推荐最佳DNS
test_and_recommend() {
declare -A dns_servers=(
["119.29.29.29"]="腾讯DNS"
["223.5.5.5"]="阿里DNS"
["114.114.114.114"]="114DNS"
["8.8.8.8"]="GoogleDNS"
["1.1.1.1"]="Cloudflare"
)
declare -A results
echo -e "${BLUE}🧪 测试各DNS性能...${NC}"
for dns_ip in "${!dns_servers[@]}"; do
total_time=0
success_count=0
test_domains=("baidu.com" "qq.com" "taobao.com")
for domain in "${test_domains[@]}"; do
start_time=$(date +%s%N)
nslookup $domain $dns_ip >/dev/null 2>&1
if [ $? -eq 0 ]; then
end_time=$(date +%s%N)
time_ms=$(( (end_time - start_time) / 1000000 ))
total_time=$(( total_time + time_ms ))
success_count=$(( success_count + 1 ))
fi
done
if [ $success_count -gt 0 ]; then
avg_time=$(( total_time / success_count ))
results[$dns_ip]=$avg_time
echo -e " ${dns_servers[$dns_ip]} ($dns_ip): ${avg_time}ms"
else
echo -e " ${RED}${dns_servers[$dns_ip]} ($dns_ip): 测试失败${NC}"
fi
done
# 找出最快的DNS
fastest_ip=""
fastest_time=999999
for ip in "${!results[@]}"; do
if [ ${results[$ip]} -lt $fastest_time ]; then
fastest_time=${results[$ip]}
fastest_ip=$ip
fi
done
echo -e "\n${GREEN}🎯 推荐使用: ${dns_servers[$fastest_ip]} ($fastest_ip)${NC}"
echo -e "${GREEN}📊 平均解析时间: ${fastest_time}ms${NC}"
# 设置推荐DNS
echo -e "\n${BLUE}🔧 是否设置为推荐DNS${NC}"
read -p "输入 y 确认设置: " confirm
if [ "$confirm" = "y" ]; then
echo "nameserver $fastest_ip" > /etc/resolv.conf
echo "nameserver 223.5.5.5" >> /etc/resolv.conf # 备用
echo -e "${GREEN}✅ 已设置为 ${dns_servers[$fastest_ip]}${NC}"
fi
}
test_and_recommend
echo -e "\n${PURPLE}==================================================${NC}"
echo -e "${CYAN}💡 重要提醒:${NC}"
echo -e "${YELLOW}• DNS只影响域名解析速度${NC}"
echo -e "${YELLOW}• 不影响已经建立连接的网络延迟${NC}"
echo -e "${YELLOW}• 主要提升: 网页第一次打开速度${NC}"
echo -e "${YELLOW}• 不影响: 视频缓冲、游戏延迟、下载速度${NC}"
echo -e "${PURPLE}==================================================${NC}"