This commit is contained in:
Toshit Chawda 2024-07-14 13:52:20 -07:00
parent b91d4ad6cd
commit c6f7c4ecbb
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
17 changed files with 1539 additions and 1539 deletions

View file

@ -1,9 +1,9 @@
import { encodeUrl } from "./shared"; import { encodeUrl } from "./shared";
navigator.sendBeacon = new Proxy(navigator.sendBeacon, { navigator.sendBeacon = new Proxy(navigator.sendBeacon, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
argArray[0] = encodeUrl(argArray[0]); argArray[0] = encodeUrl(argArray[0]);
return Reflect.apply(target, thisArg, argArray); return Reflect.apply(target, thisArg, argArray);
}, },
}); });

View file

@ -1,12 +1,12 @@
import { rewriteCss } from "./shared"; import { rewriteCss } from "./shared";
const cssProperties = ["background", "background-image", "mask", "mask-image", "list-style", "list-style-image", "border-image", "border-image-source", "cursor"]; const cssProperties = ["background", "background-image", "mask", "mask-image", "list-style", "list-style-image", "border-image", "border-image-source", "cursor"];
// const jsProperties = ["background", "backgroundImage", "mask", "maskImage", "listStyle", "listStyleImage", "borderImage", "borderImageSource", "cursor"]; // const jsProperties = ["background", "backgroundImage", "mask", "maskImage", "listStyle", "listStyleImage", "borderImage", "borderImageSource", "cursor"];
CSSStyleDeclaration.prototype.setProperty = new Proxy(CSSStyleDeclaration.prototype.setProperty, { CSSStyleDeclaration.prototype.setProperty = new Proxy(CSSStyleDeclaration.prototype.setProperty, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
if (cssProperties.includes(argArray[0])) argArray[1] = rewriteCss(argArray[1]); if (cssProperties.includes(argArray[0])) argArray[1] = rewriteCss(argArray[1]);
return Reflect.apply(target, thisArg, argArray); return Reflect.apply(target, thisArg, argArray);
}, },
}); });

View file

