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";
navigator.sendBeacon = new Proxy(navigator.sendBeacon, {
apply(target, thisArg, argArray) {
argArray[0] = encodeUrl(argArray[0]);
return Reflect.apply(target, thisArg, argArray);
},
import { encodeUrl } from "./shared";
navigator.sendBeacon = new Proxy(navigator.sendBeacon, {
apply(target, thisArg, argArray) {
argArray[0] = encodeUrl(argArray[0]);
return Reflect.apply(target, thisArg, argArray);
},
});

View file

@ -1,12 +1,12 @@
import { rewriteCss } from "./shared";
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"];
CSSStyleDeclaration.prototype.setProperty = new Proxy(CSSStyleDeclaration.prototype.setProperty, {
apply(target, thisArg, argArray) {
if (cssProperties.includes(argArray[0])) argArray[1] = rewriteCss(argArray[1]);
return Reflect.apply(target, thisArg, argArray);
},
import { rewriteCss } from "./shared";
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"];
CSSStyleDeclaration.prototype.setProperty = new Proxy(CSSStyleDeclaration.prototype.setProperty, {
apply(target, thisArg, argArray) {
if (cssProperties.includes(argArray[0])) argArray[1] = rewriteCss(argArray[1]);
return Reflect.apply(target, thisArg, argArray);
},
});

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,34 +1,34 @@
import { encodeUrl } from "./shared";
Worker = new Proxy(Worker, {
construct(target, argArray) {
argArray[0] = encodeUrl(argArray[0]);
// target is a reference to the object that you are proxying
// Reflect.construct is just a wrapper for calling target
// you could do new target(...argArray) and it would work the same effectively
return Reflect.construct(target, argArray);
}
})
Worklet.prototype.addModule = new Proxy(Worklet.prototype.addModule, {
apply(target, thisArg, argArray) {
argArray[0] = encodeUrl(argArray[0])
return Reflect.apply(target, thisArg, argArray);
},
});
// broken
// window.importScripts = new Proxy(window.importScripts, {
// apply(target, thisArg, argArray) {
// for (const i in argArray) {
// argArray[i] = encodeUrl(argArray[i]);
// }
// return Reflect.apply(target, thisArg, argArray);
// },
import { encodeUrl } from "./shared";
Worker = new Proxy(Worker, {
construct(target, argArray) {
argArray[0] = encodeUrl(argArray[0]);
// target is a reference to the object that you are proxying
// Reflect.construct is just a wrapper for calling target
// you could do new target(...argArray) and it would work the same effectively
return Reflect.construct(target, argArray);
}
})
Worklet.prototype.addModule = new Proxy(Worklet.prototype.addModule, {
apply(target, thisArg, argArray) {
argArray[0] = encodeUrl(argArray[0])
return Reflect.apply(target, thisArg, argArray);
},
});
// broken
// window.importScripts = new Proxy(window.importScripts, {
// apply(target, thisArg, argArray) {
// for (const i in argArray) {
// argArray[i] = encodeUrl(argArray[i]);
// }
// 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";
// for some reason eslint was parsing the type inside of the function params as a variable
export interface Codec {
// eslint-disable-next-line
encode: (str: string | undefined) => string;
// eslint-disable-next-line
decode: (str: string | undefined) => string;
}
const xor = {
encode: (str: string | undefined, key: number = 2) => {
if (!str) return str;
return encodeURIComponent(str.split("").map((e, i) => i % key ? String.fromCharCode(e.charCodeAt(0) ^ key) : e).join(""));
},
decode: (str: string | undefined, key: number = 2) => {
if (!str) return str;
return decodeURIComponent(str).split("").map((e, i) => i % key ? String.fromCharCode(e.charCodeAt(0) ^ key) : e).join("");
}
}
const plain = {
encode: (str: string | undefined) => {
if (!str) return str;
return encodeURIComponent(str);
},
decode: (str: string | undefined) => {
if (!str) return str;
return decodeURIComponent(str);
}
}
/*
const aes = {
encode: (str: string | undefined) => {
if (!str) return str;
return encodeURIComponent(enc(str, "dynamic").substring(10));
},
decode: (str: string | undefined) => {
if (!str) return str;
return dec("U2FsdGVkX1" + decodeURIComponent(str), "dynamic");
}
}
*/
const none = {
encode: (str: string | undefined) => str,
decode: (str: string | undefined) => str,
}
const base64 = {
encode: (str: string | undefined) => {
if (!str) return str;
return decodeURIComponent(btoa(str));
},
decode: (str: string | undefined) => {
if (!str) return str;
return atob(str);
}
}
if (!self.$scramjet) {
//@ts-expect-error really dumb workaround
self.$scramjet = {}
}
self.$scramjet.codecs = {
none, plain, base64, xor
import { enc, dec } from "./aes";
// for some reason eslint was parsing the type inside of the function params as a variable
export interface Codec {
// eslint-disable-next-line
encode: (str: string | undefined) => string;
// eslint-disable-next-line
decode: (str: string | undefined) => string;
}
const xor = {
encode: (str: string | undefined, key: number = 2) => {
if (!str) return str;
return encodeURIComponent(str.split("").map((e, i) => i % key ? String.fromCharCode(e.charCodeAt(0) ^ key) : e).join(""));
},
decode: (str: string | undefined, key: number = 2) => {
if (!str) return str;
return decodeURIComponent(str).split("").map((e, i) => i % key ? String.fromCharCode(e.charCodeAt(0) ^ key) : e).join("");
}
}
const plain = {
encode: (str: string | undefined) => {
if (!str) return str;
return encodeURIComponent(str);
},
decode: (str: string | undefined) => {
if (!str) return str;
return decodeURIComponent(str);
}
}
/*
const aes = {
encode: (str: string | undefined) => {
if (!str) return str;
return encodeURIComponent(enc(str, "dynamic").substring(10));
},
decode: (str: string | undefined) => {
if (!str) return str;
return dec("U2FsdGVkX1" + decodeURIComponent(str), "dynamic");
}
}
*/
const none = {
encode: (str: string | undefined) => str,
decode: (str: string | undefined) => str,
}
const base64 = {
encode: (str: string | undefined) => {
if (!str) return str;
return decodeURIComponent(btoa(str));
},
decode: (str: string | undefined) => {
if (!str) return str;
return atob(str);
}
}
if (!self.$scramjet) {
//@ts-expect-error really dumb workaround
self.$scramjet = {}
}
self.$scramjet.codecs = {
none, plain, base64, xor
}

View file

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

View file

@ -1,212 +1,212 @@
import { BareResponseFetch } from "@mercuryworkshop/bare-mux";
import IDBMap from "@webreflection/idb-map";
declare global {
interface Window {
ScramjetServiceWorker;
}
}
self.ScramjetServiceWorker = class ScramjetServiceWorker {
client: typeof self.$scramjet.shared.BareClient.prototype;
config: typeof self.$scramjet.config;
constructor(config = self.$scramjet.config) {
this.client = new self.$scramjet.shared.BareClient();
if (!config.prefix) config.prefix = "/scramjet/";
this.config = config;
}
route({ request }: FetchEvent) {
if (request.url.startsWith(location.origin + this.config.prefix)) return true;
else return false;
}
async fetch({ request }: FetchEvent) {
const urlParam = new URLSearchParams(new URL(request.url).search);
const { encodeUrl, decodeUrl, rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } = self.$scramjet.shared;
if (urlParam.has("url")) {
return Response.redirect(encodeUrl(urlParam.get("url"), new URL(urlParam.get("url"))))
}
try {
const url = new URL(decodeUrl(request.url));
const cookieStore = new IDBMap(url.origin, { durability: "relaxed", prefix: "Cookies" });
const response: BareResponseFetch = await this.client.fetch(url, {
method: request.method,
body: request.body,
headers: request.headers,
credentials: "omit",
mode: request.mode === "cors" ? request.mode : "same-origin",
cache: request.cache,
redirect: request.redirect,
//@ts-ignore why the fuck is this not typed mircosoft
duplex: "half",
});
let responseBody;
const responseHeaders = rewriteHeaders(response.rawHeaders, url);
for (const cookie of (responseHeaders["set-cookie"] || []) as string[]) {
let cookieParsed = cookie.split(";").map(x=>x.trim().split("="));
let [key, value] = cookieParsed.shift();
value = value.replace("\"", "");
cookieStore.set(key, { value: value, args: cookieParsed });
}
if (response.body) {
switch (request.destination) {
case "iframe":
case "document":
if (responseHeaders["content-type"].toString().startsWith("text/html")) {
responseBody = rewriteHtml(await response.text(), url);
} else {
responseBody = response.body;
}
break;
case "script":
responseBody = rewriteJs(await response.text(), url);
break;
case "style":
responseBody = rewriteCss(await response.text(), url);
break;
case "sharedworker":
case "worker":
responseBody = rewriteWorkers(await response.text(), url);
break;
default:
responseBody = response.body;
break;
}
}
// downloads
if (["document", "iframe"].includes(request.destination)) {
const header = responseHeaders["content-disposition"];
// validate header and test for filename
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";
// set the filename
const [filename] = new URL(response.finalURL).pathname
.split("/")
.slice(-1);
responseHeaders[
"content-disposition"
] = `${type}; filename=${JSON.stringify(filename)}`;
}
}
if (responseHeaders["accept"] === "text/event-stream") {
responseHeaders["content-type"] = "text/event-stream";
}
if (crossOriginIsolated) {
responseHeaders["Cross-Origin-Embedder-Policy"] = "require-corp";
}
return new Response(responseBody, {
headers: responseHeaders as HeadersInit,
status: response.status,
statusText: response.statusText
})
} catch (err) {
if (!["document", "iframe"].includes(request.destination))
return new Response(undefined, { status: 500 });
console.error(err);
return renderError(err, decodeUrl(request.url));
}
}
}
function errorTemplate(
trace: string,
fetchedURL: string,
) {
// turn script into a data URI so we don"t have to escape any HTML values
const script = `
errorTrace.value = ${JSON.stringify(trace)};
fetchedURL.textContent = ${JSON.stringify(fetchedURL)};
for (const node of document.querySelectorAll("#hostname")) node.textContent = ${JSON.stringify(
location.hostname
)};
reload.addEventListener("click", () => location.reload());
version.textContent = "0.0.1";
`
return (
`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Error</title>
<style>
* { background-color: white }
</style>
</head>
<body>
<h1 id="errorTitle">Error processing your request</h1>
<hr />
<p>Failed to load <b id="fetchedURL"></b></p>
<p id="errorMessage">Internal Server Error</p>
<textarea id="errorTrace" cols="40" rows="10" readonly></textarea>
<p>Try:</p>
<ul>
<li>Checking your internet connection</li>
<li>Verifying you entered the correct address</li>
<li>Clearing the site data</li>
<li>Contacting <b id="hostname"></b>"s administrator</li>
<li>Verify the server isn"t censored</li>
</ul>
<p>If you"re the administrator of <b id="hostname"></b>, try:</p>
<ul>
<li>Restarting your server</li>
<li>Updating Scramjet</li>
<li>Troubleshooting the error on the <a href="https://github.com/MercuryWorkshop/scramjet" target="_blank">GitHub repository</a></li>
</ul>
<button id="reload">Reload</button>
<hr />
<p><i>Scramjet v<span id="version"></span></i></p>
<script src="${
"data:application/javascript," + encodeURIComponent(script)
}"></script>
</body>
</html>
`
);
}
/**
*
* @param {unknown} err
* @param {string} fetchedURL
*/
function renderError(err, fetchedURL) {
const headers = {
"content-type": "text/html",
};
if (crossOriginIsolated) {
headers["Cross-Origin-Embedd'er-Policy"] = "require-corp";
}
return new Response(
errorTemplate(
String(err),
fetchedURL
),
{
status: 500,
headers: headers
}
);
}
import { BareResponseFetch } from "@mercuryworkshop/bare-mux";
import IDBMap from "@webreflection/idb-map";
declare global {
interface Window {
ScramjetServiceWorker;
}
}
self.ScramjetServiceWorker = class ScramjetServiceWorker {
client: typeof self.$scramjet.shared.BareClient.prototype;
config: typeof self.$scramjet.config;
constructor(config = self.$scramjet.config) {
this.client = new self.$scramjet.shared.BareClient();
if (!config.prefix) config.prefix = "/scramjet/";
this.config = config;
}
route({ request }: FetchEvent) {
if (request.url.startsWith(location.origin + this.config.prefix)) return true;
else return false;
}
async fetch({ request }: FetchEvent) {
const urlParam = new URLSearchParams(new URL(request.url).search);
const { encodeUrl, decodeUrl, rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } = self.$scramjet.shared;
if (urlParam.has("url")) {
return Response.redirect(encodeUrl(urlParam.get("url"), new URL(urlParam.get("url"))))
}
try {
const url = new URL(decodeUrl(request.url));
const cookieStore = new IDBMap(url.origin, { durability: "relaxed", prefix: "Cookies" });
const response: BareResponseFetch = await this.client.fetch(url, {
method: request.method,
body: request.body,
headers: request.headers,
credentials: "omit",
mode: request.mode === "cors" ? request.mode : "same-origin",
cache: request.cache,
redirect: request.redirect,
//@ts-ignore why the fuck is this not typed mircosoft
duplex: "half",
});
let responseBody;
const responseHeaders = rewriteHeaders(response.rawHeaders, url);
for (const cookie of (responseHeaders["set-cookie"] || []) as string[]) {
let cookieParsed = cookie.split(";").map(x=>x.trim().split("="));
let [key, value] = cookieParsed.shift();
value = value.replace("\"", "");
cookieStore.set(key, { value: value, args: cookieParsed });
}
if (response.body) {
switch (request.destination) {
case "iframe":
case "document":
if (responseHeaders["content-type"].toString().startsWith("text/html")) {
responseBody = rewriteHtml(await response.text(), url);
} else {
responseBody = response.body;
}
break;
case "script":
responseBody = rewriteJs(await response.text(), url);
break;
case "style":
responseBody = rewriteCss(await response.text(), url);
break;
case "sharedworker":
case "worker":
responseBody = rewriteWorkers(await response.text(), url);
break;
default:
responseBody = response.body;
break;
}
}
// downloads
if (["document", "iframe"].includes(request.destination)) {
const header = responseHeaders["content-disposition"];
// validate header and test for filename
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";
// set the filename
const [filename] = new URL(response.finalURL).pathname
.split("/")
.slice(-1);
responseHeaders[
"content-disposition"
] = `${type}; filename=${JSON.stringify(filename)}`;
}
}
if (responseHeaders["accept"] === "text/event-stream") {
responseHeaders["content-type"] = "text/event-stream";
}
if (crossOriginIsolated) {
responseHeaders["Cross-Origin-Embedder-Policy"] = "require-corp";
}
return new Response(responseBody, {
headers: responseHeaders as HeadersInit,
status: response.status,
statusText: response.statusText
})
} catch (err) {
if (!["document", "iframe"].includes(request.destination))
return new Response(undefined, { status: 500 });
console.error(err);
return renderError(err, decodeUrl(request.url));
}
}
}
function errorTemplate(
trace: string,
fetchedURL: string,
) {
// turn script into a data URI so we don"t have to escape any HTML values
const script = `
errorTrace.value = ${JSON.stringify(trace)};
fetchedURL.textContent = ${JSON.stringify(fetchedURL)};
for (const node of document.querySelectorAll("#hostname")) node.textContent = ${JSON.stringify(
location.hostname
)};
reload.addEventListener("click", () => location.reload());
version.textContent = "0.0.1";
`
return (
`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Error</title>
<style>
* { background-color: white }
</style>
</head>
<body>
<h1 id="errorTitle">Error processing your request</h1>
<hr />
<p>Failed to load <b id="fetchedURL"></b></p>
<p id="errorMessage">Internal Server Error</p>
<textarea id="errorTrace" cols="40" rows="10" readonly></textarea>
<p>Try:</p>
<ul>
<li>Checking your internet connection</li>
<li>Verifying you entered the correct address</li>
<li>Clearing the site data</li>
<li>Contacting <b id="hostname"></b>"s administrator</li>
<li>Verify the server isn"t censored</li>
</ul>
<p>If you"re the administrator of <b id="hostname"></b>, try:</p>
<ul>
<li>Restarting your server</li>
<li>Updating Scramjet</li>
<li>Troubleshooting the error on the <a href="https://github.com/MercuryWorkshop/scramjet" target="_blank">GitHub repository</a></li>
</ul>
<button id="reload">Reload</button>
<hr />
<p><i>Scramjet v<span id="version"></span></i></p>
<script src="${
"data:application/javascript," + encodeURIComponent(script)
}"></script>
</body>
</html>
`
);
}
/**
*
* @param {unknown} err
* @param {string} fetchedURL
*/
function renderError(err, fetchedURL) {
const headers = {
"content-type": "text/html",
};
if (crossOriginIsolated) {
headers["Cross-Origin-Embedd'er-Policy"] = "require-corp";
}
return new Response(
errorTemplate(
String(err),
fetchedURL
),
{
status: 500,
headers: headers
}
);
}