mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 06:20:02 -04:00
prettier
This commit is contained in:
parent
fa779017ad
commit
d6dd1d4ca3
20 changed files with 363 additions and 379 deletions
|
@ -25,6 +25,6 @@
|
||||||
"no-unreachable": "warn",
|
"no-unreachable": "warn",
|
||||||
"no-undef": "off",
|
"no-undef": "off",
|
||||||
"@typescript-eslint/no-explicit-any": "off",
|
"@typescript-eslint/no-explicit-any": "off",
|
||||||
"@typescript-eslint/ban-ts-comment": "off",
|
"@typescript-eslint/ban-ts-comment": "off"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
|
window.location.href = "http://example.com";
|
||||||
|
|
||||||
window.location.href = "http://example.com"
|
console.log(top.window.aaa);
|
||||||
|
consle.log(globalThis["win" + "dow"]);
|
||||||
|
|
||||||
|
globalThis.eval("..");
|
||||||
|
|
||||||
console.log(top.window.aaa)
|
let ref = { b: this.top.window, c: globalThis["win" + "dow"] };
|
||||||
consle.log(globalThis["win" + "dow"])
|
|
||||||
|
|
||||||
globalThis.eval("..")
|
|
||||||
|
|
||||||
let ref = { b: this.top.window, c: globalThis["win" + "dow"] }
|
|
||||||
|
|
|
@ -51,5 +51,5 @@ export default defineConfig({
|
||||||
// })
|
// })
|
||||||
],
|
],
|
||||||
watch: true,
|
watch: true,
|
||||||
target: "webworker"
|
target: "webworker",
|
||||||
});
|
});
|
||||||
|
|
|
@ -50,7 +50,9 @@ for (const attr of attrs) {
|
||||||
|
|
||||||
if (["nonce", "integrity", "csp"].includes(attr)) {
|
if (["nonce", "integrity", "csp"].includes(attr)) {
|
||||||
return;
|
return;
|
||||||
} else if (["src", "data", "href", "action", "formaction"].includes(attr)) {
|
} else if (
|
||||||
|
["src", "data", "href", "action", "formaction"].includes(attr)
|
||||||
|
) {
|
||||||
value = encodeUrl(value);
|
value = encodeUrl(value);
|
||||||
} else if (attr === "srcdoc") {
|
} else if (attr === "srcdoc") {
|
||||||
value = rewriteHtml(value);
|
value = rewriteHtml(value);
|
||||||
|
@ -88,7 +90,9 @@ Element.prototype.setAttribute = new Proxy(Element.prototype.setAttribute, {
|
||||||
thisArg.__origattrs[argArray[0]] = argArray[1];
|
thisArg.__origattrs[argArray[0]] = argArray[1];
|
||||||
if (["nonce", "integrity", "csp"].includes(argArray[0])) {
|
if (["nonce", "integrity", "csp"].includes(argArray[0])) {
|
||||||
return;
|
return;
|
||||||
} else if (["src", "data", "href", "action", "formaction"].includes(argArray[0])) {
|
} else if (
|
||||||
|
["src", "data", "href", "action", "formaction"].includes(argArray[0])
|
||||||
|
) {
|
||||||
argArray[1] = encodeUrl(argArray[1]);
|
argArray[1] = encodeUrl(argArray[1]);
|
||||||
} else if (argArray[0] === "srcdoc") {
|
} else if (argArray[0] === "srcdoc") {
|
||||||
argArray[1] = rewriteHtml(argArray[1]);
|
argArray[1] = rewriteHtml(argArray[1]);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { encodeUrl } from "../shared/rewriters/url"
|
import { encodeUrl } from "../shared/rewriters/url";
|
||||||
|
|
||||||
window.$sImport = function(url) {
|
window.$sImport = function (url) {
|
||||||
return (function() { }.constructor(`return import("${encodeUrl(url)}")`))();
|
return function () {}.constructor(`return import("${encodeUrl(url)}")`)();
|
||||||
}
|
};
|
||||||
|
|
|
@ -12,7 +12,7 @@ import "./storage.ts";
|
||||||
import "./css.ts";
|
import "./css.ts";
|
||||||
import "./history.ts";
|
import "./history.ts";
|
||||||
import "./worker.ts";
|
import "./worker.ts";
|
||||||
import "./beacon.ts"
|
import "./beacon.ts";
|
||||||
import "./origin.ts";
|
import "./origin.ts";
|
||||||
import "./import.ts";
|
import "./import.ts";
|
||||||
|
|
||||||
|
|
|
@ -11,24 +11,27 @@ function createLocation() {
|
||||||
return loc;
|
return loc;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const locationProxy = new Proxy({
|
export const locationProxy = new Proxy(
|
||||||
host: ""
|
{
|
||||||
}, {
|
host: "",
|
||||||
get(target, prop) {
|
|
||||||
const loc = createLocation();
|
|
||||||
|
|
||||||
return loc[prop];
|
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
get(target, prop) {
|
||||||
|
const loc = createLocation();
|
||||||
|
|
||||||
set(obj, prop, value) {
|
return loc[prop];
|
||||||
const loc = createLocation();
|
},
|
||||||
|
|
||||||
if (prop === "href") {
|
set(obj, prop, value) {
|
||||||
location.href = encodeUrl(value);
|
const loc = createLocation();
|
||||||
} else {
|
|
||||||
loc[prop] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
if (prop === "href") {
|
||||||
},
|
location.href = encodeUrl(value);
|
||||||
});
|
} else {
|
||||||
|
loc[prop] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
|
@ -33,4 +33,4 @@ window.eval = new Proxy(window.eval, {
|
||||||
return Reflect.apply(target, thisArg, [rewriteJs(argArray[0])]);
|
return Reflect.apply(target, thisArg, [rewriteJs(argArray[0])]);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,42 +1,40 @@
|
||||||
import { decodeUrl } from "../shared/rewriters/url";
|
import { decodeUrl } from "../shared/rewriters/url";
|
||||||
|
|
||||||
|
|
||||||
// const descriptor = Object.getOwnPropertyDescriptor(window, "origin");
|
// const descriptor = Object.getOwnPropertyDescriptor(window, "origin");
|
||||||
delete window.origin;
|
delete window.origin;
|
||||||
|
|
||||||
Object.defineProperty(window, "origin", {
|
Object.defineProperty(window, "origin", {
|
||||||
get() {
|
get() {
|
||||||
return new URL(decodeUrl(location.href)).origin;
|
return new URL(decodeUrl(location.href)).origin;
|
||||||
},
|
},
|
||||||
set() {
|
set() {
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
Object.defineProperty(document, "URL", {
|
Object.defineProperty(document, "URL", {
|
||||||
get() {
|
get() {
|
||||||
return decodeUrl(location.href);
|
return decodeUrl(location.href);
|
||||||
},
|
},
|
||||||
set() {
|
set() {
|
||||||
return false;
|
return false;
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
|
|
||||||
Object.defineProperty(document, "baseURI", {
|
Object.defineProperty(document, "baseURI", {
|
||||||
get() {
|
get() {
|
||||||
return decodeUrl(location.href);
|
return decodeUrl(location.href);
|
||||||
},
|
},
|
||||||
set() {
|
set() {
|
||||||
return false;
|
return false;
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
|
|
||||||
Object.defineProperty(document, "domain", {
|
Object.defineProperty(document, "domain", {
|
||||||
get() {
|
get() {
|
||||||
return new URL(decodeUrl(location.href)).hostname;
|
return new URL(decodeUrl(location.href)).hostname;
|
||||||
},
|
},
|
||||||
set() {
|
set() {
|
||||||
return false;
|
return false;
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
|
|
|
@ -10,7 +10,7 @@ WebSocket = new Proxy(WebSocket, {
|
||||||
target,
|
target,
|
||||||
{
|
{
|
||||||
"User-Agent": navigator.userAgent,
|
"User-Agent": navigator.userAgent,
|
||||||
"Origin": new URL(decodeUrl(location.href)).origin,
|
Origin: new URL(decodeUrl(location.href)).origin,
|
||||||
},
|
},
|
||||||
ArrayBuffer.prototype
|
ArrayBuffer.prototype
|
||||||
);
|
);
|
||||||
|
|
|
@ -3,7 +3,11 @@ import { documentProxy, windowProxy } from "./window";
|
||||||
|
|
||||||
function scope(identifier: any) {
|
function scope(identifier: any) {
|
||||||
// this will break iframe postmessage!
|
// this will break iframe postmessage!
|
||||||
if (identifier instanceof Window || identifier instanceof top.window.Window || identifier instanceof parent.window.Window) {
|
if (
|
||||||
|
identifier instanceof Window ||
|
||||||
|
identifier instanceof top.window.Window ||
|
||||||
|
identifier instanceof parent.window.Window
|
||||||
|
) {
|
||||||
return windowProxy;
|
return windowProxy;
|
||||||
} else if (identifier instanceof Location) {
|
} else if (identifier instanceof Location) {
|
||||||
return locationProxy;
|
return locationProxy;
|
||||||
|
|
|
@ -12,7 +12,7 @@ export const windowProxy = new Proxy(window, {
|
||||||
) {
|
) {
|
||||||
return windowProxy;
|
return windowProxy;
|
||||||
} else if (propIsString && prop == "parent") {
|
} else if (propIsString && prop == "parent") {
|
||||||
return window.parent
|
return window.parent;
|
||||||
} else if (propIsString && prop === "$scramjet") {
|
} else if (propIsString && prop === "$scramjet") {
|
||||||
return;
|
return;
|
||||||
} else if (propIsString && prop === "addEventListener") {
|
} else if (propIsString && prop === "addEventListener") {
|
||||||
|
@ -27,7 +27,6 @@ export const windowProxy = new Proxy(window, {
|
||||||
|
|
||||||
const value = Reflect.get(target, prop);
|
const value = Reflect.get(target, prop);
|
||||||
|
|
||||||
|
|
||||||
// this is bad! i don't know what the right thing to do is
|
// this is bad! i don't know what the right thing to do is
|
||||||
if (typeof value === "function") {
|
if (typeof value === "function") {
|
||||||
return new Proxy(value, {
|
return new Proxy(value, {
|
||||||
|
@ -55,7 +54,6 @@ export const windowProxy = new Proxy(window, {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
export const documentProxy = new Proxy(document, {
|
export const documentProxy = new Proxy(document, {
|
||||||
get(target, prop) {
|
get(target, prop) {
|
||||||
const propIsString = typeof prop === "string";
|
const propIsString = typeof prop === "string";
|
||||||
|
@ -78,7 +76,6 @@ export const documentProxy = new Proxy(document, {
|
||||||
},
|
},
|
||||||
set(target, prop, newValue) {
|
set(target, prop, newValue) {
|
||||||
if (typeof prop === "string" && prop === "location") {
|
if (typeof prop === "string" && prop === "location") {
|
||||||
|
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
location = new URL(encodeUrl(newValue));
|
location = new URL(encodeUrl(newValue));
|
||||||
|
|
||||||
|
@ -86,5 +83,5 @@ export const documentProxy = new Proxy(document, {
|
||||||
}
|
}
|
||||||
|
|
||||||
return Reflect.set(target, prop, newValue);
|
return Reflect.set(target, prop, newValue);
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -19,9 +19,11 @@ import * as ESTree from "estree";
|
||||||
import { initSync, rewrite_js } from "../../../rewriter/out/rewriter.js";
|
import { initSync, rewrite_js } from "../../../rewriter/out/rewriter.js";
|
||||||
import "../../../static/wasm.js";
|
import "../../../static/wasm.js";
|
||||||
|
|
||||||
initSync(new WebAssembly.Module(
|
initSync(
|
||||||
Uint8Array.from(atob(self.WASM), c => c.charCodeAt(0))
|
new WebAssembly.Module(
|
||||||
))
|
Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
global.rws = rewriteJs;
|
global.rws = rewriteJs;
|
||||||
export function rewriteJs(js: string | ArrayBuffer, origin?: URL) {
|
export function rewriteJs(js: string | ArrayBuffer, origin?: URL) {
|
||||||
|
@ -154,4 +156,3 @@ export function rewriteJs(js: string | ArrayBuffer, origin?: URL) {
|
||||||
// return js;
|
// return js;
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { rewriteJs } from "./js";
|
import { rewriteJs } from "./js";
|
||||||
export function rewriteWorkers(js: string, origin?: URL) {
|
export function rewriteWorkers(js: string, origin?: URL) {
|
||||||
let str = new String().toString();
|
let str = new String().toString();
|
||||||
|
|
||||||
["codecs", "config", "shared", "client"].forEach((script) => {
|
["codecs", "config", "shared", "client"].forEach((script) => {
|
||||||
str += `import "${self.$scramjet.config[script]}"\n`;
|
str += `import "${self.$scramjet.config[script]}"\n`;
|
||||||
});
|
});
|
||||||
|
@ -9,4 +9,3 @@ export function rewriteWorkers(js: string, origin?: URL) {
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,46 +1,40 @@
|
||||||
import { rewriteJs } from "../shared/rewriters/js";
|
import { rewriteJs } from "../shared/rewriters/js";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
onconnect = (e) => {
|
onconnect = (e) => {
|
||||||
const port = e.ports[0];
|
const port = e.ports[0];
|
||||||
|
|
||||||
|
console.log("thread: connected to port", port);
|
||||||
|
port.postMessage("ready");
|
||||||
|
|
||||||
console.log("thread: connected to port", port)
|
let syncToken = 0;
|
||||||
port.postMessage("ready");
|
port.onmessage = ({ data }) => {
|
||||||
|
console.log("thread: received message", data);
|
||||||
|
const [task, ...args] = data;
|
||||||
|
let token = syncToken++;
|
||||||
|
|
||||||
let syncToken = 0;
|
try {
|
||||||
port.onmessage = ({ data }) => {
|
let res = tasks[task](...args);
|
||||||
console.log("thread: received message", data)
|
console.log("thread: task", task, "completed with token", token);
|
||||||
const [task, ...args] = data;
|
port.postMessage({
|
||||||
let token = syncToken++;
|
token,
|
||||||
|
result: res,
|
||||||
try {
|
});
|
||||||
let res = tasks[task](...args);
|
} catch (e) {
|
||||||
console.log("thread: task", task, "completed with token", token)
|
port.postMessage({
|
||||||
port.postMessage({
|
token,
|
||||||
token,
|
error: e.message,
|
||||||
result: res
|
});
|
||||||
})
|
}
|
||||||
} catch (e) {
|
|
||||||
port.postMessage({
|
|
||||||
token,
|
|
||||||
error: e.message
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
port.postMessage("idle");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
port.postMessage("idle");
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
const tasks = {
|
const tasks = {
|
||||||
"rewriteJs": taskRewriteJs,
|
rewriteJs: taskRewriteJs,
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
function taskRewriteJs(js: ArrayBuffer, origin: string): string {
|
function taskRewriteJs(js: ArrayBuffer, origin: string): string {
|
||||||
return rewriteJs(js, new URL(origin));
|
return rewriteJs(js, new URL(origin));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
|
|
||||||
export function errorTemplate(trace: string, fetchedURL: string) {
|
export function errorTemplate(trace: string, fetchedURL: string) {
|
||||||
// turn script into a data URI so we don"t have to escape any HTML values
|
// turn script into a data URI so we don"t have to escape any HTML values
|
||||||
const script = `
|
const script = `
|
||||||
errorTrace.value = ${JSON.stringify(trace)};
|
errorTrace.value = ${JSON.stringify(trace)};
|
||||||
fetchedURL.textContent = ${JSON.stringify(fetchedURL)};
|
fetchedURL.textContent = ${JSON.stringify(fetchedURL)};
|
||||||
for (const node of document.querySelectorAll("#hostname")) node.textContent = ${JSON.stringify(
|
for (const node of document.querySelectorAll("#hostname")) node.textContent = ${JSON.stringify(
|
||||||
location.hostname
|
location.hostname
|
||||||
)};
|
)};
|
||||||
reload.addEventListener("click", () => location.reload());
|
reload.addEventListener("click", () => location.reload());
|
||||||
version.textContent = "0.0.1";
|
version.textContent = "0.0.1";
|
||||||
`;
|
`;
|
||||||
|
|
||||||
return `<!DOCTYPE html>
|
return `<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
|
@ -43,24 +42,24 @@ export function errorTemplate(trace: string, fetchedURL: string) {
|
||||||
<button id="reload">Reload</button>
|
<button id="reload">Reload</button>
|
||||||
<hr />
|
<hr />
|
||||||
<p><i>Scramjet v<span id="version"></span></i></p>
|
<p><i>Scramjet v<span id="version"></span></i></p>
|
||||||
<script src="${"data:application/javascript," + encodeURIComponent(script)
|
<script src="${
|
||||||
}"></script>
|
"data:application/javascript," + encodeURIComponent(script)
|
||||||
|
}"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function renderError(err: unknown, fetchedURL: string) {
|
export function renderError(err: unknown, fetchedURL: string) {
|
||||||
const headers = {
|
const headers = {
|
||||||
"content-type": "text/html",
|
"content-type": "text/html",
|
||||||
};
|
};
|
||||||
if (crossOriginIsolated) {
|
if (crossOriginIsolated) {
|
||||||
headers["Cross-Origin-Embedder-Policy"] = "require-corp";
|
headers["Cross-Origin-Embedder-Policy"] = "require-corp";
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response(errorTemplate(String(err), fetchedURL), {
|
return new Response(errorTemplate(String(err), fetchedURL), {
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: headers,
|
headers: headers,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,171 +4,164 @@ import { ParseResultType } from "parse-domain";
|
||||||
import { ScramjetServiceWorker } from ".";
|
import { ScramjetServiceWorker } from ".";
|
||||||
import { renderError } from "./error";
|
import { renderError } from "./error";
|
||||||
|
|
||||||
export async function swfetch(this: ScramjetServiceWorker, { request }: FetchEvent) {
|
export async function swfetch(
|
||||||
const urlParam = new URLSearchParams(new URL(request.url).search);
|
this: ScramjetServiceWorker,
|
||||||
const { encodeUrl, decodeUrl } = self.$scramjet.shared.url;
|
{ request }: FetchEvent
|
||||||
const {
|
) {
|
||||||
rewriteHeaders,
|
const urlParam = new URLSearchParams(new URL(request.url).search);
|
||||||
rewriteHtml,
|
const { encodeUrl, decodeUrl } = self.$scramjet.shared.url;
|
||||||
rewriteJs,
|
const { rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } =
|
||||||
rewriteCss,
|
self.$scramjet.shared.rewrite;
|
||||||
rewriteWorkers,
|
const { parseDomain } = self.$scramjet.shared.util;
|
||||||
} = self.$scramjet.shared.rewrite;
|
|
||||||
const { parseDomain } = self.$scramjet.shared.util;
|
|
||||||
|
|
||||||
if (urlParam.has("url")) {
|
if (urlParam.has("url")) {
|
||||||
return Response.redirect(
|
return Response.redirect(
|
||||||
encodeUrl(urlParam.get("url"), new URL(urlParam.get("url")))
|
encodeUrl(urlParam.get("url"), new URL(urlParam.get("url")))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const url = new URL(decodeUrl(request.url));
|
const url = new URL(decodeUrl(request.url));
|
||||||
|
|
||||||
const cookieStore = new IDBMap(url.host, {
|
const cookieStore = new IDBMap(url.host, {
|
||||||
durability: "relaxed",
|
durability: "relaxed",
|
||||||
prefix: "Cookies",
|
prefix: "Cookies",
|
||||||
});
|
});
|
||||||
|
|
||||||
const response: BareResponseFetch = await this.client.fetch(url, {
|
const response: BareResponseFetch = await this.client.fetch(url, {
|
||||||
method: request.method,
|
method: request.method,
|
||||||
body: request.body,
|
body: request.body,
|
||||||
headers: request.headers,
|
headers: request.headers,
|
||||||
credentials: "omit",
|
credentials: "omit",
|
||||||
mode: request.mode === "cors" ? request.mode : "same-origin",
|
mode: request.mode === "cors" ? request.mode : "same-origin",
|
||||||
cache: request.cache,
|
cache: request.cache,
|
||||||
redirect: request.redirect,
|
redirect: request.redirect,
|
||||||
//@ts-ignore why the fuck is this not typed mircosoft
|
//@ts-ignore why the fuck is this not typed mircosoft
|
||||||
duplex: "half",
|
duplex: "half",
|
||||||
});
|
});
|
||||||
|
|
||||||
let responseBody;
|
let responseBody;
|
||||||
const responseHeaders = rewriteHeaders(response.rawHeaders, url);
|
const responseHeaders = rewriteHeaders(response.rawHeaders, url);
|
||||||
|
|
||||||
for (const cookie of (responseHeaders["set-cookie"] || []) as string[]) {
|
for (const cookie of (responseHeaders["set-cookie"] || []) as string[]) {
|
||||||
let cookieParsed = cookie.split(";").map((x) => x.trim().split("="));
|
let cookieParsed = cookie.split(";").map((x) => x.trim().split("="));
|
||||||
|
|
||||||
let [key, value] = cookieParsed.shift();
|
let [key, value] = cookieParsed.shift();
|
||||||
if (!value) continue;
|
if (!value) continue;
|
||||||
value = value.replace("\"", "");
|
value = value.replace('"', "");
|
||||||
|
|
||||||
const hostArg = cookieParsed.find((x) => x[0] === "Domain");
|
const hostArg = cookieParsed.find((x) => x[0] === "Domain");
|
||||||
cookieParsed = cookieParsed.filter((x) => x[0] !== "Domain");
|
cookieParsed = cookieParsed.filter((x) => x[0] !== "Domain");
|
||||||
let host = hostArg ? hostArg[1] : undefined;
|
let host = hostArg ? hostArg[1] : undefined;
|
||||||
|
|
||||||
if (url.protocol === "http" && cookieParsed.includes(["Secure"]))
|
if (url.protocol === "http" && cookieParsed.includes(["Secure"]))
|
||||||
continue;
|
continue;
|
||||||
if (
|
if (
|
||||||
cookieParsed.includes(["SameSite", "None"]) &&
|
cookieParsed.includes(["SameSite", "None"]) &&
|
||||||
!cookieParsed.includes(["Secure"])
|
!cookieParsed.includes(["Secure"])
|
||||||
)
|
)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (host && host !== url.host) {
|
if (host && host !== url.host) {
|
||||||
if (host.startsWith(".")) host = host.slice(1);
|
if (host.startsWith(".")) host = host.slice(1);
|
||||||
const urlDomain = parseDomain(url.hostname);
|
const urlDomain = parseDomain(url.hostname);
|
||||||
|
|
||||||
if (urlDomain.type === ParseResultType.Listed) {
|
if (urlDomain.type === ParseResultType.Listed) {
|
||||||
const { subDomains: domain, topLevelDomains } = urlDomain;
|
const { subDomains: domain, topLevelDomains } = urlDomain;
|
||||||
if (!host.endsWith([domain, ...topLevelDomains].join(".")))
|
if (!host.endsWith([domain, ...topLevelDomains].join("."))) continue;
|
||||||
continue;
|
} else {
|
||||||
} else {
|
continue;
|
||||||
continue;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const realCookieStore = new IDBMap(host, {
|
const realCookieStore = new IDBMap(host, {
|
||||||
durability: "relaxed",
|
durability: "relaxed",
|
||||||
prefix: "Cookies",
|
prefix: "Cookies",
|
||||||
});
|
});
|
||||||
realCookieStore.set(key, {
|
realCookieStore.set(key, {
|
||||||
value: value,
|
value: value,
|
||||||
args: cookieParsed,
|
args: cookieParsed,
|
||||||
subdomain: true,
|
subdomain: true,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
cookieStore.set(key, {
|
cookieStore.set(key, {
|
||||||
value: value,
|
value: value,
|
||||||
args: cookieParsed,
|
args: cookieParsed,
|
||||||
subdomain: false,
|
subdomain: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const header in responseHeaders) {
|
for (const header in responseHeaders) {
|
||||||
// flatten everything past here
|
// flatten everything past here
|
||||||
if (Array.isArray(responseHeaders[header]))
|
if (Array.isArray(responseHeaders[header]))
|
||||||
responseHeaders[header] = responseHeaders[header][0];
|
responseHeaders[header] = responseHeaders[header][0];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.body) {
|
if (response.body) {
|
||||||
switch (request.destination) {
|
switch (request.destination) {
|
||||||
case "iframe":
|
case "iframe":
|
||||||
case "document":
|
case "document":
|
||||||
if (
|
if (
|
||||||
responseHeaders["content-type"]
|
responseHeaders["content-type"]?.toString()?.startsWith("text/html")
|
||||||
?.toString()
|
) {
|
||||||
?.startsWith("text/html")
|
responseBody = rewriteHtml(await response.text(), url);
|
||||||
) {
|
} else {
|
||||||
responseBody = rewriteHtml(await response.text(), url);
|
responseBody = response.body;
|
||||||
} else {
|
}
|
||||||
responseBody = response.body;
|
break;
|
||||||
}
|
case "script":
|
||||||
break;
|
responseBody = rewriteJs(await response.arrayBuffer(), url);
|
||||||
case "script":
|
// Disable threading for now, it's causing issues.
|
||||||
responseBody = rewriteJs(await response.arrayBuffer(), url);
|
// responseBody = await this.threadpool.rewriteJs(responseBody, url.toString());
|
||||||
// Disable threading for now, it's causing issues.
|
break;
|
||||||
// responseBody = await this.threadpool.rewriteJs(responseBody, url.toString());
|
case "style":
|
||||||
break;
|
responseBody = rewriteCss(await response.text(), url);
|
||||||
case "style":
|
break;
|
||||||
responseBody = rewriteCss(await response.text(), url);
|
case "sharedworker":
|
||||||
break;
|
case "worker":
|
||||||
case "sharedworker":
|
responseBody = rewriteWorkers(await response.text(), url);
|
||||||
case "worker":
|
break;
|
||||||
responseBody = rewriteWorkers(await response.text(), url);
|
default:
|
||||||
break;
|
responseBody = response.body;
|
||||||
default:
|
break;
|
||||||
responseBody = response.body;
|
}
|
||||||
break;
|
}
|
||||||
}
|
// downloads
|
||||||
}
|
if (["document", "iframe"].includes(request.destination)) {
|
||||||
// downloads
|
const header = responseHeaders["content-disposition"];
|
||||||
if (["document", "iframe"].includes(request.destination)) {
|
|
||||||
const header = responseHeaders["content-disposition"];
|
|
||||||
|
|
||||||
// validate header and test for filename
|
// validate header and test for filename
|
||||||
if (!/\s*?((inline|attachment);\s*?)filename=/i.test(header)) {
|
if (!/\s*?((inline|attachment);\s*?)filename=/i.test(header)) {
|
||||||
// if filename= wasn"t specified then maybe the remote specified to download this as an attachment?
|
// if filename= wasn"t specified then maybe the remote specified to download this as an attachment?
|
||||||
// if it"s invalid then we can still possibly test for the attachment/inline type
|
// if it"s invalid then we can still possibly test for the attachment/inline type
|
||||||
const type = /^\s*?attachment/i.test(header)
|
const type = /^\s*?attachment/i.test(header) ? "attachment" : "inline";
|
||||||
? "attachment"
|
|
||||||
: "inline";
|
|
||||||
|
|
||||||
// set the filename
|
// set the filename
|
||||||
const [filename] = new URL(response.finalURL).pathname
|
const [filename] = new URL(response.finalURL).pathname
|
||||||
.split("/")
|
.split("/")
|
||||||
.slice(-1);
|
.slice(-1);
|
||||||
|
|
||||||
responseHeaders["content-disposition"] =
|
responseHeaders["content-disposition"] =
|
||||||
`${type}; filename=${JSON.stringify(filename)}`;
|
`${type}; filename=${JSON.stringify(filename)}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (responseHeaders["accept"] === "text/event-stream") {
|
if (responseHeaders["accept"] === "text/event-stream") {
|
||||||
responseHeaders["content-type"] = "text/event-stream";
|
responseHeaders["content-type"] = "text/event-stream";
|
||||||
}
|
}
|
||||||
if (crossOriginIsolated) {
|
if (crossOriginIsolated) {
|
||||||
responseHeaders["Cross-Origin-Embedder-Policy"] = "require-corp";
|
responseHeaders["Cross-Origin-Embedder-Policy"] = "require-corp";
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response(responseBody, {
|
return new Response(responseBody, {
|
||||||
headers: responseHeaders as HeadersInit,
|
headers: responseHeaders as HeadersInit,
|
||||||
status: response.status,
|
status: response.status,
|
||||||
statusText: response.statusText,
|
statusText: response.statusText,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("ERROR FROM SERVICE WORKER FETCH", err);
|
console.error("ERROR FROM SERVICE WORKER FETCH", err);
|
||||||
if (!["document", "iframe"].includes(request.destination))
|
if (!["document", "iframe"].includes(request.destination))
|
||||||
return new Response(undefined, { status: 500 });
|
return new Response(undefined, { status: 500 });
|
||||||
|
|
||||||
return renderError(err, decodeUrl(request.url));
|
return renderError(err, decodeUrl(request.url));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,6 @@ export class ScramjetServiceWorker {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
|
||||||
this.threadpool = new ScramjetThreadpool();
|
this.threadpool = new ScramjetThreadpool();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
route({ request }: FetchEvent) {
|
route({ request }: FetchEvent) {
|
||||||
|
@ -28,7 +27,6 @@ export class ScramjetServiceWorker {
|
||||||
}
|
}
|
||||||
|
|
||||||
public fetch = swfetch;
|
public fetch = swfetch;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
self.ScramjetServiceWorker = ScramjetServiceWorker;
|
self.ScramjetServiceWorker = ScramjetServiceWorker;
|
||||||
|
|
|
@ -1,92 +1,86 @@
|
||||||
|
|
||||||
type Thread = {
|
type Thread = {
|
||||||
handle: MessagePort;
|
handle: MessagePort;
|
||||||
ready: boolean;
|
ready: boolean;
|
||||||
busy: boolean;
|
busy: boolean;
|
||||||
syncToken: number;
|
syncToken: number;
|
||||||
promises: Map<number, { resolve, reject }>;
|
promises: Map<number, { resolve; reject }>;
|
||||||
}
|
};
|
||||||
|
|
||||||
export class ScramjetThreadpool {
|
export class ScramjetThreadpool {
|
||||||
threads: Thread[] = [];
|
threads: Thread[] = [];
|
||||||
constructor() {
|
constructor() {
|
||||||
self.addEventListener("message", ({ data }) => {
|
self.addEventListener("message", ({ data }) => {
|
||||||
if (data.scramjet$type == "add") {
|
if (data.scramjet$type == "add") {
|
||||||
this.spawn(data.handle);
|
this.spawn(data.handle);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
spawn(handle) {
|
spawn(handle) {
|
||||||
const thread = {
|
const thread = {
|
||||||
handle,
|
handle,
|
||||||
ready: false,
|
ready: false,
|
||||||
busy: false,
|
busy: false,
|
||||||
syncToken: 0,
|
syncToken: 0,
|
||||||
promises: new Map()
|
promises: new Map(),
|
||||||
}
|
};
|
||||||
|
|
||||||
this.threads.push(thread);
|
this.threads.push(thread);
|
||||||
|
|
||||||
thread.handle.onmessage = (e) => {
|
thread.handle.onmessage = (e) => {
|
||||||
if (e.data === "ready") {
|
if (e.data === "ready") {
|
||||||
thread.ready = true;
|
thread.ready = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (e.data === "idle") {
|
if (e.data === "idle") {
|
||||||
thread.busy = false;
|
thread.busy = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { token, result, error } = e.data;
|
const { token, result, error } = e.data;
|
||||||
const { resolve, reject } = thread.promises.get(token);
|
const { resolve, reject } = thread.promises.get(token);
|
||||||
thread.promises.delete(token);
|
thread.promises.delete(token);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
} else {
|
} else {
|
||||||
resolve(result);
|
resolve(result);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
thread.handle.start();
|
thread.handle.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
pick(): Thread | undefined {
|
pick(): Thread | undefined {
|
||||||
const alive = this.threads.filter(t => t.ready);
|
const alive = this.threads.filter((t) => t.ready);
|
||||||
const idle = alive.filter(t => !t.busy);
|
const idle = alive.filter((t) => !t.busy);
|
||||||
|
|
||||||
// no threads
|
// no threads
|
||||||
if (!alive.length) return;
|
if (!alive.length) return;
|
||||||
|
|
||||||
// there is a thread, but it's busy
|
// there is a thread, but it's busy
|
||||||
if (!idle.length) return alive[Math.floor(Math.random() * alive.length)];
|
if (!idle.length) return alive[Math.floor(Math.random() * alive.length)];
|
||||||
|
|
||||||
// there's an open thread
|
// there's an open thread
|
||||||
return idle[Math.floor(Math.random() * idle.length)];
|
return idle[Math.floor(Math.random() * idle.length)];
|
||||||
}
|
}
|
||||||
|
|
||||||
run(task: string, args: any[], transferrable: any[]): Promise<any> {
|
run(task: string, args: any[], transferrable: any[]): Promise<any> {
|
||||||
const thread = this.pick();
|
const thread = this.pick();
|
||||||
if (!thread) throw new Error("No threads available");
|
if (!thread) throw new Error("No threads available");
|
||||||
thread.busy = true;
|
thread.busy = true;
|
||||||
|
|
||||||
|
let token = thread.syncToken++;
|
||||||
|
|
||||||
let token = thread.syncToken++;
|
// console.log("runthread: dispatching task", task, "to thread", thread, "of token", token)
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
thread.promises.set(token, { resolve, reject });
|
||||||
|
|
||||||
// console.log("runthread: dispatching task", task, "to thread", thread, "of token", token)
|
thread.handle.postMessage([task, ...args], transferrable);
|
||||||
return new Promise((resolve, reject) => {
|
});
|
||||||
thread.promises.set(token, { resolve, reject });
|
}
|
||||||
|
|
||||||
thread.handle.postMessage([
|
async rewriteJs(js: ArrayBuffer, origin: string): Promise<string> {
|
||||||
task,
|
return await this.run("rewriteJs", [js, origin], [js]);
|
||||||
...args
|
}
|
||||||
], transferrable);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async rewriteJs(js: ArrayBuffer, origin: string): Promise<string> {
|
|
||||||
return await this.run("rewriteJs", [js, origin], [js]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
56
static/ui.js
56
static/ui.js
|
@ -1,19 +1,21 @@
|
||||||
navigator.serviceWorker
|
navigator.serviceWorker.register("./sw.js").then((reg) => {
|
||||||
.register("./sw.js")
|
reg.update();
|
||||||
.then((reg) => {
|
});
|
||||||
reg.update();
|
|
||||||
});
|
|
||||||
|
|
||||||
navigator.serviceWorker.ready.then((reg) => {
|
navigator.serviceWorker.ready.then((reg) => {
|
||||||
for (let i = 0; i < 20; i++) {
|
for (let i = 0; i < 20; i++) {
|
||||||
const thread = new SharedWorker($scramjet.config.thread, { name: "thread" + i });
|
const thread = new SharedWorker($scramjet.config.thread, {
|
||||||
|
name: "thread" + i,
|
||||||
|
});
|
||||||
|
|
||||||
reg.active.postMessage({
|
reg.active.postMessage(
|
||||||
scramjet$type: "add",
|
{
|
||||||
handle: thread.port
|
scramjet$type: "add",
|
||||||
}, [thread.port]);
|
handle: thread.port,
|
||||||
|
},
|
||||||
}
|
[thread.port]
|
||||||
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const connection = new BareMux.BareMuxConnection("/baremux/worker.js");
|
const connection = new BareMux.BareMuxConnection("/baremux/worker.js");
|
||||||
|
@ -24,21 +26,21 @@ const col = css`
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
`;
|
`;
|
||||||
const store = $store(
|
const store = $store(
|
||||||
{
|
{
|
||||||
url: "https://google.com",
|
url: "https://google.com",
|
||||||
wispurl: "wss://wisp.mercurywork.shop/",
|
wispurl: "wss://wisp.mercurywork.shop/",
|
||||||
bareurl:
|
bareurl:
|
||||||
(location.protocol === "https:" ? "https" : "http") +
|
(location.protocol === "https:" ? "https" : "http") +
|
||||||
"://" +
|
"://" +
|
||||||
location.host +
|
location.host +
|
||||||
"/bare/",
|
"/bare/",
|
||||||
},
|
},
|
||||||
{ ident: "settings", backing: "localstorage", autosave: "auto" }
|
{ ident: "settings", backing: "localstorage", autosave: "auto" }
|
||||||
);
|
);
|
||||||
connection.setTransport("/baremod/index.mjs", [store.bareurl]);
|
connection.setTransport("/baremod/index.mjs", [store.bareurl]);
|
||||||
function App() {
|
function App() {
|
||||||
this.urlencoded = "";
|
this.urlencoded = "";
|
||||||
this.css = `
|
this.css = `
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
color: #e0def4;
|
color: #e0def4;
|
||||||
|
@ -97,7 +99,7 @@ function App() {
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<div>
|
<div>
|
||||||
<h1>Percury Unblocker</h1>
|
<h1>Percury Unblocker</h1>
|
||||||
<p>surf the unblocked and mostly buggy web</p>
|
<p>surf the unblocked and mostly buggy web</p>
|
||||||
|
@ -121,5 +123,5 @@ function App() {
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener("load", () => {
|
window.addEventListener("load", () => {
|
||||||
document.body.appendChild(h(App));
|
document.body.appendChild(h(App));
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue