diff options
author | Andronaco Marco <marco.andronaco@olivetti.com> | 2023-07-13 11:10:38 +0200 |
---|---|---|
committer | Andronaco Marco <marco.andronaco@olivetti.com> | 2023-07-13 11:10:38 +0200 |
commit | 19ce085fa872db7f4f15e230524889ac5322f8a5 (patch) | |
tree | e5184f1116f118d64a7a2cd0e5d90497e214da49 /MyResolver.py | |
parent | c4d85c371226380ae82f722938ca12eb5a528512 (diff) | |
download | sunstroke-19ce085fa872db7f4f15e230524889ac5322f8a5.tar.gz |
add custom resolver
Diffstat (limited to 'MyResolver.py')
-rw-r--r-- | MyResolver.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/MyResolver.py b/MyResolver.py new file mode 100644 index 0000000..cb654ba --- /dev/null +++ b/MyResolver.py @@ -0,0 +1,42 @@ +import requests + +class HostHeaderSSLAdapter(requests.adapters.HTTPAdapter): + def resolve(self, hostname): + import random + ips = [ + '104.16.89.20', # CloudFlare + '151.101.2.109', # Fastly + ] + resolutions = { + 'overpost.biz': random.choice(ips), + } + return resolutions.get(hostname) + + def send(self, request, **kwargs): + from urllib.parse import urlparse + + connection_pool_kwargs = self.poolmanager.connection_pool_kw + result = urlparse(request.url) + resolved_ip = self.resolve(result.hostname) + + if result.scheme == 'https' and resolved_ip: + request.url = request.url.replace( + 'https://' + result.hostname, + 'https://' + resolved_ip, + ) + connection_pool_kwargs['server_hostname'] = result.hostname # SNI + connection_pool_kwargs['assert_hostname'] = result.hostname + + # overwrite the host header + request.headers['Host'] = result.hostname + else: + # theses headers from a previous request may have been left + connection_pool_kwargs.pop('server_hostname', None) + connection_pool_kwargs.pop('assert_hostname', None) + + return super(HostHeaderSSLAdapter, self).send(request, **kwargs) + +def get(url: str): + session = requests.Session() + session.mount('https://', HostHeaderSSLAdapter()) + return session.get(url) |