mirror-health: don't (ab)use the proxy interface
authorJulien Cristau <jcristau@debian.org>
Wed, 9 Oct 2019 13:56:26 +0000 (15:56 +0200)
committerJulien Cristau <jcristau@debian.org>
Wed, 9 Oct 2019 13:56:26 +0000 (15:56 +0200)
monkey-patch urllib3.util.connection.create_connection to override
address resolution, which is ugly but makes this work with https.

modules/mirror_health/files/mirror-health

index 6484595..8c7498c 100755 (executable)
@@ -7,6 +7,8 @@ import calendar
 import logging
 import subprocess
 from email.utils import parsedate
+import urllib3.util.connection
+
 logging.basicConfig(level=logging.INFO)
 
 if 'MIRROR_CHECK_HOSTS' in os.environ:
@@ -21,12 +23,16 @@ HEALTH_CHECK_URL = os.environ['MIRROR_CHECK_HEALTH_URL']
 INTERVAL = int(os.environ.get('MIRROR_CHECK_INTERVAL', '60'))
 
 def retrieve_from_host(host, url):
-    proxies = {
-        'http': 'http://{}:80'.format(host),
-        'https': 'http://{}:443'.format(host),
-    }
+    orig_create_connection = urllib3.util.connection.create_connection
+    def patched_create_connection(address, *args, **kwargs):
+        _host, port = address
+        return orig_create_connection((host, port), *args, **kwargs)
     headers = {'User-Agent': 'mirror-health'}
-    return requests.get(url, headers=headers, timeout=5, proxies=proxies, allow_redirects=False)
+    urllib3.util.connection.create_connection = patched_create_connection
+    try:
+        return requests.get(url, headers=headers, timeout=5, allow_redirects=False)
+    finally:
+        urllib3.util.connection.create_connection = orig_create_connection
 
 def last_modified(response):
     lm = 0