-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
153 lines (124 loc) · 5.03 KB
/
main.py
File metadata and controls
153 lines (124 loc) · 5.03 KB
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
import re
import zstd
import gzip
import brotli
import asyncio
from mitmproxy import http
from mitmproxy.tools.dump import DumpMaster
BANNED_HOST = [
"ad.mail.ru",
"stats.vk-portal.net",
"top-fwz1.mail.ru",
"egress.yandex.net",
"googleads.g.doubleclick.net"
]
BANNED_URL = [
"yandex.ru/ads",
"yandex.ru/clck/safeclick",
"mc.yandex.ru/watch",
"yandex.ru/search/zero",
"yastatic.net/nearest.js",
"yandex.ru/ick/r",
"yandex.ru/metrika",
]
STREAM_TRUE = {
"image", "webm", "svg"
"video", "mp4", "mp2t", "wasm", "vnd.yt-ump",
"audio",
"octet-stream", "font", "css",
}
STREAM_FALSE = {
"html", "xml",
"javascript", "js",
"json", "text"
}
DECOMPRESSORS = {
'gzip': (gzip.decompress, gzip.compress),
'deflate': (gzip.decompress, gzip.compress),
'br': (brotli.decompress, brotli.compress),
'zstd': (zstd.decompress, zstd.compress)
}
host_regex = re.compile(f"^(?:{'|'.join(re.escape(i) for i in BANNED_HOST)})(?:/|$)", re.IGNORECASE)
substr_regex = re.compile(f"({'|'.join(re.escape(i) for i in BANNED_URL)})", re.IGNORECASE)
class CustomHandlers:
async def requestheaders(self, flow: http.HTTPFlow):
hostis = host_regex.search(flow.request.host)
if hostis is not None:
print(f"Banned from HOST: {hostis.group()}")
flow.response = http.Response.make(200, b"Banned from HOST filter<br>by DestroyerMITM", {"Content-Type": "text/plain"})
return
pathis = substr_regex.search(flow.request.host + flow.request.path)
if pathis is not None:
print(f"Banned from PATH: {pathis.group()}")
flow.response = http.Response.make(200, b"Banned from PATH filter\n\nby DestroyerMITM", {"Content-Type": "text/plain"})
return
async def request(self, flow: http.HTTPFlow):
1
async def responseheaders(self, flow: http.HTTPFlow):
ctype = flow.response.headers.get("content-type", "").lower()
for t in STREAM_FALSE:
if t in ctype:
flow.response.stream = False
return
for t in STREAM_TRUE:
if t in ctype:
flow.response.stream = True
return
if ctype:
print(f"НЕ ОПРЕДЛЕННЫЙ ТИП -> {ctype}")
clen = flow.response.headers.get("content-length")
if clen and int(clen) > 800000:
flow.response.stream = True
return
async def response(self, flow: http.HTTPFlow):
if not flow.response.raw_content:
return
ce = flow.response.headers.get("content-encoding", "").lower()
if ce in DECOMPRESSORS:
decompress, compress = DECOMPRESSORS[ce]
content = decompress(flow.response.raw_content)
elif flow.response.raw_content[:3] == b"\x1f\x8b\x08": # gzip
decompress, compress = DECOMPRESSORS["gzip"]
content = decompress(flow.response.raw_content)
elif flow.response.raw_content[:4] == b"(\xb5/\xfd": # zstd
decompress, compress = DECOMPRESSORS["zstd"]
content = decompress(flow.response.raw_content)
elif flow.response.raw_content[:4] == b"\x28\xb5\x2f\xfd": # zstd
decompress, compress = DECOMPRESSORS["zstd"]
content = decompress(flow.response.raw_content)
else:
compress = lambda raw_content: raw_content
content = flow.response.raw_content
contentL = content.lower()
if contentL.startswith(b"<vast") and contentL.endswith(b"/vast>") and b"![cdata" in contentL:
flow.response.raw_content = b"Banned vastAD content<br>by DestroyerMITM"
return
if flow.request.host == "www.softportal.com" and flow.request.path.startswith("/getsoft"):
content = content.replace(b"Download();", b"")
content = content.replace(b"var timeEnd = 10;", b"var timeEnd = 0;location.href = url;setTimeout('location.href = backUrl', 2000);")
flow.response.raw_content = compress(content)
return
if b"src=\"https://yandex.ru/ads/system/context.js\"" in contentL:
content = content.replace(b'src="https://yandex.ru/ads/system/context.js"', b'src=""')
flow.response.raw_content = compress(content)
return
async def start_mitmproxy():
loop = asyncio.get_event_loop()
master = DumpMaster(None, loop=loop, with_termlog=True, with_dumper=False)
master.options.add_option(
"listen_host",
str,
"0.0.0.0",
"Address to bind proxy server(s) to (may be overridden for individual modes, see `mode`).",
)
master.options.add_option(
"listen_port",
int,
8989,
"Port to bind proxy server(s) to (may be overridden for individual modes, see `mode`). "
"By default, the port is mode-specific. The default regular HTTP proxy spawns on port 8080.",
)
master.addons.add(CustomHandlers())
await master.run()
if __name__ == "__main__":
asyncio.run(start_mitmproxy())