Repository navigation
Expand file tree
/
Copy pathmain.ts
More file actions
212 lines (164 loc) · 5.86 KB
/
Copy pathmain.ts
File metadata and controls
212 lines (164 loc) · 5.86 KB
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
import { app, BrowserWindow, ipcMain, screen } from 'electron';
import { autoUpdater } from 'electron-updater';
import * as log from 'electron-log';
import * as path from 'path';
import * as url from 'url';
import { exec } from 'child_process';
import { sys } from 'ping';
class Item {
title: string;
hostname: string;
username: string;
online_status: boolean;
}
export class Main {
args: string[];
serve: boolean;
items: Item[] = [];
interval: NodeJS.Timeout;
windows: BrowserWindow = undefined;
constructor() {
this.args = process.argv.slice(1);
this.serve = this.args.some(value => value === '--serve');
this.initIPCMainListener();
this.initAutoUpdater();
}
createWindow() {
const electronScreen = screen;
const size = electronScreen.getPrimaryDisplay().workAreaSize;
this.windows = new BrowserWindow({
x: 0, y: 0, width: size.width / 4 * 3, height: size.height / 4 * 3,
webPreferences: {
nodeIntegration: true,
allowRunningInsecureContent: (this.serve) ? true : false,
contextIsolation: false,
enableRemoteModule : true
}
});
this.windows.removeMenu();
if(this.serve) {
this.windows.webContents.openDevTools();
require('electron-reload')(__dirname, {
electron: require(`${__dirname}/node_modules/electron`)
});
}
this.windows.loadURL(this.serve ? 'http://localhost:4200' : url.format({
pathname: path.join(__dirname, 'dist/index.html'),
protocol: 'file:',
slashes: true
}));
this.windows.on('closed', () => this.windows = undefined);
setTimeout(() => {
this.checkWindowsTerminalIsInstalled();
}, 1000);
}
private checkWindowsTerminalIsInstalled() {
exec('where wt', (error, stdout, stderr) => {
if(!(error)) return;
this.windows.webContents.send('windows-terminal-not-found');
});
}
private initPingTask() {
this.interval = setInterval(() => {
this.items.forEach((item) => {
sys.probe(item.hostname, (isAlive) => {
this.windows.webContents.send('status', {
hostname: item.hostname,
isAlive: isAlive
});
});
});
}, 1000 * 10);
}
private initIPCMainListener() {
ipcMain.on('items', (event, args) => {
this.items = args;
});
ipcMain.on('ping-enabled', (event, args) => {
if(args) {
this.initPingTask();
} else {
clearInterval(this.interval);
}
});
ipcMain.on('open-winscp', (event, args) => {
const path = args.path;
const keypath = args.keypath
const username = args.username;
const hostname = args.hostname;
exec(`"${path}" sftp://${username}@${hostname} /privatekey="${keypath}"`, (error, stdout, stderr) => {
if(error) console.log(error);
});
});
ipcMain.on('open-terminal', (event, args) => {
const cmd = args.console;
const command = args.command;
const port = (command.includes(':') ? command.split(':')[1] : 22);
if(cmd === 'Powershell' || cmd === 'Git Bash' || cmd === 'CMD') {
const program = (cmd === 'Powershell' ? 'powershell' : cmd === 'CMD' ? 'cmd' : 'bash');
const parameter = (cmd === 'Powershell' ? '-command' : cmd === 'CMD' ? '/k' : '-c');
exec(`start ${program} ${parameter} "ssh ${command} -p ${port}"`, (error, stdout, stderr) => {
if(error) console.log(error);
});
}
if(cmd === 'Windows Terminal') {
exec(`wt new-tab -d "%cd%" -p "PowerShell" ssh ${command} -p ${port}`, (error, stdout, stderr) => {
if(error) console.log(error);
});
}
console.log(command);
});
ipcMain.on('open-terminals', (event, args) => {
const cmd = args.console;
const commands = args.commands;
if(cmd === 'Powershell' || cmd === 'Git Bash' || cmd === 'CMD') {
const program = (cmd === 'Powershell' ? 'powershell' : cmd === 'CMD' ? 'cmd' : 'bash');
const parameter = (cmd === 'Powershell' ? '-command' : cmd === 'CMD' ? '/k' : '-c');
commands.forEach((command: string) => {
const port = (command.includes(':') ? command.split(':')[1] : 22);
exec(`start ${program} ${parameter} "ssh ${command} -p ${port}"`, (error, stdout, stderr) => {
if(error) console.log(error);
});
});
}
if(cmd === 'Windows Terminal') {
const lines = [];
commands.forEach((command: string) => {
const port = (command.includes(':') ? command.split(':')[1] : 22);
lines.push(`new-tab -d "%cd%" -p "PowerShell" ssh ${command} -p ${port}`);
});
exec(`wt ${lines.join(';')}`, (error, stdout, stderr) => {
if(error) console.log(error);
});
}
console.log(commands);
});
}
private initAutoUpdater() {
autoUpdater.on('checking-for-update', () => log.info('[Updater] Checking for update...'));
autoUpdater.on('update-available', (info) => log.info('[Updater] Update available.'));
autoUpdater.on('update-not-available', (info) => log.info('[Updater] Update not available.'));
autoUpdater.on('error', (error) => log.info('[Updater] Error in auto-updater: ' + error.toString()));
autoUpdater.on('download-progress', (progress) => log.info(`[Updater] Downloading... ${progress.percent}`));
autoUpdater.on('update-downloaded', (info) => {
log.info('[Updater] Update downloaded; will install now');
autoUpdater.quitAndInstall();
});
autoUpdater.checkForUpdatesAndNotify();
}
}
const main = new Main();
try {
app.on('ready', () => setTimeout(() => main.createWindow(), 400));
app.on('window-all-closed', () => {
if (process.platform !== 'darwin')
app.quit();
});
app.on('activate', () => {
if (main.windows === null)
main.createWindow();
});
} catch (e) {
// Catch Error
// throw e;
}