This repository has been archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
389 lines (345 loc) · 12.6 KB
/
main.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import os
import uvicorn
import json
import requests
from yarl import URL
from fastapi import FastAPI, Request, Response, status, Form, HTTPException
from starlette.exceptions import HTTPException as StarletteHTTPException
from fastapi.responses import RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from typing import Optional
from pydantic import BaseModel
from deta import Deta
from dotenv import load_dotenv
from datetime import datetime
load_dotenv()
DETA_TOKEN = os.getenv("DETA_TOKEN")
app = FastAPI(docs_url=None, redoc_url=None)
deta = Deta(DETA_TOKEN)
templates = Jinja2Templates(directory="templates")
# databases
blacklist_db = deta.Base("api-blacklist")
reports_db = deta.Base("api-reports")
app.mount("/assets", StaticFiles(directory="templates/assets"), name="assets")
"""
----------------------------------------------------------
BASE MODELS
----------------------------------------------------------
"""
class ReportItem(BaseModel):
domain: str
description: str
"""
----------------------------------------------------------
FUNCTIONS
----------------------------------------------------------
"""
def add_report(domain, description, falsepositive: bool, response):
if falsepositive:
report_type = "false-positive"
else:
report_type = "report"
try:
if "http://" in domain or "https://" in domain:
domain = URL(domain)
domain = domain.host.replace("www.", "")
except IndexError:
pass
blacklist_status = blacklist_db.fetch({"blacklisted": True}).items
blacklist_domain = blacklist_db.fetch({"domain": domain}).items
res = requests.get("https://api.stopmodreposts.org/sites.txt")
if report_type == "false-positive":
if domain in res.text:
reports_db.put({
"domain": domain,
"type": report_type,
"description": description,
"timestamp": str(datetime.now()),
"reviewed": False,
"comment": ""
})
return response, {
"detail": "Success!",
"already_listed": False,
"under_review": False,
"blacklist": False,
"data": {
"domain": domain,
"description": description,
"false-positive": falsepositive
}
}
else:
response.status_code = status.HTTP_409_CONFLICT
return response, {
"detail": "Failed to report - domain not listed",
"already_listed": False,
"under_review": False,
"blacklist": False,
"data": {
"domain": domain,
"description": description,
"false-positive": falsepositive
}
}
if domain in res.text:
response.status_code = status.HTTP_409_CONFLICT
return response, {
"detail": "Failed to report - domain already listed",
"already_listed": True,
"under_review": False,
"blacklist": False,
"data": {
"domain": domain,
"description": description,
"false-positive": falsepositive
}
}
elif len(blacklist_status) != 0 and len(blacklist_domain) != 0:
response.status_code = status.HTTP_400_BAD_REQUEST
return response, {
"detail": "Failed to report - domain blacklisted",
"already_listed": False,
"under_review": False,
"blacklist": True,
"data": {
"domain": domain,
"description": description,
"false-positive": falsepositive
}
}
elif len(reports_db.fetch({"domain": domain}).items) != 0:
response.status_code = status.HTTP_409_CONFLICT
return response, {
"detail": "Failed to report - domain already on waitinglist",
"already_listed": False,
"under_review": True,
"blacklist": False,
"data": {
"domain": domain,
"description": description,
"false-positive": falsepositive
}
}
else:
reports_db.put({
"domain": domain,
"type": report_type,
"description": description,
"timestamp": str(datetime.now()),
"reviewed": False,
"comment": ""
})
return response, {
"detail": "Success!",
"already_listed": False,
"under_review": False,
"blacklist": False,
"data": {
"domain": domain,
"description": description,
"false-positive": falsepositive
}
}
def get_alert_html(alert_type, title, message):
if alert_type == "success":
with open("templates/components/success-alert.html", "r") as f:
alert_html = f.read()
elif alert_type == "error":
with open("templates/components/error-alert.html", "r") as f:
alert_html = f.read()
else:
alert_html = """"""
if len(alert_html) != 0:
alert_html = alert_html.format(title, message)
return alert_html
def verifycaptcha(response: str):
"""
Verifies that hCaptcha result is valid
"""
data = {"secret": str(os.getenv("CAPTCHA_SECRET")), "response": response}
r = requests.post("https://hcaptcha.com/siteverify", data=data)
if r.status_code == 200 and json.loads(r.text)["success"] == True:
return True
else:
return False
"""
----------------------------------------------------------
WEB ENDPOINTS
----------------------------------------------------------
"""
@app.get("/")
def get_root(request: Request):
return templates.TemplateResponse("index.html", {
"request": request
})
@app.get("/docs")
def get_docs():
return RedirectResponse(url="https://docs.stopmodreposts.org", status_code=status.HTTP_303_SEE_OTHER)
@app.get("/forms/report")
def get_form_report(request: Request,
alert: Optional[str] = None):
if alert == "success":
alert_html = get_alert_html(
alert_type="success",
title="Submission received",
message="Your submission was received. It might take a few weeks until your site is added."
)
elif alert == "blocked":
alert_html = get_alert_html(
alert_type="error",
title="Submission failed",
message="Your submission failed because the site you're trying to submit is not allowed to be submitted."
)
elif alert == "listed":
alert_html = get_alert_html(
alert_type="error",
title="Submission failed",
message="Your submission failed because the site you're trying to submit is already listed or under review."
)
elif alert == "captcha":
alert_html = get_alert_html(
alert_type="error",
title="Submission failed",
message="You have to complete the CAPTCHA challenge first."
)
else:
alert_html = """"""
return templates.TemplateResponse("report.html", {
"request": request,
"alert_html": alert_html
})
@app.get("/forms/falsepositive")
def get_form_falsepositive(request: Request,
alert: Optional[str] = None):
if alert == "success":
alert_html = get_alert_html(
alert_type="success",
title="Submission received",
message="Your submission was received. It might take a few weeks until your site is added."
)
elif alert == "blocked":
alert_html = get_alert_html(
alert_type="error",
title="Submission failed",
message="Your submission failed because the site you're trying to submit is not allowed to be submitted."
)
elif alert == "listed":
alert_html = get_alert_html(
alert_type="error",
title="Submission failed",
message="Your submission failed because the site you're trying to submit is not listed."
)
elif alert == "captcha":
alert_html = get_alert_html(
alert_type="error",
title="Submission failed",
message="You have to complete the CAPTCHA challenge first."
)
else:
alert_html = """"""
return templates.TemplateResponse("falsepositive.html", {
"request": request,
"alert_html": alert_html
})
@app.get("/pages/progress")
def get_form_falsepositive(request: Request):
len_all = len(reports_db.fetch().items)
len_reviewed = len(reports_db.fetch({"reviewed": True}).items)
reviewed_percent = int((len_reviewed / len_all) * 100)
return templates.TemplateResponse("progress.html", {
"request": request,
"len_all": len_all,
"len_reviewed": len_reviewed,
"reviewed_percent": reviewed_percent
})
"""
----------------------------------------------------------
API ENDPOINTS
----------------------------------------------------------
"""
@app.get("/api/v1/waitlist")
def get_api_waitlist():
res = reports_db.fetch({"reviewed": False}).items
final = []
for site in res:
final.append({
"domain": site["domain"],
"type": site["type"],
"timestamp": site["timestamp"]
})
return final
@app.get("/api/v1/blacklist")
def get_api_blacklist():
res = blacklist_db.fetch({"blacklisted": True}).items
final = []
for site in res:
final.append({
"domain": site["domain"]
})
return final
@app.post("/api/v1/report", status_code=201)
def post_api_report(response: Response,
item: ReportItem,
falsepositive: Optional[bool] = False):
"""
Reporting endpoint for reposting sites and false-positives
"""
response, res = add_report(
domain=item.domain,
description=item.description,
falsepositive=falsepositive,
response=response
)
return res
@app.post("/api/v1/formreport", status_code=201)
def post_api_report(response: Response,
falsepositive: Optional[bool] = False,
domain: str = Form(None),
description: str = Form(None),
captcha: str = Form(None, alias="h-captcha-response")):
"""
Reporting endpoint for reposting sites and false-positives
"""
if falsepositive:
baseURL = "/forms/falsepositive"
else:
baseURL = "/forms/report"
if captcha is None:
return RedirectResponse(url=f"{baseURL}?alert=captcha", status_code=status.HTTP_303_SEE_OTHER)
else:
if verifycaptcha(captcha) is False:
return RedirectResponse(url=f"{baseURL}?alert=captcha", status_code=status.HTTP_303_SEE_OTHER)
if domain == None or description == None:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Required form fields missing")
response, res = add_report(
domain=domain,
description=description,
falsepositive=falsepositive,
response=response
)
if response.status_code == status.HTTP_409_CONFLICT:
return RedirectResponse(f"{baseURL}?alert=listed", status_code=status.HTTP_303_SEE_OTHER)
elif response.status_code == status.HTTP_400_BAD_REQUEST:
return RedirectResponse(f"{baseURL}?alert=blocked", status_code=status.HTTP_303_SEE_OTHER)
else:
return RedirectResponse(f"{baseURL}?alert=success", status_code=status.HTTP_303_SEE_OTHER)
"""
----------------------------------------------------------
RUNNER & ERROR HANDLER
----------------------------------------------------------
"""
@app.exception_handler(StarletteHTTPException)
async def my_custom_exception_handler(request: Request, exc: StarletteHTTPException):
"""
Handles exceptions and redirects to correct error page.
"""
if exc.status_code == 404:
return templates.TemplateResponse("error.html", {"request": request, "code": "404", "description": "The requested resource couldn't be found."})
elif exc.status_code == 500:
return templates.TemplateResponse("error.html", {"request": request, "code": "500", "description": exc.detail})
else:
return templates.TemplateResponse('error.html', {"request": request, "code": "Error", "description": exc.detail})
if __name__ == "__main__":
uvicorn.run(app, host="localhost", port=8000)