@ -1,115 +1,115 @@
import { decodeUrl } from "../shared/rewriters/url"; import { decodeUrl } from "../shared/rewriters/url";
import { encodeUrl, rewriteCss, rewriteHtml, rewriteJs, rewriteSrcset } from "./shared"; import { encodeUrl, rewriteCss, rewriteHtml, rewriteJs, rewriteSrcset } from "./shared";
const attrObject = { const attrObject = {
"nonce": [HTMLElement], "nonce": [HTMLElement],
"integrity": [HTMLScriptElement, HTMLLinkElement], "integrity": [HTMLScriptElement, HTMLLinkElement],
"csp": [HTMLIFrameElement], "csp": [HTMLIFrameElement],
"src": [HTMLImageElement, HTMLMediaElement, HTMLIFrameElement, HTMLEmbedElement, HTMLScriptElement], "src": [HTMLImageElement, HTMLMediaElement, HTMLIFrameElement, HTMLEmbedElement, HTMLScriptElement],
"href": [HTMLAnchorElement, HTMLLinkElement], "href": [HTMLAnchorElement, HTMLLinkElement],
"data": [HTMLObjectElement], "data": [HTMLObjectElement],
"action": [HTMLFormElement], "action": [HTMLFormElement],
"formaction": [HTMLButtonElement, HTMLInputElement], "formaction": [HTMLButtonElement, HTMLInputElement],
"srcdoc": [HTMLIFrameElement], "srcdoc": [HTMLIFrameElement],
"srcset": [HTMLImageElement, HTMLSourceElement], "srcset": [HTMLImageElement, HTMLSourceElement],
"imagesrcset": [HTMLLinkElement] "imagesrcset": [HTMLLinkElement]
} }
const attrs = Object.keys(attrObject); const attrs = Object.keys(attrObject);
for (const attr of attrs) { for (const attr of attrs) {
for (const element of attrObject[attr]) { for (const element of attrObject[attr]) {
const descriptor = Object.getOwnPropertyDescriptor(element.prototype, attr); const descriptor = Object.getOwnPropertyDescriptor(element.prototype, attr);
Object.defineProperty(element.prototype, attr, { Object.defineProperty(element.prototype, attr, {
get() { get() {
if (/src|href|data|action|formaction/.test(attr)) { if (/src|href|data|action|formaction/.test(attr)) {
return decodeUrl(descriptor.get.call(this)); return decodeUrl(descriptor.get.call(this));
} }
if (this.__origattrs[attr]) { if (this.__origattrs[attr]) {
return this.__origattrs[attr]; return this.__origattrs[attr];
} }
return descriptor.get.call(this); return descriptor.get.call(this);
}, },
set(value) { set(value) {
this.__origattrs[attr] = value; this.__origattrs[attr] = value;
if (/nonce|integrity|csp/.test(attr)) { if (/nonce|integrity|csp/.test(attr)) {
return; return;
} else if (/src|href|data|action|formaction/.test(attr)) { } else if (/src|href|data|action|formaction/.test(attr)) {
// @ts-expect-error // @ts-expect-error
if (value instanceof TrustedScriptURL) { if (value instanceof TrustedScriptURL) {
return; return;
} }
value = encodeUrl(value); value = encodeUrl(value);
} else if (attr === "srcdoc") { } else if (attr === "srcdoc") {
value = rewriteHtml(value); value = rewriteHtml(value);
} else if (/(image)?srcset/.test(attr)) { } else if (/(image)?srcset/.test(attr)) {
value = rewriteSrcset(value); value = rewriteSrcset(value);
} }
descriptor.set.call(this, value); descriptor.set.call(this, value);
}, },
}); });
} }
} }
declare global { declare global {
interface Element { interface Element {
__origattrs: Record<string, string>; __origattrs: Record<string, string>;
} }
} }
Element.prototype.__origattrs = {}; Element.prototype.__origattrs = {};
Element.prototype.getAttribute = new Proxy(Element.prototype.getAttribute, { Element.prototype.getAttribute = new Proxy(Element.prototype.getAttribute, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
if (attrs.includes(argArray[0]) && thisArg.__origattrs[argArray[0]]) { if (attrs.includes(argArray[0]) && thisArg.__origattrs[argArray[0]]) {
return thisArg.__origattrs[argArray[0]]; return thisArg.__origattrs[argArray[0]];
} }
return Reflect.apply(target, thisArg, argArray); return Reflect.apply(target, thisArg, argArray);
}, },
}); });
Element.prototype.setAttribute = new Proxy(Element.prototype.setAttribute, { Element.prototype.setAttribute = new Proxy(Element.prototype.setAttribute, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
if (attrs.includes(argArray[0])) { if (attrs.includes(argArray[0])) {
thisArg.__origattrs[argArray[0]] = argArray[1]; thisArg.__origattrs[argArray[0]] = argArray[1];
if (/nonce|integrity|csp/.test(argArray[0])) { if (/nonce|integrity|csp/.test(argArray[0])) {
return; return;
} else if (/src|href|data|action|formaction/.test(argArray[0])) { } else if (/src|href|data|action|formaction/.test(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]);
} else if (/(image)?srcset/.test(argArray[0])) { } else if (/(image)?srcset/.test(argArray[0])) {
argArray[1] = rewriteSrcset(argArray[1]); argArray[1] = rewriteSrcset(argArray[1]);
} else if (argArray[1] === "style") { } else if (argArray[1] === "style") {
argArray[1] = rewriteCss(argArray[1]); argArray[1] = rewriteCss(argArray[1]);
} }
} }
return Reflect.apply(target, thisArg, argArray); return Reflect.apply(target, thisArg, argArray);
}, },
}); });
const innerHTML = Object.getOwnPropertyDescriptor(Element.prototype, "innerHTML"); const innerHTML = Object.getOwnPropertyDescriptor(Element.prototype, "innerHTML");
Object.defineProperty(Element.prototype, "innerHTML", { Object.defineProperty(Element.prototype, "innerHTML", {
set(value) { set(value) {
// @ts-expect-error // @ts-expect-error
if (this instanceof HTMLScriptElement && !(value instanceof TrustedScript)) { if (this instanceof HTMLScriptElement && !(value instanceof TrustedScript)) {
value = rewriteJs(value); value = rewriteJs(value);
} else if (this instanceof HTMLStyleElement) { } else if (this instanceof HTMLStyleElement) {
value = rewriteCss(value); value = rewriteCss(value);
// @ts-expect-error // @ts-expect-error
} else if (!(value instanceof TrustedHTML)) { } else if (!(value instanceof TrustedHTML)) {
value = rewriteHtml(value); value = rewriteHtml(value);
} }
return innerHTML.set.call(this, value); return innerHTML.set.call(this, value);
}, },
}) })

