|
| 1 | +// Copyright 2020-2023 Nym Technologies SA |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +class WebWorkerClient { |
| 16 | + worker = null; |
| 17 | + |
| 18 | + constructor() { |
| 19 | + this.worker = new Worker('./worker.js'); |
| 20 | + |
| 21 | + this.worker.onmessage = (ev) => { |
| 22 | + if (ev.data && ev.data.kind) { |
| 23 | + switch (ev.data.kind) { |
| 24 | + case 'Ready': |
| 25 | + const {selfAddress} = ev.data.args; |
| 26 | + displaySenderAddress(selfAddress); |
| 27 | + break; |
| 28 | + case 'ReceiveMessage': |
| 29 | + const {message, senderTag, isTestPacket } = ev.data.args; |
| 30 | + displayReceived(message, senderTag, isTestPacket); |
| 31 | + break; |
| 32 | + case 'DisableMagicTestButton': |
| 33 | + const magicButton = document.querySelector('#magic-button'); |
| 34 | + magicButton.setAttribute('disabled', "true") |
| 35 | + break; |
| 36 | + case 'DisplayTesterResults': |
| 37 | + const {score, sentPackets, receivedPackets, receivedAcks, duplicatePackets, duplicateAcks} = ev.data.args; |
| 38 | + const resultText = `Test score: ${score}. Sent ${sentPackets} packets. Received ${receivedPackets} packets and ${receivedAcks} acks back. We also got ${duplicatePackets} duplicate packets and ${duplicateAcks} duplicate acks.` |
| 39 | + displayReceivedRawString(resultText) |
| 40 | + break; |
| 41 | + } |
| 42 | + } |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + sendMessage = (message, recipient) => { |
| 47 | + if (!this.worker) { |
| 48 | + console.error('Could not send message because worker does not exist'); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + this.worker.postMessage({ |
| 53 | + kind: 'SendMessage', |
| 54 | + args: { |
| 55 | + message, recipient, |
| 56 | + }, |
| 57 | + }); |
| 58 | + }; |
| 59 | + |
| 60 | + sendTestPacket = (mixnodeIdentity) => { |
| 61 | + if (!this.worker) { |
| 62 | + console.error('Could not send message because worker does not exist'); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + this.worker.postMessage({ |
| 67 | + kind: 'TestPacket', |
| 68 | + args: { |
| 69 | + mixnodeIdentity, |
| 70 | + }, |
| 71 | + }); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +let client = null; |
| 76 | + |
| 77 | +async function main() { |
| 78 | + client = new WebWorkerClient(); |
| 79 | + |
| 80 | + const sendButton = document.querySelector('#send-button'); |
| 81 | + sendButton.onclick = function () { |
| 82 | + sendMessageTo(); |
| 83 | + }; |
| 84 | + |
| 85 | + const magicButton = document.querySelector('#magic-button'); |
| 86 | + magicButton.onclick = function () { |
| 87 | + sendTestPacket(); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Create a Sphinx packet and send it to the mixnet through the gateway node. |
| 93 | + * |
| 94 | + * Message and recipient are taken from the values in the user interface. |
| 95 | + * |
| 96 | + */ |
| 97 | +async function sendMessageTo() { |
| 98 | + const message = document.getElementById('message').value; |
| 99 | + const recipient = document.getElementById('recipient').value; |
| 100 | + |
| 101 | + await client.sendMessage(message, recipient); |
| 102 | + displaySend(message); |
| 103 | +} |
| 104 | + |
| 105 | +async function sendTestPacket() { |
| 106 | + const mixnodeIdentity = document.getElementById('mixnode_identity').value; |
| 107 | + |
| 108 | + await client.sendTestPacket(mixnodeIdentity) |
| 109 | + displaySend(`sending test packets to: ${mixnodeIdentity}...`); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Display messages that have been sent up the websocket. Colours them blue. |
| 114 | + * |
| 115 | + * @param {string} message |
| 116 | + */ |
| 117 | +function displaySend(message) { |
| 118 | + let timestamp = new Date().toISOString().substr(11, 12); |
| 119 | + |
| 120 | + let sendDiv = document.createElement('div'); |
| 121 | + let paragraph = document.createElement('p'); |
| 122 | + paragraph.setAttribute('style', 'color: blue'); |
| 123 | + let paragraphContent = document.createTextNode(timestamp + ' sent >>> ' + message); |
| 124 | + paragraph.appendChild(paragraphContent); |
| 125 | + |
| 126 | + sendDiv.appendChild(paragraph); |
| 127 | + document.getElementById('output').appendChild(sendDiv); |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Display received text messages in the browser. Colour them green. |
| 132 | + * |
| 133 | + * @param {Uint8Array} raw |
| 134 | + */ |
| 135 | +function displayReceived(raw, sender_tag, isTestPacket) { |
| 136 | + let content = new TextDecoder().decode(raw); |
| 137 | + if (sender_tag !== undefined) { |
| 138 | + console.log("this message also contained some surbs from", sender_tag) |
| 139 | + } |
| 140 | + |
| 141 | + if (isTestPacket) { |
| 142 | + const decoded = JSON.parse(content) |
| 143 | + content = `Received packet ${decoded.msg_id} / ${decoded.total_msgs} for node ${decoded.encoded_node_identity} (test: ${decoded.test_id})` |
| 144 | + } |
| 145 | + |
| 146 | + displayReceivedRawString(content) |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | +function displayReceivedRawString(raw) { |
| 151 | + let timestamp = new Date().toISOString().substr(11, 12); |
| 152 | + let receivedDiv = document.createElement('div'); |
| 153 | + let paragraph = document.createElement('p'); |
| 154 | + paragraph.setAttribute('style', 'color: green'); |
| 155 | + let paragraphContent = document.createTextNode(timestamp + ' received >>> ' + raw); |
| 156 | + paragraph.appendChild(paragraphContent); |
| 157 | + receivedDiv.appendChild(paragraph); |
| 158 | + document.getElementById('output').appendChild(receivedDiv); |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * Display the nymClient's sender address in the user interface |
| 163 | + * |
| 164 | + * @param {String} address |
| 165 | + */ |
| 166 | +function displaySenderAddress(address) { |
| 167 | + document.getElementById('sender').value = address; |
| 168 | +} |
| 169 | + |
| 170 | +main(); |
0 commit comments