This commit is contained in:
velzie 2024-10-24 15:12:02 -04:00
commit 7bea0c60af
No known key found for this signature in database
GPG key ID: AA51AEFB0A1F3820
20 changed files with 359 additions and 248 deletions

View file

@ -9,7 +9,7 @@
<a href="https://www.npmjs.com/package/@mercuryworkshop/scramjet"><img src="https://img.shields.io/npm/v/@mercuryworkshop/scramjet.svg?maxAge=3600" alt="npm version" /></a>
Scramjet is an experimental inteception based web proxy that aims to be the successor to Ultraviolet. It is designed with security, developer friendlyness, and performance in mind. Scramjet strives to have a clean, organized codebase to improve maintainability. Scramjet is made to evade internet censorship and bypass arbitrary web browser restrictions.
Scramjet is an experimental interception based web proxy that aims to be the successor to Ultraviolet. It is designed with security, developer friendliness, and performance in mind. Scramjet strives to have a clean, organized codebase to improve maintainability. Scramjet is made to evade internet censorship and bypass arbitrary web browser restrictions.
## Supported Sites
@ -35,7 +35,7 @@ Some of the popular websites that Scramjet supports include:
#### Building
- Clone the repository with `git clone --recursive https://github.com/MercuryWorkshop/scramjet`
- Then, install your dependencies with `pnpm i`
- Then, install the dependencies with `pnpm i`
- After, build the rewriter with `pnpm rewriter:build`
- Finally, build Scramjet with `pnpm build`

13
codespace-basic-setup.sh Normal file
View file