View file

@ -1,18 +1,18 @@
import { decodeUrl } from "./shared"; import { decodeUrl } from "./shared";
window.history.pushState = new Proxy(window.history.pushState, { window.history.pushState = new Proxy(window.history.pushState, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
argArray[3] = decodeUrl(argArray[3]); argArray[3] = decodeUrl(argArray[3]);
return Reflect.apply(target, thisArg, argArray); return Reflect.apply(target, thisArg, argArray);
}, },
}); });
window.history.replaceState = new Proxy(window.history.replaceState, { window.history.replaceState = new Proxy(window.history.replaceState, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
argArray[3] = decodeUrl(argArray[3]); argArray[3] = decodeUrl(argArray[3]);
return Reflect.apply(target, thisArg, argArray); return Reflect.apply(target, thisArg, argArray);
}, },
}); });

View file

@ -1,21 +1,21 @@
import "./scope.ts"; import "./scope.ts";
import "./window.ts"; import "./window.ts";
import "./event.ts"; import "./event.ts";
import "./native/eval.ts"; import "./native/eval.ts";
import "./location.ts"; import "./location.ts";
import "./trustedTypes.ts"; import "./trustedTypes.ts";
import "./requests/fetch.ts"; import "./requests/fetch.ts";
import "./requests/xmlhttprequest.ts"; import "./requests/xmlhttprequest.ts";
import "./requests/websocket.ts" import "./requests/websocket.ts"
import "./element.ts"; import "./element.ts";
import "./storage.ts"; import "./storage.ts";
import "./css.ts"; import "./css.ts";
import "./history.ts" import "./history.ts"
import "./worker.ts"; import "./worker.ts";
import "./url.ts"; import "./url.ts";
declare global { declare global {
interface Window { interface Window {
$s: any; $s: any;
} }
} }

View file

@ -1,33 +1,33 @@
// @ts-nocheck // @ts-nocheck
import { encodeUrl, decodeUrl } from "./shared"; import { encodeUrl, decodeUrl } from "./shared";
function createLocation() { function createLocation() {
const loc = new URL(decodeUrl(location.href)); const loc = new URL(decodeUrl(location.href));
loc.assign = (url: string) => location.assign(encodeUrl(url)); loc.assign = (url: string) => location.assign(encodeUrl(url));
loc.reload = () => location.reload(); loc.reload = () => location.reload();
loc.replace = (url: string) => location.replace(encodeUrl(url)); loc.replace = (url: string) => location.replace(encodeUrl(url));
loc.toString = () => loc.href; loc.toString = () => loc.href;
return loc; return loc;
} }
export const locationProxy = new Proxy(window.location, { export const locationProxy = new Proxy(window.location, {
get(target, prop) { get(target, prop) {
const loc = createLocation(); const loc = createLocation();
return loc[prop]; return loc[prop];
}, },
set(obj, prop, value) { set(obj, prop, value) {
const loc = createLocation(); const loc = createLocation();
if (prop === "href") { if (prop === "href") {
location.href = encodeUrl(value); location.href = encodeUrl(value);
} else { } else {
loc[prop] = value; loc[prop] = value;
} }
return true; return true;
} }
}) })

View file

@ -1,28 +1,28 @@
import { rewriteJs } from "../shared"; import { rewriteJs } from "../shared";
const FunctionProxy = new Proxy(Function, { const FunctionProxy = new Proxy(Function, {
construct(target, argArray) { construct(target, argArray) {
if (argArray.length === 1) { if (argArray.length === 1) {
return Reflect.construct(target, rewriteJs(argArray[0])); return Reflect.construct(target, rewriteJs(argArray[0]));
} else { } else {
return Reflect.construct(target, rewriteJs(argArray[argArray.length - 1])) return Reflect.construct(target, rewriteJs(argArray[argArray.length - 1]))
} }
}, },
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
if (argArray.length === 1) { if (argArray.length === 1) {
return Reflect.apply(target, undefined, [rewriteJs(argArray[0])]); return Reflect.apply(target, undefined, [rewriteJs(argArray[0])]);
} else { } else {
return Reflect.apply(target, undefined, [...argArray.map((x, index) => index === argArray.length - 1), rewriteJs(argArray[argArray.length - 1])]) return Reflect.apply(target, undefined, [...argArray.map((x, index) => index === argArray.length - 1), rewriteJs(argArray[argArray.length - 1])])
} }
}, },
}); });
delete window.Function; delete window.Function;
window.Function = FunctionProxy; window.Function = FunctionProxy;
window.eval = new Proxy(window.eval, { window.eval = new Proxy(window.eval, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
return Reflect.apply(target, thisArg, [rewriteJs(argArray[0])]); return Reflect.apply(target, thisArg, [rewriteJs(argArray[0])]);
}, },
}) })

