This commit is contained in:
velzie 2024-07-14 19:17:49 -04:00
parent 4b0337db0c
commit 55b6666229
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
43 changed files with 4693 additions and 2712 deletions

View file

@ -7,15 +7,17 @@
"no-unused-labels": "error",
"no-unused-vars": "error",
"quotes": ["error", "double"],
"max-lines-per-function": ["error", {
"max-lines-per-function": [
"error",
{
"max": 200,
"skipComments": true
}],
}
],
"getter-return": "error",
"newline-before-return": "error",
"no-multiple-empty-lines": "error",
"no-var": "error",
"indent": ["warn", "tab"],
"no-this-before-super": "warn",
"no-useless-return": "error",
"no-shadow": "error",

View file

@ -7,11 +7,11 @@ It currently does not support most websites due to it being very early in the de
The UI is not finalized and only used as a means to test the web proxy.
## How to build
Running `pnpm dev` will build Scramjet and start a dev server on localhost:1337. If you only want to build the proxy without using the dev server, run `pnpm build`.
## TODO
- Finish HTML rewriting
- `<script type="importmap"></script>` rewriting
- Make an array of all possible import values and pass the array onto the JS rewriter, then rewrite all the URLs inside of it

4846
pnpm-lock.yaml generated

File diff suppressed because it is too large Load diff

6
prettier.json Normal file
View file

@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"useTabs": true,
"semi": false,
"singleQuote": false
}

View file

@ -27,19 +27,19 @@ export default defineConfig({
options: {
jsc: {
parser: {
syntax: "typescript"
}
}
syntax: "typescript",
},
type: "javascript/auto"
}
]
},
},
type: "javascript/auto",
},
],
},
output: {
filename: "scramjet.[name].js",
path: join(__dirname, "dist"),
iife: true,
clean: true
clean: true,
},
plugins: [
// new RsdoctorRspackPlugin({
@ -49,5 +49,5 @@ export default defineConfig({
// }
// })
],
watch: true
watch: true,
});

View file

@ -4,17 +4,17 @@ import { createServer } from "http";
import Fastify from "fastify";
import fastifyStatic from "@fastify/static";
import { join } from "node:path";
import { spawn } from "node:child_process"
import { spawn } from "node:child_process";
import { fileURLToPath } from "node:url";
//transports
import { baremuxPath } from "@mercuryworkshop/bare-mux/node"
import { epoxyPath } from "@mercuryworkshop/epoxy-transport"
import { libcurlPath } from "@mercuryworkshop/libcurl-transport"
import { bareModulePath } from "@mercuryworkshop/bare-as-module3"
import { baremuxPath } from "@mercuryworkshop/bare-mux/node";
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
import { bareModulePath } from "@mercuryworkshop/bare-as-module3";
const bare = createBareServer("/bare/", {
logErrors: true
logErrors: true,
});
const fastify = Fastify({
@ -26,50 +26,54 @@ const fastify = Fastify({
} else {
handler(req, res);
}
}).on("upgrade", (req, socket, head) => {
})
.on("upgrade", (req, socket, head) => {
if (bare.shouldRoute(req)) {
bare.routeUpgrade(req, socket, head);
} else {
socket.end();
}
})
}
});
},
});
fastify.register(fastifyStatic, {
root: join(fileURLToPath(new URL(".", import.meta.url)), "./static"),
decorateReply: false
decorateReply: false,
});
fastify.register(fastifyStatic, {
root: join(fileURLToPath(new URL(".", import.meta.url)), "./dist"),
prefix: "/scram/",
decorateReply: false
})
decorateReply: false,
});
fastify.register(fastifyStatic, {
root: baremuxPath,
prefix: "/baremux/",
decorateReply: false
})
decorateReply: false,
});
fastify.register(fastifyStatic, {
root: epoxyPath,
prefix: "/epoxy/",
decorateReply: false
})
decorateReply: false,
});
fastify.register(fastifyStatic, {
root: libcurlPath,
prefix: "/libcurl/",
decorateReply: false
})
decorateReply: false,
});
fastify.register(fastifyStatic, {
root: bareModulePath,
prefix: "/baremod/",
decorateReply: false
})
decorateReply: false,
});
fastify.listen({
port: process.env.PORT || 1337
port: process.env.PORT || 1337,
});
const watch = spawn("pnpm", ["rspack", "-w"], { detached: true, cwd: process.cwd() });
const watch = spawn("pnpm", ["rspack", "-w"], {
detached: true,
cwd: process.cwd(),
});
watch.stdout.on("data", (data) => {
console.log(`${data}`);

View file

@ -1,12 +1,26 @@
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"];
CSSStyleDeclaration.prototype.setProperty = new Proxy(CSSStyleDeclaration.prototype.setProperty, {
CSSStyleDeclaration.prototype.setProperty = new Proxy(
CSSStyleDeclaration.prototype.setProperty,
{
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);
},
});
},
);

View file

@ -1,19 +1,31 @@
import { decodeUrl } from "../shared/rewriters/url";
import { encodeUrl, rewriteCss, rewriteHtml, rewriteJs, rewriteSrcset } from "./shared";
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]
}
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);
@ -96,12 +108,18 @@ Element.prototype.setAttribute = new Proxy(Element.prototype.setAttribute, {
},
});
const innerHTML = Object.getOwnPropertyDescriptor(Element.prototype, "innerHTML");
const innerHTML = Object.getOwnPropertyDescriptor(
Element.prototype,
"innerHTML",
);
Object.defineProperty(Element.prototype, "innerHTML", {
set(value) {
// @ts-expect-error
if (this instanceof HTMLScriptElement && !(value instanceof TrustedScript)) {
if (
this instanceof HTMLScriptElement &&
!(value instanceof TrustedScript)
) {
value = rewriteJs(value);
} else if (this instanceof HTMLStyleElement) {
value = rewriteCss(value);
@ -112,4 +130,4 @@ Object.defineProperty(Element.prototype, "innerHTML", {
return innerHTML.set.call(this, value);
},
})
});

View file

@ -8,7 +8,6 @@ window.history.pushState = new Proxy(window.history.pushState, {
},
});
window.history.replaceState = new Proxy(window.history.replaceState, {
apply(target, thisArg, argArray) {
argArray[3] = decodeUrl(argArray[3]);

View file

@ -6,11 +6,11 @@ import "./location.ts";
import "./trustedTypes.ts";
import "./requests/fetch.ts";
import "./requests/xmlhttprequest.ts";
import "./requests/websocket.ts"
import "./requests/websocket.ts";
import "./element.ts";
import "./storage.ts";
import "./css.ts";
import "./history.ts"
import "./history.ts";
import "./worker.ts";
import "./url.ts";

View file

@ -28,6 +28,5 @@ export const locationProxy = new Proxy(window.location, {
}
return true;
}
})
},
});

View file

@ -5,14 +5,20 @@ const FunctionProxy = new Proxy(Function, {
if (argArray.length === 1) {
return Reflect.construct(target, rewriteJs(argArray[0]));
} else {
return Reflect.construct(target, rewriteJs(argArray[argArray.length - 1]))
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])])
return Reflect.apply(target, undefined, [
...argArray.map((x, index) => index === argArray.length - 1),
rewriteJs(argArray[argArray.length - 1]),
]);
}
},
});
@ -25,4 +31,4 @@ window.eval = new Proxy(window.eval, {
apply(target, thisArg, argArray) {
return Reflect.apply(target, thisArg, [rewriteJs(argArray[0])]);
},
})
});

View file

@ -16,7 +16,7 @@ Headers = new Proxy(Headers, {
return Reflect.construct(target, argArray, newTarget);
},
})
});
Request = new Proxy(Request, {
construct(target, argArray, newTarget) {

View file

@ -8,9 +8,9 @@ WebSocket = new Proxy(WebSocket, {
args[1],
target,
{
"User-Agent": navigator.userAgent
"User-Agent": navigator.userAgent,
},
ArrayBuffer.prototype
)
}
})
ArrayBuffer.prototype,
);
},
});

View file

@ -8,7 +8,9 @@ XMLHttpRequest.prototype.open = new Proxy(XMLHttpRequest.prototype.open, {
},
});
XMLHttpRequest.prototype.setRequestHeader = new Proxy(XMLHttpRequest.prototype.setRequestHeader, {
XMLHttpRequest.prototype.setRequestHeader = new Proxy(
XMLHttpRequest.prototype.setRequestHeader,
{
apply(target, thisArg, argArray) {
let headerObject = Object.fromEntries([argArray]);
headerObject = rewriteHeaders(headerObject);
@ -17,4 +19,5 @@ XMLHttpRequest.prototype.setRequestHeader = new Proxy(XMLHttpRequest.prototype.s
return Reflect.apply(target, thisArg, argArray);
},
});
},
);

View file

@ -1,12 +1,6 @@
export const {
util: {
isScramjetFile,
BareClient
},
url: {
encodeUrl,
decodeUrl,
},
util: { isScramjetFile, BareClient },
url: { encodeUrl, decodeUrl },
rewrite: {
rewriteCss,
rewriteHtml,
@ -14,5 +8,5 @@ export const {
rewriteJs,
rewriteHeaders,
rewriteWorkers,
}
},
} = self.$scramjet.shared;

View file

@ -3,43 +3,42 @@ import { locationProxy } from "./location";
const store = new IDBMapSync(locationProxy.host, {
prefix: "Storage",
durability: "relaxed"
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:
@ -58,7 +57,7 @@ function storageProxy(scope: Storage): Storage {
return true;
},
})
});
}
const localStorageProxy = storageProxy(window.localStorage);

View file

@ -24,9 +24,9 @@ trustedTypes.createPolicy = new Proxy(trustedTypes.createPolicy, {
apply(target1, thisArg1, argArray1) {
return encodeUrl(target1(...argArray1));
},
})
});
}
return Reflect.apply(target, thisArg, argArray);
},
})
});

View file

@ -9,5 +9,5 @@ if (globalThis.window) {
return Reflect.construct(target, argArray, newTarget);
},
})
});
}

View file

