-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
277 lines (246 loc) · 8.54 KB
/
util.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from ast import literal_eval
from datetime import datetime
from functools import partial
from multiprocessing.pool import ThreadPool
import random
import string
import httpx
import re
import os
import pandas as pd
from tld import get_fld
from tranco import Tranco
from mitmproxy.http import Headers
from datetime import datetime, timezone
from email.utils import format_datetime
from .db_util import Site, Url, db
from peewee import IntegrityError
# TODO: replace with (Mattermost) Webhook
WEB_HOOK = "TODO"
# TODO: replace user_agent with a link to your crawl information website with the option to opt-out
user_agent = "TODO"
def is_reachable(origin, timeout=10):
"""Check whether origin is reachable (no error and answer takes less than timeout/10s)."""
reachable = True
error = ""
try:
# Follow redirects (we only test hosts that return a valid page for a normal request/are not completely broken)
httpx.get(origin + "/", timeout=timeout, follow_redirects=True)
except (httpx.ConnectError, httpx.TimeoutException) as e:
reachable = False
error = e
except Exception as e:
reachable = False
print(f"Site error: {origin} - {e}")
error = e
finally:
return reachable, error
def add_origin(t_list, site_type, bucket, origin):
"""Add new origin to the database (with both http and https) if reachable."""
org_scheme, host = origin.split("://", maxsplit=1)
for scheme, port in [("http", 80), ("https", 443)]:
origin = f"{scheme}://{host}"
reachable, error = is_reachable(origin)
site = get_fld(origin)
rank = t_list.rank(site)
try:
s = Site.create(
site_type=site_type,
description=origin,
rank=rank,
site=site,
reachable=reachable,
error=error,
bucket=bucket,
origin=origin,
org_scheme=org_scheme,
)
s.save()
except IntegrityError:
print(f"{origin} already exist!")
continue
if not reachable:
continue
full_url = f"{origin}/"
for full_url, path, desc in get_test_urls(full_url, "/", scheme):
if path == "/":
is_base = True
else:
is_base = False
u = Url.create(
site=s,
full_url=full_url,
scheme=scheme,
host=host,
port=port,
path=path,
description=desc,
is_base=is_base,
)
u.save()
def create_sites_urls(
db_name,
recreate=False,
site_type="local",
buckets=[(1000, 1000), (10000, 1000), (100000, 1000), (1000000, 1000)],
workers=300,
):
"""Buckets (bucket, n): first entry is the bucket, e.g, Top1K, second one is the number of origins to take from this bucket.
Available buckets: 1000, 5000, 10000, 50000, 100000, 500000, 1000000
Create Sites and URLs entries in the database.
"""
db.init(db_name)
db.connect()
if len(Site.select()) != 0 and not recreate:
return
db.drop_tables([Site, Url], cascade=True)
db.create_tables([Site, Url])
t = Tranco(cache=True, cache_dir=".tranco")
t_list = t.list(date="2023-01-30")
# Local (HTTP and HTTPS counted as separated sites)
if site_type == "local":
local_file = f"{os.path.dirname(__file__)}/../testbed/.env"
with open(local_file, "r") as f:
local_file = f.read()
matches = re.findall(
"^(?!#)(.*)_(http|https)_port=(\d+)", local_file, re.MULTILINE
)
for site, scheme, port in matches:
s = Site(
site_type=site_type,
description=f"{site}-{scheme}",
rank=-1,
site=site,
origin=f"{scheme}://{site}",
bucket=-1,
)
s.save()
# For some of the local servers (caddy), the cert only works for localhost no IPs?
host = "localhost"
full_url = f"{scheme}://{host}:{port}/"
for full_url, path, desc in get_test_urls(full_url, "/", scheme):
if path == "/":
is_base = True
else:
is_base = False
u = Url(
site=s,
full_url=full_url,
scheme=scheme,
host=host,
port=port,
path=path,
description=desc,
is_base=is_base,
)
u.save()
# CrUX origins (HTTP and HTTPS together)
elif site_type == "popular":
df = pd.read_csv("helpers/202302.csv")
for bucket, sample_size in buckets:
origins = df.loc[df["rank"] == bucket].head(sample_size)
with ThreadPool(workers) as pool:
add_origin_f = partial(add_origin, t_list, site_type, bucket)
pool.map(add_origin_f, origins["origin"])
else:
raise Exception(f"No valid type: {site_type}")
def random_string(length=32):
"""Generate a random string to be used in URL (/<rand>)."""
return "".join(
random.choice(string.ascii_lowercase + string.digits) for _ in range(length)
)
def get_test_urls(full_url, path, scheme, timeout=10):
"""Generate all test URLs from one URL (landing page)."""
# Additional paths that should get tested
urls = [(full_url, path, "Base URL")]
random.seed(full_url)
random_path = random_string(length=32)
urls.append((f"{full_url}{random_path}", f"/{random_path}", "non-existing"))
try:
r_url = httpx.get(
full_url, follow_redirects=True, timeout=timeout, verify=False
).url
# Add non redirecting URL, if base URL redirects; Only if same-origin
if full_url != r_url:
if r_url.host == httpx.URL(full_url).host and r_url.scheme == scheme:
urls.append((r_url, r_url.path, "non redirecting URL"))
# print(r_url)
except Exception as e:
print(f"URL error: {full_url} - {e}")
return urls
def parse_headers(header_s):
"""String to MITMProxy Header object."""
return Headers(literal_eval(header_s))
def report_error(s):
"""Report message using MM hook."""
print(s)
n = 1
while n <= 3:
try:
httpx.request(
method="POST",
url=WEB_HOOK,
json={"text": s, "username": "HTTP testing"},
timeout=30,
)
break
except Exception as e:
n += 1
print(f"Mattermost Hook failed: {e}")
# All probes used to send PROBE requests
probes = {}
probe_methods = {
1: "GET",
2: "HEAD",
3: "POST",
4: "OPTIONS",
5: "DELETE",
6: "PUT",
7: "TRACE",
8: "PATCH",
9: "ABC",
}
probe_headers = {
1: {"user-agent": user_agent, "cache-control": "no-store"},
2: {
"user-agent": user_agent,
"cache-control": "no-store",
"upgrade-insecure-requests": "1",
},
3: {"user-agent": user_agent, "cache-control": "no-store", "Range": "bytes=0-10"},
4: {"user-agent": user_agent, "cache-control": "no-store", "If-Match": "*"},
5: {"user-agent": user_agent, "cache-control": "no-store", "If-None-Match": "*"},
6: {
"user-agent": user_agent,
"cache-control": "no-store",
"If-Modified-Since": format_datetime(datetime.now(timezone.utc), usegmt=True),
},
7: {
"user-agent": user_agent,
"cache-control": "no-store",
"If-Range": format_datetime(datetime.now(timezone.utc), usegmt=True),
},
8: {
"user-agent": user_agent,
"cache-control": "no-store",
"Early-Data": "1",
}, # Valid early data
9: {
"user-agent": user_agent,
"cache-control": "no-store",
"Early-Data": "Abc",
}, # Invalid early data
10: {
"user-agent": user_agent,
"cache-control": "no-store",
"Connection": "close",
}, # Connection close (default of HTTPX is keep-alive)
}
probe_http2 = {
1: False, # HTTP/1.1
2: True, # HTTP/2
}
for m_i, method in probe_methods.items():
for h_i, headers in probe_headers.items():
for h2_i, h2 in probe_http2.items():
probes[(m_i, h_i, h2_i)] = (method, headers, h2)