View file

@ -1,35 +1,35 @@
// ts throws an error if you dont do window.fetch // ts throws an error if you dont do window.fetch
import { encodeUrl, rewriteHeaders } from "../shared"; import { encodeUrl, rewriteHeaders } from "../shared";
window.fetch = new Proxy(window.fetch, { window.fetch = new Proxy(window.fetch, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
argArray[0] = encodeUrl(argArray[0]); argArray[0] = encodeUrl(argArray[0]);
return Reflect.apply(target, thisArg, argArray); return Reflect.apply(target, thisArg, argArray);
}, },
}); });
Headers = new Proxy(Headers, { Headers = new Proxy(Headers, {
construct(target, argArray, newTarget) { construct(target, argArray, newTarget) {
argArray[0] = rewriteHeaders(argArray[0]); argArray[0] = rewriteHeaders(argArray[0]);
return Reflect.construct(target, argArray, newTarget); return Reflect.construct(target, argArray, newTarget);
}, },
}) })
Request = new Proxy(Request, { Request = new Proxy(Request, {
construct(target, argArray, newTarget) { construct(target, argArray, newTarget) {
if (typeof argArray[0] === "string") argArray[0] = encodeUrl(argArray[0]); if (typeof argArray[0] === "string") argArray[0] = encodeUrl(argArray[0]);
return Reflect.construct(target, argArray, newTarget); return Reflect.construct(target, argArray, newTarget);
}, },
}); });
Response.redirect = new Proxy(Response.redirect, { Response.redirect = new Proxy(Response.redirect, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
argArray[0] = encodeUrl(argArray[0]); argArray[0] = encodeUrl(argArray[0]);
return Reflect.apply(target, thisArg, argArray); return Reflect.apply(target, thisArg, argArray);
}, },
}); });

View file

@ -1,16 +1,16 @@
import { BareClient } from "../shared"; import { BareClient } from "../shared";
const client = new BareClient(); const client = new BareClient();
WebSocket = new Proxy(WebSocket, { WebSocket = new Proxy(WebSocket, {
construct(target, args) { construct(target, args) {
return client.createWebSocket( return client.createWebSocket(
args[0], args[0],
args[1], args[1],
target, target,
{ {
"User-Agent": navigator.userAgent "User-Agent": navigator.userAgent
}, },
ArrayBuffer.prototype ArrayBuffer.prototype
) )
} }
}) })

View file

@ -1,20 +1,20 @@
import { encodeUrl, rewriteHeaders } from "../shared"; import { encodeUrl, rewriteHeaders } from "../shared";
XMLHttpRequest.prototype.open = new Proxy(XMLHttpRequest.prototype.open, { XMLHttpRequest.prototype.open = new Proxy(XMLHttpRequest.prototype.open, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
if (argArray[1]) argArray[1] = encodeUrl(argArray[1]); if (argArray[1]) argArray[1] = encodeUrl(argArray[1]);
return Reflect.apply(target, thisArg, argArray); return Reflect.apply(target, thisArg, argArray);
}, },
}); });
XMLHttpRequest.prototype.setRequestHeader = new Proxy(XMLHttpRequest.prototype.setRequestHeader, { XMLHttpRequest.prototype.setRequestHeader = new Proxy(XMLHttpRequest.prototype.setRequestHeader, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
let headerObject = Object.fromEntries([argArray]); let headerObject = Object.fromEntries([argArray]);
headerObject = rewriteHeaders(headerObject); headerObject = rewriteHeaders(headerObject);
argArray = Object.entries(headerObject)[0]; argArray = Object.entries(headerObject)[0];
return Reflect.apply(target, thisArg, argArray); return Reflect.apply(target, thisArg, argArray);
}, },
}); });

