-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.js
83 lines (73 loc) · 1.92 KB
/
ui.js
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
const { app, BrowserWindow, ipcMain } = require("electron");
const { io } = require("socket.io-client");
const Crypto = require("./classes/crypto");
function getRandomPass() {
/* thanks stackoverflow lol */
var s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return String(
Array(32)
.join()
.split(",")
.map(function () {
return s.charAt(Math.floor(Math.random() * s.length));
})
.join("")
);
}
const crypto = new Crypto(getRandomPass());
let window, otherUsersPublicKey, socket;
app.whenReady().then(() => {
window = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: false,
},
});
window.loadURL("http://localhost:3000");
});
//electron
ipcMain.addListener("messageFromReact", (event, arg) => {
socket.emit(
"message",
crypto.encrypt(arg, otherUsersPublicKey).cipher,
socket.id
);
window.webContents.send("messageFromMe", true, socket.id, arg);
});
ipcMain.addListener("socketIP", (event, arg) => {
socket = io(arg, {
query: {
publicKey: crypto.publicKey,
},
});
socket.on("connect", function () {
console.log("Connected!");
window.webContents.send("socketController", arg);
});
socket.on("disconnect", function () {
console.log("Disconnected!");
});
socket.on("connect_failed", function () {
window.webContents.send("socketError");
});
socket.on("keyBroadcast", (key) => {
otherUsersPublicKey = key;
socket.emit("keyBroadcastFromFirstUser", crypto.publicKey);
});
socket.on("keyBroadcastFromFirstUsersSocket", (key) => {
otherUsersPublicKey = key;
});
socket.on("messageFromSocket", (data) => {
console.log(data[0]);
window.webContents.send(
"newMessageFromServer",
false,
data[0],
crypto.decrypt(data[1]).plaintext
);
});
});
// socket io