-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsfw.py
83 lines (69 loc) · 3.22 KB
/
nsfw.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
#!/user/bin/env python3.12
import subprocess
from mitmproxy import http
from mitmproxy import ctx
import tempfile
import json
from constants.blacklist import blacklist
from utils.get_random_image import getRandomImage
import re
def checkNSFWPredictions(predictions, level):
isNSFW = False;
for prediction in predictions:
category = prediction['category']
if (category == 'hentai' or category == 'porn' or category == 'sexy'):
isNSFW = prediction['probability'] > level
if (isNSFW): break
return isNSFW
class NSFWDetector:
def load(self, loader):
loader.add_option(
name="command",
typespec=str,
help="You will want that this command run when we wanted to classify image with IA. It should include <dir>.",
default=""
)
loader.add_option(
name="level",
typespec=str,
help="Depending of the command you use to classify the image, you will get different measure types. Add here the min value accepted to consider it nsfw.",
default="0.3"
)
def request(self, flow: http.HTTPFlow) -> None:
urlProtocolRegExp = "(https|http)?(://)?(www\.)?[/]*"
# Some ads add link of target website in the referrer header. So we can block ads for specific pages
referer_url = re.sub(urlProtocolRegExp, "", (flow.request.headers.get('Referer') or ""))
site_url = re.sub(urlProtocolRegExp, "", flow.request.pretty_host)
flow.request.headers["x-blacklisted-site"] = str(site_url in blacklist or (len(referer_url) > 0 and referer_url in blacklist))
def response(self, flow: http.HTTPFlow) -> None:
if (flow.response.headers.get("Content-Type", "").startswith("video")):
if (flow.request.headers.get('x-blacklisted-site') == 'True'):
flow.response.content = None
flow.response.status_code = 403
flow.response.reason = b"Forbidden"
return
if (flow.response.headers.get("Content-Type", "").startswith("image")):
if (flow.request.headers.get('x-blacklisted-site') == 'True'):
flow.response.content = getRandomImage();
flow.response.status_code = 403
flow.response.headers["content-type"] = "image/jpg"
return
if (len(ctx.options.command) == 0):
return
with tempfile.NamedTemporaryFile(delete_on_close=True,delete=True) as tempFile:
tempFile.write(flow.response.content);
level = float(ctx.options.level)
command = ctx.options.command.replace('<dir>', tempFile.name);
commandArr = command.split(' ');
result = subprocess.run(commandArr, capture_output=True);
if (result.stderr):
print("Error processing image: ", result.stderr)
return
if (result.stdout):
jsonResult = json.loads(result.stdout)
print('level: ', level)
isNSFW = jsonResult['has_nudity'] == True or checkNSFWPredictions(jsonResult['predictions'], level)
if (isNSFW):
flow.response.content = getRandomImage();
flow.response.headers["content-type"] = "image/jpg"
addons = [NSFWDetector()]