-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cdn_responses.py
104 lines (87 loc) · 3.1 KB
/
test_cdn_responses.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
import subprocess
import random
import csv
from pprint import pprint
cdn = 'quantil'
f = open('out.csv','r')
reader = csv.reader(f)
ips = set()
domains = set()
for row in reader:
if row[2] == cdn:
ips.add(row[1])
domains.add(row[0])
f.close()
success = 0
failure = 0
fcodes = {}
while(True):
domain = random.sample(domains,1)[0]
ip = random.sample(ips,1)[0]
print('Trying %s at %s...' % (domain, ip),)
good = False
output = subprocess.getoutput("curl -k -I --resolve %s:443:%s https://%s" % (domain,ip,domain))
code = ''
try:
code = ''
for line in output.split('\n'):
if line.startswith('HTTP'):
code = line.split()[1]
if code == '':
print('Did not get initial code:')
print(output)
except:
print('EXCEPTION: ', output)
continue
if code.startswith('2'):
good = True
elif code.startswith('3'):
tries = 0
while code.startswith('3') and tries < 5:
tries += 1
# Set new domain
retries = False
for line in output.split('\n'):
if line.startswith('Location:') or line.startswith('location:'):
retries = True
if line.split()[-1].startswith('http://'):
domain = line.split()[-1].replace('http://','').strip('/')
print('New domain is %s, only we are now using http' % domain)
output = subprocess.getoutput("curl -k -s -I -m10 --resolve %s:80:%s http://%s" % (domain,ip,domain))
else:
domain = line.split()[-1].replace('https://','').strip('/')
print('New domain is %s' % domain)
output = subprocess.getoutput("curl -k -s -I -m10 --resolve %s:443:%s https://%s" % (domain,ip,domain))
try:
code = ''
for line in output.split('\n'):
if line.startswith('HTTP'):
code = line.split()[1]
except:
print('EXCEPTION: ', output)
continue
if code.startswith('2'):
good = True
else:
pass
if not retries:
print('DID NOT RETRY:')
print(output)
if good:
success += 1
else:
print('Failed (Code %s)' % code)
if code not in fcodes:
fcodes[code] = 0
fcodes[code] += 1
failure += 1
print('----------------------------------------------')
print('CDN: %s' % cdn)
print('Picking randomly from %d domains and %d IPs hosted on %s' % (len(domains), len(ips), cdn))
print('success: %d, failure: %d, success rate: %f' % (success, failure, success / (success +failure)))
print('Failure codes:')
pprint(fcodes)
print('----------------------------------------------')
if success + failure >= 100:
quit()