-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.py
executable file
·217 lines (159 loc) · 6.25 KB
/
build.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
#!/usr/bin/env python
import argparse
import collections
import csv
import datetime
import geoip2.database
import hashlib
import jinja2
import json
import logging
import os
import pprint
import requests
import socket
import typing
import urllib.request
import whois
HATE_SITES_CSV_DEFAULT_PATH = 'hate-sites.csv'
OUTPUT_DIR = 'build'
ASN_NAME_MAP = {
15169: "Google",
26347: "DreamHost",
36647: "Oath (Yahoo)",
54113: "Fastly",
}
def log_info(site: str, s: str):
logging.info(f"{site} - {s}")
def log_error(site: str, s: str):
logging.error(f"{site} - {s}")
class Asn(typing.NamedTuple):
name: str
number: int
def site_isp(site: str) -> typing.Optional[Asn]:
# log_info(site, f"Domain registrar: {whois.query(site).registrar}")
try:
ip = socket.gethostbyname(site)
except socket.gaierror:
log_error(site, "Could not DNS resolve")
return
with geoip2.database.Reader('GeoLite2-ASN.mmdb') as reader:
try:
response = reader.asn(ip)
except geoip2.errors.AddressNotFoundError:
log_error(site, "Could not find address in GeoLite2 DB")
return
isp = (
ASN_NAME_MAP.get(response.autonomous_system_number) or
asn_name(response.autonomous_system_number) or
response.autonomous_system_organization
)
log_info(site, f"Found ISP: {isp}")
return Asn(name=isp, number=response.autonomous_system_number)
def asn_name(asn_id: int) -> typing.Optional[str]:
# https://www.peeringdb.com/apidocs/#operation/retrieve%20net
url = f"https://peeringdb.com/api/net?asn={asn_id}"
response_json = requests.get(url).json()
if not response_json['data']:
return
return response_json['data'][0]['name']
def sites(limit=None) -> [[str, str, str]]:
with open(args.hate_sites_csv_path) as f:
reader = csv.reader(f)
next(reader) # Skip the heading row
if limit:
return list(reader)[:limit]
else:
return list(reader)
def build_isps_data(limit=None):
isps = collections.defaultdict(lambda: [])
for site, classification, _, page_string in sites(limit=limit):
isp = site_isp(site)
if isp is None:
continue
hate_site_response = HateSiteLoader(domain=site).load()
is_site_up = isinstance(
HateSiteResponseAnalyzer(response=hate_site_response, page_string=page_string).analyze(),
HateSiteResponseSiteUp
)
if classification != 'splc' and classification != 'islamophobia':
classification = None
isps[isp].append([mask_site(site), is_site_up, classification])
return sorted(isps.items(), key=lambda x: len(x[1]), reverse=True)
def mask_site(site: str) -> str:
domain = site.split(".")[-1]
num_asterisks = len(site) - len(domain) - 2
asterisks = "•" * num_asterisks
return f"{site[0]}{asterisks}.{domain}"
def todays_date() -> str:
return datetime.datetime.now().strftime("%B %d, %Y")
CHROME_USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'
REQUEST_HEADERS = {'User-Agent': CHROME_USER_AGENT}
class HateSiteErrorResponse(typing.NamedTuple):
reason: str
status_code: typing.Optional[int]
class HateSiteResponse(typing.NamedTuple):
body: bytes
status_code: int
class HateSiteLoader(typing.NamedTuple):
domain: str
def load(self) -> typing.Union[HateSiteResponse, HateSiteErrorResponse]:
url = 'http://' + self.domain
request = urllib.request.Request(url, headers=REQUEST_HEADERS)
try:
response = urllib.request.urlopen(request, timeout=10)
except urllib.error.HTTPError as error:
return HateSiteErrorResponse(reason=str(error.reason), status_code=error.code)
except urllib.error.URLError as error:
return HateSiteErrorResponse(reason=str(error.reason), status_code=None)
except (TimeoutError, socket.error):
return HateSiteErrorResponse(reason="Timeout", status_code=None)
except ConnectionResetError as error:
return HateSiteErrorResponse(reason="Connection reset", status_code=None)
return HateSiteResponse(body=response.read(), status_code=response.status)
class HateSiteResponseSiteUp:
pass
class HateSiteResponsePageStringNotFound:
pass
class HateSiteResponseSiteDown(typing.NamedTuple):
status_code: typing.Optional[int]
reason: str
class HateSiteResponseAnalyzer(typing.NamedTuple):
response: typing.Union[HateSiteResponse, HateSiteErrorResponse]
page_string: str
def analyze(self) -> typing.Union[HateSiteResponseSiteUp, HateSiteResponsePageStringNotFound, HateSiteResponseSiteDown]:
if isinstance(self.response, HateSiteResponse):
if self.page_string.encode() in self.response.body:
return HateSiteResponseSiteUp()
else:
return HateSiteResponsePageStringNotFound()
elif self.response.status_code:
return HateSiteResponseSiteDown(status_code=self.response.status_code, reason=self.response.reason)
else:
return HateSiteResponseSiteDown(status_code=None, reason=self.response.reason)
def render(limit=None):
env = jinja2.Environment(
loader=jinja2.FileSystemLoader('templates'),
)
template = env.get_template('index.html.j2')
if not os.path.exists(OUTPUT_DIR):
os.mkdir(OUTPUT_DIR)
with open(os.path.join(OUTPUT_DIR, 'index.html'), 'w') as f:
f.write(template.render(
isps_data=build_isps_data(limit=limit),
))
template = env.get_template('faqs.html.j2')
with open(os.path.join(OUTPUT_DIR, 'faqs.html'), 'w') as f:
f.write(template.render(
todays_date=todays_date(),
))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--hate-sites-csv-path', default=HATE_SITES_CSV_DEFAULT_PATH)
parser.add_argument('--log', action='store_true')
parser.add_argument('--limit', type=int, help='Limit the number of sites to process')
args = parser.parse_args()
logging.basicConfig(level=logging.INFO)
if not args.log:
logging.disable(logging.CRITICAL)
render(limit=args.limit)