Project

General

Profile

Bug #14390 ยป Transparent Proxy test.py

Python script - Simon Byrnand, 05/17/2023 10:18 AM

 
1
# Tested on Python 3.9
2

    
3
import time
4
import socket
5
import urllib3
6
from urllib3.exceptions import SSLError
7
from urllib3.util import ssl_
8

    
9
# hostname to query
10

    
11
hostname = "eu-irl-00001.s3.dualstack.eu-west-1.amazonaws.com"
12
#hostname = "res.cdn.office.net"
13
#hostname = "uk.ng.msg.teams.microsoft.com"
14
#hostname = "gspe79-ssl.ls.apple.com"
15

    
16
ip_address = socket.gethostbyname(hostname)
17

    
18
print(f"Resolved hostname {hostname} to {ip_address}. This will be used for all further requests to simulate an application internally caching the resolved IP address.")
19
print()
20

    
21
# Create a new SSL context with the correct SNI
22
ssl_context = ssl_.create_urllib3_context()
23
ssl_context.load_default_certs()
24

    
25
while True:
26
    # Resolve all IP addresses
27
    ip_addresses = set([info[4][0] for info in socket.getaddrinfo(hostname, None)])
28

    
29
    print(f"All currently resolved IP addresses for hostname {hostname}: {ip_addresses}")
30

    
31
    print(f"Sending request to originally resolved IP address: {ip_address}")
32
    print()
33

    
34
    # Create a connection pool
35
    http = urllib3.HTTPSConnectionPool(
36
        ip_address, 
37
        port=443, 
38
        ssl_context=ssl_context, 
39
        server_hostname=hostname,
40
        assert_hostname=hostname,
41
    )
42

    
43
    try:
44
        response = http.request('GET', '/', retries=False)
45

    
46
        print(f"HTTP response code: {response.status}")
47
        print("HTTP response headers:")
48
        for header, value in response.headers.items():
49
            print(f"{header}: {value}")
50
    except SSLError as e:
51
        print(f"An SSL error occurred: {e}")
52
    except Exception as e:
53
        print(f"An error occurred: {e}")
54

    
55
    time.sleep(25)
56

    
57
    print()
    (1-1/1)