-
-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathfronting.py
43 lines (37 loc) · 1.65 KB
/
fronting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import requests
def fronting_test(
ip: str,
timeout: float
) -> bool:
"""conducts a fronting test on an ip and return true if status 200 is received
Args:
ip (str): ip for testing
timeout (float): the timeout to wait for ``requests.get`` result
Returns:
bool: True if ``status_code`` is 200, False otherwise
"""
s = requests.Session()
s.get_adapter(
'https://').poolmanager.connection_pool_kw['server_hostname'] = "speed.cloudflare.com"
s.get_adapter(
'https://').poolmanager.connection_pool_kw['assert_hostname'] = "speed.cloudflare.com"
try:
compatible_ip = f"[{ip}]" if ":" in ip else ip
r = s.get(
f"https://{compatible_ip}",
timeout=timeout,
headers={"Host": "speed.cloudflare.com"}
)
if r.status_code != 200:
return f"[bold red1]NO[/bold red1] [orange3]{ip:15s}[/orange3][yellow1] fronting error {r.status_code} [/yellow1]"
else:
success = True
except requests.exceptions.ConnectTimeout as e:
return f"[bold red1]NO[/bold red1] [orange3]{ip:15s}[/orange3][yellow1] fronting connect timeout[/yellow1]"
except requests.exceptions.ReadTimeout as e:
return f"[bold red1]NO[/bold red1] [orange3]{ip:15s}[/orange3][yellow1] fronting read timeout[/yellow1]"
except requests.exceptions.ConnectionError as e:
return f"[bold red1]NO[/bold red1] [orange3]{ip:15s}[/orange3][yellow1] fronting connection error[/yellow1]"
except Exception as e:
return f"[bold red1]NO[/bold red1] [orange3]{ip:15s}[/orange3][yellow1] fronting Unknown error[/yellow1]"
return "OK"