@ -0,0 +1,13 @@
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env" # to import rustup in current shell
pnpm i
cargo install wasm-bindgen-cli
VER=$(curl --silent -qI https://github.com/WebAssembly/binaryen/releases/latest | awk -F '/' '/^location/ {print substr($NF, 1, length($NF)-1)}'); \
curl -LO https://github.com/WebAssembly/binaryen/releases/download/$VER/binaryen-${VER}-x86_64-linux.tar.gz
tar xvf binaryen-${VER}-x86_64-linux.tar.gz
rm -rf binaryen-${VER}-x86_64-linux.tar.gz
mv binaryen-${VER}/bin/* ~/.local/bin
mv binaryen-${VER}/lib/* ~/.local/lib
rm -rf binaryen-${VER}
pnpm rewriter:build
pnpm build

View file

@ -40,7 +40,7 @@
"@rspack/core": "^1.0.13",
"@types/eslint": "^9.6.1",
"@types/estree": "^1.0.6",
"@types/node": "^22.7.7",
"@types/node": "^22.7.8",
"@types/serviceworker": "^0.0.101",
"@typescript-eslint/eslint-plugin": "^8.11.0",
"@typescript-eslint/parser": "^8.11.0",

View file

@ -7,7 +7,7 @@ export default defineConfig({
testDir: "./tests",
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: 0,
retries: 2,
reporter: "html",
timeout: 20000,
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */

44
pnpm-lock.yaml generated
View file

@ -67,8 +67,8 @@ importers:
specifier: ^1.0.6
version: 1.0.6
'@types/node':
specifier: ^22.7.7
version: 22.7.7
specifier: ^22.7.8
version: 22.7.8
'@types/serviceworker':
specifier: ^0.0.101
version: 0.0.101
@ -451,8 +451,8 @@ packages:
'@types/node-forge@1.3.11':
resolution: {integrity: sha512-FQx220y22OKNTqaByeBGqHWYz4cl94tpcxeFdvBo3wjG6XPBuZ0BNgNZRV5J5TFmmcsJ4IzsLkmGRiQbnYsBEQ==}
'@types/node@22.7.7':
resolution: {integrity: sha512-SRxCrrg9CL/y54aiMCG3edPKdprgMVGDXjA3gB8UmmBW5TcXzRUYAh8EWzTnSJFAd1rgImPELza+A3bJ+qxz8Q==}
'@types/node@22.7.8':
resolution: {integrity: sha512-a922jJy31vqR5sk+kAdIENJjHblqcZ4RmERviFsER4WJcEONqxKcjNOlk0q7OUfrF5sddT+vng070cdfMlrPLg==}
'@types/qs@6.9.16':
resolution: {integrity: sha512-7i+zxXdPD0T4cKDuxCUXJ4wHcsJLwENa6Z3dCu8cfCK743OGy5Nu1RmAGqDPsoTDINVEcdXKRvR/zre+P2Ku1A==}
@ -2816,26 +2816,26 @@ snapshots:
'@types/body-parser@1.19.5':
dependencies:
'@types/connect': 3.4.38
'@types/node': 22.7.7
'@types/node': 22.7.8
'@types/bonjour@3.5.13':
dependencies:
'@types/node': 22.7.7
'@types/node': 22.7.8
'@types/connect-history-api-fallback@1.5.4':
dependencies:
'@types/express-serve-static-core': 5.0.0
'@types/node': 22.7.7
'@types/node': 22.7.8
'@types/connect@3.4.38':
dependencies:
'@types/node': 22.7.7
'@types/node': 22.7.8
'@types/cookie@0.4.1': {}
'@types/cors@2.8.17':
dependencies:
'@types/node': 22.7.7
'@types/node': 22.7.8
'@types/eslint@9.6.1':
dependencies:
@ -2848,14 +2848,14 @@ snapshots:
'@types/express-serve-static-core@4.19.6':
dependencies:
'@types/node': 22.7.7
'@types/node': 22.7.8
'@types/qs': 6.9.16
'@types/range-parser': 1.2.7
'@types/send': 0.17.4
'@types/express-serve-static-core@5.0.0':
dependencies:
'@types/node': 22.7.7
'@types/node': 22.7.8
'@types/qs': 6.9.16
'@types/range-parser': 1.2.7
'@types/send': 0.17.4
@ -2870,27 +2870,27 @@ snapshots:
'@types/fs-extra@11.0.4':
dependencies:
'@types/jsonfile': 6.1.4
'@types/node': 22.7.7
'@types/node': 22.7.8
'@types/http-errors@2.0.4': {}
'@types/http-proxy@1.17.15':
dependencies:
'@types/node': 22.7.7
'@types/node': 22.7.8
'@types/json-schema@7.0.15': {}
'@types/jsonfile@6.1.4':
dependencies:
'@types/node': 22.7.7
'@types/node': 22.7.8
'@types/mime@1.3.5': {}
'@types/node-forge@1.3.11':
dependencies:
'@types/node': 22.7.7
'@types/node': 22.7.8
'@types/node@22.7.7':
'@types/node@22.7.8':
dependencies:
undici-types: 6.19.8
@ -2905,7 +2905,7 @@ snapshots:
'@types/send@0.17.4':
dependencies:
'@types/mime': 1.3.5
'@types/node': 22.7.7
'@types/node': 22.7.8
'@types/serve-index@1.9.4':
dependencies:
@ -2914,14 +2914,14 @@ snapshots:
'@types/serve-static@1.15.7':
dependencies:
'@types/http-errors': 2.0.4
'@types/node': 22.7.7
'@types/node': 22.7.8
'@types/send': 0.17.4
'@types/serviceworker@0.0.101': {}
'@types/sockjs@0.3.36':
dependencies:
'@types/node': 22.7.7
'@types/node': 22.7.8
'@types/tapable@2.2.7':
dependencies:
@ -2929,7 +2929,7 @@ snapshots:
'@types/ws@8.5.12':
dependencies:
'@types/node': 22.7.7
'@types/node': 22.7.8
'@typescript-eslint/eslint-plugin@8.11.0(@typescript-eslint/parser@8.11.0(eslint@9.13.0)(typescript@5.6.3))(eslint@9.13.0)(typescript@5.6.3)':
dependencies:
@ -3480,7 +3480,7 @@ snapshots:
dependencies:
'@types/cookie': 0.4.1
'@types/cors': 2.8.17
'@types/node': 22.7.7
'@types/node': 22.7.8
accepts: 1.3.8
base64id: 2.0.0
cookie: 0.4.2
@ -4022,7 +4022,7 @@ snapshots:
jest-worker@27.5.1:
dependencies:
'@types/node': 22.7.7
'@types/node': 22.7.8
merge-stream: 2.0.0
supports-color: 8.1.1

220
rewriter/Cargo.lock generated
View file

@ -237,13 +237,14 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "compact_str"
version = "0.7.1"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f86b9c4c00838774a6d902ef931eff7470720c51d90c2e32cfe15dc304737b3f"
checksum = "6050c3a16ddab2e412160b31f2c871015704239bca62f72f6e5f0be631d3f644"
dependencies = [
"castaway",
"cfg-if",
"itoa",
"rustversion",
"ryu",
"serde",
"static_assertions",
@ -259,6 +260,12 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "cow-utils"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "417bef24afe1460300965a25ff4a24b8b45ad011948302ec221e8a0a81eb2c79"
[[package]]
name = "crossbeam-utils"
version = "0.8.20"
@ -352,19 +359,6 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "gloo-utils"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "037fcb07216cb3a30f7292bd0176b050b7b9a052ba830ef7d5d65f6dc64ba58e"
dependencies = [
"js-sys",
"serde",
"serde_json",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "hashbrown"
version = "0.14.5"
@ -519,6 +513,18 @@ dependencies = [
"hashbrown 0.15.0",
]
[[package]]
name = "instant"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222"
dependencies = [
"cfg-if",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "intrusive-collections"
version = "0.9.7"
@ -554,9 +560,9 @@ dependencies = [
[[package]]
name = "libc"
version = "0.2.159"
version = "0.2.161"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5"
checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1"
[[package]]
name = "litemap"
@ -606,7 +612,7 @@ dependencies = [
"owo-colors",
"textwrap",
"thiserror",
"unicode-width",
"unicode-width 0.1.14",
]
[[package]]
@ -620,6 +626,12 @@ dependencies = [
"syn",
]
[[package]]
name = "nonmax"
version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "610a5acd306ec67f907abe5567859a3c693fb9886eb1f012ab8f2a47bef3db51"
[[package]]
name = "num-bigint"
version = "0.4.6"
@ -705,9 +717,9 @@ checksum = "fb37767f6569cd834a413442455e0f066d0d522de8630436e2a1761d9726ba56"
[[package]]
name = "oxc_allocator"
version = "0.20.0"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d413f72d3253e9adff573635cfb5aca6f092aaee8b4252ee2d02b36fccc8fd2c"
checksum = "40baa80a3e4ae7cea94c1e04dc8820162b934f5f5cbca8a96e80a0b85a2815f4"
dependencies = [
"allocator-api2",
"bumpalo",
@ -716,63 +728,92 @@ dependencies = [
[[package]]
name = "oxc_ast"
version = "0.20.0"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c90e5316ea1cdc09844cacbd9d11dfdaf80894c84d8917a8021903238dfcb449"
checksum = "d7eb50a1c3485609b4b2051bd23352c1e6a8743b9e345dd6def70b3470948429"
dependencies = [
"bitflags",
"num-bigint",
"oxc_allocator",
"oxc_ast_macros",
"oxc_estree",
"oxc_regular_expression",
"oxc_span",
"oxc_syntax",
"serde",
"serde_json",
"tsify",
"wasm-bindgen",
]
[[package]]
name = "oxc_ast_macros"
version = "0.20.0"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e75a62baf313dede70692fe7b0dbb84bfdb8cddb118020067b924ee759b08a4e"
[[package]]
name = "oxc_diagnostics"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2963694eba6acab881fe014b843757b57f8bdb755f6014fb7f92790122bd57d"
checksum = "0a4b29106d9f2bbcca4f5ee7a9325c99844ff9721d10103625cd5278795507fe"
dependencies = [
"miette",
"owo-colors",
"textwrap",
"unicode-width",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "oxc_index"
version = "0.20.0"
name = "oxc_diagnostics"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbd279d0188e75bb7268c3d8f54a2b30837ea03135b739b90fb8360dd9f2add8"
checksum = "f2e7c7ff14a1a80cb229593d9fcfae6660c7b2cda8707caa4a89cb489656d156"
dependencies = [
"miette",
"owo-colors",
"rustc-hash",
"textwrap",
"unicode-width 0.2.0",
]
[[package]]
name = "oxc_ecmascript"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17d5cf1710748afcda1269998d2003af79df1fef7c5b4183054aeb1b2a803ff6"
dependencies = [
"num-bigint",
"num-traits",
"oxc_ast",
"oxc_span",
"oxc_syntax",
]
[[package]]
name = "oxc_estree"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "befa5f762524cdd768fee0321c4f19ee24e13e7340ca2a64629183a37875ba71"
[[package]]
name = "oxc_index"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68a576b0f0a1ef9112504880a3ba82fa895c4cceb52cdc37825180c43b01336f"
dependencies = [
"serde",
]
[[package]]
name = "oxc_parser"
version = "0.20.0"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7683a18fa64b8d7756b8a5630e84f075b4cd1dcf85adb04044055c33120e5f9"
checksum = "f43e722cd698a1ac383b645ddf6283757d4977b56f6dc278038b9ea74867a508"
dependencies = [
"assert-unchecked",
"bitflags",
"cow-utils",
"memchr",
"num-bigint",
"num-traits",
"oxc_allocator",
"oxc_ast",
"oxc_diagnostics",
"oxc_ecmascript",
"oxc_regular_expression",
"oxc_span",
"oxc_syntax",
"rustc-hash",
@ -780,34 +821,57 @@ dependencies = [
]
[[package]]
name = "oxc_span"
version = "0.20.0"
name = "oxc_regular_expression"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efafab0c8cfb6ac6da1b687fef84424d242437f4cfbed51ddfba8ac046ca4a93"
checksum = "231ed7fd6173c41270e019c4d1b2cd4bfa463a8abba32e0107bed6a7e9ceca93"
dependencies = [
"oxc_allocator",
"oxc_ast_macros",
"oxc_diagnostics",
"oxc_estree",
"oxc_span",
"phf",
"rustc-hash",
"serde",
"unicode-id-start",
"wasm-bindgen",
]
[[package]]
name = "oxc_span"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "734602328c49aa8c31840e72662196ca1b0d7bf3585eca1f9ddbef127172112f"
dependencies = [
"compact_str",
"miette",
"oxc_allocator",
"oxc_ast_macros",
"oxc_estree",
"serde",
"tsify",
"wasm-bindgen",
]
[[package]]
name = "oxc_syntax"
version = "0.20.0"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ff2ce5c9adcdaee8dc28834ff7a4d9e33b0ecd9c9ec56a5066703cdd3426bb3"
checksum = "6a56d62a59f4ace25365c1d056d7ce3129ca3fb20e9337b19a0efe62d6bc84cb"
dependencies = [
"assert-unchecked",
"bitflags",
"dashmap 6.1.0",
"nonmax",
"oxc_allocator",
"oxc_ast_macros",
"oxc_estree",
"oxc_index",
"oxc_span",
"phf",
"rustc-hash",
"ryu-js",
"serde",
"tsify",
"unicode-id-start",
"wasm-bindgen",
]
@ -917,9 +981,9 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.87"
version = "1.0.88"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3e4daa0dcf6feba26f985457cdf104d4b4256fc5a09547140f3631bb076b19a"
checksum = "7c3a7fc5db1e57d5a779a352c8cdb57b29aa4c40cc69c3a68a7fedc815fbf2f9"
dependencies = [
"unicode-ident",
]
@ -989,10 +1053,12 @@ dependencies = [
"boa_engine",
"console_error_panic_hook",
"getrandom",
"instant",
"js-sys",
"obfstr",
"oxc_allocator",
"oxc_ast",
"oxc_diagnostics",
"oxc_parser",
"oxc_span",
"oxc_syntax",
@ -1072,22 +1138,11 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_derive_internals"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e578a843d40b4189a4d66bba51d7684f57da5bd7c304c64e14bd63efbef49509"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.128"
version = "1.0.132"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8"
checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03"
dependencies = [
"itoa",
"memchr",
@ -1133,9 +1188,9 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "syn"
version = "2.0.79"
version = "2.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590"
checksum = "83540f837a8afc019423a8edb95b52a8effe46957ee402287f4292fae35be021"
dependencies = [
"proc-macro2",
"quote",
@ -1167,7 +1222,7 @@ checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9"
dependencies = [
"smawk",
"unicode-linebreak",
"unicode-width",
"unicode-width 0.1.14",
]
[[package]]
@ -1272,31 +1327,6 @@ dependencies = [
"winnow",
]
[[package]]
name = "tsify"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6b26cf145f2f3b9ff84e182c448eaf05468e247f148cf3d2a7d67d78ff023a0"
dependencies = [
"gloo-utils",
"serde",
"serde_json",
"tsify-macros",
"wasm-bindgen",
]
[[package]]
name = "tsify-macros"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a94b0f0954b3e59bfc2c246b4c8574390d94a4ad4ad246aaf2fb07d7dfd3b47"
dependencies = [
"proc-macro2",
"quote",
"serde_derive_internals",
"syn",
]
[[package]]
name = "unicode-bidi"
version = "0.3.17"
@ -1305,9 +1335,9 @@ checksum = "5ab17db44d7388991a428b2ee655ce0c212e862eff1768a455c58f9aad6e7893"
[[package]]
name = "unicode-id-start"
version = "1.3.0"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97e2a3c5fc9de285c0e805d98eba666adb4b2d9e1049ce44821ff7707cc34e91"
checksum = "2f322b60f6b9736017344fa0635d64be2f458fbc04eef65f6be22976dd1ffd5b"
[[package]]
name = "unicode-ident"
@ -1336,6 +1366,12 @@ version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
[[package]]
name = "unicode-width"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd"
[[package]]
name = "url"
version = "2.5.2"

View file

@ -25,13 +25,15 @@ panic = "abort"
[dependencies]
console_error_panic_hook = "0.1.7"
getrandom = { version = "0.2.15", features = ["js"] }
instant = { version = "0.1.13", features = ["wasm-bindgen"] }
js-sys = "0.3.69"
obfstr = "0.4.3"
oxc_allocator = "0.20.0"
oxc_ast = { version = "0.20.0", features = ["serialize"]}
oxc_parser = { version = "0.20.0" }
oxc_span = "0.20.0"
oxc_syntax = "0.20.0"
oxc_allocator = "0.32.0"
oxc_ast = { version = "0.32.0", features = ["serialize"]}
oxc_diagnostics = "0.32.0"
oxc_parser = "0.32.0"
oxc_span = "0.32.0"
oxc_syntax = "0.32.0"
rand = "0.8.5"
serde = "1.0.204"
serde-wasm-bindgen = "0.6.5"

View file

@ -9,6 +9,8 @@ pub enum RewriterError {
Url(#[from] url::ParseError),
#[error("str fromutf8 error: {0}")]
Str(#[from] std::str::Utf8Error),
#[error("reflect set failed: {0}")]
ReflectSetFail(String),
#[error("{0} was not {1}")]
Not(String, &'static str),

View file

@ -1,14 +1,27 @@
pub mod error;
pub mod rewrite;
use std::{panic, str::FromStr};
use std::{panic, str::FromStr, sync::Arc, time::Duration};
use error::{Result, RewriterError};
use instant::Instant;
use js_sys::{Function, Object, Reflect};
use oxc_diagnostics::{NamedSource, OxcDiagnostic};
use rewrite::{rewrite, Config, EncodeFn};
use url::Url;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(typescript_custom_section)]
const REWRITER_OUTPUT: &'static str = r#"
type RewriterOutput = { js: Uint8Array, errors: string[], duration: bigint };
"#;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "RewriterOutput")]
pub type RewriterOutput;
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
@ -43,6 +56,14 @@ fn get_str(obj: &JsValue, k: &str) -> Result<String> {
.ok_or_else(|| RewriterError::not_str(k, obj))
}
fn set_obj(obj: &Object, k: &str, v: &JsValue) -> Result<()> {
if !Reflect::set(&obj.into(), &k.into(), v)? {
Err(RewriterError::ReflectSetFail(k.to_string()))
} else {
Ok(())
}
}
fn get_flag(scramjet: &Object, url: &str, flag: &str) -> Result<bool> {
let fenabled = get_obj(scramjet, "flagEnabled")?
.dyn_into::<Function>()
@ -90,25 +111,68 @@ fn drmcheck() -> bool {
return vec![obfstr!("http://localhost:1337")].contains(&true_origin.as_str());
}
fn duration_to_millis_f64(duration: Duration) -> f64 {
(duration.as_secs() as f64) * 1_000f64 + (duration.subsec_nanos() as f64) / 1_000_000f64
}
fn create_rewriter_output(
out: (Vec<u8>, Vec<OxcDiagnostic>),
url: String,
src: String,
duration: Duration,
) -> Result<RewriterOutput> {
let src = Arc::new(NamedSource::new(url, src).with_language("javascript"));
let errs: Vec<_> = out
.1
.into_iter()
.map(|x| format!("{:?}", x.with_source_code(src.clone())))
.collect();
let obj = Object::new();
set_obj(&obj, "js", &out.0.into())?;
set_obj(&obj, "errors", &errs.into())?;
set_obj(&obj, "duration", &duration_to_millis_f64(duration).into())?;
Ok(RewriterOutput::from(JsValue::from(obj)))
}
#[wasm_bindgen]
pub fn rewrite_js(js: &str, url: &str, scramjet: &Object) -> Result<Vec<u8>> {
pub fn rewrite_js(
js: String,
url: &str,
script_url: String,
scramjet: &Object,
) -> Result<RewriterOutput> {
#[cfg(feature = "drm")]
if !drmcheck() {
return Vec::new();
}
rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)
let before = Instant::now();
let out = rewrite(&js, Url::from_str(url)?, get_config(scramjet, url)?)?;
let after = Instant::now();
create_rewriter_output(out, script_url, js, after - before)
}
#[wasm_bindgen]
pub fn rewrite_js_from_arraybuffer(js: &[u8], url: &str, scramjet: &Object) -> Result<Vec<u8>> {
pub fn rewrite_js_from_arraybuffer(
js: Vec<u8>,
url: &str,
script_url: String,
scramjet: &Object,
) -> Result<RewriterOutput> {
#[cfg(feature = "drm")]
if !drmcheck() {
return Vec::new();
}
// we know that this is a valid utf-8 string
let js = unsafe { std::str::from_utf8_unchecked(js) };
let js = unsafe { String::from_utf8_unchecked(js) };
rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)
let before = Instant::now();
let out = rewrite(&js, Url::from_str(url)?, get_config(scramjet, url)?)?;
let after = Instant::now();
create_rewriter_output(out, script_url, js, after - before)
}

View file

@ -130,6 +130,7 @@ fn dorewrite(source_text: &str) -> Result<String> {
strict_rewrites: true,
},
)?
.0
.as_slice(),
)?
.to_string())

View file

@ -9,8 +9,9 @@ use oxc_ast::{
visit::walk,
Visit,
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_parser::Parser;
use oxc_span::{Atom, SourceType, Span};
use oxc_span::{Atom, GetSpan, SourceType, Span};
use oxc_syntax::operator::{AssignmentOperator, UnaryOperator};
use url::Url;
@ -148,7 +149,7 @@ impl<'a> Visit<'a> for Rewriter {
&& !matches!(s.object, Expression::MetaProperty(_))
&& !matches!(s.object, Expression::Super(_))
{
let span = expression_span(&s.object);
let span = s.object.span();
self.jschanges.push(JsChange::GenericChange {
span: Span::new(span.start, span.start),
text: " $scramitize(".to_string(),
@ -359,7 +360,7 @@ impl<'a> Visit<'a> for Rewriter {
self.jschanges.push(JsChange::Assignment {
name: s.name.to_string(),
entirespan: it.span,
rhsspan: expression_span(&it.right),
rhsspan: it.right.span(),
op: it.operator,
});
@ -383,55 +384,6 @@ impl<'a> Visit<'a> for Rewriter {
}
}
fn expression_span(e: &Expression) -> Span {
// enums.split("\n").filter(f=>f).map(p=>p.trimLeft()).filter(p=>!p.startsWith("#")).map(p=>p.replace(/\(.*/,"")).map(p=>`E::${p}(s) => s.span`).join(",\n")
use Expression as E;
match e {
E::BooleanLiteral(s) => s.span,
E::NullLiteral(s) => s.span,
E::NumericLiteral(s) => s.span,
E::BigIntLiteral(s) => s.span,
E::RegExpLiteral(s) => s.span,
E::StringLiteral(s) => s.span,
E::TemplateLiteral(s) => s.span,
E::Identifier(s) => s.span,
E::MetaProperty(s) => s.span,
E::Super(s) => s.span,
E::ArrayExpression(s) => s.span,
E::ArrowFunctionExpression(s) => s.span,
E::AssignmentExpression(s) => s.span,
E::AwaitExpression(s) => s.span,
E::BinaryExpression(s) => s.span,
E::CallExpression(s) => s.span,
E::ChainExpression(s) => s.span,
E::ClassExpression(s) => s.span,
E::ConditionalExpression(s) => s.span,
E::FunctionExpression(s) => s.span,
E::ImportExpression(s) => s.span,
E::LogicalExpression(s) => s.span,
E::NewExpression(s) => s.span,
E::ObjectExpression(s) => s.span,
E::ParenthesizedExpression(s) => s.span,
E::SequenceExpression(s) => s.span,
E::TaggedTemplateExpression(s) => s.span,
E::ThisExpression(s) => s.span,
E::UnaryExpression(s) => s.span,
E::UpdateExpression(s) => s.span,
E::YieldExpression(s) => s.span,
E::PrivateInExpression(s) => s.span,
E::JSXElement(s) => s.span,
E::JSXFragment(s) => s.span,
E::TSAsExpression(s) => s.span,
E::TSSatisfiesExpression(s) => s.span,
E::TSTypeAssertion(s) => s.span,
E::TSNonNullExpression(s) => s.span,
E::TSInstantiationExpression(s) => s.span,
E::ComputedMemberExpression(s) => s.span,
E::StaticMemberExpression(s) => s.span,
E::PrivateFieldExpression(s) => s.span,
}
}
// js MUST not be able to get a reference to any of these because sbx
const UNSAFE_GLOBALS: &[&str] = &[
"window",
@ -459,19 +411,11 @@ fn random_string() -> String {
.to_string()
}
pub fn rewrite(js: &str, url: Url, config: Config) -> Result<Vec<u8>> {
pub fn rewrite(js: &str, url: Url, config: Config) -> Result<(Vec<u8>, Vec<OxcDiagnostic>)> {
let allocator = Allocator::default();
let source_type = SourceType::default();
let ret = Parser::new(&allocator, js, source_type).parse();
for err in ret.errors {
let cloned = js.to_string();
let err = err.with_source_code(cloned);
println!("oxc parse error {err:?}");
#[cfg(target_family = "wasm")]
crate::error(&format!("oxc parse error {err:?}"))
}
let program = ret.program;
let sourcetag = random_string();
@ -575,9 +519,9 @@ pub fn rewrite(js: &str, url: Url, config: Config) -> Result<Vec<u8>> {
format!(
"((t)=>$scramjet$tryset({},\"{}\",t)||({}{}t))({})",
name,
fmt_op(*op),
op.as_str(),
name,
fmt_op(*op),
op.as_str(),
&js[rhsspan.start as usize..rhsspan.end as usize]
)
.as_bytes(),
@ -607,10 +551,10 @@ pub fn rewrite(js: &str, url: Url, config: Config) -> Result<Vec<u8>> {
sourcemap.extend_from_slice(&buffer);
return Ok(sourcemap);
return Ok((sourcemap, ret.errors));
}
Ok(buffer)
Ok((buffer, ret.errors))
}
fn json_escape_string(s: &str) -> String {
@ -629,24 +573,3 @@ fn json_escape_string(s: &str) -> String {
}
out
}
fn fmt_op(op: AssignmentOperator) -> &'static str {
match op {
AssignmentOperator::Assign => "=",
AssignmentOperator::Addition => "+=",
AssignmentOperator::Subtraction => "-=",
AssignmentOperator::Multiplication => "*=",
AssignmentOperator::Division => "/=",
AssignmentOperator::Remainder => "%=",
AssignmentOperator::Exponential => "**=",
AssignmentOperator::ShiftLeft => "<<=",
AssignmentOperator::ShiftRight => ">>=",
AssignmentOperator::ShiftRightZeroFill => ">>>=",
AssignmentOperator::BitwiseAnd => "&=",
AssignmentOperator::BitwiseXOR => "^=",
AssignmentOperator::BitwiseOR => "|=",
AssignmentOperator::LogicalAnd => "&&=",
AssignmentOperator::LogicalOr => "||=",
AssignmentOperator::LogicalNullish => "??=",
}
}

View file

@ -13,7 +13,7 @@ import {
unrewriteUrl,
rewriteUrl,
} from "../shared";
import { BareClient as BareClientType } from "@mercuryworkshop/bare-mux";
import type { BareClient as BareClientType } from "@mercuryworkshop/bare-mux";
import { createWrapFn } from "./shared/wrap";
import { NavigateEvent } from "./events";
import type { URLMeta } from "../shared/rewriters/url";
@ -328,7 +328,7 @@ export class ScramjetClient {
if ((err.stack as any) instanceof Object) {
//@ts-expect-error i'm not going to explain this
err.stack = err.stack.stack;
console.error("ERROR FROM SCRMAJET INTERNALS", err);
console.error("ERROR FROM SCRAMJET INTERNALS", err);
} else {
throw err;
}

View file

@ -3,7 +3,7 @@ import { rewriteUrl } from "../../shared";
import { UrlChangeEvent } from "../events";
export default function (client: ScramjetClient, self: typeof globalThis) {
client.Proxy("history.pushState", {
client.Proxy("History.prototype.pushState", {
apply(ctx) {
if (ctx.args[2]) ctx.args[2] = rewriteUrl(ctx.args[2], client.meta);
ctx.call();
@ -13,7 +13,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
},
});
client.Proxy("history.replaceState", {
client.Proxy("History.prototype.replaceState", {
apply(ctx) {
if (ctx.args[2]) ctx.args[2] = rewriteUrl(ctx.args[2], client.meta);
ctx.call();

View file

@ -2,7 +2,7 @@ import { rewriteUrl } from "../../../shared";
import { ScramjetClient } from "../../client";
export default function (client: ScramjetClient, self) {
client.Proxy("navigator.sendBeacon", {
client.Proxy("Navigator.prototype.sendBeacon", {
apply(ctx) {
ctx.args[0] = rewriteUrl(ctx.args[0], client.meta);
},

View file

@ -16,12 +16,6 @@ export default function (client: ScramjetClient, _self: typeof globalThis) {
},
});
// client.Proxy("Headers", {
// construct(ctx) {
// ctx.args[0] = rewriteHeaders(ctx.args[0]);
// },
// });
client.Proxy("Request", {
construct(ctx) {
if (typeof ctx.args[0] === "string" || ctx.args[0] instanceof URL) {
@ -43,10 +37,4 @@ export default function (client: ScramjetClient, _self: typeof globalThis) {
return unrewriteUrl(ctx.get() as string);
},
});
// client.Proxy("Response.redirect", {
// apply(ctx) {
// ctx.args[0] = encodeUrl(ctx.args[0]);
// },
// });
}

View file

@ -9,7 +9,7 @@ export function createWrapFn(client: ScramjetClient, self: typeof globalThis) {
return function (identifier: any) {
if (identifier === self) return client.globalProxy;
if (identifier === self.location) return client.locationProxy;
if (identifier === eval) return indirectEval;
if (identifier === eval) return indirectEval.bind(client);
if (iswindow) {
if (identifier === self.parent) {
@ -22,9 +22,23 @@ export function createWrapFn(client: ScramjetClient, self: typeof globalThis) {
}
} else if (identifier === self.document) {
return client.documentProxy;
}
} else if (identifier === self.top) {
// instead of returning top, we need to return the uppermost parent that's inside a scramjet context
let current = self.self;
// TODO .top
for (;;) {
const test = current.parent.self;
if (test === current) break; // there is no parent, actual or emulated.
// ... then `test` represents a window outside of the proxy context, and therefore `current` is the topmost window in the proxy context
if (!(SCRAMJETCLIENT in test)) break;
// test is also insde a proxy, so we should continue up the chain
current = test;
}
return current[SCRAMJETCLIENT].globalProxy.window;
}
}
return identifier;
@ -44,6 +58,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
Object.defineProperty(self, config.globals.wrapthisfn, {
value: function (i) {
if (i === self) return client.globalProxy;
return i;
},
writable: false,

View file

@ -6,6 +6,7 @@ import {
initSync,
rewrite_js,
rewrite_js_from_arraybuffer,
RewriterOutput,
} from "../../../rewriter/out/rewriter.js";
import { $scramjet, flagEnabled } from "../../scramjet";
@ -19,6 +20,53 @@ init();
Error.stackTraceLimit = 50;
const decoder = new TextDecoder();
function rewriteJsWrapper(
input: string | ArrayBuffer,
meta: URLMeta
): string | ArrayBuffer {
let out: RewriterOutput;
if (typeof input === "string") {
out = rewrite_js(
input,
meta.base.href,
"PERCS_PLEASE_FILL_THIS_IN.js",
$scramjet
);
} else {
out = rewrite_js_from_arraybuffer(
new Uint8Array(input),
meta.base.href,
"PERCS_PLEASE_FILL_THIS_IN.js",
$scramjet
);
}
const { js, errors, duration } = out;
// TODO: maybe make this a scram flag?
if (true) {
for (const error of errors) {
console.error("oxc parse error", error);
}
}
// TODO: maybe make this a scram flag?
if (true) {
let timespan: string;
if (duration < 1n) {
timespan = "BLAZINGLY FAST";
} else if (duration < 500n) {
timespan = "decent speed";
} else {
timespan = "really slow";
}
console.log(`oxc rewrite was ${timespan} (${duration}ms)`);
}
return typeof input === "string" ? decoder.decode(js) : js;
}
export function rewriteJs(js: string | ArrayBuffer, meta: URLMeta) {
if (flagEnabled("naiiveRewriter", meta.origin)) {
const text = typeof js === "string" ? js : new TextDecoder().decode(js);
@ -28,19 +76,7 @@ export function rewriteJs(js: string | ArrayBuffer, meta: URLMeta) {
return rewriteJsNaiive(text);
}
// const before = performance.now();
if (typeof js === "string") {
js = new TextDecoder().decode(rewrite_js(js, meta.base.href, $scramjet));
} else {
js = rewrite_js_from_arraybuffer(
new Uint8Array(js),
meta.base.href,
$scramjet
);
}
// const after = performance.now();
// dbg.debug("Rewrite took", Math.floor((after - before) * 10) / 10, "ms");
js = rewriteJsWrapper(js, meta);
return js;
}

View file

@ -1,3 +1,11 @@
// dumb hack to allow firefox to work (please dont do this in prod)
if (navigator.userAgent.includes("Firefox")) {
Object.defineProperty(globalThis, "crossOriginIsolated", {
value: true,
writable: false,
});
}
importScripts(
"/scram/scramjet.wasm.js",
"/scram/scramjet.shared.js",

View file

@ -0,0 +1,17 @@
import { Page } from "@playwright/test";
export function registerInspect(page: Page) {
let hasOxcError = false;
let hasScramjetError = false;
page.on("console", async (msg) => {
if (msg.type() === "error") {
if (msg.text().includes("oxc parse error") && !hasOxcError) {
hasOxcError = true;
console.log("OXC parse error detected! Please review manually.")
} else if (msg.text().includes("ERROR FROM SCRAMJET INTERNALS") && !hasScramjetError) {
hasScramjetError = true;
console.log("Scramjet error detected! Please review manually.");
}
}
});
}

View file

@ -1,8 +1,11 @@
/* eslint-disable no-async-promise-executor */
import { expect, FrameLocator, Page } from "@playwright/test";
import { registerInspect } from "./inspectConsole";
export function setupPage(page: Page, url: string): Promise<FrameLocator> {
return new Promise(async (resolve) => {
// Hack to disable HTTP cache.
await page.route("**", route => route.continue());
// Goto base url defined in config.
await page.goto("/");
await page.waitForSelector(".version > b");
@ -18,6 +21,9 @@ export function setupPage(page: Page, url: string): Promise<FrameLocator> {
await page.waitForTimeout(1000);
await bar.press("Enter");
registerInspect(page);
resolve(frame);
});
}