-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathws.html
executable file
·343 lines (305 loc) · 10.6 KB
/
ws.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<html>
<head>
<title>ws test</title>
<style type="text/css">
#result {
white-space: pre;
font-family: monospace;
}
.node {
padding-top: 5px;
border-top: 2px solid #000;
height: 25%;
width: 60%;
}
.topic, .key {
height: 15%;
width: 60%;
}
.key {
padding-top: 5px;
border-top: 2px solid #000;
}
.log {
height: 100%;
width: 35%;
float: right;
border-left: 2px dashed #000;
padding-left: 10px;
overflow: scroll;
}
</style>
</head>
<body>
<script language="javascript">
var faketopic = [1, 2, 3, 4];
var seq;
// the addresses are the swarm overlay addresses of the pss nodes we are connecting to
var addr = {};
var pubkey = {};
var subs = {};
var conn = {};
var up = false;
var seq = {
"left": 0,
"right": 0
};
var cmd = {};
var symkey = [];
function addLog(elemid, str) {
var loglist = document.getElementById("log");
var logitem = document.createElement("p");
logitem.setAttribute("class", "logitem");
logitem.innerHTML = "[" + elemid + "]: " + str;
//document.getElementById("log").appendChild(logitem);
loglist.insertBefore(logitem, loglist.firstChild);
}
function send(elemid, method, params) {
conn[elemid].send('{"jsonrpc":"2.0","id":' + seq[elemid]++ + ',"method":"' + method + '","params":' + params + '}');
}
function getAddr(elemid) {
addLog(elemid, "sending pss_baseAddr()");
send(elemid, "pss_baseAddr", "null");
cmd[elemid] = "addr";
}
function getPubKey(elemid) {
addLog(elemid, "sending pss_getPublicKey()");
send(elemid, "pss_getPublicKey", "null");
cmd[elemid] = "pubkey";
}
function setHint(elemid, v) {
var targetid = "left";
if (elemid == "left") {
targetid = "right";
}
var sel = document.getElementById("pss_" + elemid + "_hint");
if (sel.options[0].value == "Set") {
sel.remove(0);
}
var topic = document.getElementById("pss_topic_send").value;
addLog(elemid, "sending pss_setPeerPublicKey() with pubkey " + pubkey[targetid].substring(0, 18) + " addr hint " + addr[targetid].substring(0, 2+(2*v)));
send(elemid, "pss_setPeerPublicKey", '["' + pubkey[targetid] + '","' + topic + '","' + addr[targetid].substring(0, 2+(2*v)) + '"]');
cmd[elemid] = "hint";
}
function addSymKey() {
var a = new Uint8Array(32);
for (i = 0; i < 32; i++) {
a[i] = Math.floor(Math.random() * 255);
}
var topic = document.getElementById("pss_topic_send").value;
addLog("left", "sending pss_setSymmetricKey() key '" + toHexString(a) + "' topic '" + topic + "'");
send("left", "pss_setSymmetricKey", '[[' + a + '], "' + topic + '", [' + addr["right"] + '], true]');
cmd["left"] = "symkey";
addLog("right", "sending pss_setSymmetricKey() key '" + toHexString(a) + "' topic '" + topic + "'");
send("right", "pss_setSymmetricKey", '[[' + a + '], "' + topic + '", [' + addr["left"] + '], true]');
cmd["right"] = "symkey";
symkey.push({
"key": a,
"left": "",
"right": "",
});
}
function subscribe(topic) {
if (topic.length == 0 ) {
return
}
addLog("left", "sending pss_stringToTopic() with string '" + topic + "'");
send("left", "pss_stringToTopic", '["' + topic.value + '"]');
cmd["left"] = "topic";
}
function sendMsg(elemid, msg) {
targetid = "left";
if (elemid == "left") {
targetid = "right";
}
var topic = document.getElementById("pss_topic_send").value;
var key = document.getElementById("pss_key_send").value;
var hexmsg = toHexString(string2Bin(msg))
if (key == "pub") {
addLog(elemid, "sending pss_sendAsym() msg '0x" + hexmsg + "' to pubkey '" + pubkey[targetid].substring(0,18) + "' topic '" + topic + "' using address hint '" + document.getElementById("pss_" + elemid + "_hint_addr").innerHTML + "'");
send(elemid, "pss_sendAsym", '["' + pubkey[targetid] + '","' + topic + '","0x' + hexmsg + '"]');
} else {
addLog(elemid, "sending pss_sendSym() to symkey '" + toHexString(symkey[key]["key"].slice(0,4)) + "' topic '" + topic + "' using address hint '" + document.getElementById("pss_" + elemid + "_hint_addr").innerHTML + "'");
send(elemid, "pss_sendSym", '["' + symkey[key][elemid] + '","' + topic + '",[' + string2Bin(msg) + ']]');
}
}
function handshake(elemid) {
var topic = document.getElementById("pss_topic_send").value;
targetid = "left";
if (elemid == "left") {
targetid = "right";
}
addLog(elemid, "sending handshake to pubkey'" + pubkey[targetid].substring(0,18) + "'");
send("left", "pss_addHandshake", '["' + topic + '"]');
send("right", "pss_addHandshake", '["' + topic + '"]');
send(elemid, "pss_handshake", '["' + pubkey[targetid] + '","' + topic + '",' + true + ',' + true + ']');
}
function recvNode(msg, senderid) {
switch (cmd[senderid]) {
case "addr":
addr[senderid] = msg.result;
document.getElementById("pss_" + senderid + "_addr_val").innerHTML = addr[senderid].substring(0,66);
break;
case "pubkey":
pubkey[senderid] = msg.result;
document.getElementById("pss_" + senderid + "_pubkey_val").innerHTML = pubkey[senderid] + "...";
break;
case "topic":
addLog("left", "sending pss_Subscribe 'receive' with topic hex '" + msg.result + "'");
addLog("right", "sending pss_Subscribe 'receive' with topic hex '" + msg.result + "'");
send("left", "pss_subscribe", '["receive","' + msg.result + '"]');
send("right", "pss_subscribe", '["receive","' + msg.result + '"]');
cmd["left"] = "subscribe";
cmd["right"] = "subscribe";
var sel = document.getElementById("pss_topic_send");
var opt = document.createElement("option");
opt.text = msg.result;
opt.value = msg.result;
sel.appendChild(opt);
if (sel.options[0].value == "0") {
sel.removeAttribute("disabled");
sel.remove(0);
}
break;
case "symkey":
var sendsel = document.getElementById("pss_key_send");
var opt = document.createElement("option");
var lastsymkey = symkey[symkey.length-1];
if (lastsymkey["left"] == "" && lastsymkey["right"] == "") {
opt.value = symkey.length-1;
opt.text = toHexString(lastsymkey["key"].slice(0, 16));
sendsel.appendChild(opt);
}
lastsymkey[senderid] = msg.result;
break;
case "subscribe":
break;
case "hint":
document.getElementById("pss_" + senderid + "_hint_addr").innerHTML = addr[senderid].substring(0, 2+(2*document.getElementById("pss_" + senderid + "_hint").value));
break;
default:
if (msg.method == "pss_subscription") {
var keytype = msg.params.result.Asymmetric ? "pubkey" : "symkey";
var key;
if (keytype == "pubkey") {
key = msg.params.result.Key.substring(0, 18);
} else {
for (i = 0; i < symkey.length; i++) {
if (symkey[i][senderid] == msg.params.result.Key) {
key = toHexString(symkey[i]["key"].slice(0,8));
}
}
}
addLog(senderid, keytype + ":" + key + " <<< " + msg.params.result.Msg);
}
}
if (Object.keys(addr).length == 2 && Object.keys(pubkey).length == 2 && up == false) {
document.getElementById("pss_left_hint").removeAttribute("disabled");
document.getElementById("pss_right_hint").removeAttribute("disabled");
up = true;
}
cmd[senderid] = "";
}
if ('WebSocket' in window) {
// "local" and "remote" are just names, they're both as local and remote as the other :)
conn["left"] = new WebSocket("ws://localhost:8546");
conn["right"] = new WebSocket("ws://localhost:8547");
// local only does step 0 of the script
conn["left"].onopen = function() {
console.log("local ws open");
}
conn["right"].onopen = function() {
console.log("local ws open");
}
// local receives messages
conn["left"].onmessage = function(m) {
msg = JSON.parse(m.data);
console.log("left msg: " + m.data);
recvNode(msg, "left");
}
conn["right"].onmessage = function(m) {
msg = JSON.parse(m.data);
console.log("right msg: " + m.data);
recvNode(msg, "right");
}
conn["left"].onerror = connerror;
conn["right"].onerror = connerror;
function connerror(e) {
console.log("error: " + e);
}
}
// triggers
function script(step) {
switch (step) {
case 0:
// get the local's address
break;
}
}
// cheekily borrowed from https://stackoverflow.com/questions/34309988/byte-array-to-hex-string-conversion-in-javascript
function toHexString(byteArray) {
return Array.from(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
}
// equally cheekily borrowed from https://stackoverflow.com/questions/17720394/javascript-string-to-byte-to-string
function string2Bin(str) {
var result = [];
for (var i = 0; i < str.length; i++) {
result.push(str.charCodeAt(i));
}
return result;
}
</script>
<div class="log" id="log">
</div>
<div class="topic" id="topic">
<div class="info"><input type="text" id="pss_topic" / > <button id="pss_topic_add" onClick="subscribe(document.getElementById('pss_topic').value);">Add topic</button> </div>
<div class="action">
Send topic: <select id="pss_topic_send" disabled><option value="0">None</option></select>
</div>
</div>
<div class="node" id="left">
<div class="info"><button id="pss_left_addr_get" onClick="getAddr('left');">Addr</button> <span id="pss_left_addr_val"></span></div>
<div class="info"><button id="pss_left_pubkey_get" onClick="getPubKey('left');">Pubkey:</button> <span id="pss_left_pubkey_val"></span></div>
<div class="info">Hint:
<select onChange="setHint('left', this.value);" id="pss_left_hint" disabled>
<option defaultselected>Set</option>
<option>32</option>
<option>4</option>
<option>1</option>
<option>0</option>
</select>
<span id="pss_left_hint_addr"></span>
</div>
<div class="action">
<input type="text" id="pss_left" /> <button onClick="sendMsg('left', document.getElementById('pss_left').value);" id="pss_left_send">Send</button>
</div>
<div class="action">
<button onClick="handshake('left');" id="pss_handshake">Handshake</button>
</div>
</div>
<div class="node" id="right">
<div class="info"><button id="pss_right_addr_get" onClick="getAddr('right');">Addr</button> <span id="pss_right_addr_val"></span></div>
<div class="info"><button id="pss_right_pubkey_get" onClick="getPubKey('right');">Pubkey:</button> <span id="pss_right_pubkey_val"></span></div>
<div class="info">Hint:
<select onChange="setHint('right', this.value);" id="pss_right_hint" disabled>
<option defaultselected>Set</option>
<option>32</option>
<option>4</option>
<option>1</option>
<option>0</option>
</select>
<span id="pss_right_hint_addr"></span>
</div>
<div class="action">
<input type="text" id="pss_right" /> <button onClick="sendMsg('right', document.getElementById('pss_right').value);" id="pss_right_send">Send</button>
</div>
</div>
<div class="key" id="key">
<div class="info"> <button id="pss_symkey_add" onClick="addSymKey();"</button> Add symkey</div>
<div class="action">
Send key: <select id="pss_key_send"><option value="pub">Public key</option></select>
</div>
</body>
</html>