From c23aaf493e90e0acccbc1d3348b8ebbb0ab3455e Mon Sep 17 00:00:00 2001 From: Ali G Date: Mon, 15 Sep 2025 12:13:05 +0200 Subject: [PATCH] Update dns_tester.sh actual tests --- dns_tester.sh | 85 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 33 deletions(-) diff --git a/dns_tester.sh b/dns_tester.sh index ab903c3..d321410 100755 --- a/dns_tester.sh +++ b/dns_tester.sh @@ -75,12 +75,15 @@ test_dns_performance() { local successful_tests=0 local test_count=0 + # Get the first IPv4 DNS server for testing + local primary_dns=$(echo "$dns_servers" | cut -d',' -f1) + IFS=',' read -ra TARGET_ARRAY <<< "$targets" for target in "${TARGET_ARRAY[@]}"; do target=$(echo "$target" | xargs) if [[ -n "$target" ]]; then test_count=$((test_count + 1)) - local latency=$(ping_dns_resolved "$target" "$dns_servers" 2>/dev/null || echo "999") + local latency=$(measure_dns_query_time "$target" "$primary_dns" 2>/dev/null || echo "999") if [[ "$latency" != "999" ]]; then total_latency=$((total_latency + latency)) successful_tests=$((successful_tests + 1)) @@ -90,16 +93,7 @@ test_dns_performance() { if [[ $successful_tests -gt 0 ]]; then local avg_latency=$((total_latency / successful_tests)) - local simulated_latency=$avg_latency - case $dns_name in - "Cloudflare") simulated_latency=$((avg_latency + 5)) ;; - "Google") simulated_latency=$((avg_latency + 8)) ;; - "Level3") simulated_latency=$((avg_latency + 12)) ;; - "OpenDNS") simulated_latency=$((avg_latency + 15)) ;; - "Quad9") simulated_latency=$((avg_latency + 18)) ;; - "Comodo") simulated_latency=$((avg_latency + 20)) ;; - esac - RESULTS["$dns_name"]=$simulated_latency + RESULTS["$dns_name"]=$avg_latency DNS_NAMES["$dns_name"]="$dns_servers" else RESULTS["$dns_name"]=999 @@ -107,29 +101,50 @@ test_dns_performance() { fi } -ping_dns_resolved() { +measure_dns_query_time() { local target=$1 - local dns_servers=$2 + local dns_server=$2 - local best_latency=999 + local latency=999 - local fallback_ip=$(nslookup "$target" 2>/dev/null | grep "Address:" | grep -v "::" | tail -1 | awk '{print $2}') - if [[ -z "$fallback_ip" ]]; then - fallback_ip=$(nslookup "$target" 2>/dev/null | grep "Address:" | tail -1 | awk '{print $2}') - fi - - if [[ -n "$fallback_ip" ]]; then - if [[ "$fallback_ip" == *":"* ]]; then - local latency=$(ping6 -c 1 -W 2 "$fallback_ip" 2>/dev/null | grep "time=" | cut -d'=' -f2 | cut -d' ' -f1 | cut -d'.' -f1) - else - local latency=$(ping -c 1 -W 2 "$fallback_ip" 2>/dev/null | grep "time=" | cut -d'=' -f2 | cut -d' ' -f1 | cut -d'.' -f1) + # Use dig to measure actual DNS query time + if command -v dig &> /dev/null; then + # First verify the query works + local dig_output=$(timeout 5 dig @"$dns_server" "$target" A +short 2>/dev/null) + if [[ -n "$dig_output" ]]; then + # Measure the actual query time + local start_time=$(date +%s%3N) + timeout 5 dig @"$dns_server" "$target" A +short >/dev/null 2>&1 + local end_time=$(date +%s%3N) + latency=$(( (end_time - start_time) )) + + # Convert to milliseconds if needed + if [[ $latency -gt 1000 ]]; then + latency=$(( latency / 1000 )) + fi fi - if [[ -n "$latency" ]]; then - best_latency=$latency + elif command -v nslookup &> /dev/null; then + # Fallback to nslookup with timing + local start_time=$(date +%s%3N) + local nslookup_output=$(timeout 5 nslookup "$target" "$dns_server" 2>/dev/null) + local end_time=$(date +%s%3N) + + if echo "$nslookup_output" | grep -q "Address:"; then + latency=$(( (end_time - start_time) )) + + # Convert to milliseconds if needed + if [[ $latency -gt 1000 ]]; then + latency=$(( latency / 1000 )) + fi fi fi - echo "$best_latency" + # Ensure latency is reasonable (not negative or too high) + if [[ $latency -lt 0 || $latency -gt 5000 ]]; then + latency=999 + fi + + echo "$latency" } run_tests() { @@ -235,20 +250,23 @@ show_post_results_menu() { check_dependencies() { local missing_deps=() - if ! command -v ping &> /dev/null; then - missing_deps+=("iputils-ping") - fi - - if ! command -v nslookup &> /dev/null && ! command -v dig &> /dev/null; then + if ! command -v dig &> /dev/null && ! command -v nslookup &> /dev/null; then missing_deps+=("dnsutils or bind-utils") fi if [[ ${#missing_deps[@]} -gt 0 ]]; then echo -e "${RED}Missing dependencies: ${missing_deps[*]}${NC}" echo -e "${YELLOW}Please install them and run the script again.${NC}" - echo -e "${CYAN}Example: sudo apt install dnsutils iputils-ping${NC}" + echo -e "${CYAN}Example: sudo apt install dnsutils${NC}" + echo -e "${CYAN}Or: sudo yum install bind-utils${NC}" exit 1 fi + + if command -v dig &> /dev/null; then + echo -e "${GREEN}Using dig for DNS queries (recommended)${NC}" + else + echo -e "${YELLOW}Using nslookup for DNS queries (dig preferred)${NC}" + fi } main() { @@ -299,3 +317,4 @@ main() { } main "$@" +