@ -5,18 +5,21 @@ export const windowProxy = new Proxy(window, {
const propIsString = typeof prop === "string";
if (propIsString && prop === "location") {
return locationProxy;
} else if (propIsString && ["window", "top", "parent", "self", "globalThis"].includes(prop)) {
} else if (
propIsString &&
["window", "top", "parent", "self", "globalThis"].includes(prop)
) {
return windowProxy;
} else if (propIsString && prop === "$scramjet") {
return;
} else if (propIsString && prop === "addEventListener") {
console.log("addEventListener getteetetetetet")
console.log("addEventListener getteetetetetet");
return new Proxy(window.addEventListener, {
apply(target1, thisArg, argArray) {
window.addEventListener(argArray[0], argArray[1]);
},
})
});
}
const value = Reflect.get(target, prop);
@ -30,7 +33,12 @@ export const windowProxy = new Proxy(window, {
set(target, prop, newValue) {
// ensures that no apis are overwritten
if (typeof prop === "string" && ["window", "top", "parent", "self", "globalThis", "location"].includes(prop)) {
if (
typeof prop === "string" &&
["window", "top", "parent", "self", "globalThis", "location"].includes(
prop,
)
) {
return false;
}

View file

@ -9,18 +9,17 @@ Worker = new Proxy(Worker, {
// 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])
argArray[0] = encodeUrl(argArray[0]);
return Reflect.apply(target, thisArg, argArray);
},
});
// broken
// window.importScripts = new Proxy(window.importScripts, {

View file

@ -9,14 +9,14 @@ function enc_utf8(s) {
} catch (e) {
throw "Error on UTF-8 encode";
}
};
}
function dec_utf8(s) {
try {
return decodeURIComponent(escape(s));
} catch (e) {
throw "Bad Key";
}
};
}
function padBlock(byteArr) {
var array = [],
cpad,
@ -46,7 +46,7 @@ function padBlock(byteArr) {
array[i] = byteArr[i];
}
return array;
};
}
function block2s(block, lastBlock) {
var string = "",
padding,
@ -68,7 +68,7 @@ function block2s(block, lastBlock) {
}
}
return string;
};
}
function a2h(numArr) {
var string = "",
i;
@ -76,14 +76,14 @@ function a2h(numArr) {
string += (numArr[i] < 16 ? "0" : "") + numArr[i].toString(16);
}
return string;
};
}
function h2a(s) {
var ret = [];
s.replace(/(..)/g, function (s) {
ret.push(parseInt(s, 16));
});
return ret;
};
}
function s2a(string, binary) {
var array = [],
i;
@ -97,7 +97,7 @@ function s2a(string, binary) {
}
return array;
};
}
function size(newsize) {
switch (newsize) {
case 128:
@ -115,7 +115,7 @@ function size(newsize) {
default:
throw "Invalid Key Size Specified:" + newsize;
}
};
}
function randArr(num) {
var result = [],
i;
@ -123,7 +123,7 @@ function randArr(num) {
result = result.concat(Math.floor(Math.random() * 256));
}
return result;
};
}
function openSSLKey(passwordArr, saltArr) {
var rounds = Nr >= 12 ? 3 : 2,
key = [],
@ -144,7 +144,7 @@ function openSSLKey(passwordArr, saltArr) {
key: key,
iv: iv,
};
};
}
function rawEncrypt(plaintext, key, iv) {
key = expandKey(key);
var numBlocks = Math.ceil(plaintext.length / 16),
@ -168,7 +168,7 @@ function rawEncrypt(plaintext, key, iv) {
cipherBlocks[i] = encryptBlock(blocks[i], key);
}
return cipherBlocks;
};
}
function rawDecrypt(cryptArr, key, iv, binary) {
key = expandKey(key);
var numBlocks = cryptArr.length / 16,
@ -191,7 +191,7 @@ function rawDecrypt(cryptArr, key, iv, binary) {
}
string += block2s(plainBlocks[i], true);
return binary ? string : dec_utf8(string);
};
}
function encryptBlock(block, words) {
Decrypt = false;
var state = addRoundKey(block, words, 0),
@ -206,7 +206,7 @@ function encryptBlock(block, words) {
}
return state;
};
}
function decryptBlock(block, words) {
Decrypt = true;
var state = addRoundKey(block, words, Nr),
@ -221,7 +221,7 @@ function decryptBlock(block, words) {
}
return state;
};
}
function subBytes(state) {
var S = Decrypt ? SBoxInv : SBox,
temp = [],
@ -230,7 +230,7 @@ function subBytes(state) {
temp[i] = S[state[i]];
}
return temp;
};
}
function shiftRows(state) {
var temp = [],
shiftBy = Decrypt
@ -241,7 +241,7 @@ function shiftRows(state) {
temp[i] = state[shiftBy[i]];
}
return temp;
};
}
function mixColumns(state) {
var t = [],
c;
@ -294,7 +294,7 @@ function mixColumns(state) {
}
return t;
};
}
function addRoundKey(state, words, round) {
var temp = [],
i;
@ -302,7 +302,7 @@ function addRoundKey(state, words, round) {
temp[i] = state[i] ^ words[round][i];
}
return temp;
};
}
function xorBlocks(block1, block2) {
var temp = [],
i;
@ -310,7 +310,7 @@ function xorBlocks(block1, block2) {
temp[i] = block1[i] ^ block2[i];
}
return temp;
};
}
function expandKey(key) {
var w = [],
temp = [],
@ -347,18 +347,18 @@ function expandKey(key) {
w[i * 4 + j][0],
w[i * 4 + j][1],
w[i * 4 + j][2],
w[i * 4 + j][3]
w[i * 4 + j][3],
);
}
}
return flat;
};
}
function subWord(w) {
for (var i = 0; i < 4; i++) {
w[i] = SBox[w[i]];
}
return w;
};
}
function rotWord(w) {
var tmp = w[0],
i;
@ -367,7 +367,7 @@ function rotWord(w) {
}
w[3] = tmp;
return w;
};
}
function strhex(str, size) {
var i,
ret = [];
@ -375,7 +375,7 @@ function strhex(str, size) {
ret[i / size] = parseInt(str.substr(i, size), 16);
}
return ret;
};
}
function invertArr(arr) {
var i,
ret = [];
@ -383,7 +383,7 @@ function invertArr(arr) {
ret[arr[i]] = i;
}
return ret;
};
}
function Gxx(a, b) {
var i, ret;
@ -395,7 +395,7 @@ function Gxx(a, b) {
}
return ret;
};
}
function Gx(x) {
var i,
r = [];
@ -403,15 +403,15 @@ function Gx(x) {
r[i] = Gxx(x, i);
}
return r;
};
}
var SBox = strhex(
"637c777bf26b6fc53001672bfed7ab76ca82c97dfa5947f0add4a2af9ca472c0b7fd9326363ff7cc34a5e5f171d8311504c723c31896059a071280e2eb27b27509832c1a1b6e5aa0523bd6b329e32f8453d100ed20fcb15b6acbbe394a4c58cfd0efaafb434d338545f9027f503c9fa851a3408f929d38f5bcb6da2110fff3d2cd0c13ec5f974417c4a77e3d645d197360814fdc222a908846eeb814de5e0bdbe0323a0a4906245cc2d3ac629195e479e7c8376d8dd54ea96c56f4ea657aae08ba78252e1ca6b4c6e8dd741f4bbd8b8a703eb5664803f60e613557b986c11d9ee1f8981169d98e949b1e87e9ce5528df8ca1890dbfe6426841992d0fb054bb16",
2
2,
);
var SBoxInv = invertArr(SBox);
var Rcon = strhex(
"01020408102040801b366cd8ab4d9a2f5ebc63c697356ad4b37dfaefc591",
2
2,
);
var G2X = Gx(2);
var G3X = Gx(3);
@ -431,7 +431,7 @@ function enc(string, pass, binary) {
cipherBlocks = saltBlock.concat(cipherBlocks);
return Base64.encode(cipherBlocks);
};
}
function dec(string, pass, binary) {
var cryptArr = Base64.decode(string),
salt = cryptArr.slice(8, 16),
@ -442,7 +442,7 @@ function dec(string, pass, binary) {
string = rawDecrypt(cryptArr, key, iv, binary);
return string;
};
}
function MD5(numArr) {
function rotateLeft(lValue, iShiftBits) {
return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
@ -550,7 +550,7 @@ function MD5(numArr) {
d,
rnd = strhex(
"67452301efcdab8998badcfe10325476d76aa478e8c7b756242070dbc1bdceeef57c0faf4787c62aa8304613fd469501698098d88b44f7afffff5bb1895cd7be6b901122fd987193a679438e49b40821f61e2562c040b340265e5a51e9b6c7aad62f105d02441453d8a1e681e7d3fbc821e1cde6c33707d6f4d50d87455a14eda9e3e905fcefa3f8676f02d98d2a4c8afffa39428771f6816d9d6122fde5380ca4beea444bdecfa9f6bb4b60bebfbc70289b7ec6eaa127fad4ef308504881d05d9d4d039e6db99e51fa27cf8c4ac5665f4292244432aff97ab9423a7fc93a039655b59c38f0ccc92ffeff47d85845dd16fa87e4ffe2ce6e0a30143144e0811a1f7537e82bd3af2352ad7d2bbeb86d391",
8
8,
);
x = convertToWordArray(numArr);
@ -636,7 +636,7 @@ function MD5(numArr) {
}
return wordToHex(a).concat(wordToHex(b), wordToHex(c), wordToHex(d));
};
}
// function encString (plaintext, key, iv) {
// var i;
// plaintext = s2a(plaintext, false);
@ -799,7 +799,7 @@ const Base64 = {
}
flatArr = flatArr.slice(0, flatArr.length - (flatArr.length % 16));
return flatArr;
}
}
},
};
export { enc, dec };

