@@ -2,26 +2,28 @@ import { MethodsDefinition, RpcHelper } from "@mercuryworkshop/rpc";
22import type * as ScramjetGlobal from "@mercuryworkshop/scramjet" ;
33
44declare const $scramjet : typeof ScramjetGlobal ;
5- const {
6- setWasm,
7- setConfig,
8- setInterface,
9- rewriteUrl,
10- ScramjetFetchHandler,
11- ScramjetHeaders,
12- CookieJar,
13- } = $scramjet ;
5+ // const {
6+ // setWasm,
7+ // setConfig,
8+ // rewriteUrl,
9+ // ScramjetFetchHandler,
10+ // ScramjetHeaders,
11+ // CookieJar,
12+ // } = $scramjet;
1413
1514import { Controllerbound , SWbound } from "./types" ;
1615import LibcurlClient from "@mercuryworkshop/libcurl-transport" ;
17- import { BareClient } from "@mercuryworkshop/bare-mux-custom" ;
16+ import {
17+ BareClient ,
18+ BareResponseFetch ,
19+ } from "@mercuryworkshop/bare-mux-custom" ;
1820
1921let lc = new LibcurlClient ( {
2022 wisp : "wss://anura.pro/" ,
2123} ) ;
2224const client = new BareClient ( lc ) ;
2325
24- const cookieJar = new CookieJar ( ) ;
26+ const cookieJar = new $scramjet . CookieJar ( ) ;
2527
2628type Config = {
2729 wasmPath : string ;
@@ -31,103 +33,20 @@ type Config = {
3133} ;
3234
3335fetch ( "/scramjet/scramjet.wasm.wasm" ) . then ( async ( resp ) => {
34- setWasm ( await resp . arrayBuffer ( ) ) ;
36+ $scramjet . setWasm ( await resp . arrayBuffer ( ) ) ;
3537} ) ;
3638
3739export const config : Config = {
3840 prefix : "/~/sj" ,
3941 virtualWasmPath : "/scramjet.wasm.js" ,
4042 scramjetPath : "/scramjet/scramjet.js" ,
43+ wasmPath : "/scramjet/scramjet.wasm.wasm" ,
4144} ;
4245
4346const cfg = {
44- prefix : "/scramjet/" ,
45- globals : {
46- wrapfn : "$scramjet$wrap" ,
47- wrappropertybase : "$scramjet__" ,
48- wrappropertyfn : "$scramjet$prop" ,
49- cleanrestfn : "$scramjet$clean" ,
50- importfn : "$scramjet$import" ,
51- rewritefn : "$scramjet$rewrite" ,
52- metafn : "$scramjet$meta" ,
53- wrappostmessagefn : "$scramjet$wrappostmessage" ,
54- pushsourcemapfn : "$scramjet$pushsourcemap" ,
55- trysetfn : "$scramjet$tryset" ,
56- templocid : "$scramjet$temploc" ,
57- tempunusedid : "$scramjet$tempunused" ,
58- } ,
59- flags : {
60- syncxhr : false ,
61- strictRewrites : true ,
62- rewriterLogs : false ,
63- captureErrors : true ,
64- cleanErrors : false ,
65- scramitize : false ,
66- sourcemaps : true ,
67- destructureRewrites : false ,
68- allowInvalidJs : false ,
69- allowFailedIntercepts : false ,
70- antiAntiDebugger : false ,
71- } ,
72- siteFlags : { } ,
73- codec : {
74- encode : `(url) => {
75- if (!url) return url;
76-
77- return encodeURIComponent(url);
78- }` ,
79- decode : `(url) => {
80- if (!url) return url;
81-
82- return decodeURIComponent(url);
83- }` ,
84- } ,
8547 maskedfiles : [ "inject.js" , "scramjet.wasm.js" ] ,
8648} ;
8749
88- setConfig ( cfg ) ;
89-
90- const getInjectScripts = ( meta , handler , cfg , cookiejar , script ) => {
91- return [
92- script ( config . scramjetPath ) ,
93- script (
94- meta . prefix . href . substring ( 0 , meta . prefix . href . length - 1 ) +
95- config . virtualWasmPath
96- ) ,
97- script (
98- "data:text/javascript;base64," +
99- btoa ( `
100- console.log("execute me twin");
101- $scramjet.setWasm(Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0)));
102- delete self.WASM;
103-
104- $scramjet.loadAndHook({
105- interface: {
106- getInjectScripts: ${ getInjectScripts . toString ( ) } ,
107- onClientbound: async (type, msg) => {
108- },
109- sendServerbound: async (type, msg) => {
110- },
111- },
112- config: ${ JSON . stringify ( cfg ) } ,
113- cookies: ${ cookiejar . dump ( ) } ,
114- transport: null,
115- })
116-
117- document.currentScript.remove();
118- ` )
119- ) ,
120- ] ;
121- } ;
122- setInterface ( {
123- getInjectScripts,
124- onClientbound ( ) { } ,
125- sendServerbound ( type , data ) { } ,
126- async fetchDataUrl ( dataUrl : string ) {
127- return await fetch ( dataUrl ) ;
128- } ,
129- } ) ;
130-
13150const frames : Record < string , Frame > = { } ;
13251
13352let wasmPayload : string | null = null ;
@@ -136,11 +55,23 @@ function makeId(): string {
13655 return Math . random ( ) . toString ( 36 ) . substring ( 2 , 10 ) ;
13756}
13857
58+ const codecEncode = ( url : string ) => {
59+ if ( ! url ) return url ;
60+
61+ return encodeURIComponent ( url ) ;
62+ } ;
63+
64+ const codecDecode = ( url : string ) => {
65+ if ( ! url ) return url ;
66+
67+ return decodeURIComponent ( url ) ;
68+ } ;
69+
13970export class Controller {
14071 id : string ;
14172 prefix : string ;
14273 frames : Frame [ ] = [ ] ;
143- cookieJar = new CookieJar ( ) ;
74+ cookieJar = new $scramjet . CookieJar ( ) ;
14475
14576 private rpc : RpcHelper < Controllerbound , SWbound > ;
14677 private ready : Promise < void > ;
@@ -156,7 +87,6 @@ export class Controller {
15687 const frame = this . frames . find ( ( f ) => path . startsWith ( f . prefix ) ) ;
15788 if ( ! frame ) throw new Error ( "No frame found for request" ) ;
15889
159- console . log ( path , frame . prefix + config . virtualWasmPath ) ;
16090 if (
16191 path . startsWith (
16292 frame . prefix . substring ( 0 , frame . prefix . length - 1 ) +
@@ -196,7 +126,7 @@ export class Controller {
196126 ] ;
197127 }
198128
199- let sjheaders = new ScramjetHeaders ( ) ;
129+ let sjheaders = new $scramjet . ScramjetHeaders ( ) ;
200130 for ( let [ k , v ] of Object . entries ( data . initialHeaders ) ) {
201131 for ( let vv of v ) {
202132 sjheaders . set ( k , vv ) ;
@@ -213,10 +143,8 @@ export class Controller {
213143 method : data . method ,
214144 mode : data . mode ,
215145 referrer : data . referrer ,
216- forceCrossOriginIsolated : data . forceCrossOriginIsolated ,
217146 body : data . body ,
218147 cache : data . cache ,
219- cookieStore : this . cookieJar ,
220148 } ) ;
221149
222150 return [
@@ -283,30 +211,106 @@ export class Controller {
283211 }
284212}
285213
214+ function yieldGetInjectScripts (
215+ cookieJar : ScramjetGlobal . CookieJar ,
216+ config : Config ,
217+ sjconfig : ScramjetGlobal . ScramjetConfig ,
218+ prefix : URL
219+ ) {
220+ return function getInjectScripts ( meta , handler , script ) {
221+ return [
222+ script ( config . scramjetPath ) ,
223+ script (
224+ prefix . href . substring ( 0 , prefix . href . length - 1 ) +
225+ config . virtualWasmPath
226+ ) ,
227+ script (
228+ "data:text/javascript;base64," +
229+ btoa ( `
230+ (()=>{
231+ $scramjet.setWasm(Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0)));
232+ delete self.WASM;
233+ const cookieJar = new $scramjet.CookieJar();
234+ const config = ${ JSON . stringify ( config ) } ;
235+ const sjconfig = ${ JSON . stringify ( sjconfig ) } ;
236+ cookieJar.load(${ cookieJar . dump ( ) } );
237+
238+ const prefix = new URL("${ prefix . href } ");
239+
240+ $scramjet.loadAndHook({
241+ context: {
242+ interface: {
243+ getInjectScripts: (${ yieldGetInjectScripts . toString ( ) } )(cookieJar, config, sjconfig, prefix),
244+ codecEncode: ${ codecEncode . toString ( ) } ,
245+ codecDecode: ${ codecDecode . toString ( ) } ,
246+ },
247+ prefix,
248+ cookieJar,
249+ config: sjconfig
250+ },
251+ transport: null,
252+ })
253+
254+ document.currentScript.remove();
255+ })();
256+ ` )
257+ ) ,
258+ ] ;
259+ } ;
260+ }
261+
286262class Frame {
287- fetchHandler : ScramjetFetchHandler ;
263+ fetchHandler : ScramjetGlobal . ScramjetFetchHandler ;
288264 id : string ;
289265 prefix : string ;
290266
267+ getInjectScripts ( meta , handler , script ) { }
268+
269+ get context ( ) {
270+ return {
271+ cookieJar,
272+ prefix : new URL ( this . prefix , location . href ) ,
273+ config : {
274+ ...$scramjet . defaultConfig ,
275+ ...cfg ,
276+ } ,
277+ interface : {
278+ getInjectScripts : yieldGetInjectScripts (
279+ this . controller . cookieJar ,
280+ config ,
281+ { ...$scramjet . defaultConfig , ...cfg } ,
282+ new URL ( this . prefix , location . href )
283+ ) ,
284+ getWorkerInjectScripts : ( ) => "" ,
285+ codecEncode,
286+ codecDecode,
287+ } ,
288+ } ;
289+ }
290+
291291 constructor (
292292 public controller : Controller ,
293293 public element : HTMLIFrameElement
294294 ) {
295295 this . id = makeId ( ) ;
296296 this . prefix = this . controller . prefix + "/" + this . id + "/" ;
297297
298- this . fetchHandler = new ScramjetFetchHandler ( {
299- client,
300- cookieJar : this . controller . cookieJar ,
301- prefix : new URL ( this . prefix , location . href ) ,
302- sendClientbound : ( type , msg ) => { } ,
303- onServerbound : ( type , listener ) => { } ,
298+ this . fetchHandler = new $scramjet . ScramjetFetchHandler ( {
299+ crossOriginIsolated : self . crossOriginIsolated ,
300+ context : this . context ,
301+ transport : lc ,
302+ async sendSetCookie ( url , cookie ) { } ,
303+ async fetchBlobUrl ( url ) {
304+ return ( await fetch ( url ) ) as BareResponseFetch ;
305+ } ,
306+ async fetchDataUrl ( url ) {
307+ return ( await fetch ( url ) ) as BareResponseFetch ;
308+ } ,
304309 } ) ;
305310 }
306311
307312 go ( url : string ) {
308- const encoded = rewriteUrl ( url , {
309- prefix : new URL ( this . prefix , location . href ) ,
313+ const encoded = $scramjet . rewriteUrl ( url , this . context , {
310314 origin : new URL ( location . href ) ,
311315 base : new URL ( location . href ) ,
312316 } ) ;
0 commit comments