View file

@ -1,71 +1,71 @@
import IDBMapSync from "@webreflection/idb-map/sync"; import IDBMapSync from "@webreflection/idb-map/sync";
import { locationProxy } from "./location"; import { locationProxy } from "./location";
const store = new IDBMapSync(locationProxy.host, { const store = new IDBMapSync(locationProxy.host, {
prefix: "Storage", prefix: "Storage",
durability: "relaxed" durability: "relaxed"
}); });
await store.sync(); await store.sync();
function storageProxy(scope: Storage): Storage { function storageProxy(scope: Storage): Storage {
return new Proxy(scope, { return new Proxy(scope, {
get(target, prop) { get(target, prop) {
switch (prop) { switch (prop) {
case "getItem": case "getItem":
return (key: string) => { return (key: string) => {
return store.get(key); return store.get(key);
} }
case "setItem": case "setItem":
return (key: string, value: string) => { return (key: string, value: string) => {
store.set(key, value); store.set(key, value);
store.sync(); store.sync();
} }
case "removeItem": case "removeItem":
return (key: string) => { return (key: string) => {
store.delete(key); store.delete(key);
store.sync(); store.sync();
} }
case "clear": case "clear":
return () => { return () => {
store.clear(); store.clear();
store.sync(); store.sync();
} }
case "key": case "key":
return (index: number) => { return (index: number) => {
store.keys()[index]; store.keys()[index];
} }
case "length": case "length":
return store.size; return store.size;
default: default:
return store.get(prop); return store.get(prop);
} }
}, },
//@ts-ignore //@ts-ignore
set(target, prop, value) { set(target, prop, value) {
store.set(prop, value); store.set(prop, value);
store.sync(); store.sync();
}, },
defineProperty(target, property, attributes) { defineProperty(target, property, attributes) {
store.set(property as string, attributes.value); store.set(property as string, attributes.value);
return true; return true;
}, },
}) })
} }
const localStorageProxy = storageProxy(window.localStorage); const localStorageProxy = storageProxy(window.localStorage);
const sessionStorageProxy = storageProxy(window.sessionStorage); const sessionStorageProxy = storageProxy(window.sessionStorage);
delete window.localStorage; delete window.localStorage;
delete window.sessionStorage; delete window.sessionStorage;
window.localStorage = localStorageProxy; window.localStorage = localStorageProxy;
window.sessionStorage = sessionStorageProxy; window.sessionStorage = sessionStorageProxy;

View file

@ -1,32 +1,32 @@
import { rewriteHtml, rewriteJs, encodeUrl } from "./shared"; import { rewriteHtml, rewriteJs, encodeUrl } from "./shared";
// @ts-expect-error // @ts-expect-error
trustedTypes.createPolicy = new Proxy(trustedTypes.createPolicy, { trustedTypes.createPolicy = new Proxy(trustedTypes.createPolicy, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
if (argArray[1].createHTML) { if (argArray[1].createHTML) {
argArray[1].createHTML = new Proxy(argArray[1].createHTML, { argArray[1].createHTML = new Proxy(argArray[1].createHTML, {
apply(target1, thisArg1, argArray1) { apply(target1, thisArg1, argArray1) {
return rewriteHtml(target1(...argArray1)); return rewriteHtml(target1(...argArray1));
}, },
}); });
} }
if (argArray[1].createScript) { if (argArray[1].createScript) {
argArray[1].createScript = new Proxy(argArray[1].createScript, { argArray[1].createScript = new Proxy(argArray[1].createScript, {
apply(target1, thisArg1, argArray1) { apply(target1, thisArg1, argArray1) {
return rewriteJs(target1(...argArray1)); return rewriteJs(target1(...argArray1));
}, },
}); });
} }
if (argArray[1].createScriptURL) { if (argArray[1].createScriptURL) {
argArray[1].createScriptURL = new Proxy(argArray[1].createScriptURL, { argArray[1].createScriptURL = new Proxy(argArray[1].createScriptURL, {
apply(target1, thisArg1, argArray1) { apply(target1, thisArg1, argArray1) {
return encodeUrl(target1(...argArray1)); return encodeUrl(target1(...argArray1));
}, },
}) })
} }
return Reflect.apply(target, thisArg, argArray); return Reflect.apply(target, thisArg, argArray);
}, },
}) })

View file

