MyResolver.py (view raw)
1import requests
2
3class HostHeaderSSLAdapter(requests.adapters.HTTPAdapter):
4 def resolve(self, hostname):
5 import random
6 ips = [
7 '104.16.89.20', # CloudFlare
8 '151.101.2.109', # Fastly
9 ]
10 resolutions = {
11 'overpost.biz': random.choice(ips),
12 }
13 return resolutions.get(hostname)
14
15 def send(self, request, **kwargs):
16 from urllib.parse import urlparse
17
18 connection_pool_kwargs = self.poolmanager.connection_pool_kw
19 result = urlparse(request.url)
20 resolved_ip = self.resolve(result.hostname)
21
22 if result.scheme == 'https' and resolved_ip:
23 request.url = request.url.replace(
24 'https://' + result.hostname,
25 'https://' + resolved_ip,
26 )
27 connection_pool_kwargs['server_hostname'] = result.hostname # SNI
28 connection_pool_kwargs['assert_hostname'] = result.hostname
29
30 # overwrite the host header
31 request.headers['Host'] = result.hostname
32 else:
33 # theses headers from a previous request may have been left
34 connection_pool_kwargs.pop('server_hostname', None)
35 connection_pool_kwargs.pop('assert_hostname', None)
36
37 return super(HostHeaderSSLAdapter, self).send(request, **kwargs)
38
39def get(url: str):
40 session = requests.Session()
41 session.mount('https://', HostHeaderSSLAdapter())
42 return session.get(url)