// Copyright 2020-2023 Nym Technologies SA // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. const RUST_WASM_URL = "mix_fetch_wasm_bg.wasm" const GO_WASM_URL = "go_conn.wasm" importScripts('mix_fetch_wasm.js'); importScripts('wasm_exec.js'); console.log('Initializing worker'); // wasm_bindgen creates a global variable (with the exports attached) that is in scope after `importScripts` const { default_debug, no_cover_debug, NymClient, set_panic_hook, Config, GatewayEndpointConfig, ClientStorage, MixFetchConfig, send_client_data, start_new_mixnet_connection, setupMixFetch, disconnectMixFetch, setupMixFetchWithConfig, mix_fetch_initialised, finish_mixnet_connection } = wasm_bindgen; let client = null; let tester = null; const go = new Go(); // Defined in wasm_exec.js var goWasm; let mixFetchReady = false; function sendLog(message, level = 'info') { self.postMessage({ kind: 'Log', args: { message, level }, }); } function sendReady() { self.postMessage({ kind: 'MixFetchReady' }); } function sendError(error) { self.postMessage({ kind: 'MixFetchError', args: { error: String(error) }, }); } async function logFetchResult(res) { console.log(res) let text = await res.text() console.log("HEADERS: ", ...res.headers) console.log("STATUS: ", res.status) console.log("STATUS TEXT: ", res.statusText) console.log("OK: ", res.ok) console.log("TYPE: ", res.type) console.log("URL: ", res.url) console.log("BODYUSED: ", res.bodyUsed) console.log("REDIRECTED: ", res.redirected) console.log("TEXT: ", text) self.postMessage({ kind: 'DisplayString', args: { rawString: text, }, }); } async function wasm_bindgenSetup() { const preferredGateway = "6qQYb4ArXANU6HJDxzH4PFCUqYb39Dae2Gem2KpxescM"; const validator = 'https://qa-nym-api.qa.nymte.ch/api'; // local const mixFetchNetworkRequesterAddress = "2o47bhnXWna6VEyt4mXMGQQAbXfpKmX7BkjkxUz8uQVi.6uQGnCqSczpXwh86NdbsCoDDXuqZQM9Uwko8GE7uC9g8@6qQYb4ArXANU6HJDxzH4PFCUqYb39Dae2Gem2KpxescM"; // const mixFetchNetworkRequesterAddress= "GqiGWmKRCbGQFSqH88BzLKijvZgipnqhmbNFsmkZw84t.4L8sXFuAUyUYyHZYgMdM3AtiusKnYUft6Pd8e41rrCHA@6qQYb4ArXANU6HJDxzH4PFCUqYb39Dae2Gem2KpxescM"; // STEP 1. construct config // those are just some examples, there are obviously more permutations; // note, the extra optional argument is of the following type: // /* // export interface MixFetchConfigOpts { // id?: string; // nymApi?: string; // nyxd?: string; // debug?: DebugWasm; // } // */ // // const debug = no_cover_debug() // // #1 // const config = new MixFetchConfig(mixFetchNetworkRequesterAddress, { id: 'my-awesome-mix-fetch-client', nymApi: validator, debug: debug} ); // #2 // const config = new MixFetchConfig(mixFetchNetworkRequesterAddress, { nymApi: validator, debug: debug} ); // #3 // const config = new MixFetchConfig(mixFetchNetworkRequesterAddress, { id: 'my-awesome-mix-fetch-client' } ); // // #4 const differentDebug = default_debug() const updatedTraffic = differentDebug.traffic; updatedTraffic.use_extended_packet_size = true updatedTraffic.average_packet_delay_ms = 666; differentDebug.traffic = updatedTraffic; const config = new MixFetchConfig(mixFetchNetworkRequesterAddress, {debug: differentDebug}); // // // STEP 2. setup the client // // note, the extra optional argument is of the following type: // /* // export interface MixFetchOptsSimple { // preferredGateway?: string; // storagePassphrase?: string; // } // */ // #1 await setupMixFetchWithConfig(config) // // #2 // await setupMixFetchWithConfig(config, { storagePassphrase: "foomp" }) // // #3 // await setupMixFetchWithConfig(config, { storagePassphrase: "foomp", preferredGateway }) } async function nativeSetup(preferredGateway) { sendLog('Setting up MixFetch...'); if (preferredGateway) { sendLog(`Using preferred gateway: ${preferredGateway}`); } else { sendLog('Using random gateway selection'); } const noCoverTrafficOverride = { traffic: {disableMainPoissonPacketDistribution: true}, coverTraffic: {disableLoopCoverTrafficStream: true}, } const mixFetchOverride = { requestTimeoutMs: 60000 } const opts = { forceTls: true, clientId: "my-client", clientOverride: noCoverTrafficOverride, mixFetchOverride, }; if (preferredGateway) { opts.preferredGateway = preferredGateway; } sendLog('Calling setupMixFetch...'); await setupMixFetch(opts); sendLog('setupMixFetch completed'); } async function startMixFetch(preferredGateway) { sendLog('Instantiating MixFetch...'); try { await nativeSetup(preferredGateway); mixFetchReady = true; sendLog('MixFetch client running!'); sendReady(); } catch (e) { sendLog('Failed to start MixFetch: ' + e, 'error'); sendError(e); } } async function handleFetchPayload(target) { if (!mixFetchReady) { sendLog('MixFetch not ready yet', 'error'); return; } const url = target; const args = {mode: "unsafe-ignore-cors"}; try { sendLog(`Fetching: ${url}`); const mixFetchRes = await mixFetch(url, args); sendLog('Fetch completed'); await logFetchResult(mixFetchRes); } catch (e) { sendLog('Fetch request failure: ' + e, 'error'); console.error("mix fetch request failure: ", e); } } async function handlePostPayload(url, body) { if (!mixFetchReady) { sendLog('MixFetch not ready yet', 'error'); return; } const args = { method: 'POST', mode: "unsafe-ignore-cors", headers: { 'Content-Type': 'application/json', }, body: body, }; try { sendLog(`POST request to: ${url}`); sendLog(`POST body: ${body}`); const mixFetchRes = await mixFetch(url, args); sendLog('POST completed'); await logFetchResult(mixFetchRes); } catch (e) { sendLog('POST request failure: ' + e, 'error'); console.error("mix fetch POST request failure: ", e); } } function setupMessageHandler() { self.onmessage = async event => { if (event.data && event.data.kind) { switch (event.data.kind) { case 'StartMixFetch': { const { preferredGateway } = event.data.args; await startMixFetch(preferredGateway); break; } case 'FetchPayload': { const { target } = event.data.args; await handleFetchPayload(target); break; } case 'PostPayload': { const { url, body } = event.data.args; await handlePostPayload(url, body); break; } } } }; } // TODO: look into https://www.aaron-powell.com/posts/2019-02-08-golang-wasm-5-compiling-with-webpack/ async function loadGoWasm() { const resp = await fetch(GO_WASM_URL); if ('instantiateStreaming' in WebAssembly) { const wasmObj = await WebAssembly.instantiateStreaming(resp, go.importObject) goWasm = wasmObj.instance go.run(goWasm) } else { const bytes = await resp.arrayBuffer() const wasmObj = await WebAssembly.instantiate(bytes, go.importObject) goWasm = wasmObj.instance go.run(goWasm) } } function setupRsGoBridge() { // (note: reason for intermediate `__rs_go_bridge__` object is to decrease global scope bloat // and to discourage users from trying to call those methods directly) self.__rs_go_bridge__ = {} self.__rs_go_bridge__.send_client_data = send_client_data self.__rs_go_bridge__.start_new_mixnet_connection = start_new_mixnet_connection self.__rs_go_bridge__.mix_fetch_initialised = mix_fetch_initialised self.__rs_go_bridge__.finish_mixnet_connection = finish_mixnet_connection } async function main() { sendLog('Worker starting...'); // load rust WASM package sendLog('Loading Rust WASM...'); await wasm_bindgen(RUST_WASM_URL); sendLog('Loaded Rust WASM'); // load go WASM package sendLog('Loading Go WASM...'); await loadGoWasm(); sendLog('Loaded Go WASM'); // sets up better stack traces in case of in-rust panics set_panic_hook(); setupRsGoBridge(); goWasmSetLogging("trace") // Set up message handler (MixFetch will be started on demand) setupMessageHandler(); sendLog('Worker ready - click Start MixFetch to begin'); } // Let's get started! main();