-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
274 lines (248 loc) · 9.4 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
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
/**
* Virtuosoft - Devstria Personal Web (a localhost development server)
*
* TODO: rewrite everything using VueJS :)
*/
// Initialize our application
const app = require('electron').app;
global.ipcMain = require('electron').ipcMain;
global.Settings = require('./settings.js');
global.Util = require('./util.js');
global.VMS = require('./vms.js');
global.Window = require('./window.js');
global.Tray = require('./tray.js');
global.Menu = require('electron').Menu;
app.on('ready', () => {
// On Windows, check for Hyper-V, if not enabled ask to enable/reboot
if (process.platform === 'win32') {
let isHyperVEnabled = false;
try {
const { execSync } = require('child_process');
const stdout = execSync('powershell -Command get-service | findstr vmcompute');
if (stdout.indexOf('Hyper-V') != -1) {
isHyperVEnabled = true;
}
}catch(err) {
isHyperVEnabled = false;
}
if (!isHyperVEnabled) {
// Display message to user to enable Hyper-V
const { dialog } = require('electron');
const options = {
type: 'info',
title: 'Devstia Personal Web - Hyper-V is not enabled',
message: "Hyper-V is not enabled, please enable it.\n\nClick Windows' Start button, type \"Turn Windows Features on or off\", check Hyper-V.",
buttons: ['OK']
};
dialog.showMessageBox(null, options).then( (response) => {
app.quit();
});
return;
}
}
// Read our settings
const pwSettings = global.Settings.read();
Window.registerUIEvents();
// Copy over our user customizable scripts folder
Util.appFolder = pwSettings.appFolder;
if (Util.allowOneInstance()) return;
Util.copyScripts();
// Hide pull down menus
const customMenu = Menu.buildFromTemplate([]);
// Create our tray icon
VMS.pwSettings = pwSettings;
function createTray() {
Tray.create();
Tray.on('localhost', () => {
showLocalhost();
});
Tray.on('terminal', () => {
showTerminal();
});
Tray.on('files', () => {
showFiles();
});
Tray.on('settings', () => {
showSettings();
});
Tray.on('quit', () => {
global.doQuitting();
});
}
createTray();
// Respond to VMS errors and quit
function VMSError(msg) {
Window.show('./web/error.html');
Window.setElmTextById('error', msg.error);
}
VMS.on('downloadError', VMSError);
VMS.on('extractError', VMSError);
VMS.on('startupError', VMSError);
VMS.on('showSettings', () => { showSettings(); });
// Show the main application window state
var vms_state = '';
function showWindow() {
vms_state = global.VMS.state();
// Download compatible VMS (virtual machine server) runtime
if (vms_state == 'download') {
Window.show('./web/download.html');
VMS.on('downloadProgress', (msg) => {
let percent = msg.value;
if (isNaN(percent)) percent = 0;
Window.setElmTextById('txtDLPercent', percent + '%');
});
VMS.on('downloadComplete', () => {
showWindow(); // Done, re-check state, and extract
});
VMS.download();
}
// Extract the VMS archive runtime
if (vms_state == 'extract') {
Window.show('./web/extract.html');
VMS.on('extractProgress', (msg) => {
let percent = msg.value;
if (isNaN(percent)) percent = 0;
Window.setElmTextById('txtExtractPercent', percent + '%');
});
VMS.on('extractComplete', () => {
showWindow(); // Done, re-check state, and start
});
VMS.extract();
}
// Start the VMS runtime
if (vms_state == 'startup') {
VMS.on('startupComplete', (msg) => {
if (msg.value == false) { // Restarted = false
showWindow();
}
});
VMS.on('certsKeysPublished', () => {
Tray.setMenuState('localhost', true);
Tray.setMenuState('terminal', true);
Tray.setMenuState('files', (pwSettings.fsMode.toLowerCase() != 'none'));
});
VMS.startup();
}
// Running state, enable menu items
if (vms_state == 'running') {
Tray.setMenuState('settings', true);
showSettings();
}
}
showWindow();
// Show the localhost webpage
global.showLocalhost = function() {
// Write trusted token for auto-login, this verifies the source of the request
const altContent = global.Util.uuidv4().toString();
global.VMS.sudo("echo '" + altContent + "' > /tmp/alt.txt");
const shell = require('electron').shell;
shell.openExternal('http://localhost/?alt=' + altContent);
}
// Show a terminal instance
global.showTerminal = function() {
const { spawn } = require('child_process');
const path = require('path');
const pwSettings = Settings.read();
const runtimePath = path.join(__dirname, 'runtime', process.platform + '_' + process.arch, 'bin')
+ path.delimiter + `${process.env.PATH}${path.delimiter}`;
let scriptTerminal = null;
if (process.platform === 'win32') {
scriptTerminal = path.join(pwSettings.appFolder, 'scripts', 'terminal.bat');
}else{
scriptTerminal = path.join(pwSettings.appFolder, 'scripts', 'terminal.sh');
}
const p = spawn(scriptTerminal, [pwSettings.sshPort.toString()], {
cwd: path.dirname(scriptTerminal),
detached: true,
stdio: 'ignore',
env: { PATH: runtimePath }
});
p.unref();
}
// Show files from filesystem
global.showFiles = function() {
const os = require('os');
if (os.platform() === 'darwin') {
// Samba mount for macOS, works well and is fast.
const { exec } = require('child_process');
const pwSettings = Settings.read();
let cmd = `[ -n "$(mount -t smbfs | grep '/tmp/devstia')" ] && { open /tmp/devstia; } `;
cmd += '|| { rm -rf /tmp/devstia; mkdir -p /tmp/devstia; mount -t smbfs //devstia:';
cmd += pwSettings.pwPass + '@localhost/Devstia /tmp/devstia && open /tmp/devstia; }';
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`Error mounting samba: ${error.message}`);
return;
}
});
}else if (os.platform() === 'win32') {
// WebDAV mount for Windows, works well and is fast.
const { execSync } = require('child_process');
const pwSettings = Settings.read();
try {
execSync('net use P: https://webdav-devstia.dev.pw /user:devstia "' + pwSettings.pwPass + '"');
execSync(`powershell -Command "$a = New-Object -ComObject shell.application; $a.NameSpace('P:\\').self.name = 'Devstia'"`);
}catch(err) {
console.error(`Error mounting webdav: ${err.message}`);
}
execSync("IF EXIST P:\\ (start P:)");
}else{
// TODO: Support for Linux
}
}
// Show the settings window
function showSettings() {
Window.show('./web/settings.html');
}
// Quit the application
global.doQuitting = function() {
VMS.quitting = true;
Tray.tray.destroy();
const os = require('os');
if (os.platform() === 'darwin') {
// Unmount Samba share for macOS
if (pwSettings.fsMode.toLowerCase() == 'samba') {
const { exec } = require('child_process');
let cmd = 'umount /tmp/devstia && rm -rf /tmp/devstia';
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`Error unmounting samba: ${error.message}`);
return;
}
});
}
}else if (os.platform() === 'win32') {
// Unmount WebDAV share for Windows
if (pwSettings.fsMode.toLowerCase() == 'webdav') {
const { exec } = require('child_process');
let cmd = 'net use P: /delete';
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`Error unmounting webdav: ${error.message}`);
return;
}
});
}
}else{
// TODO: Support for Linux
}
VMS.shutdown(function() {
app.quit();
});
}
// When the dock icon is clicked, show the settings window
app.on('activate', () => {
if (global.Window.win != null) {
global.Window.win.focus();
}else{
showSettings();
}
});
});
// Prevent the application from closing when all windows are closed
app.on('window-all-closed', (event) => {
if (Window.quitOnClose != true) event.preventDefault();
});
app.on('before-quit', () => {
Window.close();
});