From d707eef267d96871eb045bd3ad0bd7c61b3a14e8 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 25 Feb 2024 15:15:38 +0000 Subject: [PATCH] Absolute DNS lookups --- check.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/check.py b/check.py index 55a5581..fd3ae0e 100755 --- a/check.py +++ b/check.py @@ -11,7 +11,33 @@ from email.mime.text import MIMEText from email.utils import formatdate, make_msgid import requests +from requests.adapters import HTTPAdapter from urllib3.exceptions import InsecureRequestWarning # type: ignore +from urllib3.util.url import parse_url # type: ignore + + +class AbsoluteDNSAdapter(HTTPAdapter): + """A custom adapter for requests to ensure hostnames are treated as absolute.""" + + def add_dot_to_hostname(self, url: str) -> str: + """Append a dot to the hostname to treat it as an absolute domain name.""" + parsed_url = parse_url(url) + + # Append a dot to the hostname if it's not already there. + hostname = parsed_url.host + if not hostname.endswith("."): + hostname += "." + + # Reconstruct the URL with the modified hostname. + new_url: str = parsed_url._replace(host=hostname).url + return new_url + + def send(self, request, **kwargs): # type: ignore + """Override the send method to modify the request URL before sending.""" + # Modify the request URL to ensure the hostname is treated as absolute. + request.url = self.add_dot_to_hostname(request.url) + return super().send(request, **kwargs) + config_file_path = os.path.expanduser( os.path.join( @@ -35,6 +61,12 @@ headers = {"User-Agent": AGENT, "Accept": "text/html"} s = requests.Session() s.headers.update(headers) +# Create a session and mount the custom adapter for both HTTP and HTTPS requests. +adapter = AbsoluteDNSAdapter() +s.mount("http://", adapter) +s.mount("https://", adapter) + + MAIL_FROM = "edward@4angle.com" MAIL_TO_NAME = "Edward Betts" MAIL_TO_ADDRESS = "edward@4angle.com"