-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddon.py
More file actions
127 lines (120 loc) · 4.7 KB
/
addon.py
File metadata and controls
127 lines (120 loc) · 4.7 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
# pylint: disable=missing-module-docstring,missing-function-docstring,missing-class-docstring
# pylint: disable=invalid-name,consider-using-f-string,line-too-long
import re
import os
import threading
import subprocess
from urllib import parse
from mitmproxy import http
import CurrOS
class addon:
def __init__(self):
self.data_path = os.path.join(CurrOS.appdata_path, "n0", "AnyWhere")
self.iframe_src = []
if not os.path.exists(self.data_path):
os.makedirs(self.data_path)
# MARK: Scripts Loading
# TODO: Change Loading whole js to many single ones
def _load_scripts(self, url):
scripts = [
filename
for filename in os.listdir(self.data_path)
if filename.endswith(".user.js")
]
formatted_scripts = ""
for script in scripts:
with open(os.path.join(self.data_path,script), "r", encoding="utf-8") as f:
fcontent=f.read()
if "@match" not in fcontent:
formatted_scripts += "<script>" + fcontent + "</script>"
else:
for i in fcontent.splitlines():
if i == "// ==/UserScript==":
break
if "@match" in i:
if re.match(
re.compile(
re.escape(i.split("@match").strip()).replace(
"\\*", ".*"
)
),
url,
):
formatted_scripts += "<script>" + fcontent + "</script>"
break
return formatted_scripts
# MARK: Script Generating
def request(self, flow: http.HTTPFlow):
if flow.request.host == "any.where":
if flow.request.path_components[-1] == "userscript":
flow.response = http.Response.make(
200,
bytes(
self._load_scripts(parse.unquote(flow.request.query)),
encoding="utf-8",
),
{"Content-Type": "text/javascript"},
)
elif flow.request.url.endswith(".user.js"):
flow.request.headers["Cache-Control"] = "no-cache"
flow.request.headers["Pragma"] = "no-cache"
flow.request.headers["If-Modified-Since"] = "0"
# MARK: Response
def response(self, flow: http.HTTPFlow):
if flow.response.headers.get("content-type", "").startswith("text/html"):
# MARK: Iframe dealing
iframe_src = re.findall(
r'<iframe[^>]*src="([^"]+)"[^>]*>',
flow.response.get_text(),
)
if flow.request.url in self.iframe_src:
self.iframe_src.remove(flow.request.url)
else:
flow.response.set_text(
re.sub(
r"</head>",
f"{self._load_scripts(flow.request.url)}</head>",
flow.response.get_text(),
count=1,
)
)
self.iframe_src += iframe_src
for i in iframe_src:
threading.Timer(
60,
lambda j=i: (
self.iframe_src.remove(j) if j in self.iframe_src else None
),
).start()
# MARK: Script Installing
elif flow.request.url.endswith(".user.js"):
script_file_name = flow.request.path_components[-1]
script_name = (
flow.response.get_text().split("@name")[1].split("\n")[0].strip()
if "@name" in flow.response.get_text()
else script_file_name
)
os.makedirs(os.path.join(os.environ["TEMP"], "anywhere"), exist_ok=True)
with open(
os.path.join(os.environ["TEMP"], "anywhere", script_file_name),
"w",
encoding="utf-8",
) as f:
f.write(flow.response.get_text())
if os.name == "nt":
# TODO: script logo
subprocess.Popen(
[
"powershell",
"-ep",
"Bypass",
"-File",
"win/toast.ps1",
os.getcwd(),
script_name,
script_file_name,
]
)
else:
pass
addons = [addon()]