mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-12 22:10:01 -04:00
rewriter stuff
This commit is contained in:
parent
3981e40deb
commit
0a538c9bd1
7 changed files with 33 additions and 99 deletions
35
rewriter/Cargo.lock
generated
35
rewriter/Cargo.lock
generated
|
@ -246,16 +246,6 @@ dependencies = [
|
|||
"static_assertions",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "console_error_panic_hook"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cow-utils"
|
||||
version = "0.1.3"
|
||||
|
@ -349,10 +339,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"js-sys",
|
||||
"libc",
|
||||
"wasi",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -693,12 +681,6 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "obfstr"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0d354e9a302760d07e025701d40534f17dd1fe4c4db955b4e3bd2907c63bdee"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.20.2"
|
||||
|
@ -1050,15 +1032,9 @@ name = "rewriter"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"boa_engine",
|
||||
"console_error_panic_hook",
|
||||
"getrandom",
|
||||
"instant",
|
||||
"js-sys",
|
||||
"obfstr",
|
||||
"oxc",
|
||||
"rand",
|
||||
"serde",
|
||||
"serde-wasm-bindgen",
|
||||
"thiserror",
|
||||
"url",
|
||||
"wasm-bindgen",
|
||||
|
@ -1110,17 +1086,6 @@ dependencies = [
|
|||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde-wasm-bindgen"
|
||||
version = "0.6.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8302e169f0eddcc139c70f139d19d6467353af16f9fce27e8c30158036a1e16b"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"serde",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.210"
|
||||
|
|
|
@ -9,7 +9,6 @@ crate-type = ["cdylib"]
|
|||
[features]
|
||||
default = ["debug"]
|
||||
debug = []
|
||||
drm = []
|
||||
|
||||
[profile.speed]
|
||||
inherits = "release"
|
||||
|
@ -23,15 +22,9 @@ codegen-units = 1
|
|||
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 = "0.34.0"
|
||||
rand = "0.8.5"
|
||||
serde = "1.0.204"
|
||||
serde-wasm-bindgen = "0.6.5"
|
||||
thiserror = "1.0.64"
|
||||
url = "2.5.2"
|
||||
wasm-bindgen = "0.2.92"
|
||||
|
|
|
@ -14,8 +14,16 @@ if [ "$(wasm-bindgen -V)" != "$WBG" ]; then
|
|||
exit 1
|
||||
fi
|
||||
|
||||
RUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+simd128 -Zlocation-detail=none' cargo build --lib --target wasm32-unknown-unknown -Z build-std=panic_abort,std -Z build-std-features=panic_immediate_abort --features "${FEATURES:-}" --release
|
||||
wasm-bindgen --weak-refs --target web --out-dir out/ target/wasm32-unknown-unknown/release/rewriter.wasm
|
||||
if ! [ "${RELEASE:-0}" = "1" ]; then
|
||||
: "${WASMOPTFLAGS:=-g}"
|
||||
: "${FEATURES:=debug}"
|
||||
else
|
||||
: "${WASMOPTFLAGS:=}"
|
||||
: "${FEATURES:=}"
|
||||
fi
|
||||
|
||||
RUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+simd128 -Zlocation-detail=none' cargo build --lib --target wasm32-unknown-unknown -Z build-std=panic_abort,std -Z build-std-features=panic_immediate_abort --no-default-features --features "$FEATURES" --release
|
||||
wasm-bindgen --target web --out-dir out/ target/wasm32-unknown-unknown/release/rewriter.wasm
|
||||
|
||||
sed -i 's/import.meta.url/""/g' out/rewriter.js
|
||||
|
||||
|
@ -23,13 +31,8 @@ cd ..
|
|||
|
||||
WASM=rewriter/out/rewriter_bg.wasm
|
||||
|
||||
if ! [ "${RELEASE:-0}" = "1" ]; then
|
||||
WASMOPTFLAGS="-g"
|
||||
else
|
||||
WASMOPTFLAGS=""
|
||||
fi
|
||||
|
||||
time wasm-opt $WASMOPTFLAGS -tnh -O4 --vacuum --dce --enable-threads --enable-bulk-memory --enable-simd "$WASM" -o rewriter/out/optimized.wasm
|
||||
# shellcheck disable=SC2086
|
||||
time wasm-opt $WASMOPTFLAGS --converge -tnh -O4 --vacuum --dce --enable-threads --enable-bulk-memory --enable-simd "$WASM" -o rewriter/out/optimized.wasm
|
||||
|
||||
mkdir dist/ || true
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
pub mod error;
|
||||
pub mod rewrite;
|
||||
|
||||
use std::{panic, str::FromStr, sync::Arc, time::Duration};
|
||||
use std::{str::FromStr, sync::Arc, time::Duration};
|
||||
|
||||
use error::{Result, RewriterError};
|
||||
use instant::Instant;
|
||||
|
@ -16,6 +16,18 @@ const REWRITER_OUTPUT: &'static str = r#"
|
|||
type RewriterOutput = { js: Uint8Array, errors: string[], duration: bigint };
|
||||
"#;
|
||||
|
||||
#[wasm_bindgen(inline_js = r#"
|
||||
// slightly modified https://github.com/ungap/random-uuid/blob/main/index.js
|
||||
export function scramtag() {
|
||||
return (""+1e10).replace(/[018]/g,
|
||||
c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
|
||||
);
|
||||
}
|
||||
"#)]
|
||||
extern "C" {
|
||||
pub fn scramtag() -> String;
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
#[wasm_bindgen(typescript_type = "RewriterOutput")]
|
||||
|
@ -28,11 +40,6 @@ extern "C" {
|
|||
fn error(s: &str);
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn init() {
|
||||
panic::set_hook(Box::new(console_error_panic_hook::hook));
|
||||
}
|
||||
|
||||
fn create_encode_function(encode: JsValue) -> Result<EncodeFn> {
|
||||
let encode = encode.dyn_into::<Function>()?;
|
||||
|
||||
|
@ -101,16 +108,6 @@ fn get_config(scramjet: &Object, url: &str) -> Result<Config> {
|
|||
})
|
||||
}
|
||||
|
||||
#[cfg(feature = "drm")]
|
||||
#[inline(always)]
|
||||
fn drmcheck() -> bool {
|
||||
use js_sys::global;
|
||||
use obfstr::obfstr;
|
||||
|
||||
let true_origin = get_str(&get_obj(&global(), obfstr!("location")), obfstr!("origin"));
|
||||
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
|
||||
}
|
||||
|
@ -122,6 +119,7 @@ fn create_rewriter_output(
|
|||
duration: Duration,
|
||||
) -> Result<RewriterOutput> {
|
||||
let src = Arc::new(NamedSource::new(url, src).with_language("javascript"));
|
||||
#[cfg(feature = "debug")]
|
||||
let errs: Vec<_> = out
|
||||
.1
|
||||
.into_iter()
|
||||
|
@ -130,7 +128,10 @@ fn create_rewriter_output(
|
|||
|
||||
let obj = Object::new();
|
||||
set_obj(&obj, "js", &out.0.into())?;
|
||||
#[cfg(feature = "debug")]
|
||||
set_obj(&obj, "errors", &errs.into())?;
|
||||
#[cfg(not(feature = "debug"))]
|
||||
set_obj(&obj, "errors", &js_sys::Array::new())?;
|
||||
set_obj(&obj, "duration", &duration_to_millis_f64(duration).into())?;
|
||||
|
||||
Ok(RewriterOutput::from(JsValue::from(obj)))
|
||||
|
@ -143,13 +144,8 @@ pub fn rewrite_js(
|
|||
script_url: String,
|
||||
scramjet: &Object,
|
||||
) -> Result<RewriterOutput> {
|
||||
#[cfg(feature = "drm")]
|
||||
if !drmcheck() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let before = Instant::now();
|
||||
let out = rewrite(&js, Url::from_str(url)?, get_config(scramjet, url)?)?;
|
||||
let out = rewrite(&js, Url::from_str(url)?, scramtag(), get_config(scramjet, url)?)?;
|
||||
let after = Instant::now();
|
||||
|
||||
create_rewriter_output(out, script_url, js, after - before)
|
||||
|
@ -162,16 +158,11 @@ pub fn rewrite_js_from_arraybuffer(
|
|||
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 { String::from_utf8_unchecked(js) };
|
||||
|
||||
let before = Instant::now();
|
||||
let out = rewrite(&js, Url::from_str(url)?, get_config(scramjet, url)?)?;
|
||||
let out = rewrite(&js, Url::from_str(url)?, scramtag(), get_config(scramjet, url)?)?;
|
||||
let after = Instant::now();
|
||||
|
||||
create_rewriter_output(out, script_url, js, after - before)
|
||||
|
|
|
@ -114,6 +114,7 @@ fn dorewrite(source_text: &str) -> Result<String> {
|
|||
rewrite(
|
||||
source_text,
|
||||
Url::from_str("https://google.com/glorngle/si.js").unwrap(),
|
||||
"glongle1".to_string(),
|
||||
Config {
|
||||
prefix: "/scrammedjet/".to_string(),
|
||||
encode: Box::new(encode_string),
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use core::str;
|
||||
use std::str::from_utf8;
|
||||
|
||||
use oxc::{
|
||||
allocator::Allocator,
|
||||
|
@ -405,20 +404,7 @@ const UNSAFE_GLOBALS: &[&str] = &[
|
|||
"frames",
|
||||
];
|
||||
|
||||
fn random_string() -> String {
|
||||
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
||||
|
||||
from_utf8(
|
||||
&thread_rng()
|
||||
.sample_iter(&Alphanumeric)
|
||||
.take(10)
|
||||
.collect::<Vec<u8>>(),
|
||||
)
|
||||
.unwrap()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
pub fn rewrite(js: &str, url: Url, config: Config) -> Result<(Vec<u8>, Vec<OxcDiagnostic>)> {
|
||||
pub fn rewrite(js: &str, url: Url, sourcetag: String, config: Config) -> Result<(Vec<u8>, Vec<OxcDiagnostic>)> {
|
||||
let allocator = Allocator::default();
|
||||
let source_type = SourceType::default();
|
||||
let ret = Parser::new(&allocator, js, source_type)
|
||||
|
@ -431,8 +417,6 @@ pub fn rewrite(js: &str, url: Url, config: Config) -> Result<(Vec<u8>, Vec<OxcDi
|
|||
|
||||
let program = ret.program;
|
||||
|
||||
let sourcetag = random_string();
|
||||
|
||||
let mut ast_pass = Rewriter {
|
||||
jschanges: Vec::new(),
|
||||
base: url,
|
||||
|
|
|
@ -2,7 +2,6 @@ import { URLMeta } from "./url";
|
|||
|
||||
// i am a cat. i like to be petted. i like to be fed. i like to be
|
||||
import {
|
||||
init,
|
||||
initSync,
|
||||
rewrite_js,
|
||||
rewrite_js_from_arraybuffer,
|
||||
|
@ -16,8 +15,6 @@ initSync({
|
|||
),
|
||||
});
|
||||
|
||||
init();
|
||||
|
||||
Error.stackTraceLimit = 50;
|
||||
|
||||
const decoder = new TextDecoder();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue