-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mts
269 lines (235 loc) · 9.15 KB
/
index.mts
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
import {
DOMParser
} from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";
console.log("[Created-By] @amex2189 / @EdamAme-x")
function isRelativePath(url: string): boolean {
try {
new URL(url);
return false;
} catch {
return true;
}
}
function convertRelativeToAbsolute(baseURL: string, relativeURL: string): string {
const absoluteURL = new URL(relativeURL, baseURL);
return absoluteURL.toString();
}
async function modifyHTMLContent(content: string, baseURL: string, proxyURL: string): Promise<string> {
const dom = new DOMParser().parseFromString(content, "text/html");
const aTags = dom.querySelectorAll("a");
aTags.forEach((a) => {
const href = a.getAttribute("href");
if (!href) return;
if (!href.startsWith("http")) {
const absoluteURL = convertRelativeToAbsolute(baseURL, href);
a.setAttribute("href", proxyURL + "/" + absoluteURL);
}else if (href.startsWith("http") || href.startsWith("//")) {
a.setAttribute("href", proxyURL + "/" + href);
}
});
const srcHrefs = dom.querySelectorAll("[src]");
srcHrefs.forEach((tag) => {
const href = tag.getAttribute("src");
if (!href) return;
if (!href.startsWith("http")) {
const absoluteURL = convertRelativeToAbsolute(baseURL, href);
tag.setAttribute("src", proxyURL + "/" + absoluteURL);
}else if (href.startsWith("http") || href.startsWith("//")) {
tag.setAttribute("src", proxyURL + "/" + href);
}
});
const actionHrefs = dom.querySelectorAll("[action]");
actionHrefs.forEach((tag) => {
const href = tag.getAttribute("action");
if (!href) return;
if (!href.startsWith("http")) {
const absoluteURL = convertRelativeToAbsolute(baseURL, href);
tag.setAttribute("action", proxyURL + "/" + absoluteURL);
}else if (href.startsWith("http") || href.startsWith("//")) {
tag.setAttribute("action", proxyURL + "/" + href);
}
});
let nonce = "";
try {
nonce = dom.querySelector("script").getAttribute("nonce") ?? "";
}catch {}
try {
dom.querySelectorAll("[http-equiv='content-security-policy']").forEach(el => {
el.remove()
})
}catch {}
const scriptTag =
`
<!-- DENO-X-PROXY -->
<script deno-x-proxy ${nonce !== "" && `nonce="${nonce}"`}>
console.log("%c[Created-By]", "color: #00cc00", "@amex2189 / @EdamAme-x")
window._open = window.open;
window.open = (target, ...args) => {
const proxyHostname = new URL(window.location.href).origin;
const proxyTarget = new URL(window.location.href).pathname.replace(/\\//, "");
if (target.startsWith("http") || target.startsWith("//")) {
const url = proxyHostname + "/" + target;
return window._open(url, ...args);
}else {
const url = proxyHostname + "/" + proxyTarget + "/" + target.replace(/^\\.*\\//, "");
return window._open(url, ...args);
}
}
/* FETCH */
window._fetch = window.fetch;
window.fetch = (target, ...args) => {
const proxyHostname = new URL(window.location.href).origin;
const proxyTarget = new URL(window.location.href).pathname.replace(/\\//, "");
if (target instanceof Request) {
const url = proxyHostname + "/" + target.url;
return window._fetch(url, {
headers: target.headers,
method: target.method,
body: target.body ?? null
})
}
if (target.startsWith("http") || target.startsWith("//")) {
const url = proxyHostname + "/" + target;
return window._fetch(url, ...args);
}else {
const url = proxyHostname + "/" + proxyTarget + "/" + target.replace(/^\\.*\\//, "");
return window._fetch(url, ...args);
}
}
/* XML-HTTP-REQUEST */
window._XMLHttpRequest = window.XMLHttpRequest;
window.XMLHttpRequest = function() {
const xhr = new window._XMLHttpRequest();
const proxyHostname = new URL(window.location.href).origin;
const proxyTarget = new URL(window.location.href).pathname.replace(/\\//, "");
this._open = xhr.open;
xhr.open = function(method, url, ...args) {
if (url instanceof URL) {
url = url.toString()
}
let newUrl = "";
if (url.startsWith("http") || url.startsWith("//")) {
newUrl = proxyHostname + "/" + url;
} else {
newUrl = proxyHostname + "/" + proxyTarget + "/" + url.replace(/^\\.*\\//, "");
}
return this._open(method, newUrl, ...args);
};
return this;
}
/* OBSERVER */
const url = new URL(window.location.href);
const proxyURL = url.origin;
const baseURL = new URL(url.pathname.replace(/\\//, "")).origin;
function normalizeURL(relativeURL) {
const trimmedRelativeURL = relativeURL.startsWith("/") ? relativeURL.slice(1) : relativeURL;
return baseURL + "/" + trimmedRelativeURL;
}
const observer = new MutationObserver((mutationsList, observer) => {
for (let mutation of mutationsList) {
if (mutation.type === "childList") {
document.body.querySelectorAll("*").forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
const aTags = node.querySelectorAll("a");
aTags.forEach((a) => {
const href = a.getAttribute("href");
if (!href) return;
if (href.includes(window.location.hostname)) return;
if (!href.startsWith("http") && !href.startsWith("//")) {
const absoluteURL = normalizeURL(href);
a.setAttribute("href", proxyURL + "/" + absoluteURL);
} else if (href.startsWith("http") || href.startsWith("//")) {
a.setAttribute("href", proxyURL + "/" + href);
}
});
const srcHrefs = node.querySelectorAll("[src]");
srcHrefs.forEach((tag) => {
const href = tag.getAttribute("src");
if (!href) return;
if (href.includes(window.location.hostname)) return;
if (!href.startsWith("http") && !href.startsWith("//")) {
const absoluteURL = normalizeURL(href);
tag.setAttribute("src", proxyURL + "/" + absoluteURL);
} else if (href.startsWith("http") || href.startsWith("//")) {
tag.setAttribute("src", proxyURL + "/" + href);
}
});
const actionHrefs = node.querySelectorAll("[action]");
actionHrefs.forEach((tag) => {
const href = tag.getAttribute("action");
if (!href) return;
if (href.includes(window.location.hostname)) return;
if (!href.startsWith("http") && !href.startsWith("//")) {
const absoluteURL = normalizeURL(href);
tag.setAttribute("action", proxyURL + "/" + absoluteURL);
} else if (href.startsWith("http") || href.startsWith("//")) {
tag.setAttribute("action", proxyURL + "/" + href);
}
});
observer.observe(node, { childList: true, attributes: true, subtree: true });
}
});
}
}
});
observer.observe(document.body, { childList: true, attributes: true, subtree: true });
</script>
<!-- DENO-X-PROXY -->
`;
const modifiedContentWithScript = dom.documentElement.outerHTML.replace("</body>", `${scriptTag}</body>`);
return modifiedContentWithScript;
}
Deno.serve(async (req: Request) => {
const url = new URL(req.url);
const method = req.method;
const params = url.searchParams;
const headers = req.headers;
const body = await req.text();
const _target = url.pathname.replace(/\//, "");
let target = _target;
if (isRelativePath(_target) && headers.get("Referer")) {
const targetOrigin = new URL(headers.get("Referer")).pathname.replace(/\//, "");
try {
target = new URL(_target, targetOrigin).toString();
}catch {
return new Response(`BAD REQUEST | Please access directly in \`/ + {URL}\` format.`, {
status: 400,
});
}
} else if (isRelativePath(_target)) {
return new Response(`BAD REQUEST | / + {URL}`, {
status: 400,
});
}
let res;
if (method === "GET" || method === "HEAD") {
const targetURL = new URL(target);
targetURL.search = params.toString();
res = await fetch(targetURL, {
headers: headers,
method: method,
});
} else {
res = await fetch(target, {
method: method,
headers: headers,
body: body,
});
}
if (res.headers.get("content-type")?.includes("text/html")) {
const content = await res.text();
const modifiedContent = await modifyHTMLContent(content, target, "//" + url.hostname);
res = new Response(modifiedContent, {
...res,
status: res.status,
headers: res.headers,
});
}
return new Response(await res.blob(), {
...res,
headers: {
...res.headers,
"Content-Security-Policy": ""
},
});
});