View file

@ -12,14 +12,24 @@ 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(""));
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("");
}
}
return decodeURIComponent(str)
.split("")
.map((e, i) => (i % key ? String.fromCharCode(e.charCodeAt(0) ^ key) : e))
.join("");
},
};
const plain = {
encode: (str: string | undefined) => {
@ -31,8 +41,8 @@ const plain = {
if (!str) return str;
return decodeURIComponent(str);
}
}
},
};
/*
const aes = {
@ -52,7 +62,7 @@ const aes = {
const none = {
encode: (str: string | undefined) => str,
decode: (str: string | undefined) => str,
}
};
const base64 = {
encode: (str: string | undefined) => {
@ -64,13 +74,16 @@ const base64 = {
if (!str) return str;
return atob(str);
}
}
},
};
if (!self.$scramjet) {
//@ts-expect-error really dumb workaround
self.$scramjet = {}
self.$scramjet = {};
}
self.$scramjet.codecs = {
none, plain, base64, xor
}
none,
plain,
base64,
xor,
};

View file

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

View file

@ -9,12 +9,12 @@ import { BareClient } from "@mercuryworkshop/bare-mux";
if (!self.$scramjet) {
//@ts-expect-error really dumb workaround
self.$scramjet = {}
self.$scramjet = {};
}
self.$scramjet.shared = {
util: {
isScramjetFile,
BareClient
BareClient,
},
url: {
encodeUrl,
@ -27,5 +27,5 @@ self.$scramjet.shared = {
rewriteJs,
rewriteHeaders,
rewriteWorkers,
}
}
},
};

View file

@ -5,7 +5,7 @@ import { encodeUrl } from "./url";
export function rewriteCss(css: string, origin?: URL) {
const regex =
/(@import\s+(?!url\())?\s*url\(\s*(['"]?)([^'")]+)\2\s*\)|@import\s+(['"])([^'"]+)\4/g
/(@import\s+(?!url\())?\s*url\(\s*(['"]?)([^'")]+)\2\s*\)|@import\s+(['"])([^'"]+)\4/g;
return css.replace(
regex,
@ -15,21 +15,20 @@ export function rewriteCss(css: string, origin?: URL) {
urlQuote,
urlContent,
importQuote,
importContent
importContent,
) => {
const url = urlContent || importContent
const encodedUrl = encodeUrl(url.trim(), origin)
const url = urlContent || importContent;
const encodedUrl = encodeUrl(url.trim(), origin);
if (importStatement) {
return `@import url(${urlQuote}${encodedUrl}${urlQuote})`
return `@import url(${urlQuote}${encodedUrl}${urlQuote})`;
}
if (importQuote) {
return `@import ${importQuote}${encodedUrl}${importQuote}`
return `@import ${importQuote}${encodedUrl}${importQuote}`;
}
return `url(${urlQuote}${encodedUrl}${urlQuote})`
}
)
return `url(${urlQuote}${encodedUrl}${urlQuote})`;
},
);
}

View file

@ -19,14 +19,10 @@ const cspHeaders = [
"x-xss-protection",
// This needs to be emulated, but for right now it isn't that important of a feature to be worried about
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Clear-Site-Data
"clear-site-data"
"clear-site-data",
];
const urlHeaders = [
"location",
"content-location",
"referer"
];
const urlHeaders = ["location", "content-location", "referer"];
export function rewriteHeaders(rawHeaders: BareHeaders, origin?: URL) {
const headers = {};
@ -45,7 +41,9 @@ export function rewriteHeaders(rawHeaders: BareHeaders, origin?: URL) {
});
if (headers["link"]) {
headers["link"] = headers["link"].replace(/<(.*?)>/gi, (match) => encodeUrl(match, origin));
headers["link"] = headers["link"].replace(/<(.*?)>/gi, (match) =>
encodeUrl(match, origin),
);
}
return headers;

View file

@ -54,15 +54,28 @@ function traverseParsedHtml(node, origin?: URL) {
}
}
if (hasAttrib(node, "srcdoc")) node.attribs.srcdoc = rewriteHtml(node.attribs.srcdoc, origin);
if (hasAttrib(node, "style")) node.attribs.style = rewriteCss(node.attribs.style, origin);
if (hasAttrib(node, "srcdoc"))
node.attribs.srcdoc = rewriteHtml(node.attribs.srcdoc, origin);
if (hasAttrib(node, "style"))
node.attribs.style = rewriteCss(node.attribs.style, origin);
if (node.name === "style" && node.children[0] !== undefined) node.children[0].data = rewriteCss(node.children[0].data, origin);
if (node.name === "script" && /(application|text)\/javascript|importmap|undefined/.test(node.attribs.type) && node.children[0] !== undefined) node.children[0].data = rewriteJs(node.children[0].data, origin);
if (node.name === "style" && node.children[0] !== undefined)
node.children[0].data = rewriteCss(node.children[0].data, origin);
if (
node.name === "script" &&
/(application|text)\/javascript|importmap|undefined/.test(
node.attribs.type,
) &&
node.children[0] !== undefined
)
node.children[0].data = rewriteJs(node.children[0].data, origin);
if (node.name === "meta" && hasAttrib(node, "http-equiv")) {
if (node.attribs["http-equiv"] === "content-security-policy") {
node = {};
} else if (node.attribs["http-equiv"] === "refresh" && node.attribs.content.includes("url")) {
} else if (
node.attribs["http-equiv"] === "refresh" &&
node.attribs.content.includes("url")
) {
const contentArray = node.attribs.content.split("url=");
contentArray[1] = encodeUrl(contentArray[1].trim(), origin);
node.attribs.content = contentArray.join("url=");
@ -72,10 +85,12 @@ function traverseParsedHtml(node, origin?: URL) {
if (node.name === "head") {
const scramjetScripts = [];
["codecs", "config", "shared", "client"].forEach((script) => {
scramjetScripts.push(new Element("script", {
scramjetScripts.push(
new Element("script", {
src: self.$scramjet.config[script],
"data-scramjet": ""
}));
"data-scramjet": "",
}),
);
});
node.children.unshift(...scramjetScripts);
@ -83,7 +98,10 @@ function traverseParsedHtml(node, origin?: URL) {
if (node.childNodes) {
for (const childNode in node.childNodes) {
node.childNodes[childNode] = traverseParsedHtml(node.childNodes[childNode], origin);
node.childNodes[childNode] = traverseParsedHtml(
node.childNodes[childNode],
origin,
);
}
}

View file

@ -16,14 +16,13 @@ import * as ESTree from "estree";
// top
// parent
export function rewriteJs(js: string, origin?: URL) {
const htmlcomment = /<!--[\s\S]*?-->/g;
js = js.replace(htmlcomment, "");
try {
const ast = parseModule(js, {
module: true,
webcompat: true
webcompat: true,
});
const identifierList = [
@ -33,8 +32,8 @@ export function rewriteJs(js: string, origin?: URL) {
"this",
"parent",
"top",
"location"
]
"location",
];
const customTraveler = makeTraveler({
ImportDeclaration: (node: ESTree.ImportDeclaration) => {
@ -58,30 +57,44 @@ export function rewriteJs(js: string, origin?: URL) {
ExportNamedDeclaration: (node: ESTree.ExportNamedDeclaration) => {
// strings are Literals in ESTree syntax but these will always be strings
if (node.source) node.source.value = encodeUrl(node.source.value as string, origin);
if (node.source)
node.source.value = encodeUrl(node.source.value as string, origin);
},
MemberExpression: (node: ESTree.MemberExpression) => {
if (node.object.type === "Identifier" && identifierList.includes(node.object.name)) {
if (
node.object.type === "Identifier" &&
identifierList.includes(node.object.name)
) {
node.object.name = `$s(${node.object.name})`;
}
},
AssignmentExpression: (node: ESTree.AssignmentExpression) => {
if (node.left.type === "Identifier" && identifierList.includes(node.left.name)) {
if (
node.left.type === "Identifier" &&
identifierList.includes(node.left.name)
) {
node.left.name = `$s(${node.left.name})`;
}
if (node.right.type === "Identifier" && identifierList.includes(node.right.name)) {
if (
node.right.type === "Identifier" &&
identifierList.includes(node.right.name)
) {
node.right.name = `$s(${node.right.name})`;
}
},
VariableDeclarator: (node: ESTree.VariableDeclarator) => {
if (node.init && node.init.type === "Identifier" && identifierList.includes(node.init.name)) {
if (
node.init &&
node.init.type === "Identifier" &&
identifierList.includes(node.init.name)
) {
node.init.name = `$s(${node.init.name})`;
}
}
},
});
customTraveler.go(ast);

View file

@ -18,7 +18,13 @@ export function encodeUrl(url: string | URL, origin?: URL) {
}
if (!origin) {
origin = new URL(self.$scramjet.config.codec.decode(location.href.slice((location.origin + self.$scramjet.config.prefix).length)));
origin = new URL(
self.$scramjet.config.codec.decode(
location.href.slice(
(location.origin + self.$scramjet.config.prefix).length,
),
),
);
}
if (url.startsWith("javascript:")) {
@ -26,7 +32,11 @@ export function encodeUrl(url: string | URL, origin?: URL) {
} else if (/^(#|mailto|about|data)/.test(url)) {
return url;
} else if (canParseUrl(url, origin)) {
return location.origin + self.$scramjet.config.prefix + self.$scramjet.config.codec.encode(new URL(url, origin).href);
return (
location.origin +
self.$scramjet.config.prefix +
self.$scramjet.config.codec.encode(new URL(url, origin).href)
);
}
}
@ -39,7 +49,9 @@ export function decodeUrl(url: string | URL) {
if (/^(#|about|data|mailto|javascript)/.test(url)) {
return url;
} else if (canParseUrl(url)) {
return self.$scramjet.config.codec.decode(url.slice((location.origin + self.$scramjet.config.prefix).length))
return self.$scramjet.config.codec.decode(
url.slice((location.origin + self.$scramjet.config.prefix).length),
);
} else {
return url;
}

View file

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

38
src/types.d.ts vendored
View file

@ -5,7 +5,7 @@ import { rewriteJs } from "./shared/rewriters/js";
import { rewriteHeaders } from "./shared/rewriters/headers";
import { rewriteWorkers } from "./shared/rewriters/worker";
import { isScramjetFile } from "./shared/rewriters/html";
import type { Codec } from "./codecs"
import type { Codec } from "./codecs";
import { BareClient } from "@mercuryworkshop/bare-mux";
declare global {
@ -13,37 +13,37 @@ declare global {
$scramjet: {
shared: {
url: {
encodeUrl: typeof encodeUrl,
decodeUrl: typeof decodeUrl,
}
encodeUrl: typeof encodeUrl;
decodeUrl: typeof decodeUrl;
};
rewrite: {
rewriteCss: typeof rewriteCss,
rewriteHtml: typeof rewriteHtml,
rewriteSrcset: typeof rewriteSrcset,
rewriteJs: typeof rewriteJs,
rewriteHeaders: typeof rewriteHeaders,
rewriteWorkers: typeof rewriteWorkers,
}
rewriteCss: typeof rewriteCss;
rewriteHtml: typeof rewriteHtml;
rewriteSrcset: typeof rewriteSrcset;
rewriteJs: typeof rewriteJs;
rewriteHeaders: typeof rewriteHeaders;
rewriteWorkers: typeof rewriteWorkers;
};
util: {
BareClient: typeof BareClient,
isScramjetFile: typeof isScramjetFile,
}
}
BareClient: typeof BareClient;
isScramjetFile: typeof isScramjetFile;
};
};
config: {
prefix: string;
codec: Codec
codec: Codec;
config: string;
shared: string;
worker: string;
client: string;
codecs: string;
},
};
codecs: {
none: Codec;
plain: Codec;
base64: Codec;
xor: Codec;
}
}
};
};
}
}

View file

@ -18,23 +18,35 @@ self.ScramjetServiceWorker = class ScramjetServiceWorker {
}
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;
}
async fetch({ 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;
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 {
const url = new URL(decodeUrl(request.url));
const cookieStore = new IDBMap(url.host, { durability: "relaxed", prefix: "Cookies" });
const cookieStore = new IDBMap(url.host, {
durability: "relaxed",
prefix: "Cookies",
});
const response: BareResponseFetch = await this.client.fetch(url, {
method: request.method,
@ -52,18 +64,25 @@ self.ScramjetServiceWorker = class ScramjetServiceWorker {
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 cookieParsed = cookie.split(";").map((x) => x.trim().split("="));
let [key, value] = cookieParsed.shift();
value = value.replace("\"", "");
value = value.replace('"', "");
const hostArg = cookieParsed.find(x => x[0].toLowerCase() === "domain");
cookieParsed = cookieParsed.filter(x => x[0].toLowerCase() !== "domain");
const hostArg = cookieParsed.find(
(x) => x[0].toLowerCase() === "domain",
);
cookieParsed = cookieParsed.filter(
(x) => x[0].toLowerCase() !== "domain",
);
let host = hostArg ? hostArg[1] : undefined;
if (host && host !== url.host) {
if (host.startsWith(".")) host = host.slice(1);
const cookieStore = new IDBMap(host, { durability: "relaxed", prefix: "Cookies" });
const cookieStore = new IDBMap(host, {
durability: "relaxed",
prefix: "Cookies",
});
cookieStore.set(key, { value: value, args: cookieParsed });
} else {
cookieStore.set(key, { value: value, args: cookieParsed });
@ -72,14 +91,19 @@ self.ScramjetServiceWorker = class ScramjetServiceWorker {
for (let header in responseHeaders) {
// flatten everything past here
if (responseHeaders[header] instanceof Array) responseHeaders[header] = responseHeaders[header][0];
if (responseHeaders[header] instanceof Array)
responseHeaders[header] = responseHeaders[header][0];
}
if (response.body) {
switch (request.destination) {
case "iframe":
case "document":
if (responseHeaders["content-type"]?.toString()?.startsWith("text/html")) {
if (
responseHeaders["content-type"]
?.toString()
?.startsWith("text/html")
) {
responseBody = rewriteHtml(await response.text(), url);
} else {
responseBody = response.body;
@ -117,9 +141,8 @@ self.ScramjetServiceWorker = class ScramjetServiceWorker {
.split("/")
.slice(-1);
responseHeaders[
"content-disposition"
] = `${type}; filename=${JSON.stringify(filename)}`;
responseHeaders["content-disposition"] =
`${type}; filename=${JSON.stringify(filename)}`;
}
}
if (responseHeaders["accept"] === "text/event-stream") {
@ -132,8 +155,8 @@ self.ScramjetServiceWorker = class ScramjetServiceWorker {
return new Response(responseBody, {
headers: responseHeaders as HeadersInit,
status: response.status,
statusText: response.statusText
})
statusText: response.statusText,
});
} catch (err) {
if (!["document", "iframe"].includes(request.destination))
return new Response(undefined, { status: 500 });
@ -143,26 +166,21 @@ self.ScramjetServiceWorker = class ScramjetServiceWorker {
return renderError(err, decodeUrl(request.url));
}
}
}
};
function errorTemplate(
trace: string,
fetchedURL: string,
) {
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
location.hostname,
)};
reload.addEventListener("click", () => location.reload());
version.textContent = "0.0.1";
`
`;
return (
`<!DOCTYPE html>
return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
@ -194,12 +212,12 @@ function errorTemplate(
<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>
`
);
`;
}
/**
@ -215,15 +233,8 @@ function renderError(err, fetchedURL) {
headers["Cross-Origin-Embedder-Policy"] = "require-corp";
}
return new Response(
errorTemplate(
String(err),
fetchedURL
),
{
return new Response(errorTemplate(String(err), fetchedURL), {
status: 500,
headers: headers
}
);
headers: headers,
});
}

View file

@ -1,29 +1,39 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="prefetch" href="/scram/scramjet.worker.js">
<link rel="prefetch" href="/scram/scramjet.shared.js">
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;1,100;1,200;1,300;1,400;1,500;1,600;1,700&amp;family=Inter+Tight:ital,wght@0,100..900;1,100..900&amp;family=Inter:wght@100..900&amp;display=swap&amp;" rel="stylesheet">
<link rel="prefetch" href="/scram/scramjet.worker.js" />
<link rel="prefetch" href="/scram/scramjet.shared.js" />
<link
href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;1,100;1,200;1,300;1,400;1,500;1,600;1,700&amp;family=Inter+Tight:ital,wght@0,100..900;1,100..900&amp;family=Inter:wght@100..900&amp;display=swap&amp;"
rel="stylesheet"
/>
<style>
body, html, #app {
font-family: "Inter", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
width:100vw;
height:100vh;
body,
html,
#app {
font-family:
"Inter",
system-ui,
-apple-system,
BlinkMacSystemFont,
sans-serif;
width: 100vw;
height: 100vh;
margin: 0;
padding: 0;
background-color:#121212;
background-color: #121212;
overflow: hidden;
}
</style>
</head>
<body>
</head>
<body>
<script src="https://unpkg.com/dreamland"></script>
<script src="/baremux/index.js" defer></script>
<script src="/scram/scramjet.codecs.js" defer></script>
<script src="/scram/scramjet.config.js" defer></script>
<script src="ui.js" defer></script>
</body>
</body>
</html>

View file

@ -2,7 +2,7 @@ importScripts(
"/scram/scramjet.codecs.js",
"/scram/scramjet.config.js",
"/scram/scramjet.shared.js",
"/scram/scramjet.worker.js"
"/scram/scramjet.worker.js",
);
const scramjet = new ScramjetServiceWorker();
@ -12,7 +12,7 @@ async function handleRequest(event) {
return scramjet.fetch(event);
}
return fetch(event.request)
return fetch(event.request);
}
self.addEventListener("fetch", (event) => {

View file

@ -1,17 +1,30 @@
navigator.serviceWorker.register("./sw.js", {
scope: $scramjet.config.prefix
}).then((reg) => {
navigator.serviceWorker
.register("./sw.js", {
scope: $scramjet.config.prefix,
})
.then((reg) => {
reg.update();
});
const connection = new BareMux.BareMuxConnection("/baremux/worker.js")
const flex = css`display: flex;`;
const col = css`flex-direction: column;`;
const store = $store({
});
const connection = new BareMux.BareMuxConnection("/baremux/worker.js");
const flex = css`
display: flex;
`;
const col = css`
flex-direction: column;
`;
const store = $store(
{
url: "https://google.com",
wispurl: "wss://wisp.mercurywork.shop/",
bareurl: (location.protocol === "https:" ? "https" : "http") + "://" + location.host + "/bare/",
}, { ident: "settings", backing: "localstorage", autosave: "auto" });
connection.setTransport("/baremod/index.mjs", [store.bareurl])
bareurl:
(location.protocol === "https:" ? "https" : "http") +
"://" +
location.host +
"/bare/",
},
{ ident: "settings", backing: "localstorage", autosave: "auto" },
);
connection.setTransport("/baremod/index.mjs", [store.bareurl]);
function App() {
this.urlencoded = "";
this.css = `
@ -90,12 +103,12 @@ function App() {
<button on:click=${() => window.open(this.urlencoded)}>open in fullscreen</button>
</div>
</div>
<input class="bar" bind:value=${use(store.url)} on:input=${(e) => (store.url = e.target.value)} on:keyup=${(e) => e.keyCode == 13 && console.log(this.urlencoded = $scramjet.config.prefix + $scramjet.config.codec.encode(e.target.value))}></input>
<input class="bar" bind:value=${use(store.url)} on:input=${(e) => (store.url = e.target.value)} on:keyup=${(e) => e.keyCode == 13 && console.log((this.urlencoded = $scramjet.config.prefix + $scramjet.config.codec.encode(e.target.value)))}></input>
<iframe src=${use(this.urlencoded)}></iframe>
</div>
`
`;
}
window.addEventListener("load", () => {
document.body.appendChild(h(App))
})
document.body.appendChild(h(App));
});

View file

@ -4,4 +4,4 @@
location = "http://www.google.com";
}
</script>
<button onclick='f()'>Google</button>
<button onclick="f()">Google</button>

View file

@ -4,7 +4,7 @@
"rootDir": "./src",
"target": "ES2022",
"moduleResolution": "Bundler",
"module": "ES2022",
"module": "ES2022"
},
"include": ["src"],
"include": ["src"]
}