@ -1,34 +1,34 @@
import { encodeUrl } from "./shared"; import { encodeUrl } from "./shared";
Worker = new Proxy(Worker, { Worker = new Proxy(Worker, {
construct(target, argArray) { construct(target, argArray) {
argArray[0] = encodeUrl(argArray[0]); argArray[0] = encodeUrl(argArray[0]);
// target is a reference to the object that you are proxying // target is a reference to the object that you are proxying
// Reflect.construct is just a wrapper for calling target // Reflect.construct is just a wrapper for calling target
// you could do new target(...argArray) and it would work the same effectively // you could do new target(...argArray) and it would work the same effectively
return Reflect.construct(target, argArray); return Reflect.construct(target, argArray);
} }
}) })
Worklet.prototype.addModule = new Proxy(Worklet.prototype.addModule, { Worklet.prototype.addModule = new Proxy(Worklet.prototype.addModule, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
argArray[0] = encodeUrl(argArray[0]) argArray[0] = encodeUrl(argArray[0])
return Reflect.apply(target, thisArg, argArray); return Reflect.apply(target, thisArg, argArray);
}, },
}); });
// broken // broken
// window.importScripts = new Proxy(window.importScripts, { // window.importScripts = new Proxy(window.importScripts, {
// apply(target, thisArg, argArray) { // apply(target, thisArg, argArray) {
// for (const i in argArray) { // for (const i in argArray) {
// argArray[i] = encodeUrl(argArray[i]); // argArray[i] = encodeUrl(argArray[i]);
// } // }
// return Reflect.apply(target, thisArg, argArray); // return Reflect.apply(target, thisArg, argArray);
// }, // },
// }); // });

File diff suppressed because it is too large Load diff

View file

@ -1,76 +1,76 @@
import { enc, dec } from "./aes"; import { enc, dec } from "./aes";
// for some reason eslint was parsing the type inside of the function params as a variable // for some reason eslint was parsing the type inside of the function params as a variable
export interface Codec { export interface Codec {
// eslint-disable-next-line // eslint-disable-next-line
encode: (str: string | undefined) => string; encode: (str: string | undefined) => string;
// eslint-disable-next-line // eslint-disable-next-line
decode: (str: string | undefined) => string; decode: (str: string | undefined) => string;
} }
const xor = { const xor = {
encode: (str: string | undefined, key: number = 2) => { encode: (str: string | undefined, key: number = 2) => {
if (!str) return str; if (!str) return str;
return encodeURIComponent(str.split("").map((e, i) => i % key ? String.fromCharCode(e.charCodeAt(0) ^ key) : e).join("")); return encodeURIComponent(str.split("").map((e, i) => i % key ? String.fromCharCode(e.charCodeAt(0) ^ key) : e).join(""));
}, },
decode: (str: string | undefined, key: number = 2) => { decode: (str: string | undefined, key: number = 2) => {
if (!str) return str; if (!str) return str;
return decodeURIComponent(str).split("").map((e, i) => i % key ? String.fromCharCode(e.charCodeAt(0) ^ key) : e).join(""); return decodeURIComponent(str).split("").map((e, i) => i % key ? String.fromCharCode(e.charCodeAt(0) ^ key) : e).join("");
} }
} }
const plain = { const plain = {
encode: (str: string | undefined) => { encode: (str: string | undefined) => {
if (!str) return str; if (!str) return str;
return encodeURIComponent(str); return encodeURIComponent(str);
}, },
decode: (str: string | undefined) => { decode: (str: string | undefined) => {
if (!str) return str; if (!str) return str;
return decodeURIComponent(str); return decodeURIComponent(str);
} }
} }
/* /*
const aes = { const aes = {
encode: (str: string | undefined) => { encode: (str: string | undefined) => {
if (!str) return str; if (!str) return str;
return encodeURIComponent(enc(str, "dynamic").substring(10)); return encodeURIComponent(enc(str, "dynamic").substring(10));
}, },
decode: (str: string | undefined) => { decode: (str: string | undefined) => {
if (!str) return str; if (!str) return str;
return dec("U2FsdGVkX1" + decodeURIComponent(str), "dynamic"); return dec("U2FsdGVkX1" + decodeURIComponent(str), "dynamic");
} }
} }
*/ */
const none = { const none = {
encode: (str: string | undefined) => str, encode: (str: string | undefined) => str,
decode: (str: string | undefined) => str, decode: (str: string | undefined) => str,
} }
const base64 = { const base64 = {
encode: (str: string | undefined) => { encode: (str: string | undefined) => {
if (!str) return str; if (!str) return str;
return decodeURIComponent(btoa(str)); return decodeURIComponent(btoa(str));
}, },
decode: (str: string | undefined) => { decode: (str: string | undefined) => {
if (!str) return str; if (!str) return str;
return atob(str); return atob(str);
} }
} }
if (!self.$scramjet) { if (!self.$scramjet) {
//@ts-expect-error really dumb workaround //@ts-expect-error really dumb workaround
self.$scramjet = {} self.$scramjet = {}
} }
self.$scramjet.codecs = { self.$scramjet.codecs = {
none, plain, base64, xor none, plain, base64, xor
} }

