This commit is contained in:
velzie 2024-07-17 08:07:41 -04:00
commit fafeef060a
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
22 changed files with 376 additions and 385 deletions

View file

@ -25,6 +25,6 @@
"no-unreachable": "warn",
"no-undef": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/ban-ts-comment": "off"
}
}

View file

@ -3,6 +3,7 @@
</div>
---
<a href="https://www.npmjs.com/package/@mercuryworkshop/scramjet"><img src="https://img.shields.io/npm/v/@mercuryworkshop/scramjet.svg?maxAge=3600" alt="npm version" /></a>
Scramjet is an experimental web proxy that aims to be the successor to Ultraviolet.

View file

@ -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)
consle.log(globalThis["win" + "dow"])
globalThis.eval("..")
let ref = { b: this.top.window, c: globalThis["win" + "dow"] }
let ref = { b: this.top.window, c: globalThis["win" + "dow"] };

View file

@ -51,5 +51,5 @@ export default defineConfig({
// })
],
watch: true,
target: "webworker"
target: "webworker",
});

View file

@ -34,23 +34,25 @@ for (const attr of attrs) {
const descriptor = Object.getOwnPropertyDescriptor(element.prototype, attr);
Object.defineProperty(element.prototype, attr, {
get() {
if (/src|href|data|action|formaction/.test(attr)) {
if (["src", "data", "href", "action", "formaction"].includes(attr)) {
return decodeUrl(descriptor.get.call(this));
}
if (this.__origattrs[attr]) {
return this.__origattrs[attr];
if (this.$origattrs[attr]) {
return this.$origattrs[attr];
}
return descriptor.get.call(this);
},
set(value) {
this.__origattrs[attr] = value;
this.$origattrs[attr] = value;
if (["nonce", "integrity", "csp"].includes(attr)) {
return;
} else if (["src", "data", "href", "action", "formaction"].includes(attr)) {
} else if (
["src", "data", "href", "action", "formaction"].includes(attr)
) {
value = encodeUrl(value);
} else if (attr === "srcdoc") {
value = rewriteHtml(value);
@ -66,16 +68,16 @@ for (const attr of attrs) {
declare global {
interface Element {
__origattrs: Record<string, string>;
$origattrs: Record<string, string>;
}
}
Element.prototype.__origattrs = {};
Element.prototype.$origattrs = {};
Element.prototype.getAttribute = new Proxy(Element.prototype.getAttribute, {
apply(target, thisArg, argArray) {
if (attrs.includes(argArray[0]) && thisArg.__origattrs[argArray[0]]) {
return thisArg.__origattrs[argArray[0]];
if (attrs.includes(argArray[0]) && thisArg.$origattrs[argArray[0]]) {
return thisArg.$origattrs[argArray[0]];
}
return Reflect.apply(target, thisArg, argArray);
@ -85,10 +87,12 @@ Element.prototype.getAttribute = new Proxy(Element.prototype.getAttribute, {
Element.prototype.setAttribute = new Proxy(Element.prototype.setAttribute, {
apply(target, thisArg, argArray) {
if (attrs.includes(argArray[0])) {
thisArg.__origattrs[argArray[0]] = argArray[1];
thisArg.$origattrs[argArray[0]] = argArray[1];
if (["nonce", "integrity", "csp"].includes(argArray[0])) {
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]);
} else if (argArray[0] === "srcdoc") {
argArray[1] = rewriteHtml(argArray[1]);

View file

@ -1,9 +1,8 @@
import { decodeUrl, encodeUrl } from "../shared/rewriters/url"
import { encodeUrl } from "../shared/rewriters/url";
window.$sImport = function(base) {
return function(url) {
let resolved = new URL(url, base).href
console.log(resolved)
return (function() { }.constructor(`return import("${encodeUrl(resolved)}")`))();
}
}

View file

@ -12,7 +12,7 @@ import "./storage.ts";
import "./css.ts";
import "./history.ts";
import "./worker.ts";
import "./beacon.ts"
import "./beacon.ts";
import "./origin.ts";
import "./import.ts";

View file

@ -11,7 +11,11 @@ function createLocation() {
return loc;
}
export const locationProxy = new Proxy({}, {
export const locationProxy = new Proxy(
{
host: "",
},
{
get(target, prop) {
const loc = createLocation();
@ -29,4 +33,5 @@ export const locationProxy = new Proxy({}, {
return true;
},
});
}
);

View file

@ -1,6 +1,5 @@
import { decodeUrl } from "../shared/rewriters/url";
// const descriptor = Object.getOwnPropertyDescriptor(window, "origin");
delete window.origin;
@ -13,15 +12,14 @@ Object.defineProperty(window, "origin", {
},
});
Object.defineProperty(document, "URL", {
get() {
return decodeUrl(location.href);
},
set() {
return false;
}
})
},
});
Object.defineProperty(document, "baseURI", {
get() {
@ -29,8 +27,8 @@ Object.defineProperty(document, "baseURI", {
},
set() {
return false;
}
})
},
});
Object.defineProperty(document, "domain", {
get() {
@ -38,5 +36,5 @@ Object.defineProperty(document, "domain", {
},
set() {
return false;
}
})
},
});

View file

@ -10,7 +10,7 @@ WebSocket = new Proxy(WebSocket, {
target,
{
"User-Agent": navigator.userAgent,
"Origin": new URL(decodeUrl(location.href)).origin,
Origin: new URL(decodeUrl(location.href)).origin,
},
ArrayBuffer.prototype
);

View file

@ -3,7 +3,11 @@ import { documentProxy, windowProxy } from "./window";
function scope(identifier: any) {
// 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;
} else if (identifier instanceof Location) {
return locationProxy;

View file

@ -12,7 +12,7 @@ export const windowProxy = new Proxy(window, {
) {
return windowProxy;
} else if (propIsString && prop == "parent") {
return window.parent
return window.parent;
} else if (propIsString && prop === "$scramjet") {
return;
} else if (propIsString && prop === "addEventListener") {
@ -27,7 +27,6 @@ export const windowProxy = new Proxy(window, {
const value = Reflect.get(target, prop);
// this is bad! i don't know what the right thing to do is
if (typeof value === "function") {
return new Proxy(value, {
@ -55,7 +54,6 @@ export const windowProxy = new Proxy(window, {
},
});
export const documentProxy = new Proxy(document, {
get(target, prop) {
const propIsString = typeof prop === "string";
@ -78,7 +76,6 @@ export const documentProxy = new Proxy(document, {
},
set(target, prop, newValue) {
if (typeof prop === "string" && prop === "location") {
//@ts-ignore
location = new URL(encodeUrl(newValue));
@ -86,5 +83,5 @@ export const documentProxy = new Proxy(document, {
}
return Reflect.set(target, prop, newValue);
}
},
});

View file

@ -20,6 +20,10 @@ Worklet.prototype.addModule = new Proxy(Worklet.prototype.addModule, {
},
});
if ("serviceWorker" in window.navigator) {
//@ts-expect-error temporary until nested sw support
delete window.Navigator.prototype.serviceWorker;
}
// broken
// window.importScripts = new Proxy(window.importScripts, {

View file

@ -19,9 +19,11 @@ import * as ESTree from "estree";
import { initSync, rewrite_js } from "../../../rewriter/out/rewriter.js";
import "../../../static/wasm.js";
initSync(new WebAssembly.Module(
Uint8Array.from(atob(self.WASM), c => c.charCodeAt(0))
))
initSync(
new WebAssembly.Module(
Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0))
)
);
global.rws = rewriteJs;
export function rewriteJs(js: string | ArrayBuffer, origin?: URL) {
@ -155,4 +157,3 @@ export function rewriteJs(js: string | ArrayBuffer, origin?: URL) {
// return js;
// }
}

View file

@ -1,13 +1,11 @@
import { rewriteJs } from "./js";
export function rewriteWorkers(js: string, origin?: URL) {
let str = new String().toString()[
//@ts-expect-error
("codecs", "config", "shared", "client")
].forEach((script) => {
let str = new String().toString();
["codecs", "config", "shared", "client"].forEach((script) => {
str += `import "${self.$scramjet.config[script]}"\n`;
});
str += rewriteJs(js, origin);
return str;
}

View file

@ -1,45 +1,39 @@
import { rewriteJs } from "../shared/rewriters/js";
// @ts-ignore
onconnect = (e) => {
const port = e.ports[0];
console.log("thread: connected to port", port)
console.log("thread: connected to port", port);
port.postMessage("ready");
let syncToken = 0;
port.onmessage = ({ data }) => {
console.log("thread: received message", data)
console.log("thread: received message", data);
const [task, ...args] = data;
let token = syncToken++;
try {
let res = tasks[task](...args);
console.log("thread: task", task, "completed with token", token)
console.log("thread: task", task, "completed with token", token);
port.postMessage({
token,
result: res
})
result: res,
});
} catch (e) {
port.postMessage({
token,
error: e.message
})
error: e.message,
});
}
port.postMessage("idle");
}
}
};
};
const tasks = {
"rewriteJs": taskRewriteJs,
}
rewriteJs: taskRewriteJs,
};
function taskRewriteJs(js: ArrayBuffer, origin: string): string {
return rewriteJs(js, new URL(origin));

View file

@ -1,4 +1,3 @@
export function errorTemplate(trace: string, fetchedURL: string) {
// turn script into a data URI so we don"t have to escape any HTML values
const script = `
@ -43,7 +42,8 @@ export function errorTemplate(trace: string, fetchedURL: string) {
<button id="reload">Reload</button>
<hr />
<p><i>Scramjet v<span id="version"></span></i></p>
<script src="${"data:application/javascript," + encodeURIComponent(script)
<script src="${
"data:application/javascript," + encodeURIComponent(script)
}"></script>
</body>
</html>
@ -63,4 +63,3 @@ export function renderError(err: unknown, fetchedURL: string) {
headers: headers,
});
}

View file

@ -4,16 +4,14 @@ import { ParseResultType } from "parse-domain";
import { ScramjetServiceWorker } from ".";
import { renderError } from "./error";
export async function swfetch(this: ScramjetServiceWorker, { request }: FetchEvent) {
export async function swfetch(
this: ScramjetServiceWorker,
{ request }: FetchEvent
) {
const urlParam = new URLSearchParams(new URL(request.url).search);
const { encodeUrl, decodeUrl } = self.$scramjet.shared.url;
const {
rewriteHeaders,
rewriteHtml,
rewriteJs,
rewriteCss,
rewriteWorkers,
} = self.$scramjet.shared.rewrite;
const { rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } =
self.$scramjet.shared.rewrite;
const { parseDomain } = self.$scramjet.shared.util;
if (urlParam.has("url")) {
@ -50,7 +48,7 @@ export async function swfetch(this: ScramjetServiceWorker, { request }: FetchEve
let [key, value] = cookieParsed.shift();
if (!value) continue;
value = value.replace("\"", "");
value = value.replace('"', "");
const hostArg = cookieParsed.find((x) => x[0] === "Domain");
cookieParsed = cookieParsed.filter((x) => x[0] !== "Domain");
@ -70,8 +68,7 @@ export async function swfetch(this: ScramjetServiceWorker, { request }: FetchEve
if (urlDomain.type === ParseResultType.Listed) {
const { subDomains: domain, topLevelDomains } = urlDomain;
if (!host.endsWith([domain, ...topLevelDomains].join(".")))
continue;
if (!host.endsWith([domain, ...topLevelDomains].join("."))) continue;
} else {
continue;
}
@ -105,9 +102,7 @@ export async function swfetch(this: ScramjetServiceWorker, { request }: FetchEve
case "iframe":
case "document":
if (
responseHeaders["content-type"]
?.toString()
?.startsWith("text/html")
responseHeaders["content-type"]?.toString()?.startsWith("text/html")
) {
responseBody = rewriteHtml(await response.text(), url);
} else {
@ -115,7 +110,9 @@ export async function swfetch(this: ScramjetServiceWorker, { request }: FetchEve
}
break;
case "script":
responseBody = await this.threadpool.rewriteJs(await response.arrayBuffer(), url.toString());
responseBody = rewriteJs(await response.arrayBuffer(), url);
// Disable threading for now, it's causing issues.
// responseBody = await this.threadpool.rewriteJs(responseBody, url.toString());
break;
case "style":
responseBody = rewriteCss(await response.text(), url);
@ -137,9 +134,7 @@ export async function swfetch(this: ScramjetServiceWorker, { request }: FetchEve
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 it"s invalid then we can still possibly test for the attachment/inline type
const type = /^\s*?attachment/i.test(header)
? "attachment"
: "inline";
const type = /^\s*?attachment/i.test(header) ? "attachment" : "inline";
// set the filename
const [filename] = new URL(response.finalURL).pathname

View file

@ -18,7 +18,6 @@ export class ScramjetServiceWorker {
this.config = config;
this.threadpool = new ScramjetThreadpool();
}
route({ request }: FetchEvent) {
@ -28,7 +27,6 @@ export class ScramjetServiceWorker {
}
public fetch = swfetch;
};
}
self.ScramjetServiceWorker = ScramjetServiceWorker;

View file

@ -1,11 +1,10 @@
type Thread = {
handle: MessagePort;
ready: boolean;
busy: boolean;
syncToken: number;
promises: Map<number, { resolve, reject }>;
}
promises: Map<number, { resolve; reject }>;
};
export class ScramjetThreadpool {
threads: Thread[] = [];
@ -23,8 +22,8 @@ export class ScramjetThreadpool {
ready: false,
busy: false,
syncToken: 0,
promises: new Map()
}
promises: new Map(),
};
this.threads.push(thread);
@ -47,14 +46,14 @@ export class ScramjetThreadpool {
} else {
resolve(result);
}
}
};
thread.handle.start();
}
pick(): Thread | undefined {
const alive = this.threads.filter(t => t.ready);
const idle = alive.filter(t => !t.busy);
const alive = this.threads.filter((t) => t.ready);
const idle = alive.filter((t) => !t.busy);
// no threads
if (!alive.length) return;
@ -71,21 +70,16 @@ export class ScramjetThreadpool {
if (!thread) throw new Error("No threads available");
thread.busy = true;
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 });
thread.handle.postMessage([
task,
...args
], transferrable);
thread.handle.postMessage([task, ...args], transferrable);
});
}
async rewriteJs(js: ArrayBuffer, origin: string): Promise<string> {
return await this.run("rewriteJs", [js, origin], [js]);
}

View file

@ -1,18 +1,20 @@
navigator.serviceWorker
.register("./sw.js")
.then((reg) => {
navigator.serviceWorker.register("./sw.js").then((reg) => {
reg.update();
});
navigator.serviceWorker.ready.then((reg) => {
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
}, [thread.port]);
handle: thread.port,
},
[thread.port]
);
}
});