Dns check refactor (#328)

* refactor: streamline proxy checking in search function

* refactor: update DNS check method, try a real dns resolution instead of checking dns provider

* refactor: enhance DNS resolution check to support multiple domains across platforms

* refactor: replace os.socket with socket for DNS resolution consistency

---------

Co-authored-by: None <62809003+Arrowar@users.noreply.github.com>
This commit is contained in:
Alessandro Perazzetta 2025-05-31 11:30:59 +02:00 committed by GitHub
parent 1776538c6c
commit 73cc2662b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 63 additions and 28 deletions

View File

@ -12,7 +12,7 @@ import inspect
import subprocess import subprocess
import contextlib import contextlib
import importlib.metadata import importlib.metadata
import socket
# External library # External library
from unidecode import unidecode from unidecode import unidecode
@ -283,37 +283,61 @@ class InternManager():
else: else:
return f"{bytes / (1024 * 1024):.2f} MB/s" return f"{bytes / (1024 * 1024):.2f} MB/s"
def check_dns_provider(self): # def check_dns_provider(self):
# """
# Check if the system's current DNS server matches any known DNS providers.
# Returns:
# bool: True if the current DNS server matches a known provider,
# False if no match is found or in case of errors
# """
# dns_providers = {
# "Cloudflare": ["1.1.1.1", "1.0.0.1"],
# "Google": ["8.8.8.8", "8.8.4.4"],
# "OpenDNS": ["208.67.222.222", "208.67.220.220"],
# "Quad9": ["9.9.9.9", "149.112.112.112"],
# "AdGuard": ["94.140.14.14", "94.140.15.15"],
# "Comodo": ["8.26.56.26", "8.20.247.20"],
# "Level3": ["209.244.0.3", "209.244.0.4"],
# "Norton": ["199.85.126.10", "199.85.127.10"],
# "CleanBrowsing": ["185.228.168.9", "185.228.169.9"],
# "Yandex": ["77.88.8.8", "77.88.8.1"]
# }
# try:
# resolver = dns.resolver.Resolver()
# nameservers = resolver.nameservers
# if not nameservers:
# return False
# for server in nameservers:
# for provider, ips in dns_providers.items():
# if server in ips:
# return True
# return False
# except Exception:
# return False
def check_dns_resolve(self):
""" """
Check if the system's current DNS server matches any known DNS providers. Check if the system's current DNS server can resolve a domain name.
Works on both Windows and Unix-like systems.
Returns: Returns:
bool: True if the current DNS server matches a known provider, bool: True if the current DNS server can resolve a domain name,
False if no match is found or in case of errors False if can't resolve or in case of errors
""" """
dns_providers = { test_domains = ["github.com", "google.com", "microsoft.com", "amazon.com"]
"Cloudflare": ["1.1.1.1", "1.0.0.1"],
"Google": ["8.8.8.8", "8.8.4.4"],
"OpenDNS": ["208.67.222.222", "208.67.220.220"],
"Quad9": ["9.9.9.9", "149.112.112.112"],
}
try: try:
resolver = dns.resolver.Resolver() for domain in test_domains:
nameservers = resolver.nameservers # socket.gethostbyname() works consistently across all platforms
socket.gethostbyname(domain)
if not nameservers: return True
return False except (socket.gaierror, socket.error):
for server in nameservers:
for provider, ips in dns_providers.items():
if server in ips:
return True
return False return False
except Exception:
return False
class OsSummary: class OsSummary:
def __init__(self): def __init__(self):

View File

@ -210,7 +210,19 @@ def main(script_id = 0):
log_not = Logger() log_not = Logger()
initialize() initialize()
if not internet_manager.check_dns_provider(): # if not internet_manager.check_dns_provider():
# print()
# console.print("[red]❌ ERROR: DNS configuration is required!")
# console.print("[red]The program cannot function correctly without proper DNS settings.")
# console.print("[yellow]Please configure one of these DNS servers:")
# console.print("[blue]• Cloudflare (1.1.1.1) 'https://developers.cloudflare.com/1.1.1.1/setup/windows/'")
# console.print("[blue]• Quad9 (9.9.9.9) 'https://docs.quad9.net/Setup_Guides/Windows/Windows_10/'")
# console.print("\n[yellow]⚠️ The program will not work until you configure your DNS settings.")
# time.sleep(2)
# msg.ask("[yellow]Press Enter to continue ...")
if not internet_manager.check_dns_resolve():
print() print()
console.print("[red]❌ ERROR: DNS configuration is required!") console.print("[red]❌ ERROR: DNS configuration is required!")
console.print("[red]The program cannot function correctly without proper DNS settings.") console.print("[red]The program cannot function correctly without proper DNS settings.")
@ -219,8 +231,7 @@ def main(script_id = 0):
console.print("[blue]• Quad9 (9.9.9.9) 'https://docs.quad9.net/Setup_Guides/Windows/Windows_10/'") console.print("[blue]• Quad9 (9.9.9.9) 'https://docs.quad9.net/Setup_Guides/Windows/Windows_10/'")
console.print("\n[yellow]⚠️ The program will not work until you configure your DNS settings.") console.print("\n[yellow]⚠️ The program will not work until you configure your DNS settings.")
time.sleep(2) os._exit(0)
msg.ask("[yellow]Press Enter to continue ...")
# Load search functions # Load search functions
search_functions = load_search_functions() search_functions = load_search_functions()