View file

@ -1,13 +1,13 @@
if (!self.$scramjet) { if (!self.$scramjet) {
//@ts-expect-error really dumb workaround //@ts-expect-error really dumb workaround
self.$scramjet = {} self.$scramjet = {}
} }
self.$scramjet.config = { self.$scramjet.config = {
prefix: "/scramjet/", prefix: "/scramjet/",
codec: self.$scramjet.codecs.plain, codec: self.$scramjet.codecs.plain,
config: "/scram/scramjet.config.js", config: "/scram/scramjet.config.js",
shared: "/scram/scramjet.shared.js", shared: "/scram/scramjet.shared.js",
worker: "/scram/scramjet.worker.js", worker: "/scram/scramjet.worker.js",
client: "/scram/scramjet.client.js", client: "/scram/scramjet.client.js",
codecs: "/scram/scramjet.codecs.js" codecs: "/scram/scramjet.codecs.js"
} }

View file

@ -1,212 +1,212 @@
import { BareResponseFetch } from "@mercuryworkshop/bare-mux"; import { BareResponseFetch } from "@mercuryworkshop/bare-mux";
import IDBMap from "@webreflection/idb-map"; import IDBMap from "@webreflection/idb-map";
declare global { declare global {
interface Window { interface Window {
ScramjetServiceWorker; ScramjetServiceWorker;
} }
} }
self.ScramjetServiceWorker = class ScramjetServiceWorker { self.ScramjetServiceWorker = class ScramjetServiceWorker {
client: typeof self.$scramjet.shared.BareClient.prototype; client: typeof self.$scramjet.shared.BareClient.prototype;
config: typeof self.$scramjet.config; config: typeof self.$scramjet.config;
constructor(config = self.$scramjet.config) { constructor(config = self.$scramjet.config) {
this.client = new self.$scramjet.shared.BareClient(); this.client = new self.$scramjet.shared.BareClient();
if (!config.prefix) config.prefix = "/scramjet/"; if (!config.prefix) config.prefix = "/scramjet/";
this.config = config; this.config = config;
} }
route({ request }: FetchEvent) { route({ request }: FetchEvent) {
if (request.url.startsWith(location.origin + this.config.prefix)) return true; if (request.url.startsWith(location.origin + this.config.prefix)) return true;
else return false; else return false;
} }
async fetch({ request }: FetchEvent) { async fetch({ request }: FetchEvent) {
const urlParam = new URLSearchParams(new URL(request.url).search); const urlParam = new URLSearchParams(new URL(request.url).search);
const { encodeUrl, decodeUrl, rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } = self.$scramjet.shared; const { encodeUrl, decodeUrl, rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } = self.$scramjet.shared;
if (urlParam.has("url")) { if (urlParam.has("url")) {
return Response.redirect(encodeUrl(urlParam.get("url"), new URL(urlParam.get("url")))) return Response.redirect(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.origin, { durability: "relaxed", prefix: "Cookies" }); const cookieStore = new IDBMap(url.origin, { durability: "relaxed", 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();
value = value.replace("\"", ""); value = value.replace("\"", "");
cookieStore.set(key, { value: value, args: cookieParsed }); cookieStore.set(key, { value: value, args: cookieParsed });
} }
if (response.body) { if (response.body) {
switch (request.destination) { switch (request.destination) {
case "iframe": case "iframe":
case "document": case "document":
if (responseHeaders["content-type"].toString().startsWith("text/html")) { if (responseHeaders["content-type"].toString().startsWith("text/html")) {
responseBody = rewriteHtml(await response.text(), url); responseBody = rewriteHtml(await response.text(), url);
} else { } else {
responseBody = response.body; responseBody = response.body;
} }
break; break;
case "script": case "script":
responseBody = rewriteJs(await response.text(), url); responseBody = rewriteJs(await response.text(), url);
break; break;
case "style": case "style":
responseBody = rewriteCss(await response.text(), url); responseBody = rewriteCss(await response.text(), url);
break; break;
case "sharedworker": case "sharedworker":
case "worker": case "worker":
responseBody = rewriteWorkers(await response.text(), url); responseBody = rewriteWorkers(await response.text(), url);
break; break;
default: default:
responseBody = response.body; responseBody = response.body;
break; break;
} }
} }
// downloads // downloads
if (["document", "iframe"].includes(request.destination)) { if (["document", "iframe"].includes(request.destination)) {
const header = responseHeaders["content-disposition"]; 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" ? "attachment"
: "inline"; : "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[ responseHeaders[
"content-disposition" "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) {
if (!["document", "iframe"].includes(request.destination)) if (!["document", "iframe"].includes(request.destination))
return new Response(undefined, { status: 500 }); return new Response(undefined, { status: 500 });
console.error(err); console.error(err);
return renderError(err, decodeUrl(request.url)); return renderError(err, decodeUrl(request.url));
} }
} }
} }
function errorTemplate( function errorTemplate(
trace: string, trace: string,
fetchedURL: 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 ( return (
`<!DOCTYPE html> `<!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<title>Error</title> <title>Error</title>
<style> <style>
* { background-color: white } * { background-color: white }
</style> </style>
</head> </head>
<body> <body>
<h1 id="errorTitle">Error processing your request</h1> <h1 id="errorTitle">Error processing your request</h1>
<hr /> <hr />
<p>Failed to load <b id="fetchedURL"></b></p> <p>Failed to load <b id="fetchedURL"></b></p>
<p id="errorMessage">Internal Server Error</p> <p id="errorMessage">Internal Server Error</p>
<textarea id="errorTrace" cols="40" rows="10" readonly></textarea> <textarea id="errorTrace" cols="40" rows="10" readonly></textarea>
<p>Try:</p> <p>Try:</p>
<ul> <ul>
<li>Checking your internet connection</li> <li>Checking your internet connection</li>
<li>Verifying you entered the correct address</li> <li>Verifying you entered the correct address</li>
<li>Clearing the site data</li> <li>Clearing the site data</li>
<li>Contacting <b id="hostname"></b>"s administrator</li> <li>Contacting <b id="hostname"></b>"s administrator</li>
<li>Verify the server isn"t censored</li> <li>Verify the server isn"t censored</li>
</ul> </ul>
<p>If you"re the administrator of <b id="hostname"></b>, try:</p> <p>If you"re the administrator of <b id="hostname"></b>, try:</p>
<ul> <ul>
<li>Restarting your server</li> <li>Restarting your server</li>
<li>Updating Scramjet</li> <li>Updating Scramjet</li>
<li>Troubleshooting the error on the <a href="https://github.com/MercuryWorkshop/scramjet" target="_blank">GitHub repository</a></li> <li>Troubleshooting the error on the <a href="https://github.com/MercuryWorkshop/scramjet" target="_blank">GitHub repository</a></li>
</ul> </ul>
<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="${ <script src="${
"data:application/javascript," + encodeURIComponent(script) "data:application/javascript," + encodeURIComponent(script)
}"></script> }"></script>
</body> </body>
</html> </html>
` `
); );
} }
/** /**
* *
* @param {unknown} err * @param {unknown} err
* @param {string} fetchedURL * @param {string} fetchedURL
*/ */
function renderError(err, fetchedURL) { function renderError(err, fetchedURL) {
const headers = { const headers = {
"content-type": "text/html", "content-type": "text/html",
}; };
if (crossOriginIsolated) { if (crossOriginIsolated) {
headers["Cross-Origin-Embedd'er-Policy"] = "require-corp"; headers["Cross-Origin-Embedd'er-Policy"] = "require-corp";
} }
return new Response( return new Response(
errorTemplate( errorTemplate(
String(err), String(err),
fetchedURL fetchedURL
), ),
{ {
status: 500, status: 500,
headers: headers headers: headers
} }
); );
} }