-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
daemon_get_masto_api.py
320 lines (303 loc) · 12.4 KB
/
daemon_get_masto_api.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
__filename__ = "daemon_get_masto_api.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.5.0"
__maintainer__ = "Bob Mottram"
__email__ = "[email protected]"
__status__ = "Production"
__module_group__ = "Core GET"
import json
from httpheaders import set_headers
from httpcodes import write2
from mastoapiv1 import masto_api_v1_response
from mastoapiv2 import masto_api_v2_response
from siteactive import referer_is_active
from httpcodes import http_400
from httpcodes import http_404
from httpcodes import http_503
from utils import get_json_content_from_accept
from utils import convert_domains
from utils import local_network_host
from crawlers import update_known_crawlers
from blocking import broch_mode_is_active
from daemon_utils import has_accept
def masto_api(self, path: str, calling_domain: str,
ua_str: str,
authorized: bool, http_prefix: str,
base_dir: str, nickname: str, domain: str,
domain_full: str,
onion_domain: str, i2p_domain: str,
translate: {},
registration: bool,
system_language: str,
project_version: str,
custom_emoji: [],
show_node_info_accounts: bool,
referer_domain: str, debug: bool,
known_crawlers: {},
sites_unavailable: [],
unit_test: bool,
allow_local_network_access: bool) -> bool:
if _masto_api_v2(self, path, calling_domain, ua_str, authorized,
http_prefix, base_dir, nickname, domain,
domain_full, onion_domain, i2p_domain,
translate, registration, system_language,
project_version,
show_node_info_accounts,
referer_domain, debug, 5,
known_crawlers, sites_unavailable, unit_test,
allow_local_network_access):
return True
return _masto_api_v1(self, path, calling_domain, ua_str, authorized,
http_prefix, base_dir, nickname, domain,
domain_full, onion_domain, i2p_domain,
translate, registration, system_language,
project_version, custom_emoji,
show_node_info_accounts,
referer_domain, debug, 5,
known_crawlers, sites_unavailable,
unit_test,
allow_local_network_access)
def _masto_api_v1(self, path: str, calling_domain: str,
ua_str: str,
authorized: bool,
http_prefix: str,
base_dir: str, nickname: str, domain: str,
domain_full: str,
onion_domain: str, i2p_domain: str,
translate: {},
registration: bool,
system_language: str,
project_version: str,
custom_emoji: [],
show_node_info_accounts: bool,
referer_domain: str,
debug: bool,
calling_site_timeout: int,
known_crawlers: {},
sites_unavailable: [],
unit_test: bool,
allow_local_network_access: bool) -> bool:
"""This is a vestigil mastodon API for the purpose
of returning an empty result to sites like
https://mastopeek.app-dist.eu
"""
if not path.startswith('/api/v1/'):
return False
if not referer_domain:
if not (debug and unit_test):
print('mastodon api request has no referer domain ' +
str(ua_str))
http_400(self)
return True
if referer_domain == domain_full:
print('mastodon api request from self')
http_400(self)
return True
if self.server.masto_api_is_active:
print('mastodon api is busy during request from ' +
referer_domain)
http_503(self)
return True
self.server.masto_api_is_active = True
# is this a real website making the call ?
if not debug and not unit_test and referer_domain:
# Does calling_domain look like a domain?
if ' ' in referer_domain or \
';' in referer_domain or \
'.' not in referer_domain:
print('mastodon api ' +
'referer does not look like a domain ' +
referer_domain)
http_400(self)
self.server.masto_api_is_active = False
return True
if not allow_local_network_access:
if local_network_host(referer_domain):
print('mastodon api referer domain is from the ' +
'local network ' + referer_domain)
http_400(self)
self.server.masto_api_is_active = False
return True
if not referer_is_active(http_prefix,
referer_domain, ua_str,
calling_site_timeout,
sites_unavailable):
print('mastodon api referer url is not active ' +
referer_domain)
http_400(self)
self.server.masto_api_is_active = False
return True
print('mastodon api v1: ' + path)
print('mastodon api v1: authorized ' + str(authorized))
print('mastodon api v1: nickname ' + str(nickname))
print('mastodon api v1: referer ' + str(referer_domain))
crawl_time = \
update_known_crawlers(ua_str, base_dir,
known_crawlers,
self.server.last_known_crawler)
if crawl_time is not None:
self.server.last_known_crawler = crawl_time
broch_mode = broch_mode_is_active(base_dir)
send_json, send_json_str = \
masto_api_v1_response(path,
calling_domain,
ua_str,
authorized,
http_prefix,
base_dir,
nickname, domain,
domain_full,
onion_domain,
i2p_domain,
translate,
registration,
system_language,
project_version,
custom_emoji,
show_node_info_accounts,
broch_mode)
if send_json is not None:
msg_str = json.dumps(send_json)
msg_str = convert_domains(calling_domain, referer_domain,
msg_str, http_prefix, domain,
onion_domain, i2p_domain)
msg = msg_str.encode('utf-8')
msglen = len(msg)
if has_accept(self, calling_domain):
protocol_str = \
get_json_content_from_accept(self.headers.get('Accept'))
set_headers(self, protocol_str, msglen,
None, calling_domain, True)
else:
set_headers(self, 'application/ld+json', msglen,
None, calling_domain, True)
write2(self, msg)
if send_json_str:
print(send_json_str)
self.server.masto_api_is_active = False
return True
# no api endpoints were matched
http_404(self, 1)
self.server.masto_api_is_active = False
return True
def _masto_api_v2(self, path: str, calling_domain: str,
ua_str: str,
authorized: bool,
http_prefix: str,
base_dir: str, nickname: str, domain: str,
domain_full: str,
onion_domain: str, i2p_domain: str,
translate: {},
registration: bool,
system_language: str,
project_version: str,
show_node_info_accounts: bool,
referer_domain: str,
debug: bool,
calling_site_timeout: int,
known_crawlers: {},
sites_unavailable: [],
unit_test: bool,
allow_local_network_access: bool) -> bool:
"""This is a vestigil mastodon v2 API for the purpose
of returning an empty result to sites like
https://mastopeek.app-dist.eu
"""
if not path.startswith('/api/v2/'):
return False
if not referer_domain:
if not (debug and unit_test):
print('mastodon api v2 request has no referer domain ' +
str(ua_str))
http_400(self)
return True
if referer_domain == domain_full:
print('mastodon api v2 request from self')
http_400(self)
return True
if self.server.masto_api_is_active:
print('mastodon api v2 is busy during request from ' +
referer_domain)
http_503(self)
return True
self.server.masto_api_is_active = True
# is this a real website making the call ?
if not debug and not unit_test and referer_domain:
# Does calling_domain look like a domain?
if ' ' in referer_domain or \
';' in referer_domain or \
'.' not in referer_domain:
print('mastodon api v2 ' +
'referer does not look like a domain ' +
referer_domain)
http_400(self)
self.server.masto_api_is_active = False
return True
if not allow_local_network_access:
if local_network_host(referer_domain):
print('mastodon api v2 referer domain is from the ' +
'local network ' + referer_domain)
http_400(self)
self.server.masto_api_is_active = False
return True
if not referer_is_active(http_prefix,
referer_domain, ua_str,
calling_site_timeout,
sites_unavailable):
print('mastodon api v2 referer url is not active ' +
referer_domain)
http_400(self)
self.server.masto_api_is_active = False
return True
print('mastodon api v2: ' + path)
print('mastodon api v2: authorized ' + str(authorized))
print('mastodon api v2: nickname ' + str(nickname))
print('mastodon api v2: referer ' + str(referer_domain))
crawl_time = \
update_known_crawlers(ua_str, base_dir,
known_crawlers,
self.server.last_known_crawler)
if crawl_time is not None:
self.server.last_known_crawler = crawl_time
broch_mode = broch_mode_is_active(base_dir)
send_json, send_json_str = \
masto_api_v2_response(path,
calling_domain,
ua_str,
http_prefix,
base_dir,
domain,
domain_full,
onion_domain,
i2p_domain,
translate,
registration,
system_language,
project_version,
show_node_info_accounts,
broch_mode)
if send_json is not None:
msg_str = json.dumps(send_json)
msg_str = convert_domains(calling_domain, referer_domain,
msg_str, http_prefix, domain,
onion_domain, i2p_domain)
msg = msg_str.encode('utf-8')
msglen = len(msg)
if has_accept(self, calling_domain):
protocol_str = \
get_json_content_from_accept(self.headers.get('Accept'))
set_headers(self, protocol_str, msglen,
None, calling_domain, True)
else:
set_headers(self, 'application/ld+json', msglen,
None, calling_domain, True)
write2(self, msg)
if send_json_str:
print(send_json_str)
self.server.masto_api_is_active = False
return True
# no api v2 endpoints were matched
http_404(self, 2)
self.server.masto_api_is_active = False
return True