forked from kjs104901/torchat3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
181 lines (153 loc) · 5.35 KB
/
main.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
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
const { app, BrowserWindow, Menu, Tray } = require('electron');
const path = require('path');
global.__base = __dirname;
const netServer = require(`${__base}/core/network/netServer`);
const netUserList = require(`${__base}/core/netUserList`);
const notification = require(`${__base}/core/notification`);
const fileHandler = require(`${__base}/core/fileIO/fileHandler`);
const parser = require(`${__base}/core/network/parser`);
const config = require(`${__base}/core/config`);
const langs = require(`${__base}/core/langs`);
const debug = require(`${__base}/core/debug`);
debug.setLogAvailable(false);
const tor = require(`${__base}/tor/tor`);
//// ------------ App ------------ ////
//Security: force sandbox mode
if (process.argv.indexOf('--enable-sandbox') === -1 || process.argv.indexOf('--no-sandbox') > -1) {
console.log("[Error] Not in sandbox mode. Use --enable-sandbox");
app.quit();
return;
}
const instanceLock = app.requestSingleInstanceLock()
if (!instanceLock) {
console.log("[Error] The app is already running");
app.quit();
return;
}
app.on('second-instance', (event, commandLine, workingDirectory) => {
if (!mainWindow) { openMainWindow(); }
if (mainWindow) {
if (mainWindow.isMinimized()) { mainWindow.restore(); }
if (!mainWindow.isVisible()) { showMainWindow(); }
mainWindow.focus()
}
})
//Security: set a fake proxy to prevent the app from connecting to internet
const fakeProxy = 'null://255.255.255.0:65535'
app.commandLine.appendSwitch('host-rules', 'MAP * 127.0.0.1'); // map every hostname to local host
app.commandLine.appendSwitch('proxy-server', fakeProxy);
app.commandLine.appendSwitch('disable-extensions');
app.commandLine.appendSwitch('disable-plugins');
app.commandLine.appendSwitch('disable-bundled-ppapi-flash');
app.on('ready', () => {
openMainWindow();
boot();
})
app.on('window-all-closed', () => {
appQuit();
})
app.on('will-quit', () => {
fileHandler.cleanTempDir();
})
let appQuiting = false;
function appQuit() {
if (!appQuiting) {
appQuiting = true;
tor.destroy();
if (mainWindow) { mainWindow.close(); }
if (tray) { tray.destroy(); tray = null; }
setTimeout(() => {
app.quit();
process.exit();
}, 1000 * 3); // wait for Tor to exit safely
}
}
//// ------------ Main windows ------------ ////
let mainWindow
let mainWindowHided = false;
let mainWindowSetting = {
width: 800, height: 700,
minWidth: 350, minHeight: 460,
backgroundColor: '#ffffff',
resizable: true,
webPreferences: {
sandbox: true,
preload: path.join(__dirname, 'ui/src/preload.js')
}
};
function openMainWindow() {
if (!mainWindow) {
mainWindow = new BrowserWindow(mainWindowSetting);
mainWindow.setMenu(null);
//Security: set a fake proxy to prevent the app from connecting to internet
mainWindow.webContents.session.setProxy({ proxyRules: fakeProxy }, () => {
mainWindow.loadURL(`file://${__dirname}/ui/index.html`);
});
//Security: prevent the page from navigating to another web page.
mainWindow.webContents.on('will-navigate', (event, url) => { event.preventDefault(); })
mainWindow.webContents.on('will-redirect', (event, url) => { event.preventDefault(); })
mainWindow.once('ready-to-show', () => { });
mainWindow.on('close', (event) => {
if (!appQuiting && config.getSetting().minimizeToTray && tor.getSuccess()) {
event.preventDefault();
hideMainWindow();
}
else {
mainWindow = null
}
return false;
});
//mainWindow.webContents.openDevTools();
}
}
function hideMainWindow() {
if (mainWindow) { mainWindow.hide(); }
mainWindowHided = true;
notification.clearHistory();
}
function showMainWindow() {
mainWindowHided = false;
if (mainWindow) { mainWindow.show(); }
}
//// ------------ Tray ------------ ////
let tray = null
app.on('ready', () => {
tray = new Tray(`${__base}/data/logoTray.png`);
const contextMenu = Menu.buildFromTemplate([
{ label: langs.get('TrayMenuOpen'), click: () => { showMainWindow(); } },
{ label: langs.get('TrayMenuQuit'), click: () => { appQuit(); } }
])
tray.on('double-click', () => { showMainWindow(); })
tray.setContextMenu(contextMenu);
})
//// ------------ Notifications ------------ ////
netUserList.event.on('userSocketBothConnected', (address) => {
if (mainWindowHided && config.getSetting().notification) {
if (parser.isMyHostname(address)) {
notification.notify("", "Tor", langs.get('NotificationConnected'));
}
else {
notification.newConnection(address);
}
}
})
netUserList.event.on('userMessage', (address, message, options) => {
if (mainWindowHided && config.getSetting().notification) {
notification.newMessage(address, message);
}
});
notification.event.on('click', (address) => {
showMainWindow();
})
//// ------------ Core ------------ ////
async function boot() {
await netServer.start();
tor.event.once('success', () => {
setInterval(autoAddUser, 1000 * 0.1);
});
tor.event.once('fail', (err) => { debug.log(err); });
tor.start();
}
function autoAddUser() {
netUserList.addUserFromFriendList();
}