-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin.html
373 lines (342 loc) · 11.9 KB
/
join.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>JOIN P2P + E2E CHAT</title>
<script src="https://cdn.tailwindcss.com"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/crypto-js.min.js"
integrity="sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/peerjs/1.5.2/peerjs.min.js"
integrity="sha512-o/HHmG8jeysxAxgj7HH5ZZnXOwWYS7D9oGVJH+TdRIpEYj01/m9Dm26vmcOtfhou2QbzPXFdnewbEp6v9I0U0g=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
></script>
<style>
@media only screen and (max-width: 600px) {
.max-w-full {
max-width: 100% !important;
}
}
body {
font-family: "DejaVu Sans", Arial, Helvetica, sans-serif;
}
.msg-time {
color: blue;
}
.cueMsg {
color: orange;
}
.selfMsg {
color: green;
}
.peerMsg {
color: red;
}
</style>
</head>
<body class="bg-gray-700">
<div
class="bg-white rounded-lg shadow-md p-4 max-w-full mx-auto sm:max-w-full md:max-w-full lg:max-w-lg xl:max-w-xl 2xl:max-w-2xl"
>
<div class="flex items-center mb-4">
<div
id="flt"
class="w-12 h-12 bg-blue-500 text-white font-bold text-2xl rounded-full flex items-center justify-center mr-4"
>
C
</div>
<div class="text-gray-900 font-bold text-md">
<input
class="bg-gray-100 rounded-lg p-1 mb-1"
type="text"
id="receiver-id"
placeholder="Enter the host id"
/>
<button
class="bg-green-500 text-white text-sm font-bold py-1 px-1 rounded-lg"
type="submit"
id="connect-button"
>
Connect
</button>
</div>
</div>
<div class="text-gray-700 text-sm" id="status">Show the status</div>
<div>
<input
class="bg-gray-100 rounded-lg p-1 mb-1"
type="text"
id="key"
placeholder="Enter the key"
/>
<button
class="bg-yellow-500 text-white text-sm font-bold py-1 px-1 rounded-lg"
type="submit"
id="getkey"
onclick="getkey(event)"
>
Secure Chat
</button>
<button
class="bg-red-500 text-sm text-white font-bold py-1 px-1 rounded-lg"
type="submit"
id="clearMsgsButton"
>
Clear Msgs (Local)
</button>
</div>
<div class="mt-4 border-t border-gray-300 pt-4 flex flex-col">
<input
class="bg-gray-100 rounded-lg p-2 mb-2"
type="text"
placeholder="Type your message here..."
id="sendMessageBox"
/>
<button
class="bg-blue-500 text-white font-bold py-2 px-4 rounded-lg"
type="button"
id="sendButton"
>
Send
</button>
</div>
<div class="mt-4 overflow-y-auto max-h-96" id="message">
<!-- Messages will appear here -->
</div>
</div>
<script type="text/javascript">
// encryption and decryption mechanism
let globalkey;
let otherName;
let ourName;
function getkey(event) {
var keyelm = document.getElementById("key");
globalkey = keyelm.value;
keyelm.value = "";
event.currentTarget.innerHTML = "Change Key";
}
function enc(msg) {
if (!globalkey) {
return msg;
}
// Encrypt
var ciphertext = CryptoJS.AES.encrypt(msg, globalkey).toString();
return ciphertext;
}
function dec(ciphertext) {
if (!globalkey) {
return ciphertext;
}
// Decrypt
var bytes = CryptoJS.AES.decrypt(ciphertext, globalkey);
var originalText = bytes.toString(CryptoJS.enc.Utf8);
if (!originalText) {
return ciphertext;
}
return originalText;
}
(function () {
var lastPeerId = null;
var peer = null; // own peer object
var conn = null;
var recvIdInput = document.getElementById("receiver-id");
var status = document.getElementById("status");
var message = document.getElementById("message");
var sendMessageBox = document.getElementById("sendMessageBox");
var sendButton = document.getElementById("sendButton");
var clearMsgsButton = document.getElementById("clearMsgsButton");
var connectButton = document.getElementById("connect-button");
var cueString = `<span class="cueMsg">${otherName}: </span>`;
var profile = document.getElementById("flt");
/**
* Create the Peer object for our end of the connection.
*
* Sets up callbacks that handle any events related to our
* peer object.
*/
function initialize() {
// Create own peer object with connection to shared PeerJS server
peer = new Peer(null, {
debug: 2,
});
peer.on("open", function (id) {
// Workaround for peer.reconnect deleting previous id
if (peer.id === null) {
console.log("Received null id from peer open");
peer.id = lastPeerId;
} else {
lastPeerId = peer.id;
}
console.log("ID: " + peer.id);
});
peer.on("connection", function (c) {
// Disallow incoming connections
c.on("open", function () {
c.send("Sender does not accept incoming connections");
setTimeout(function () {
c.close();
}, 500);
});
});
peer.on("disconnected", function () {
status.innerHTML = "Connection lost. Please reconnect";
console.log("Connection lost. Please reconnect");
// Workaround for peer.reconnect deleting previous id
peer.id = lastPeerId;
peer._lastServerId = lastPeerId;
peer.reconnect();
});
peer.on("close", function () {
conn = null;
status.innerHTML = "Connection destroyed. Please refresh";
console.log("Connection destroyed");
});
peer.on("error", function (err) {
console.log(err);
alert("" + err);
});
}
/**
* Create the connection between the two Peers.
*
* Sets up callbacks that handle any events related to the
* connection and data received on it.
*/
function join() {
// Close old connection
if (conn) {
conn.close();
}
// Create connection to destination peer specified in the input field
conn = peer.connect(recvIdInput.value, {
reliable: true,
});
recvIdInput.value = "";
conn.on("open", function () {
status.innerHTML = "Connected to: " + conn.peer;
console.log("Connected to: " + conn.peer);
// taking name input and then signal to other side
ourName = prompt("Please enter your name", "Jhon Dee"); // little flaw
if (ourName == null || ourName == "") {
ourName = "Me";
profile.innerHTML = ourName[0];
} else {
profile.innerHTML = ourName[0];
signal(ourName);
}
// Check URL params for comamnds that should be sent immediately
var command = getUrlParam("command");
if (command) conn.send(command);
});
// Handle incoming data (messages only since this is the signal sender)
conn.on("data", function (data) {
switch (data.startsWith("++")) {
case true: // has a flow no need to regexify it for now
// this case is gonna be used to set the user name talking to each other
otherName = data.substring(2);
addMessage(
`<span class="cueMsg">Log:Others Name changed to </span>` +
otherName
);
break;
default:
addMessage(
`<span class="peerMsg">${otherName}: </span>` + dec(data)
);
break;
}
});
conn.on("close", function () {
status.innerHTML = "Connection closed";
});
}
/**
* Get first "GET style" parameter from href.
* This enables delivering an initial command upon page load.
*
* Would have been easier to use location.hash.
*/
function getUrlParam(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null) return null;
else return results[1];
}
/**
* Send a signal via the peer connection and add it to the log.
* This will only occur if the connection is still alive.
*/
function signal(sigName) {
if (conn && conn.open) {
conn.send(`++${sigName}`);
addMessage(
`<span class="cueMsg">Log:Mine Name changed to </span>` + sigName
);
} else {
console.log("Connection is closed");
}
}
function addMessage(msg) {
var now = new Date();
var h = now.getHours();
var m = addZero(now.getMinutes());
var s = addZero(now.getSeconds());
if (h > 12) h -= 12;
else if (h === 0) h = 12;
function addZero(t) {
if (t < 10) t = "0" + t;
return t;
}
message.innerHTML =
'<br><span class="msg-time">' +
h +
":" +
m +
":" +
s +
"</span> - " +
msg +
message.innerHTML;
}
function clearMessages() {
message.innerHTML = "";
addMessage("Msgs cleared");
}
// Listen for enter in message box
sendMessageBox.addEventListener("keypress", function (e) {
var event = e || window.event;
var char = event.which || event.keyCode;
if (char == "13") sendButton.click();
});
// Send message
sendButton.addEventListener("click", function () {
if (conn && conn.open) {
var msg = sendMessageBox.value;
if (msg) {
sendMessageBox.value = "";
conn.send(enc(msg)); // sending encrypted msgs
console.log("Sent: " + msg);
addMessage(`<span class="selfMsg">${ourName}: </span> ` + msg);
}
} else {
console.log("Connection is closed");
}
});
// Clear messages box
clearMsgsButton.addEventListener("click", clearMessages);
// Start peer connection on click
connectButton.addEventListener("click", join);
// Since all our callbacks are setup, start the process of obtaining an ID
initialize();
})();
</script>
</body>
</html>