-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.html
344 lines (318 loc) · 10.8 KB
/
host.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
<!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>
/* Just simple css for the msgs and timestamps */
@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">
<div id="receiver-id">ID :</div>
<button
class="bg-pink-500 text-sm text-white font-bold py-1 px-1 rounded-lg"
onclick="copy()"
>
Copy!
</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-white text-sm 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
function copy() {
// Get the text field
var copyText = document.getElementById("receiver-id");
navigator.clipboard.writeText(
String(copyText.innerHTML).split(":")[1].trim()
);
// can show toast
}
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 peerId = null;
var conn = null;
var recvId = document.getElementById("receiver-id");
var status = document.getElementById("status");
var message = document.getElementById("message");
var standbyBox = document.getElementById("standby");
var sendMessageBox = document.getElementById("sendMessageBox");
var sendButton = document.getElementById("sendButton");
var clearMsgsButton = document.getElementById("clearMsgsButton");
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) {
peer.id = lastPeerId;
} else {
lastPeerId = peer.id;
}
recvId.innerHTML = "ID: " + peer.id;
status.innerHTML = "Awaiting connection...";
});
peer.on("connection", function (c) {
// Allow only a single connection
if (conn && conn.open) {
c.on("open", function () {
c.send("Already connected to another client");
setTimeout(function () {
c.close();
}, 500);
});
return;
}
conn = c;
console.log("Connected to: " + conn.peer);
status.innerHTML = "Connected";
ready();
});
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);
});
}
/**
* Triggered once a connection has been achieved.
* Defines callbacks to handle incoming data and connection events.
*/
function ready() {
// needed to this here as doing it over peer will trigger in very time a clinet try to connect
conn.on("open", function () {
// taking name input and then signal to other side
ourName = prompt("Please enter your name", "Jhon Dee");
if (ourName == null || ourName == "") {
ourName = "Me";
profile.innerHTML = ourName[0];
} else {
profile.innerHTML = ourName[0];
signal(ourName);
}
});
conn.on("data", function (data) {
var cueString = `<span class="cueMsg">Log:Others Name changed to </span>`;
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(cueString + otherName);
break;
default:
addMessage(
`<span class="peerMsg">${otherName}: </span>` + dec(data)
);
break;
}
});
conn.on("close", function () {
status.innerHTML = "Connection reset<br>Awaiting connection...";
conn = null;
});
}
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");
}
});
/**
* 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");
}
}
// Clear messages box
clearMsgsButton.addEventListener("click", clearMessages);
initialize();
})();
</script>
</body>
</html>