mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 15:00:01 -04:00
commit
a914e9a933
45 changed files with 8085 additions and 3028 deletions
|
@ -7,15 +7,17 @@
|
||||||
"no-unused-labels": "error",
|
"no-unused-labels": "error",
|
||||||
"no-unused-vars": "error",
|
"no-unused-vars": "error",
|
||||||
"quotes": ["error", "double"],
|
"quotes": ["error", "double"],
|
||||||
"max-lines-per-function": ["error", {
|
"max-lines-per-function": [
|
||||||
|
"error",
|
||||||
|
{
|
||||||
"max": 200,
|
"max": 200,
|
||||||
"skipComments": true
|
"skipComments": true
|
||||||
}],
|
}
|
||||||
|
],
|
||||||
"getter-return": "error",
|
"getter-return": "error",
|
||||||
"newline-before-return": "error",
|
"newline-before-return": "error",
|
||||||
"no-multiple-empty-lines": "error",
|
"no-multiple-empty-lines": "error",
|
||||||
"no-var": "error",
|
"no-var": "error",
|
||||||
"indent": ["warn", 4],
|
|
||||||
"no-this-before-super": "warn",
|
"no-this-before-super": "warn",
|
||||||
"no-useless-return": "error",
|
"no-useless-return": "error",
|
||||||
"no-shadow": "error",
|
"no-shadow": "error",
|
||||||
|
|
|
@ -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.
|
The UI is not finalized and only used as a means to test the web proxy.
|
||||||
|
|
||||||
## How to build
|
## 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`.
|
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
|
## TODO
|
||||||
|
|
||||||
- Finish HTML rewriting
|
- Finish HTML rewriting
|
||||||
- `<script type="importmap"></script>` 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
|
- Make an array of all possible import values and pass the array onto the JS rewriter, then rewrite all the URLs inside of it
|
||||||
|
|
15
package.json
15
package.json
|
@ -9,9 +9,8 @@
|
||||||
"url": "https://github.com/MercuryWorkshop/scramjet"
|
"url": "https://github.com/MercuryWorkshop/scramjet"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "rollup -c",
|
"build": "rspack build",
|
||||||
"dev": "node server.js",
|
"dev": "node server.js",
|
||||||
"temp": "rollup -c -w",
|
|
||||||
"prepublish": "pnpm build",
|
"prepublish": "pnpm build",
|
||||||
"pub": "pnpm publish --no-git-checks --access public"
|
"pub": "pnpm publish --no-git-checks --access public"
|
||||||
},
|
},
|
||||||
|
@ -27,9 +26,9 @@
|
||||||
"@mercuryworkshop/bare-as-module3": "^2.2.2",
|
"@mercuryworkshop/bare-as-module3": "^2.2.2",
|
||||||
"@mercuryworkshop/epoxy-transport": "^2.1.3",
|
"@mercuryworkshop/epoxy-transport": "^2.1.3",
|
||||||
"@mercuryworkshop/libcurl-transport": "^1.3.6",
|
"@mercuryworkshop/libcurl-transport": "^1.3.6",
|
||||||
"@rollup/plugin-inject": "^5.0.5",
|
"@rsdoctor/rspack-plugin": "^0.3.7",
|
||||||
"@rollup/plugin-replace": "^5.0.5",
|
"@rspack/cli": "^0.7.5",
|
||||||
"@rollup/plugin-node-resolve": "^15.2.3",
|
"@rspack/core": "^0.7.5",
|
||||||
"@tomphttp/bare-server-node": "^2.0.3",
|
"@tomphttp/bare-server-node": "^2.0.3",
|
||||||
"@types/eslint": "^8.56.10",
|
"@types/eslint": "^8.56.10",
|
||||||
"@types/estree": "^1.0.5",
|
"@types/estree": "^1.0.5",
|
||||||
|
@ -40,8 +39,7 @@
|
||||||
"dotenv": "^16.4.5",
|
"dotenv": "^16.4.5",
|
||||||
"eslint": "^8.57.0",
|
"eslint": "^8.57.0",
|
||||||
"fastify": "^4.26.2",
|
"fastify": "^4.26.2",
|
||||||
"rollup": "^4.17.2",
|
"prettier": "^3.3.3",
|
||||||
"rollup-plugin-typescript2": "^0.36.0",
|
|
||||||
"tslib": "^2.6.2",
|
"tslib": "^2.6.2",
|
||||||
"typescript": "^5.4.5"
|
"typescript": "^5.4.5"
|
||||||
},
|
},
|
||||||
|
@ -55,6 +53,7 @@
|
||||||
"domhandler": "^5.0.3",
|
"domhandler": "^5.0.3",
|
||||||
"domutils": "^3.1.0",
|
"domutils": "^3.1.0",
|
||||||
"htmlparser2": "^9.1.0",
|
"htmlparser2": "^9.1.0",
|
||||||
"meriyah": "^4.4.2"
|
"meriyah": "^4.4.2",
|
||||||
|
"parse-domain": "^8.0.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
6331
pnpm-lock.yaml
generated
6331
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load diff
6
prettier.json
Normal file
6
prettier.json
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"trailingComma": "es5",
|
||||||
|
"useTabs": true,
|
||||||
|
"semi": true,
|
||||||
|
"singleQuote": false
|
||||||
|
}
|
|
@ -1,34 +0,0 @@
|
||||||
import typescript from "rollup-plugin-typescript2";
|
|
||||||
import { nodeResolve } from '@rollup/plugin-node-resolve';
|
|
||||||
import { join } from "node:path";
|
|
||||||
import fs from "node:fs"
|
|
||||||
import { fileURLToPath } from "node:url";
|
|
||||||
|
|
||||||
// check if its
|
|
||||||
const production = !process.env.ROLLUP_WATCH;
|
|
||||||
console.log(production)
|
|
||||||
fs.rmSync(join(fileURLToPath(new URL(".", import.meta.url)), "./dist"), { recursive: true, force: true })
|
|
||||||
|
|
||||||
const commonPlugins = () => [
|
|
||||||
typescript(),
|
|
||||||
nodeResolve(),
|
|
||||||
]
|
|
||||||
|
|
||||||
export default {
|
|
||||||
plugins: commonPlugins(),
|
|
||||||
treeshake: "recommended",
|
|
||||||
input: {
|
|
||||||
client: "./src/client/index.ts",
|
|
||||||
shared: "./src/shared/index.ts",
|
|
||||||
worker: "./src/worker/index.ts",
|
|
||||||
codecs: "./src/codecs/index.ts",
|
|
||||||
config: "./src/scramjet.config.ts"
|
|
||||||
},
|
|
||||||
output: {
|
|
||||||
entryFileNames: "scramjet.[name].js",
|
|
||||||
dir: "./dist",
|
|
||||||
format: "esm",
|
|
||||||
sourcemap: true,
|
|
||||||
compact: production,
|
|
||||||
},
|
|
||||||
};
|
|
53
rspack.config.js
Normal file
53
rspack.config.js
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
import { defineConfig } from "@rspack/cli";
|
||||||
|
// import { RsdoctorRspackPlugin } from "@rsdoctor/rspack-plugin";
|
||||||
|
import { join } from "path";
|
||||||
|
import { fileURLToPath } from "url";
|
||||||
|
|
||||||
|
const __dirname = fileURLToPath(new URL(".", import.meta.url));
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
// change to production when needed
|
||||||
|
mode: "development",
|
||||||
|
entry: {
|
||||||
|
shared: join(__dirname, "src/shared/index.ts"),
|
||||||
|
worker: join(__dirname, "src/worker/index.ts"),
|
||||||
|
client: join(__dirname, "src/client/index.ts"),
|
||||||
|
config: join(__dirname, "src/scramjet.config.ts"),
|
||||||
|
codecs: join(__dirname, "src/codecs/index.ts"),
|
||||||
|
},
|
||||||
|
resolve: {
|
||||||
|
extensions: [".ts", ".js"],
|
||||||
|
},
|
||||||
|
module: {
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
test: /\.ts$/,
|
||||||
|
use: "builtin:swc-loader",
|
||||||
|
exclude: ["/node_modules/"],
|
||||||
|
options: {
|
||||||
|
jsc: {
|
||||||
|
parser: {
|
||||||
|
syntax: "typescript",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
type: "javascript/auto",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
output: {
|
||||||
|
filename: "scramjet.[name].js",
|
||||||
|
path: join(__dirname, "dist"),
|
||||||
|
iife: true,
|
||||||
|
clean: true,
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
// new RsdoctorRspackPlugin({
|
||||||
|
// supports: {
|
||||||
|
// parseBundle: true,
|
||||||
|
// banner: true
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
],
|
||||||
|
watch: true,
|
||||||
|
});
|
50
server.js
50
server.js
|
@ -4,19 +4,17 @@ import { createServer } from "http";
|
||||||
import Fastify from "fastify";
|
import Fastify from "fastify";
|
||||||
import fastifyStatic from "@fastify/static";
|
import fastifyStatic from "@fastify/static";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import fs from "node:fs"
|
import { spawn } from "node:child_process";
|
||||||
import { spawn } from "node:child_process"
|
|
||||||
import { fileURLToPath } from "node:url";
|
import { fileURLToPath } from "node:url";
|
||||||
import { loadConfigFile } from "rollup/loadConfigFile"
|
|
||||||
|
|
||||||
//transports
|
//transports
|
||||||
import { baremuxPath } from "@mercuryworkshop/bare-mux/node"
|
import { baremuxPath } from "@mercuryworkshop/bare-mux/node";
|
||||||
import { epoxyPath } from "@mercuryworkshop/epoxy-transport"
|
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
|
||||||
import { libcurlPath } from "@mercuryworkshop/libcurl-transport"
|
import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
|
||||||
import { bareModulePath } from "@mercuryworkshop/bare-as-module3"
|
import { bareModulePath } from "@mercuryworkshop/bare-as-module3";
|
||||||
|
|
||||||
const bare = createBareServer("/bare/", {
|
const bare = createBareServer("/bare/", {
|
||||||
logErrors: true
|
logErrors: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const fastify = Fastify({
|
const fastify = Fastify({
|
||||||
|
@ -28,50 +26,54 @@ const fastify = Fastify({
|
||||||
} else {
|
} else {
|
||||||
handler(req, res);
|
handler(req, res);
|
||||||
}
|
}
|
||||||
}).on("upgrade", (req, socket, head) => {
|
})
|
||||||
|
.on("upgrade", (req, socket, head) => {
|
||||||
if (bare.shouldRoute(req)) {
|
if (bare.shouldRoute(req)) {
|
||||||
bare.routeUpgrade(req, socket, head);
|
bare.routeUpgrade(req, socket, head);
|
||||||
} else {
|
} else {
|
||||||
socket.end();
|
socket.end();
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
fastify.register(fastifyStatic, {
|
fastify.register(fastifyStatic, {
|
||||||
root: join(fileURLToPath(new URL(".", import.meta.url)), "./static"),
|
root: join(fileURLToPath(new URL(".", import.meta.url)), "./static"),
|
||||||
decorateReply: false
|
decorateReply: false,
|
||||||
});
|
});
|
||||||
fastify.register(fastifyStatic, {
|
fastify.register(fastifyStatic, {
|
||||||
root: join(fileURLToPath(new URL(".", import.meta.url)), "./dist"),
|
root: join(fileURLToPath(new URL(".", import.meta.url)), "./dist"),
|
||||||
prefix: "/scram/",
|
prefix: "/scram/",
|
||||||
decorateReply: false
|
decorateReply: false,
|
||||||
})
|
});
|
||||||
fastify.register(fastifyStatic, {
|
fastify.register(fastifyStatic, {
|
||||||
root: baremuxPath,
|
root: baremuxPath,
|
||||||
prefix: "/baremux/",
|
prefix: "/baremux/",
|
||||||
decorateReply: false
|
decorateReply: false,
|
||||||
})
|
});
|
||||||
fastify.register(fastifyStatic, {
|
fastify.register(fastifyStatic, {
|
||||||
root: epoxyPath,
|
root: epoxyPath,
|
||||||
prefix: "/epoxy/",
|
prefix: "/epoxy/",
|
||||||
decorateReply: false
|
decorateReply: false,
|
||||||
})
|
});
|
||||||
fastify.register(fastifyStatic, {
|
fastify.register(fastifyStatic, {
|
||||||
root: libcurlPath,
|
root: libcurlPath,
|
||||||
prefix: "/libcurl/",
|
prefix: "/libcurl/",
|
||||||
decorateReply: false
|
decorateReply: false,
|
||||||
})
|
});
|
||||||
fastify.register(fastifyStatic, {
|
fastify.register(fastifyStatic, {
|
||||||
root: bareModulePath,
|
root: bareModulePath,
|
||||||
prefix: "/baremod/",
|
prefix: "/baremod/",
|
||||||
decorateReply: false
|
decorateReply: false,
|
||||||
})
|
});
|
||||||
fastify.listen({
|
fastify.listen({
|
||||||
port: process.env.PORT || 1337
|
port: process.env.PORT || 1337,
|
||||||
});
|
});
|
||||||
|
|
||||||
const watch = spawn("rollup", ["-c", "-w"], { detached: true });
|
const watch = spawn("pnpm", ["rspack", "-w"], {
|
||||||
|
detached: true,
|
||||||
|
cwd: process.cwd(),
|
||||||
|
});
|
||||||
|
|
||||||
watch.stdout.on("data", (data) => {
|
watch.stdout.on("data", (data) => {
|
||||||
console.log(`${data}`);
|
console.log(`${data}`);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
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) {
|
||||||
|
|
|
@ -1,26 +1,26 @@
|
||||||
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 = [
|
||||||
const jsProperties = ["background", "backgroundImage", "mask", "maskImage", "listStyle", "listStyleImage", "borderImage", "borderImageSource", "cursor"];
|
"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 = new Proxy(CSSStyleDeclaration.prototype.setProperty, {
|
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);
|
||||||
},
|
},
|
||||||
});
|
}
|
||||||
|
);
|
||||||
jsProperties.forEach((prop) => {
|
|
||||||
const propDescriptor = Object.getOwnPropertyDescriptor(CSSStyleDeclaration.prototype, prop);
|
|
||||||
|
|
||||||
Object.defineProperty(CSSStyleDeclaration.prototype, prop, {
|
|
||||||
get() {
|
|
||||||
return propDescriptor.get.call(this);
|
|
||||||
},
|
|
||||||
set(v) {
|
|
||||||
return propDescriptor.set.call(this, rewriteCss(v));
|
|
||||||
},
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
|
@ -1,18 +1,31 @@
|
||||||
import { encodeUrl, rewriteCss, rewriteHtml, rewriteJs, rewriteSrcset } from "../shared";
|
import { decodeUrl } from "../shared/rewriters/url";
|
||||||
|
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: [
|
||||||
"href": [HTMLAnchorElement, HTMLLinkElement],
|
HTMLImageElement,
|
||||||
"data": [HTMLObjectElement],
|
HTMLMediaElement,
|
||||||
"action": [HTMLFormElement],
|
HTMLIFrameElement,
|
||||||
"formaction": [HTMLButtonElement, HTMLInputElement],
|
HTMLEmbedElement,
|
||||||
"srcdoc": [HTMLIFrameElement],
|
HTMLScriptElement,
|
||||||
"srcset": [HTMLImageElement, HTMLSourceElement],
|
],
|
||||||
"imagesrcset": [HTMLLinkElement]
|
href: [HTMLAnchorElement, HTMLLinkElement],
|
||||||
}
|
data: [HTMLObjectElement],
|
||||||
|
action: [HTMLFormElement],
|
||||||
|
formaction: [HTMLButtonElement, HTMLInputElement],
|
||||||
|
srcdoc: [HTMLIFrameElement],
|
||||||
|
srcset: [HTMLImageElement, HTMLSourceElement],
|
||||||
|
imagesrcset: [HTMLLinkElement],
|
||||||
|
};
|
||||||
|
|
||||||
const attrs = Object.keys(attrObject);
|
const attrs = Object.keys(attrObject);
|
||||||
|
|
||||||
|
@ -21,19 +34,23 @@ for (const attr of attrs) {
|
||||||
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() {
|
||||||
return this.dataset[attr];
|
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) {
|
set(value) {
|
||||||
this.dataset[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
|
|
||||||
if (value instanceof TrustedScriptURL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
value = encodeUrl(value);
|
value = encodeUrl(value);
|
||||||
} else if (attr === "srcdoc") {
|
} else if (attr === "srcdoc") {
|
||||||
value = rewriteHtml(value);
|
value = rewriteHtml(value);
|
||||||
|
@ -47,10 +64,18 @@ for (const attr of attrs) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface Element {
|
||||||
|
__origattrs: Record<string, string>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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.dataset[argArray[0]]) {
|
if (attrs.includes(argArray[0]) && thisArg.__origattrs[argArray[0]]) {
|
||||||
return thisArg.dataset[argArray[0]];
|
return thisArg.__origattrs[argArray[0]];
|
||||||
}
|
}
|
||||||
|
|
||||||
return Reflect.apply(target, thisArg, argArray);
|
return Reflect.apply(target, thisArg, argArray);
|
||||||
|
@ -60,11 +85,10 @@ Element.prototype.getAttribute = new Proxy(Element.prototype.getAttribute, {
|
||||||
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.dataset[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])) {
|
||||||
console.log(thisArg);
|
|
||||||
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]);
|
||||||
|
@ -79,20 +103,21 @@ 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", {
|
Object.defineProperty(Element.prototype, "innerHTML", {
|
||||||
set(value) {
|
set(value) {
|
||||||
// @ts-expect-error
|
if (
|
||||||
if (this instanceof HTMLScriptElement && !(value instanceof TrustedScript)) {
|
this instanceof HTMLScriptElement
|
||||||
|
) {
|
||||||
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
|
|
||||||
} else if (!(value instanceof TrustedHTML)) {
|
|
||||||
value = rewriteHtml(value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return innerHTML.set.call(this, value);
|
return innerHTML.set.call(this, value);
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
|
|
|
@ -1,18 +1,16 @@
|
||||||
|
import { decodeUrl } from "./shared";
|
||||||
import { encodeUrl } 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] = encodeUrl(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] = encodeUrl(argArray[3]);
|
argArray[3] = decodeUrl(argArray[3]);
|
||||||
|
|
||||||
return Reflect.apply(target, thisArg, argArray);
|
return Reflect.apply(target, thisArg, argArray);
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import "./scope.ts";
|
||||||
import "./window.ts";
|
import "./window.ts";
|
||||||
import "./event.ts";
|
import "./event.ts";
|
||||||
import "./native/eval.ts";
|
import "./native/eval.ts";
|
||||||
|
@ -5,34 +6,16 @@ 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 "./scope.ts";
|
import "./url.ts";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
//@ts-ignore scope function cant be typed
|
$s: any;
|
||||||
__s: any;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const scripts = document.querySelectorAll("script:not([data-scramjet])");
|
|
||||||
|
|
||||||
for (const script of scripts) {
|
|
||||||
const clone = document.createElement("script");
|
|
||||||
|
|
||||||
for (const attr of Array.from(script.attributes)) {
|
|
||||||
clone.setAttribute(attr.name, attr.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (script.innerHTML !== "") {
|
|
||||||
clone.innerHTML = script.innerHTML;
|
|
||||||
}
|
|
||||||
|
|
||||||
script.insertAdjacentElement("afterend", clone);
|
|
||||||
script.remove();
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,18 +1,26 @@
|
||||||
// @ts-nocheck
|
// @ts-nocheck
|
||||||
import { encodeUrl, decodeUrl } from "../shared";
|
import { encodeUrl, decodeUrl } from "./shared";
|
||||||
|
|
||||||
const loc = new URL(decodeUrl(location.href));
|
function createLocation() {
|
||||||
loc.assign = (url: string) => location.assign(encodeUrl(url));
|
const loc = new URL(decodeUrl(location.href));
|
||||||
loc.reload = () => location.reload();
|
loc.assign = (url: string) => location.assign(encodeUrl(url));
|
||||||
loc.replace = (url: string) => location.replace(encodeUrl(url));
|
loc.reload = () => location.reload();
|
||||||
loc.toString = () => loc.href;
|
loc.replace = (url: string) => location.replace(encodeUrl(url));
|
||||||
|
loc.toString = () => loc.href;
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
return loc[prop];
|
return loc[prop];
|
||||||
},
|
},
|
||||||
|
|
||||||
set(obj, prop, value) {
|
set(obj, prop, value) {
|
||||||
|
const loc = createLocation();
|
||||||
|
|
||||||
if (prop === "href") {
|
if (prop === "href") {
|
||||||
location.href = encodeUrl(value);
|
location.href = encodeUrl(value);
|
||||||
} else {
|
} else {
|
||||||
|
@ -20,6 +28,5 @@ export const locationProxy = new Proxy(window.location, {
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,18 +1,24 @@
|
||||||
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]),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -21,8 +27,8 @@ delete window.Function;
|
||||||
|
|
||||||
window.Function = FunctionProxy;
|
window.Function = FunctionProxy;
|
||||||
|
|
||||||
delete window.eval;
|
window.eval = new Proxy(window.eval, {
|
||||||
|
apply(target, thisArg, argArray) {
|
||||||
// since the function proxy is already rewriting the js we can just reuse it for the eval proxy
|
return Reflect.apply(target, thisArg, [rewriteJs(argArray[0])]);
|
||||||
|
},
|
||||||
window.eval = (str: string) => window.Function(str);
|
});
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// 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) {
|
||||||
|
@ -16,7 +16,7 @@ Headers = new Proxy(Headers, {
|
||||||
|
|
||||||
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) {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { BareClient } from "@mercuryworkshop/bare-mux"
|
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(
|
||||||
|
@ -7,9 +8,9 @@ WebSocket = new Proxy(WebSocket, {
|
||||||
args[1],
|
args[1],
|
||||||
target,
|
target,
|
||||||
{
|
{
|
||||||
"User-Agent": navigator.userAgent
|
"User-Agent": navigator.userAgent,
|
||||||
},
|
},
|
||||||
ArrayBuffer.prototype
|
ArrayBuffer.prototype
|
||||||
)
|
);
|
||||||
}
|
},
|
||||||
});
|
});
|
|
@ -1,4 +1,4 @@
|
||||||
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) {
|
||||||
|
@ -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) {
|
apply(target, thisArg, argArray) {
|
||||||
let headerObject = Object.fromEntries([argArray]);
|
let headerObject = Object.fromEntries([argArray]);
|
||||||
headerObject = rewriteHeaders(headerObject);
|
headerObject = rewriteHeaders(headerObject);
|
||||||
|
@ -17,4 +19,5 @@ XMLHttpRequest.prototype.setRequestHeader = new Proxy(XMLHttpRequest.prototype.s
|
||||||
|
|
||||||
return Reflect.apply(target, thisArg, argArray);
|
return Reflect.apply(target, thisArg, argArray);
|
||||||
},
|
},
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
|
@ -12,4 +12,4 @@ function scope(identifier: any) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// shorthand because this can get out of hand reall quickly
|
// shorthand because this can get out of hand reall quickly
|
||||||
window.__s = scope;
|
window.$s = scope;
|
||||||
|
|
12
src/client/shared.ts
Normal file
12
src/client/shared.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
export const {
|
||||||
|
util: { isScramjetFile, BareClient },
|
||||||
|
url: { encodeUrl, decodeUrl },
|
||||||
|
rewrite: {
|
||||||
|
rewriteCss,
|
||||||
|
rewriteHtml,
|
||||||
|
rewriteSrcset,
|
||||||
|
rewriteJs,
|
||||||
|
rewriteHeaders,
|
||||||
|
rewriteWorkers,
|
||||||
|
},
|
||||||
|
} = self.$scramjet.shared;
|
|
@ -3,43 +3,42 @@ 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:
|
||||||
|
@ -58,7 +57,7 @@ function storageProxy(scope: Storage): Storage {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const localStorageProxy = storageProxy(window.localStorage);
|
const localStorageProxy = storageProxy(window.localStorage);
|
||||||
|
|
|
@ -1,32 +1,40 @@
|
||||||
import { rewriteHtml, rewriteJs, encodeUrl } from "../shared";
|
// import { rewriteHtml, rewriteJs, encodeUrl } from "./shared";
|
||||||
|
|
||||||
// @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) {
|
||||||
|
// 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);
|
||||||
|
// },
|
||||||
|
// });
|
||||||
|
|
||||||
if (argArray[1].createScript) {
|
//@ts-nocheck
|
||||||
argArray[1].createScript = new Proxy(argArray[1].createScript, {
|
delete window.TrustedHTML;
|
||||||
apply(target1, thisArg1, argArray1) {
|
delete window.TrustedScript;
|
||||||
return rewriteJs(target1(...argArray1));
|
delete window.TrustedScriptURL;
|
||||||
},
|
delete window.TrustedTypePolicy;
|
||||||
});
|
delete window.TrustedTypePolicyFactory;
|
||||||
}
|
delete window.trustedTypes;
|
||||||
|
|
||||||
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);
|
|
||||||
},
|
|
||||||
})
|
|
13
src/client/url.ts
Normal file
13
src/client/url.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import { encodeUrl } from "../shared/rewriters/url";
|
||||||
|
export const URL = globalThis.URL;
|
||||||
|
|
||||||
|
if (globalThis.window) {
|
||||||
|
window.URL = new Proxy(URL, {
|
||||||
|
construct(target, argArray, newTarget) {
|
||||||
|
if (typeof argArray[0] === "string") argArray[0] = encodeUrl(argArray[0]);
|
||||||
|
if (typeof argArray[1] === "string") argArray[1] = encodeUrl(argArray[1]);
|
||||||
|
|
||||||
|
return Reflect.construct(target, argArray, newTarget);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
|
@ -5,16 +5,40 @@ export const windowProxy = new Proxy(window, {
|
||||||
const propIsString = typeof prop === "string";
|
const propIsString = typeof prop === "string";
|
||||||
if (propIsString && prop === "location") {
|
if (propIsString && prop === "location") {
|
||||||
return locationProxy;
|
return locationProxy;
|
||||||
} else if (propIsString && ["window", "top", "parent", "self", "globalThis"].includes(prop)) {
|
} else if (
|
||||||
|
propIsString &&
|
||||||
|
["window", "top", "parent", "self", "globalThis"].includes(prop)
|
||||||
|
) {
|
||||||
return windowProxy;
|
return windowProxy;
|
||||||
|
} else if (propIsString && prop === "$scramjet") {
|
||||||
|
return;
|
||||||
|
} else if (propIsString && prop === "addEventListener") {
|
||||||
|
console.log("addEventListener getteetetetetet");
|
||||||
|
|
||||||
|
return new Proxy(window.addEventListener, {
|
||||||
|
apply(target1, thisArg, argArray) {
|
||||||
|
window.addEventListener(argArray[0], argArray[1]);
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return target[prop];
|
const value = Reflect.get(target, prop);
|
||||||
|
|
||||||
|
if (typeof value === "function") {
|
||||||
|
return value.bind(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
},
|
},
|
||||||
|
|
||||||
set(target, prop, newValue) {
|
set(target, prop, newValue) {
|
||||||
// ensures that no apis are overwritten
|
// 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { encodeUrl } from "../shared";
|
import { encodeUrl } from "./shared";
|
||||||
|
|
||||||
Worker = new Proxy(Worker, {
|
Worker = new Proxy(Worker, {
|
||||||
construct(target, argArray) {
|
construct(target, argArray) {
|
||||||
|
@ -9,23 +9,25 @@ Worker = new Proxy(Worker, {
|
||||||
// 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);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
window.importScripts = new Proxy(window.importScripts, {
|
// broken
|
||||||
apply(target, thisArg, argArray) {
|
|
||||||
for (const i in argArray) {
|
|
||||||
argArray[i] = encodeUrl(argArray[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Reflect.apply(target, thisArg, argArray);
|
// 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);
|
||||||
|
// },
|
||||||
|
// });
|
||||||
|
|
|
@ -9,14 +9,14 @@ function enc_utf8(s) {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw "Error on UTF-8 encode";
|
throw "Error on UTF-8 encode";
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
function dec_utf8(s) {
|
function dec_utf8(s) {
|
||||||
try {
|
try {
|
||||||
return decodeURIComponent(escape(s));
|
return decodeURIComponent(escape(s));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw "Bad Key";
|
throw "Bad Key";
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
function padBlock(byteArr) {
|
function padBlock(byteArr) {
|
||||||
var array = [],
|
var array = [],
|
||||||
cpad,
|
cpad,
|
||||||
|
@ -46,7 +46,7 @@ function padBlock(byteArr) {
|
||||||
array[i] = byteArr[i];
|
array[i] = byteArr[i];
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
};
|
}
|
||||||
function block2s(block, lastBlock) {
|
function block2s(block, lastBlock) {
|
||||||
var string = "",
|
var string = "",
|
||||||
padding,
|
padding,
|
||||||
|
@ -68,7 +68,7 @@ function block2s(block, lastBlock) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return string;
|
return string;
|
||||||
};
|
}
|
||||||
function a2h(numArr) {
|
function a2h(numArr) {
|
||||||
var string = "",
|
var string = "",
|
||||||
i;
|
i;
|
||||||
|
@ -76,14 +76,14 @@ function a2h(numArr) {
|
||||||
string += (numArr[i] < 16 ? "0" : "") + numArr[i].toString(16);
|
string += (numArr[i] < 16 ? "0" : "") + numArr[i].toString(16);
|
||||||
}
|
}
|
||||||
return string;
|
return string;
|
||||||
};
|
}
|
||||||
function h2a(s) {
|
function h2a(s) {
|
||||||
var ret = [];
|
var ret = [];
|
||||||
s.replace(/(..)/g, function (s) {
|
s.replace(/(..)/g, function (s) {
|
||||||
ret.push(parseInt(s, 16));
|
ret.push(parseInt(s, 16));
|
||||||
});
|
});
|
||||||
return ret;
|
return ret;
|
||||||
};
|
}
|
||||||
function s2a(string, binary) {
|
function s2a(string, binary) {
|
||||||
var array = [],
|
var array = [],
|
||||||
i;
|
i;
|
||||||
|
@ -97,7 +97,7 @@ function s2a(string, binary) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return array;
|
return array;
|
||||||
};
|
}
|
||||||
function size(newsize) {
|
function size(newsize) {
|
||||||
switch (newsize) {
|
switch (newsize) {
|
||||||
case 128:
|
case 128:
|
||||||
|
@ -115,7 +115,7 @@ function size(newsize) {
|
||||||
default:
|
default:
|
||||||
throw "Invalid Key Size Specified:" + newsize;
|
throw "Invalid Key Size Specified:" + newsize;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
function randArr(num) {
|
function randArr(num) {
|
||||||
var result = [],
|
var result = [],
|
||||||
i;
|
i;
|
||||||
|
@ -123,7 +123,7 @@ function randArr(num) {
|
||||||
result = result.concat(Math.floor(Math.random() * 256));
|
result = result.concat(Math.floor(Math.random() * 256));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
};
|
}
|
||||||
function openSSLKey(passwordArr, saltArr) {
|
function openSSLKey(passwordArr, saltArr) {
|
||||||
var rounds = Nr >= 12 ? 3 : 2,
|
var rounds = Nr >= 12 ? 3 : 2,
|
||||||
key = [],
|
key = [],
|
||||||
|
@ -144,7 +144,7 @@ function openSSLKey(passwordArr, saltArr) {
|
||||||
key: key,
|
key: key,
|
||||||
iv: iv,
|
iv: iv,
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
function rawEncrypt(plaintext, key, iv) {
|
function rawEncrypt(plaintext, key, iv) {
|
||||||
key = expandKey(key);
|
key = expandKey(key);
|
||||||
var numBlocks = Math.ceil(plaintext.length / 16),
|
var numBlocks = Math.ceil(plaintext.length / 16),
|
||||||
|
@ -168,7 +168,7 @@ function rawEncrypt(plaintext, key, iv) {
|
||||||
cipherBlocks[i] = encryptBlock(blocks[i], key);
|
cipherBlocks[i] = encryptBlock(blocks[i], key);
|
||||||
}
|
}
|
||||||
return cipherBlocks;
|
return cipherBlocks;
|
||||||
};
|
}
|
||||||
function rawDecrypt(cryptArr, key, iv, binary) {
|
function rawDecrypt(cryptArr, key, iv, binary) {
|
||||||
key = expandKey(key);
|
key = expandKey(key);
|
||||||
var numBlocks = cryptArr.length / 16,
|
var numBlocks = cryptArr.length / 16,
|
||||||
|
@ -191,7 +191,7 @@ function rawDecrypt(cryptArr, key, iv, binary) {
|
||||||
}
|
}
|
||||||
string += block2s(plainBlocks[i], true);
|
string += block2s(plainBlocks[i], true);
|
||||||
return binary ? string : dec_utf8(string);
|
return binary ? string : dec_utf8(string);
|
||||||
};
|
}
|
||||||
function encryptBlock(block, words) {
|
function encryptBlock(block, words) {
|
||||||
Decrypt = false;
|
Decrypt = false;
|
||||||
var state = addRoundKey(block, words, 0),
|
var state = addRoundKey(block, words, 0),
|
||||||
|
@ -206,7 +206,7 @@ function encryptBlock(block, words) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return state;
|
return state;
|
||||||
};
|
}
|
||||||
function decryptBlock(block, words) {
|
function decryptBlock(block, words) {
|
||||||
Decrypt = true;
|
Decrypt = true;
|
||||||
var state = addRoundKey(block, words, Nr),
|
var state = addRoundKey(block, words, Nr),
|
||||||
|
@ -221,7 +221,7 @@ function decryptBlock(block, words) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return state;
|
return state;
|
||||||
};
|
}
|
||||||
function subBytes(state) {
|
function subBytes(state) {
|
||||||
var S = Decrypt ? SBoxInv : SBox,
|
var S = Decrypt ? SBoxInv : SBox,
|
||||||
temp = [],
|
temp = [],
|
||||||
|
@ -230,7 +230,7 @@ function subBytes(state) {
|
||||||
temp[i] = S[state[i]];
|
temp[i] = S[state[i]];
|
||||||
}
|
}
|
||||||
return temp;
|
return temp;
|
||||||
};
|
}
|
||||||
function shiftRows(state) {
|
function shiftRows(state) {
|
||||||
var temp = [],
|
var temp = [],
|
||||||
shiftBy = Decrypt
|
shiftBy = Decrypt
|
||||||
|
@ -241,7 +241,7 @@ function shiftRows(state) {
|
||||||
temp[i] = state[shiftBy[i]];
|
temp[i] = state[shiftBy[i]];
|
||||||
}
|
}
|
||||||
return temp;
|
return temp;
|
||||||
};
|
}
|
||||||
function mixColumns(state) {
|
function mixColumns(state) {
|
||||||
var t = [],
|
var t = [],
|
||||||
c;
|
c;
|
||||||
|
@ -294,7 +294,7 @@ function mixColumns(state) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return t;
|
return t;
|
||||||
};
|
}
|
||||||
function addRoundKey(state, words, round) {
|
function addRoundKey(state, words, round) {
|
||||||
var temp = [],
|
var temp = [],
|
||||||
i;
|
i;
|
||||||
|
@ -302,7 +302,7 @@ function addRoundKey(state, words, round) {
|
||||||
temp[i] = state[i] ^ words[round][i];
|
temp[i] = state[i] ^ words[round][i];
|
||||||
}
|
}
|
||||||
return temp;
|
return temp;
|
||||||
};
|
}
|
||||||
function xorBlocks(block1, block2) {
|
function xorBlocks(block1, block2) {
|
||||||
var temp = [],
|
var temp = [],
|
||||||
i;
|
i;
|
||||||
|
@ -310,7 +310,7 @@ function xorBlocks(block1, block2) {
|
||||||
temp[i] = block1[i] ^ block2[i];
|
temp[i] = block1[i] ^ block2[i];
|
||||||
}
|
}
|
||||||
return temp;
|
return temp;
|
||||||
};
|
}
|
||||||
function expandKey(key) {
|
function expandKey(key) {
|
||||||
var w = [],
|
var w = [],
|
||||||
temp = [],
|
temp = [],
|
||||||
|
@ -352,13 +352,13 @@ function expandKey(key) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return flat;
|
return flat;
|
||||||
};
|
}
|
||||||
function subWord(w) {
|
function subWord(w) {
|
||||||
for (var i = 0; i < 4; i++) {
|
for (var i = 0; i < 4; i++) {
|
||||||
w[i] = SBox[w[i]];
|
w[i] = SBox[w[i]];
|
||||||
}
|
}
|
||||||
return w;
|
return w;
|
||||||
};
|
}
|
||||||
function rotWord(w) {
|
function rotWord(w) {
|
||||||
var tmp = w[0],
|
var tmp = w[0],
|
||||||
i;
|
i;
|
||||||
|
@ -367,7 +367,7 @@ function rotWord(w) {
|
||||||
}
|
}
|
||||||
w[3] = tmp;
|
w[3] = tmp;
|
||||||
return w;
|
return w;
|
||||||
};
|
}
|
||||||
function strhex(str, size) {
|
function strhex(str, size) {
|
||||||
var i,
|
var i,
|
||||||
ret = [];
|
ret = [];
|
||||||
|
@ -375,7 +375,7 @@ function strhex(str, size) {
|
||||||
ret[i / size] = parseInt(str.substr(i, size), 16);
|
ret[i / size] = parseInt(str.substr(i, size), 16);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
};
|
}
|
||||||
function invertArr(arr) {
|
function invertArr(arr) {
|
||||||
var i,
|
var i,
|
||||||
ret = [];
|
ret = [];
|
||||||
|
@ -383,7 +383,7 @@ function invertArr(arr) {
|
||||||
ret[arr[i]] = i;
|
ret[arr[i]] = i;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
};
|
}
|
||||||
function Gxx(a, b) {
|
function Gxx(a, b) {
|
||||||
var i, ret;
|
var i, ret;
|
||||||
|
|
||||||
|
@ -395,7 +395,7 @@ function Gxx(a, b) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
};
|
}
|
||||||
function Gx(x) {
|
function Gx(x) {
|
||||||
var i,
|
var i,
|
||||||
r = [];
|
r = [];
|
||||||
|
@ -403,7 +403,7 @@ function Gx(x) {
|
||||||
r[i] = Gxx(x, i);
|
r[i] = Gxx(x, i);
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
};
|
}
|
||||||
var SBox = strhex(
|
var SBox = strhex(
|
||||||
"637c777bf26b6fc53001672bfed7ab76ca82c97dfa5947f0add4a2af9ca472c0b7fd9326363ff7cc34a5e5f171d8311504c723c31896059a071280e2eb27b27509832c1a1b6e5aa0523bd6b329e32f8453d100ed20fcb15b6acbbe394a4c58cfd0efaafb434d338545f9027f503c9fa851a3408f929d38f5bcb6da2110fff3d2cd0c13ec5f974417c4a77e3d645d197360814fdc222a908846eeb814de5e0bdbe0323a0a4906245cc2d3ac629195e479e7c8376d8dd54ea96c56f4ea657aae08ba78252e1ca6b4c6e8dd741f4bbd8b8a703eb5664803f60e613557b986c11d9ee1f8981169d98e949b1e87e9ce5528df8ca1890dbfe6426841992d0fb054bb16",
|
"637c777bf26b6fc53001672bfed7ab76ca82c97dfa5947f0add4a2af9ca472c0b7fd9326363ff7cc34a5e5f171d8311504c723c31896059a071280e2eb27b27509832c1a1b6e5aa0523bd6b329e32f8453d100ed20fcb15b6acbbe394a4c58cfd0efaafb434d338545f9027f503c9fa851a3408f929d38f5bcb6da2110fff3d2cd0c13ec5f974417c4a77e3d645d197360814fdc222a908846eeb814de5e0bdbe0323a0a4906245cc2d3ac629195e479e7c8376d8dd54ea96c56f4ea657aae08ba78252e1ca6b4c6e8dd741f4bbd8b8a703eb5664803f60e613557b986c11d9ee1f8981169d98e949b1e87e9ce5528df8ca1890dbfe6426841992d0fb054bb16",
|
||||||
2
|
2
|
||||||
|
@ -431,7 +431,7 @@ function enc(string, pass, binary) {
|
||||||
|
|
||||||
cipherBlocks = saltBlock.concat(cipherBlocks);
|
cipherBlocks = saltBlock.concat(cipherBlocks);
|
||||||
return Base64.encode(cipherBlocks);
|
return Base64.encode(cipherBlocks);
|
||||||
};
|
}
|
||||||
function dec(string, pass, binary) {
|
function dec(string, pass, binary) {
|
||||||
var cryptArr = Base64.decode(string),
|
var cryptArr = Base64.decode(string),
|
||||||
salt = cryptArr.slice(8, 16),
|
salt = cryptArr.slice(8, 16),
|
||||||
|
@ -442,7 +442,7 @@ function dec(string, pass, binary) {
|
||||||
|
|
||||||
string = rawDecrypt(cryptArr, key, iv, binary);
|
string = rawDecrypt(cryptArr, key, iv, binary);
|
||||||
return string;
|
return string;
|
||||||
};
|
}
|
||||||
function MD5(numArr) {
|
function MD5(numArr) {
|
||||||
function rotateLeft(lValue, iShiftBits) {
|
function rotateLeft(lValue, iShiftBits) {
|
||||||
return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
|
return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
|
||||||
|
@ -636,7 +636,7 @@ function MD5(numArr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return wordToHex(a).concat(wordToHex(b), wordToHex(c), wordToHex(d));
|
return wordToHex(a).concat(wordToHex(b), wordToHex(c), wordToHex(d));
|
||||||
};
|
}
|
||||||
// function encString (plaintext, key, iv) {
|
// function encString (plaintext, key, iv) {
|
||||||
// var i;
|
// var i;
|
||||||
// plaintext = s2a(plaintext, false);
|
// plaintext = s2a(plaintext, false);
|
||||||
|
@ -799,7 +799,7 @@ const Base64 = {
|
||||||
}
|
}
|
||||||
flatArr = flatArr.slice(0, flatArr.length - (flatArr.length % 16));
|
flatArr = flatArr.slice(0, flatArr.length - (flatArr.length % 16));
|
||||||
return flatArr;
|
return flatArr;
|
||||||
}
|
},
|
||||||
}
|
};
|
||||||
|
|
||||||
export { enc, dec };
|
export { enc, dec };
|
|
@ -12,14 +12,24 @@ 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) => {
|
||||||
|
@ -31,8 +41,8 @@ const plain = {
|
||||||
if (!str) return str;
|
if (!str) return str;
|
||||||
|
|
||||||
return decodeURIComponent(str);
|
return decodeURIComponent(str);
|
||||||
}
|
},
|
||||||
}
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
const aes = {
|
const aes = {
|
||||||
|
@ -52,7 +62,7 @@ const aes = {
|
||||||
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) => {
|
||||||
|
@ -64,20 +74,16 @@ const base64 = {
|
||||||
if (!str) return str;
|
if (!str) return str;
|
||||||
|
|
||||||
return atob(str);
|
return atob(str);
|
||||||
}
|
},
|
||||||
}
|
};
|
||||||
|
|
||||||
declare global {
|
if (!self.$scramjet) {
|
||||||
interface Window {
|
//@ts-expect-error really dumb workaround
|
||||||
__scramjet$codecs: {
|
self.$scramjet = {};
|
||||||
none: Codec;
|
|
||||||
plain: Codec;
|
|
||||||
base64: Codec;
|
|
||||||
xor: Codec;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.__scramjet$codecs = {
|
|
||||||
none, plain, base64, xor
|
|
||||||
}
|
}
|
||||||
|
self.$scramjet.codecs = {
|
||||||
|
none,
|
||||||
|
plain,
|
||||||
|
base64,
|
||||||
|
xor,
|
||||||
|
};
|
||||||
|
|
|
@ -1,25 +1,13 @@
|
||||||
import { Codec } from "./codecs";
|
if (!self.$scramjet) {
|
||||||
|
//@ts-expect-error really dumb workaround
|
||||||
declare global {
|
self.$scramjet = {};
|
||||||
interface Window {
|
|
||||||
__scramjet$config: {
|
|
||||||
prefix: string;
|
|
||||||
codec: Codec
|
|
||||||
config: string;
|
|
||||||
shared: string;
|
|
||||||
worker: string;
|
|
||||||
client: string;
|
|
||||||
codecs: string;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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",
|
||||||
}
|
};
|
||||||
|
|
|
@ -1,16 +1,33 @@
|
||||||
export { encodeUrl, decodeUrl } from "./rewriters/url";
|
import { encodeUrl, decodeUrl } from "./rewriters/url";
|
||||||
export { rewriteCss } from "./rewriters/css";
|
import { rewriteCss } from "./rewriters/css";
|
||||||
export { rewriteHtml, rewriteSrcset } from "./rewriters/html";
|
import { rewriteHtml, rewriteSrcset } from "./rewriters/html";
|
||||||
export { rewriteJs } from "./rewriters/js";
|
import { rewriteJs } from "./rewriters/js";
|
||||||
export { rewriteHeaders } from "./rewriters/headers";
|
import { rewriteHeaders } from "./rewriters/headers";
|
||||||
export { rewriteWorkers } from "./rewriters/worker"
|
import { rewriteWorkers } from "./rewriters/worker";
|
||||||
export { BareClient } from "@mercuryworkshop/bare-mux"
|
import { isScramjetFile } from "./rewriters/html";
|
||||||
|
import { BareClient } from "@mercuryworkshop/bare-mux";
|
||||||
|
import { parseDomain } from "parse-domain";
|
||||||
|
|
||||||
export function isScramjetFile(src: string) {
|
if (!self.$scramjet) {
|
||||||
let bool = false;
|
//@ts-expect-error really dumb workaround
|
||||||
["codecs", "client", "shared", "worker", "config"].forEach((file) => {
|
self.$scramjet = {};
|
||||||
if (src === self.__scramjet$config[file]) bool = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
return bool;
|
|
||||||
}
|
}
|
||||||
|
self.$scramjet.shared = {
|
||||||
|
util: {
|
||||||
|
isScramjetFile,
|
||||||
|
parseDomain,
|
||||||
|
BareClient,
|
||||||
|
},
|
||||||
|
url: {
|
||||||
|
encodeUrl,
|
||||||
|
decodeUrl,
|
||||||
|
},
|
||||||
|
rewrite: {
|
||||||
|
rewriteCss,
|
||||||
|
rewriteHtml,
|
||||||
|
rewriteSrcset,
|
||||||
|
rewriteJs,
|
||||||
|
rewriteHeaders,
|
||||||
|
rewriteWorkers,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { encodeUrl } from "./url";
|
||||||
|
|
||||||
export function rewriteCss(css: string, origin?: URL) {
|
export function rewriteCss(css: string, origin?: URL) {
|
||||||
const regex =
|
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(
|
return css.replace(
|
||||||
regex,
|
regex,
|
||||||
|
@ -17,19 +17,18 @@ export function rewriteCss(css: string, origin?: URL) {
|
||||||
importQuote,
|
importQuote,
|
||||||
importContent
|
importContent
|
||||||
) => {
|
) => {
|
||||||
const url = urlContent || importContent
|
const url = urlContent || importContent;
|
||||||
const encodedUrl = encodeUrl(url.trim(), origin)
|
const encodedUrl = encodeUrl(url.trim(), origin);
|
||||||
|
|
||||||
if (importStatement) {
|
if (importStatement) {
|
||||||
return `@import url(${urlQuote}${encodedUrl}${urlQuote})`
|
return `@import url(${urlQuote}${encodedUrl}${urlQuote})`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (importQuote) {
|
if (importQuote) {
|
||||||
return `@import ${importQuote}${encodedUrl}${importQuote}`
|
return `@import ${importQuote}${encodedUrl}${importQuote}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return `url(${urlQuote}${encodedUrl}${urlQuote})`
|
return `url(${urlQuote}${encodedUrl}${urlQuote})`;
|
||||||
}
|
}
|
||||||
)
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,14 +19,10 @@ const cspHeaders = [
|
||||||
"x-xss-protection",
|
"x-xss-protection",
|
||||||
// This needs to be emulated, but for right now it isn't that important of a feature to be worried about
|
// 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
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Clear-Site-Data
|
||||||
"clear-site-data"
|
"clear-site-data",
|
||||||
];
|
];
|
||||||
|
|
||||||
const urlHeaders = [
|
const urlHeaders = ["location", "content-location", "referer"];
|
||||||
"location",
|
|
||||||
"content-location",
|
|
||||||
"referer"
|
|
||||||
];
|
|
||||||
|
|
||||||
export function rewriteHeaders(rawHeaders: BareHeaders, origin?: URL) {
|
export function rewriteHeaders(rawHeaders: BareHeaders, origin?: URL) {
|
||||||
const headers = {};
|
const headers = {};
|
||||||
|
@ -45,7 +41,9 @@ export function rewriteHeaders(rawHeaders: BareHeaders, origin?: URL) {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (headers["link"]) {
|
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;
|
return headers;
|
||||||
|
|
|
@ -4,8 +4,16 @@ import { hasAttrib } from "domutils";
|
||||||
import render from "dom-serializer";
|
import render from "dom-serializer";
|
||||||
import { encodeUrl } from "./url";
|
import { encodeUrl } from "./url";
|
||||||
import { rewriteCss } from "./css";
|
import { rewriteCss } from "./css";
|
||||||
// import { rewriteJs } from "./js";
|
import { rewriteJs } from "./js";
|
||||||
import { isScramjetFile } from "..";
|
|
||||||
|
export function isScramjetFile(src: string) {
|
||||||
|
let bool = false;
|
||||||
|
["codecs", "client", "shared", "worker", "config"].forEach((file) => {
|
||||||
|
if (src === self.$scramjet.config[file]) bool = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
return bool;
|
||||||
|
}
|
||||||
|
|
||||||
export function rewriteHtml(html: string, origin?: URL) {
|
export function rewriteHtml(html: string, origin?: URL) {
|
||||||
const handler = new DomHandler((err, dom) => dom);
|
const handler = new DomHandler((err, dom) => dom);
|
||||||
|
@ -29,7 +37,7 @@ function traverseParsedHtml(node, origin?: URL) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* url attributes */
|
/* url attributes */
|
||||||
for (const urlAttr of ["src", "href", "data", "action", "formaction"]) {
|
for (const urlAttr of ["src", "href", "action", "formaction"]) {
|
||||||
if (hasAttrib(node, urlAttr) && !isScramjetFile(node.attribs[urlAttr])) {
|
if (hasAttrib(node, urlAttr) && !isScramjetFile(node.attribs[urlAttr])) {
|
||||||
const value = node.attribs[urlAttr];
|
const value = node.attribs[urlAttr];
|
||||||
node.attribs[`data-${urlAttr}`] = value;
|
node.attribs[`data-${urlAttr}`] = value;
|
||||||
|
@ -46,15 +54,32 @@ function traverseParsedHtml(node, origin?: URL) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasAttrib(node, "srcdoc")) node.attribs.srcdoc = rewriteHtml(node.attribs.srcdoc, origin);
|
if (hasAttrib(node, "srcdoc"))
|
||||||
if (hasAttrib(node, "style")) node.attribs.style = rewriteCss(node.attribs.style, origin);
|
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 === "style" && node.children[0] !== undefined)
|
||||||
// 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);
|
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
|
||||||
|
) {
|
||||||
|
let js = node.children[0].data
|
||||||
|
const htmlcomment = /<!--[\s\S]*?-->/g;
|
||||||
|
js = js.replace(htmlcomment, "");
|
||||||
|
node.children[0].data = rewriteJs(js, origin);
|
||||||
|
}
|
||||||
if (node.name === "meta" && hasAttrib(node, "http-equiv")) {
|
if (node.name === "meta" && hasAttrib(node, "http-equiv")) {
|
||||||
if (node.attribs["http-equiv"] === "content-security-policy") {
|
if (node.attribs["http-equiv"] === "content-security-policy") {
|
||||||
node = {};
|
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=");
|
const contentArray = node.attribs.content.split("url=");
|
||||||
contentArray[1] = encodeUrl(contentArray[1].trim(), origin);
|
contentArray[1] = encodeUrl(contentArray[1].trim(), origin);
|
||||||
node.attribs.content = contentArray.join("url=");
|
node.attribs.content = contentArray.join("url=");
|
||||||
|
@ -64,11 +89,12 @@ function traverseParsedHtml(node, origin?: URL) {
|
||||||
if (node.name === "head") {
|
if (node.name === "head") {
|
||||||
const scramjetScripts = [];
|
const scramjetScripts = [];
|
||||||
["codecs", "config", "shared", "client"].forEach((script) => {
|
["codecs", "config", "shared", "client"].forEach((script) => {
|
||||||
scramjetScripts.push(new Element("script", {
|
scramjetScripts.push(
|
||||||
src: self.__scramjet$config[script],
|
new Element("script", {
|
||||||
type: "module",
|
src: self.$scramjet.config[script],
|
||||||
"data-scramjet": ""
|
"data-scramjet": "",
|
||||||
}));
|
})
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
node.children.unshift(...scramjetScripts);
|
node.children.unshift(...scramjetScripts);
|
||||||
|
@ -76,7 +102,10 @@ function traverseParsedHtml(node, origin?: URL) {
|
||||||
|
|
||||||
if (node.childNodes) {
|
if (node.childNodes) {
|
||||||
for (const childNode in node.childNodes) {
|
for (const childNode in node.childNodes) {
|
||||||
node.childNodes[childNode] = traverseParsedHtml(node.childNodes[childNode], origin);
|
node.childNodes[childNode] = traverseParsedHtml(
|
||||||
|
node.childNodes[childNode],
|
||||||
|
origin
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,12 +16,11 @@ import * as ESTree from "estree";
|
||||||
// top
|
// top
|
||||||
// parent
|
// parent
|
||||||
|
|
||||||
|
|
||||||
export function rewriteJs(js: string, origin?: URL) {
|
export function rewriteJs(js: string, origin?: URL) {
|
||||||
try {
|
try {
|
||||||
const ast = parseModule(js, {
|
const ast = parseModule(js, {
|
||||||
module: true,
|
module: true,
|
||||||
webcompat: true
|
webcompat: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const identifierList = [
|
const identifierList = [
|
||||||
|
@ -31,9 +30,8 @@ export function rewriteJs(js: string, origin?: URL) {
|
||||||
"this",
|
"this",
|
||||||
"parent",
|
"parent",
|
||||||
"top",
|
"top",
|
||||||
"this",
|
"location",
|
||||||
"location"
|
];
|
||||||
]
|
|
||||||
|
|
||||||
const customTraveler = makeTraveler({
|
const customTraveler = makeTraveler({
|
||||||
ImportDeclaration: (node: ESTree.ImportDeclaration) => {
|
ImportDeclaration: (node: ESTree.ImportDeclaration) => {
|
||||||
|
@ -57,21 +55,51 @@ export function rewriteJs(js: string, origin?: URL) {
|
||||||
|
|
||||||
ExportNamedDeclaration: (node: ESTree.ExportNamedDeclaration) => {
|
ExportNamedDeclaration: (node: ESTree.ExportNamedDeclaration) => {
|
||||||
// strings are Literals in ESTree syntax but these will always be strings
|
// 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);
|
||||||
},
|
},
|
||||||
|
|
||||||
// js rweriting notrdone
|
|
||||||
MemberExpression: (node: ESTree.MemberExpression) => {
|
MemberExpression: (node: ESTree.MemberExpression) => {
|
||||||
if (node.object.type === "Identifier" && identifierList.includes(node.object.name)) {
|
if (
|
||||||
node.object.name = `__s(${node.object.name})`;
|
node.object.type === "Identifier" &&
|
||||||
|
identifierList.includes(node.object.name)
|
||||||
|
) {
|
||||||
|
node.object.name = `globalThis.$s(${node.object.name})`;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
AssignmentExpression: (node: ESTree.AssignmentExpression) => {
|
||||||
|
if (
|
||||||
|
node.left.type === "Identifier" &&
|
||||||
|
identifierList.includes(node.left.name)
|
||||||
|
) {
|
||||||
|
node.left.name = `globalThis.$s(${node.left.name})`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
node.right.type === "Identifier" &&
|
||||||
|
identifierList.includes(node.right.name)
|
||||||
|
) {
|
||||||
|
node.right.name = `globalThis.$s(${node.right.name})`;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
VariableDeclarator: (node: ESTree.VariableDeclarator) => {
|
||||||
|
if (
|
||||||
|
node.init &&
|
||||||
|
node.init.type === "Identifier" &&
|
||||||
|
identifierList.includes(node.init.name)
|
||||||
|
) {
|
||||||
|
node.init.name = `globalThis.$s(${node.init.name})`;
|
||||||
|
}
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
customTraveler.go(ast);
|
customTraveler.go(ast);
|
||||||
|
|
||||||
return generate(ast);
|
return generate(ast);
|
||||||
} catch {
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
console.log(js);
|
console.log(js);
|
||||||
|
|
||||||
return js;
|
return js;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { URL } from "../../client/url";
|
||||||
import { rewriteJs } from "./js";
|
import { rewriteJs } from "./js";
|
||||||
|
|
||||||
function canParseUrl(url: string, origin?: URL) {
|
function canParseUrl(url: string, origin?: URL) {
|
||||||
|
@ -11,26 +12,49 @@ function canParseUrl(url: string, origin?: URL) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// something is broken with this but i didn't debug it
|
// something is broken with this but i didn't debug it
|
||||||
export function encodeUrl(url: string, origin?: URL) {
|
export function encodeUrl(url: string | URL, origin?: URL) {
|
||||||
if (!origin) {
|
if (url instanceof URL) {
|
||||||
origin = new URL(self.__scramjet$config.codec.decode(location.href.slice((location.origin + self.__scramjet$config.prefix).length)));
|
return url.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!origin) {
|
||||||
|
origin = new URL(
|
||||||
|
self.$scramjet.config.codec.decode(
|
||||||
|
location.href.slice(
|
||||||
|
(location.origin + self.$scramjet.config.prefix).length
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// is this the correct behavior?
|
||||||
|
if (!url) url = origin.href;
|
||||||
|
|
||||||
if (url.startsWith("javascript:")) {
|
if (url.startsWith("javascript:")) {
|
||||||
return "javascript:" + rewriteJs(url.slice("javascript:".length));
|
return "javascript:" + rewriteJs(url.slice("javascript:".length));
|
||||||
} else if (/^(#|mailto|about|data)/.test(url)) {
|
} else if (/^(#|mailto|about|data)/.test(url)) {
|
||||||
return url;
|
return url;
|
||||||
} else if (canParseUrl(url, origin)) {
|
} 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)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// something is also broken with this but i didn't debug it
|
// something is also broken with this but i didn't debug it
|
||||||
export function decodeUrl(url: string) {
|
export function decodeUrl(url: string | URL) {
|
||||||
|
if (url instanceof URL) {
|
||||||
|
return url.toString();
|
||||||
|
}
|
||||||
|
|
||||||
if (/^(#|about|data|mailto|javascript)/.test(url)) {
|
if (/^(#|about|data|mailto|javascript)/.test(url)) {
|
||||||
return url;
|
return url;
|
||||||
} else if (canParseUrl(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 {
|
} else {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import { rewriteJs } from "./js";
|
import { rewriteJs } from "./js";
|
||||||
export function rewriteWorkers(js: string, origin?: URL) {
|
export function rewriteWorkers(js: string, origin?: URL) {
|
||||||
let str = new String().toString()
|
let str = new String().toString()[
|
||||||
//@ts-expect-error
|
//@ts-expect-error
|
||||||
["codecs", "config", "shared", "client"].forEach((script) => {
|
("codecs", "config", "shared", "client")
|
||||||
str += `import "${self.__scramjet$config[script]}"\n`
|
].forEach((script) => {
|
||||||
})
|
str += `import "${self.$scramjet.config[script]}"\n`;
|
||||||
|
});
|
||||||
str += rewriteJs(js, origin);
|
str += rewriteJs(js, origin);
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
|
|
51
src/types.d.ts
vendored
Normal file
51
src/types.d.ts
vendored
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
import { encodeUrl, decodeUrl } from "./shared/rewriters/url";
|
||||||
|
import { rewriteCss } from "./shared/rewriters/css";
|
||||||
|
import { rewriteHtml, rewriteSrcset } from "./shared/rewriters/html";
|
||||||
|
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 { BareClient } from "@mercuryworkshop/bare-mux";
|
||||||
|
import { parseDomain } from "parse-domain";
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface Window {
|
||||||
|
$scramjet: {
|
||||||
|
shared: {
|
||||||
|
url: {
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
util: {
|
||||||
|
BareClient: typeof BareClient;
|
||||||
|
isScramjetFile: typeof isScramjetFile;
|
||||||
|
parseDomain: typeof parseDomain;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
config: {
|
||||||
|
prefix: string;
|
||||||
|
codec: Codec;
|
||||||
|
config: string;
|
||||||
|
shared: string;
|
||||||
|
worker: string;
|
||||||
|
client: string;
|
||||||
|
codecs: string;
|
||||||
|
};
|
||||||
|
codecs: {
|
||||||
|
none: Codec;
|
||||||
|
plain: Codec;
|
||||||
|
base64: Codec;
|
||||||
|
xor: Codec;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
import { BareClient } from "@mercuryworkshop/bare-mux";
|
|
||||||
import { BareResponseFetch } from "@mercuryworkshop/bare-mux";
|
import { BareResponseFetch } from "@mercuryworkshop/bare-mux";
|
||||||
import { encodeUrl, decodeUrl, rewriteCss, rewriteHeaders, rewriteHtml, rewriteJs, rewriteWorkers } from "../shared";
|
import IDBMap from "@webreflection/idb-map";
|
||||||
|
import { ParseResultType } from "parse-domain";
|
||||||
|
import { parse } from "path";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
|
@ -8,30 +9,48 @@ declare global {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class ScramjetServiceWorker {
|
self.ScramjetServiceWorker = class ScramjetServiceWorker {
|
||||||
client: typeof BareClient.prototype;
|
client: typeof self.$scramjet.shared.util.BareClient.prototype;
|
||||||
config: typeof self.__scramjet$config;
|
config: typeof self.$scramjet.config;
|
||||||
constructor(config = self.__scramjet$config) {
|
|
||||||
this.client = new BareClient();
|
constructor(config = self.$scramjet.config) {
|
||||||
|
this.client = new self.$scramjet.shared.util.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 } = self.$scramjet.shared.url;
|
||||||
|
const {
|
||||||
|
rewriteHeaders,
|
||||||
|
rewriteHtml,
|
||||||
|
rewriteJs,
|
||||||
|
rewriteCss,
|
||||||
|
rewriteWorkers,
|
||||||
|
} = self.$scramjet.shared.rewrite;
|
||||||
|
const { parseDomain } = self.$scramjet.shared.util;
|
||||||
|
|
||||||
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.host, {
|
||||||
|
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,
|
||||||
|
@ -40,15 +59,76 @@ export default class ScramjetServiceWorker {
|
||||||
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
|
||||||
|
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[]) {
|
||||||
|
let cookieParsed = cookie.split(";").map((x) => x.trim().split("="));
|
||||||
|
|
||||||
|
let [key, value] = cookieParsed.shift();
|
||||||
|
value = value.replace('"', "");
|
||||||
|
|
||||||
|
const hostArg = cookieParsed.find((x) => x[0] === "Domain");
|
||||||
|
cookieParsed = cookieParsed.filter((x) => x[0] !== "Domain");
|
||||||
|
let host = hostArg ? hostArg[1] : undefined;
|
||||||
|
|
||||||
|
if (url.protocol === "http" && cookieParsed.includes(["Secure"]))
|
||||||
|
continue;
|
||||||
|
if (
|
||||||
|
cookieParsed.includes(["SameSite", "None"]) &&
|
||||||
|
!cookieParsed.includes(["Secure"])
|
||||||
|
)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (host && host !== url.host) {
|
||||||
|
if (host.startsWith(".")) host = host.slice(1);
|
||||||
|
const urlDomain = parseDomain(url.hostname);
|
||||||
|
|
||||||
|
if (urlDomain.type === ParseResultType.Listed) {
|
||||||
|
const { subDomains: _, domain, topLevelDomains } = urlDomain;
|
||||||
|
if (!host.endsWith([domain, ...topLevelDomains].join(".")))
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const realCookieStore = new IDBMap(host, {
|
||||||
|
durability: "relaxed",
|
||||||
|
prefix: "Cookies",
|
||||||
|
});
|
||||||
|
realCookieStore.set(key, {
|
||||||
|
value: value,
|
||||||
|
args: cookieParsed,
|
||||||
|
subdomain: true,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
cookieStore.set(key, {
|
||||||
|
value: value,
|
||||||
|
args: cookieParsed,
|
||||||
|
subdomain: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let header in responseHeaders) {
|
||||||
|
// flatten everything past here
|
||||||
|
if (responseHeaders[header] instanceof Array)
|
||||||
|
responseHeaders[header] = responseHeaders[header][0];
|
||||||
|
}
|
||||||
|
|
||||||
if (response.body) {
|
if (response.body) {
|
||||||
switch (request.destination) {
|
switch (request.destination) {
|
||||||
case "iframe":
|
case "iframe":
|
||||||
case "document":
|
case "document":
|
||||||
if (responseHeaders["content-type"].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;
|
||||||
|
@ -86,9 +166,8 @@ export default class ScramjetServiceWorker {
|
||||||
.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") {
|
||||||
|
@ -101,8 +180,8 @@ export default class ScramjetServiceWorker {
|
||||||
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 });
|
||||||
|
@ -112,13 +191,9 @@ export default class ScramjetServiceWorker {
|
||||||
return renderError(err, decodeUrl(request.url));
|
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
|
// 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)};
|
||||||
|
@ -128,10 +203,9 @@ function errorTemplate(
|
||||||
)};
|
)};
|
||||||
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" />
|
||||||
|
@ -168,8 +242,7 @@ function errorTemplate(
|
||||||
}"></script>
|
}"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
`
|
`;
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -182,18 +255,11 @@ function renderError(err, fetchedURL) {
|
||||||
"content-type": "text/html",
|
"content-type": "text/html",
|
||||||
};
|
};
|
||||||
if (crossOriginIsolated) {
|
if (crossOriginIsolated) {
|
||||||
headers["Cross-Origin-Embedd'er-Policy"] = "require-corp";
|
headers["Cross-Origin-Embedder-Policy"] = "require-corp";
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response(
|
return new Response(errorTemplate(String(err), fetchedURL), {
|
||||||
errorTemplate(
|
|
||||||
String(err),
|
|
||||||
fetchedURL
|
|
||||||
),
|
|
||||||
{
|
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: headers
|
headers: headers,
|
||||||
}
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,29 +1,39 @@
|
||||||
<!DOCTYPE html>
|
<!doctype html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>Document</title>
|
<title>Document</title>
|
||||||
<link rel="prefetch" href="/scram/scramjet.worker.js" type="module">
|
<link rel="prefetch" href="/scram/scramjet.worker.js" />
|
||||||
<link rel="prefetch" href="/scram/scramjet.shared.js" type="module">
|
<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&family=Inter+Tight:ital,wght@0,100..900;1,100..900&family=Inter:wght@100..900&display=swap&" rel="stylesheet">
|
<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&family=Inter+Tight:ital,wght@0,100..900;1,100..900&family=Inter:wght@100..900&display=swap&"
|
||||||
|
rel="stylesheet"
|
||||||
|
/>
|
||||||
<style>
|
<style>
|
||||||
body, html, #app {
|
body,
|
||||||
font-family: "Inter", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
|
html,
|
||||||
width:100vw;
|
#app {
|
||||||
height:100vh;
|
font-family:
|
||||||
|
"Inter",
|
||||||
|
system-ui,
|
||||||
|
-apple-system,
|
||||||
|
BlinkMacSystemFont,
|
||||||
|
sans-serif;
|
||||||
|
width: 100vw;
|
||||||
|
height: 100vh;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
background-color:#121212;
|
background-color: #121212;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script src="https://unpkg.com/dreamland"></script>
|
<script src="https://unpkg.com/dreamland"></script>
|
||||||
<script src="/baremux/index.js" defer></script>
|
<script src="/baremux/index.js" defer></script>
|
||||||
<script src="/scram/scramjet.codecs.js" type="module"></script>
|
<script src="/scram/scramjet.codecs.js" defer></script>
|
||||||
<script src="/scram/scramjet.config.js" type="module"></script>
|
<script src="/scram/scramjet.config.js" defer></script>
|
||||||
<script src="ui.js" defer></script>
|
<script src="ui.js" defer></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
11
static/sw.js
11
static/sw.js
|
@ -1,6 +1,9 @@
|
||||||
import ScramjetServiceWorker from "/scram/scramjet.worker.js";
|
importScripts(
|
||||||
import "/scram/scramjet.codecs.js";
|
"/scram/scramjet.codecs.js",
|
||||||
import "/scram/scramjet.config.js";
|
"/scram/scramjet.config.js",
|
||||||
|
"/scram/scramjet.shared.js",
|
||||||
|
"/scram/scramjet.worker.js"
|
||||||
|
);
|
||||||
|
|
||||||
const scramjet = new ScramjetServiceWorker();
|
const scramjet = new ScramjetServiceWorker();
|
||||||
|
|
||||||
|
@ -9,7 +12,7 @@ async function handleRequest(event) {
|
||||||
return scramjet.fetch(event);
|
return scramjet.fetch(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch(event.request)
|
return fetch(event.request);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.addEventListener("fetch", (event) => {
|
self.addEventListener("fetch", (event) => {
|
||||||
|
|
44
static/ui.js
44
static/ui.js
|
@ -1,16 +1,30 @@
|
||||||
navigator.serviceWorker.register("./sw.js", {
|
navigator.serviceWorker
|
||||||
scope: __scramjet$config.prefix,
|
.register("./sw.js", {
|
||||||
type: "module"
|
scope: $scramjet.config.prefix,
|
||||||
})
|
})
|
||||||
const connection = new BareMux.BareMuxConnection("/baremux/worker.js")
|
.then((reg) => {
|
||||||
const flex = css`display: flex;`;
|
reg.update();
|
||||||
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",
|
url: "https://google.com",
|
||||||
wispurl: "wss://wisp.mercurywork.shop/",
|
wispurl: "wss://wisp.mercurywork.shop/",
|
||||||
bareurl: (location.protocol === "https:" ? "https" : "http") + "://" + location.host + "/bare/",
|
bareurl:
|
||||||
}, { ident: "settings", backing: "localstorage", autosave: "auto" });
|
(location.protocol === "https:" ? "https" : "http") +
|
||||||
connection.setTransport("/baremod/index.mjs", [store.bareurl])
|
"://" +
|
||||||
|
location.host +
|
||||||
|
"/bare/",
|
||||||
|
},
|
||||||
|
{ ident: "settings", backing: "localstorage", autosave: "auto" }
|
||||||
|
);
|
||||||
|
connection.setTransport("/baremod/index.mjs", [store.bareurl]);
|
||||||
function App() {
|
function App() {
|
||||||
this.urlencoded = "";
|
this.urlencoded = "";
|
||||||
this.css = `
|
this.css = `
|
||||||
|
@ -89,12 +103,12 @@ function App() {
|
||||||
<button on:click=${() => window.open(this.urlencoded)}>open in fullscreen</button>
|
<button on:click=${() => window.open(this.urlencoded)}>open in fullscreen</button>
|
||||||
</div>
|
</div>
|
||||||
</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>
|
<iframe src=${use(this.urlencoded)}></iframe>
|
||||||
</div>
|
</div>
|
||||||
`
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener("load", () => {
|
window.addEventListener("load", () => {
|
||||||
document.body.appendChild(h(App))
|
document.body.appendChild(h(App));
|
||||||
})
|
});
|
||||||
|
|
7
tests/location.html
Normal file
7
tests/location.html
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<head></head>
|
||||||
|
<script>
|
||||||
|
function f() {
|
||||||
|
location = "http://www.google.com";
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<button onclick="f()">Google</button>
|
|
@ -4,7 +4,7 @@
|
||||||
"rootDir": "./src",
|
"rootDir": "./src",
|
||||||
"target": "ES2022",
|
"target": "ES2022",
|
||||||
"moduleResolution": "Bundler",
|
"moduleResolution": "Bundler",
|
||||||
"module": "ES2022",
|
"module": "ES2022"
|
||||||
},
|
},
|
||||||
"include": ["src"],
|
"include": ["src"]
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue