-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
407 lines (380 loc) · 21.8 KB
/
index.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>PM - A voice chat application that respects your privacy.</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link href="index.css" rel="stylesheet">
</head>
<body class="bg-dark text-white d-flex">
<div class="d-flex flex-direction-row gap-3 flex-wrap ms-auto me-auto justify-content-center">
<div class="p-3 flex-md-1 w-45">
<div>
<div class="mb-3">
<label for="server" class="form-label">Server</label>
<input type="text" class="form-control" id="server">
</div>
<div class="mb-3">
<label for="port" class="form-label">Port</label>
<input type="text" class="form-control" id="port" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1'); if(this.value>65535){this.value = 65535;}" maxlength="5" placeholder="Default port for PM is 20979" value="20979">
</div>
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" maxlength="8">
</div>
<div class="mb-3">
<label for="passwd" class="form-label">Password (if server has any)</label>
<input type="text" class="form-control" id="passwd" maxlength="8">
</div>
<div class="mb-3">
<label for="apasswd" class="form-label">Admin password (can be left empty)</label>
<input type="text" class="form-control" id="apasswd" maxlength="8">
</div>
<div class="mb-3">
<label for="pgp-public" class="form-label">PGP public key (will be sent over to the server)</label>
<input type="file" class="form-control" id="pgp-public">
</div>
<div class="mb-3">
<label for="pgp-public" class="form-label">PGP private key (is kept and used in browser only)</label>
<input type="file" class="form-control" id="pgp-private">
</div>
<div class="d-flex flex-row gap-3 flex-wrap">
<button type="submit" class="btn btn-primary" onclick="connect()" id="connectbtn">Connect</button>
<button class="btn btn-secondary" onclick="generate(prompt('Enter your E-mail'), prompt('Enter your name'))">Generate keys</button>
</div>
</div>
</div>
<div class="p-3 flex-sm-2 w-45">
<div>
<div class="mh-50 gap-2 d-flex flex-column" id="members">
</div>
</div>
</div>
<div class="p-3 flex-md-1 w-80 d-none" id="chat-outer">
<div>
<div class="mh-30 gap-2 d-flex flex-column" id="chat-inner">
<div class="card">
<div class="card-body">
<h5 class="card-title">PM</h5>
<p class="card-text">Please connect to view the chat!</p>
</div>
</div>
</div>
<div class="d-flex flex-row mt-3">
<div class="form-floating w-100 h-4">
<input type="text" class="form-control w-100" autocomplete="off" id="floatingMsg" placeholder="Message" style="height: 4rem;">
<label for="floatingMsg">Message</label>
</div>
<button class="btn btn-primary h-4 w-4" onclick="uploadFile(); ">File</button>
</div>
</div>
</div>
</div>
<script>
window.pageLoaded = false;
setInterval(()=>{window.pageLoaded = document.readyState === 'complete'; if(window.pageLoaded) onLoaded()}, 450);
let keys = {};
const onLoaded = ()=>{
document.getElementById('server').setAttribute('placeholder', `If empty - we\'ll connect to ${window.location.hostname}`)
document.getElementById('pgp-public').addEventListener('change', onReadFile);
document.getElementById('pgp-private').addEventListener('change', onReadFile);
document.getElementById('floatingMsg').addEventListener("keypress", (event)=>{
if(event.key == "Enter" && event.target.value != '' && window.ws_connected && window.oks >= 2 && window.pgplist != undefined){
let txt = event.target.value;
event.target.value = '';
let packets = [];
Object.keys(window.pgplist).forEach(async (e)=>{
try{
const key = window.pgplist[e];
const encrypted = await openpgp.encrypt({
message: await openpgp.createMessage({ text: txt}), // input as Message object
encryptionKeys: await openpgp.readKey({ armoredKey: key}),
});
packets.push(`TEXT_${e}_${encrypted}`);
}catch(e){
console.warn(`Failed to generate packet for ${e}. This could be simple network issue, or somebody is trying to intercept this communication!`)
}
if(packets.length == Object.keys(window.pgplist).length){
packets.forEach((packet)=>{
ws.send(packet);
});
delete packets;
}
});
}
});
}
const uploadFile = async ()=>{
if(!confirm('Files will be stored unencrypted. Continue?')){return;}
const fr = new FileReader();
let f = (await (await showOpenFilePicker())[0].getFile());
fr.readAsArrayBuffer(f);
fr.onloadend = ()=>{
if(fr.result.byteLength > 500000000 && !confirm("Uploading files > 0.5GB might take a while. Continue?")){ return; }
window.lastfn = f.name;
window.frr = fr.result;
window.lastfid = '';
window.fileoks = 0;
ws.send('FILE_'+arrayBufferToBase64(window.frr.slice(0, 512000)));
delete f;
}
}
const uploadFile2 = async(url) =>{
let packets = [];
Object.keys(window.pgplist).forEach(async (e)=>{
try{
const key = window.pgplist[e];
const encrypted = await openpgp.encrypt({
message: await openpgp.createMessage({ text: url}), // input as Message object
encryptionKeys: await openpgp.readKey({ armoredKey: key}),
});
packets.push(`POSTFILE_${e}_${encrypted}`);
}catch(e){
console.warn(`Failed to generate packet for ${e}. This could be simple network issue, or somebody is trying to intercept this communication!`)
}
if(packets.length == Object.keys(window.pgplist).length){
packets.forEach((packet)=>{
ws.send(packet);
});
delete packets;
}
});
}
const generate = async (email, name)=>{
if(email == 'undefined' || name == 'undefined' || email == null || name == null || email == '' || name == '') return;
try{
const { privateKey, publicKey, revocationCertificate } = await openpgp.generateKey({
type: 'ecc',
curve: 'curve25519',
userIDs: [{ name: name, email: email}], // you can pass multiple user IDs
format: 'armored'
});
let file = `${privateKey}\n${publicKey}\n${revocationCertificate}`;
let e = document.createElement('a');
e.download = 'keys.txt';
e.href = URL.createObjectURL(new Blob([file], {'type': 'text/plain'}));
e.click();
URL.revokeObjectURL(e.href);
e.href = URL.createObjectURL(new Blob([privateKey], {'type': 'text/plain'}));
e.download="private.txt";
e.click();
URL.revokeObjectURL(e.href);
e.href = URL.createObjectURL(new Blob([publicKey], {'type': 'text/plain'}));
e.download="public.txt";
e.click();
URL.revokeObjectURL(e.href);
e.remove();
delete file;
keys['public'] = publicKey;
keys['private'] = privateKey;
document.getElementById('pgp-private').setAttribute('disabled', 'disabled');
document.getElementById('pgp-public').setAttribute('disabled', 'disabled');
}catch(e){
alert(e); return;
}
}
const onReadFile = (e)=>{
const fr = new FileReader();
let el = document.getElementById(e.target.id);
fr.readAsText(el.files[0]);
fr.onloadend = ()=>{
keys[e.target.id.split('-')[1]] = fr.result;
}
}
const disableInputs = (val=true)=>{
let inputs = document.getElementsByTagName('input');
for (let index = 0; index < inputs.length; index++) {
const element = inputs[index];
if (element.id=='floatingMsg') continue;
if(val)
element.setAttribute('disabled', 'disabled');
else
element.removeAttribute('disabled');
}
}
const connect = () => {
if (keys=={}){
alert("Import or generate keys.");
return;
}
window.dlbase = `${window.location.protocol}//${(document.getElementById('server').value.trim() == '') ? window.location.hostname : document.getElementById('server').value}:${document.getElementById('port').value}/`;
window.ws = new WebSocket(`ws${(window.location.protocol == 'https:') ? 's' : ''}://${(document.getElementById('server').value.trim() == '') ? window.location.hostname : document.getElementById('server').value}:${document.getElementById('port').value}`);
window.ws.onmessage = async (msg)=>{
if (msg.data == 'OK'){
window.oks+=1;
return;
}
try{
let data = JSON.parse(msg.data);
if (data['type'] == 'fid'){
window.fileoks += 1;
window.lastfid = data['fid'];
let sliceTo = (window.fileoks+1)*512000;
if(sliceTo > window.frr.byteLength){
sliceTo = window.frr.byteLength;
}
let fileval = arrayBufferToBase64(window.frr.slice(window.fileoks*512000,sliceTo ));
if(fileval!=''){
ws.send('FILE_'+fileval+'_'+window.lastfid);
}else{
uploadFile2(`${window.dlbase}${window.lastfid}/${window.lastfn}`);
}
}else if (data['type'] == 'pgplist'){
window.pgplist = data['result'];
window.waitPGPList = false;
}else if(data['type'] == 'receive'){
console.log(data);
const pkey = await openpgp.readPrivateKey({ armoredKey: keys['private'] });
const decrypted = await openpgp.decrypt({
message: await openpgp.readMessage ({armoredMessage: data['message']}),
decryptionKeys: pkey,
format: 'binary'
});
let url = URL.createObjectURL(new Blob([decrypted.data], { type: 'audio/webm' }));
let aud = new Audio(url);
await aud.play();
aud.addEventListener("ended", () => {
aud.remove();
URL.revokeObjectURL(url);
});
}else if(data['type'] == 'receive_msg'){
let cid = `${Math.round(Math.random()*1000000)}${md5(JSON.stringify(data))}`;
let nid = `${Math.round(Math.random()*1000000)}${md5(JSON.stringify(data))}`;
const pkey = await openpgp.readPrivateKey({ armoredKey: keys['private'] });
const decrypted = await openpgp.decrypt({
message: await openpgp.readMessage ({armoredMessage: data['message']}),
decryptionKeys: pkey
});
document.getElementById('chat-inner').innerHTML+=`<div class="card">
<div class="card-body">
<h5 class="card-title" id="name-${nid}"></h5>
<p class="card-text" id="card-${cid}"></p>
</div>
</div>`;
document.getElementById(`name-${nid}`).innerText = data['from'];
document.getElementById(`card-${cid}`).innerText = decrypted.data;
delete cid;
}else if(data['type'] == 'receive_file'){
let cid = `${Math.round(Math.random()*1000000)}${md5(JSON.stringify(data))}`;
let nid = `${Math.round(Math.random()*1000000)}${md5(JSON.stringify(data))}`;
const pkey = await openpgp.readPrivateKey({ armoredKey: keys['private'] });
const decrypted = await openpgp.decrypt({
message: await openpgp.readMessage ({armoredMessage: data['message']}),
decryptionKeys: pkey
});
document.getElementById('chat-inner').innerHTML+=`<div class="card">
<div class="card-body">
<h5 class="card-title" id="name-${nid}"></h5>
<p class="card-text" id="card-${cid}"></p>
</div>
</div>`;
document.getElementById(`name-${nid}`).innerText = data['from'];
document.getElementById(`card-${cid}`).innerText = decrypted.data;
document.getElementById(`card-${cid}`).innerHTML = `<a href="javascript:window.open('${document.getElementById(`card-${cid}`).innerText}');">Download file</a>`;
delete cid;
}else if(data['type'] == 'file'){
await uploadFile2(data['url']);
}
}catch(e){console.log(e)}
}
window.ws.onopen = (e)=>{
alert("Connected.");
document.getElementById('connectbtn').setAttribute('onclick', 'ws.close()');
document.getElementById('connectbtn').innerText = 'Disconnect';
document.getElementById('chat-outer').classList.remove('d-none');
document.getElementById('chat-inner').innerHTML = '';
disableInputs()
window.ws_connected = true;
window.oks = 0;
window.ws.send(`AUTH_${document.getElementById('passwd').value}`);
window.ws.send(`ADMIN_${document.getElementById('apasswd').value}`);
window.ws.send(`USERNAME_${document.getElementById('username').value}`);
window.ws.send(`PGP_${keys['public']}`);
setInterval(()=>{if(window.oks >= 2 && window.waitPGPList != true) window.ws.send('PGPLIST'); window.waitPGPList = true; updMembers(); }, 500);
init();
}
window.ws.onclose = (e)=>{
alert("Disconnected.");
disableInputs(false);
document.getElementById('connectbtn').setAttribute('onclick', 'connect()');
document.getElementById('connectbtn').innerText = 'Connect';
document.getElementById('chat-outer').classList.add('d-none');
document.getElementById('members').innerHTML = '';
window.ws_connected = false;
}
}
const updMembers = ()=>{
let m = document.getElementById('members');
let outHTML = '';
let ids = [];
Object.keys(window.pgplist).forEach((e)=>{
let cid = Math.round(Math.random()*1000000);
ids.push({'id': cid, 'name': e});
outHTML+=`<div class="alert alert-primary" role="alert">
<span id="name_${cid}"></span> <span title="MD5 hash of user's public key">(${md5(window.pgplist[e])})</span>
</div>`;
delete cid;
});
if(outHTML!= m.innerHTML){
m.innerHTML = outHTML;
}
ids.forEach((id)=>{
document.getElementById(`name_${id['id']}`).innerText = id['name'];
});
}
const arrayBufferToBase64 = (arrayBuffer)=>{
return btoa(new Uint8Array(arrayBuffer).reduce((data, byte) => data + String.fromCharCode(byte), ''));
}
const buf2hex = (buffer, join="") => {
return [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')).join(join);
}
const hex2buf = (hex)=>{
return new Uint8Array(hex.match(/../g).map(h=>parseInt(h,16))).buffer
}
const init = async ()=>{
if(!window.pageLoaded){
alert('Wait for page to load first.'); return;
}
navigator.mediaDevices.getUserMedia({
audio: true
}).then(stream => {
console.log(stream);
const mediaRecorder = new MediaRecorder(stream);
mediaRecorder.addEventListener("dataavailable", async (event) => {
if(event.data.size!=0){
if(window.ws_connected && window.oks >= 2 && window.pgplist != undefined){
let packets = [];
let voicepacket = new Uint8Array(await event.data.arrayBuffer());
Object.keys(window.pgplist).forEach(async (e)=>{
try{
const key = window.pgplist[e];
const encrypted = await openpgp.encrypt({
message: await openpgp.createMessage({ binary: voicepacket}), // input as Message object
encryptionKeys: await openpgp.readKey({ armoredKey: key}),
});
packets.push(`VOICE_${e}_${encrypted}`);
}catch(e){
console.warn(`Failed to generate packet for ${e}. This could be simple network issue, or somebody is trying to intercept this communication!`)
}
console.log(packets);
if(packets.length == Object.keys(window.pgplist).length){
packets.forEach((packet)=>{
ws.send(packet);
});
delete packets, voicepacket;
}
});
}
}
});
mediaRecorder.start();
setInterval(()=>{mediaRecorder.requestData(); mediaRecorder.stop(); mediaRecorder.start();}, 125);
});
}
</script>
<script src="md5.min.js"></script>
<script src="openpgp.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</body>
</html>