-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
664 lines (619 loc) · 17.6 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/**
* Electron入口文件
* @description 多路取证
* @author Yuet
*/
const path = require('path');
const {
app,
ipcMain,
BrowserWindow,
dialog,
globalShortcut,
Menu,
MenuItem,
shell
} = require('electron');
const { WindowsBalloon } = require('node-notifier');
const cors = require('cors');
const { renderFile } = require('ejs');
const express = require('express');
const log = require('./src/renderer/log');
const { getConfigMenuConf } = require('./src/main/menu');
const {
loadConf,
existManufaturer,
readManufaturer,
runProc,
runFetch,
portStat,
writeNetJson,
writeReportJson,
useGPURender
} = require('./src/main/utils');
const {
all,
find,
findOne,
findByPage,
count,
insert,
remove,
update
} = require('./src/main/db-handle');
const api = require('./src/main/api');
const mode = process.env['NODE_ENV'];
const { resourcesPath } = process;
const cwd = process.cwd();
const appPath = app.getAppPath();
const server = express();
let httpPort = 9900;
let config = null;
let existManuJson = false;
let mainWindow = null;
let timerWindow = null; //计时
let sqliteWindow = null; //SQLite查询
let fetchRecordWindow = null; //采集记录
let reportWindow = null; //报告
let protocolWindow = null; //协议阅读
let imageVerifyWindow = null; //选图验证
let fetchProcess = null; //采集进程
let parseProcess = null; //解析进程
let imageOcrProcess = null; //图像OCR进程
let yunProcess = null; //云取服务进程
let appQueryProcess = null; //应用痕迹进程
let quickFetchProcess = null; //快速点验进程
let httpServerIsRunning = false; //是否已启动HttpServer
config = loadConf(mode, appPath);
existManuJson = existManufaturer(mode, appPath);
if (config === null) {
dialog.showErrorBox('启动失败', '配置文件读取失败, 请联系技术支持');
app.exit(0);
}
if (!existManuJson) {
dialog.showErrorBox('启动失败', 'manufaturer配置读取失败, 请联系技术支持');
app.exit(0);
}
if (!useGPURender()) {
app.commandLine.appendSwitch('no-sandbox');
app.commandLine.appendSwitch('disable-gpu');
app.commandLine.appendSwitch('disable-gpu-compositing');
app.commandLine.appendSwitch('disable-gpu-rasterization');
app.commandLine.appendSwitch('disable-gpu-sandbox');
app.commandLine.appendSwitch('disable-software-rasterizer');
app.commandLine.appendSwitch('--no-sandbox');
app.disableHardwareAcceleration();
log.info('禁用GPU渲染');
} else {
log.info('启用GPU渲染');
}
const manu = readManufaturer();
writeReportJson(config.reportType, config.hideCad);
var notifier = new WindowsBalloon({
withFallback: false,
customPath: undefined
});
//# 配置Http服务器相关
server.use(express.json());
server.use(express.urlencoded({ extended: true }));
server.use(
cors({
origin: '*',
methods: ['GET', 'POST'],
optionsSuccessStatus: 200
})
);
server.engine('html', renderFile);
server.set('views', path.join(__dirname, 'src/ejs'));
server.set('view engine', 'ejs');
/**
* 销毁所有窗口
*/
function destroyAllWindow() {
if (reportWindow !== null) {
reportWindow.destroy();
reportWindow = null;
}
if (fetchRecordWindow !== null) {
fetchRecordWindow.destroy();
fetchRecordWindow = null;
}
if (sqliteWindow !== null) {
sqliteWindow.destroy();
sqliteWindow = null;
}
if (timerWindow !== null) {
timerWindow.destroy();
timerWindow = null;
}
if (protocolWindow !== null) {
protocolWindow.destroy();
protocolWindow = null;
}
if (mainWindow !== null) {
mainWindow.destroy();
mainWindow = null;
}
}
/**
* 退出应用
*/
function exitApp(platform) {
if (platform !== 'darwin') {
globalShortcut.unregisterAll();
destroyAllWindow();
if (fetchProcess !== null) {
fetchProcess.kill(); //杀掉采集进程
}
if (parseProcess !== null) {
parseProcess.kill(); //杀掉解析进程
}
if (imageOcrProcess !== null) {
imageOcrProcess.kill(); //杀掉图像OCR进程
}
if (yunProcess !== null) {
yunProcess.kill(); //杀掉云服务进程
}
if (appQueryProcess !== null) {
appQueryProcess.kill(); //杀掉应用痕迹进程
}
if (quickFetchProcess !== null) {
quickFetchProcess.kill(); //杀掉快速点验进程
}
app.exit(0);
}
}
process.on('uncaughtException', (err) => {
log.error(`main.js UncaughtException: ${err.stack}`);
app.exit(1);
});
app.on('before-quit', () =>
//移除mainWindow上的listeners
mainWindow.removeAllListeners('close')
);
app.on('render-process-gone', (event, webContents, { reason, exitCode }) =>
log.error(`main.js RenderProcessGone: ${JSON.stringify({ reason, exitCode })}`)
);
const instanceLock = app.requestSingleInstanceLock();
if (!instanceLock) {
app.quit(0);
} else {
app.on('second-instance', () => {
//单例应用
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
mainWindow.show();
}
});
app.on('ready', () => {
(async () => {
if (!httpServerIsRunning) {
try {
httpPort = await portStat(config.httpPort ?? 9900);
//启动HTTP服务
server.use(api(mainWindow.webContents));
server.listen(httpPort, () => {
httpServerIsRunning = true;
console.log(`HTTP服务启动在端口${httpPort}`);
});
} catch (error) {
console.log(`HTTP服务启动失败:${error.message}`);
}
}
})();
sqliteWindow = new BrowserWindow({
title: 'SQLite',
width: 600,
height: 400,
show: false,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
javascript: true
}
});
sqliteWindow.loadFile(path.join(__dirname, './src/renderer/sqlite/sqlite.html'));
if (mode === 'development') {
sqliteWindow.openDevTools();
}
mainWindow = new BrowserWindow({
title: `${manu?.materials_name ?? '智能终端快速取证'}(${
manu?.materials_software_version ?? ''
})`,
icon: config.logo ? path.join(appPath, `../config/${config.logo}`) : undefined,
width: config.windowWidth ?? 1280, //主窗体宽
height: config.windowHeight ?? 800, //主窗体高
autoHideMenuBar: true, //隐藏主窗口菜单
center: config.center ?? true, //居中显示
minHeight: config.minHeight ?? 768, //最小高度
minWidth: config.minWidth ?? 960, //最小宽度
backgroundColor: '#d3deef',
webPreferences: {
webSecurity: false,
contextIsolation: false,
nodeIntegration: true,
javascript: true
}
});
if (mode === 'development') {
mainWindow.webContents.openDevTools();
mainWindow.loadURL(config.devPageUrl);
} else {
if (config.max <= 2) {
//采集路数为2路以下,默认最大化窗口
mainWindow.maximize();
}
mainWindow.loadFile(path.join(resourcesPath, 'app.asar.unpacked/dist/default.html'));
}
mainWindow.webContents.on('did-finish-load', async () => {
mainWindow.show();
if (timerWindow) {
timerWindow.reload();
}
});
mainWindow.webContents.addListener('new-window', (event) => event.preventDefault());
mainWindow.on('close', (event) => {
//关闭事件到mainWindow中去处理
event.preventDefault();
mainWindow.webContents.send('will-close');
});
timerWindow = new BrowserWindow({
title: '计时服务',
width: 600,
height: 400,
show: false,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
javascript: true
}
});
timerWindow.loadFile(path.join(__dirname, './src/renderer/timer/timer.html'));
if (mode === 'development') {
timerWindow.openDevTools();
}
fetchRecordWindow = new BrowserWindow({
width: 600,
height: 400,
show: false,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
javascript: true
}
});
fetchRecordWindow.loadFile(
path.join(__dirname, './src/renderer/fetchRecord/fetchRecord.html')
);
if (mode === 'development') {
fetchRecordWindow.openDevTools();
}
if (mode === 'development') {
timerWindow.webContents.openDevTools();
sqliteWindow.webContents.openDevTools();
fetchRecordWindow.webContents.openDevTools();
}
// #生产模式屏蔽快捷键(发布把注释放开)
if (mode !== 'development') {
globalShortcut.register('Control+R', () => {});
globalShortcut.register('Control+Shift+R', () => {});
globalShortcut.register('CommandOrControl+Shift+I', () => {});
}
// #默认菜单置空(发布把注释放开)
if (mode !== 'development') {
Menu.setApplicationMenu(null);
}
});
}
//#region 消息事件
//显示原生系统消息
ipcMain.on('show-notice', (_, { title, message }) =>
notifier.notify({
sound: true,
type: 'info',
title: title ?? '消息',
message: message ?? '有消息反馈请查阅'
})
);
//显示notification消息,参数为消息文本
ipcMain.on('show-notification', (_, args) => {
mainWindow.webContents.send('show-notification', args);
});
//显示窗口进度
ipcMain.on('show-progress', (_, show) => {
if (show) {
mainWindow.setProgressBar(1, {
mode: 'indeterminate'
});
} else {
mainWindow.setProgressBar(0, {
mode: 'none'
});
}
});
/**
* 重启应用
*/
ipcMain.on('do-relaunch', () => {
app.relaunch();
exitApp(process.platform);
});
//启动后台服务(采集,解析,云取证)
ipcMain.on('run-service', (_, tcpPort, ocrPort) => {
// runProc(
// fetchProcess,
// config.fetchExe ?? 'n_fetch.exe',
// path.join(appPath, '../../../', config.fetchPath ?? './n_fetch')
// );
runFetch(
fetchProcess,
config.fetchExe ?? 'n_fetch.exe',
path.join(appPath, '../../../', config.fetchPath ?? './n_fetch'),
mainWindow
);
runProc(
parseProcess,
config.parseExe ?? 'parse.exe',
path.join(appPath, '../../../', config.parsePath ?? './parse'),
['--listen_port', ocrPort.toString()]
);
runProc(imageOcrProcess, 'ImageOcr.exe', path.join(appPath, '../../../tools/ImageOcr'));
if (config.useServerCloud) {
//有云取功能,调起云RPC服务
runProc(
yunProcess,
config.yqExe ?? 'yqRPC.exe',
path.join(appPath, '../../../', config.yqPath ?? './yq'),
['-config', './agent.json', '-log_dir', './log']
);
}
if (config.useTraceLogin) {
//有应用痕迹查询,调起服务
runProc(
appQueryProcess,
config.appQueryExe ?? 'AppQuery.exe',
path.join(appPath, '../../../', config.appQueryPath ?? './AppQuery')
);
}
if (config.useQuickFetch) {
//有快速点验功能,调起服务
const quickFetchExePath = path.join(
appPath,
'../../../',
config.quickFetchPath ?? './QuickFetch'
);
runProc(
quickFetchProcess,
config.quickFetchExe ?? 'QuickFetchServer.exe',
quickFetchExePath,
{
cwd: quickFetchExePath,
stdio: 'ignore'
}
);
}
});
//退出应用
ipcMain.on(
'do-close',
(
event //mainWindow通知退出程序
) => exitApp(process.platform)
);
//显示阅读协议
ipcMain.on('show-image-verify', (event, url) => {
event.preventDefault();
if (imageVerifyWindow === null || imageVerifyWindow.isDestroyed()) {
imageVerifyWindow = new BrowserWindow({
width: 800,
height: 600,
show: true,
frame: true,
alwaysOnTop: true,
parent: mainWindow,
modal: true,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
javascript: true
}
});
imageVerifyWindow.setMenu(null);
imageVerifyWindow.loadURL(url);
} else {
imageVerifyWindow.show();
}
});
//启动&停止计时
ipcMain.on('time', (_, usb, isStart) => {
if (timerWindow !== null) {
timerWindow.webContents.send('time', usb, isStart);
}
});
//向主窗口发送计时时间
ipcMain.on('receive-time', (_, usb, timeString) => {
// console.log(`${usb}:${timeString}`);
if (mainWindow && mainWindow.webContents !== null) {
mainWindow.webContents.send('receive-time', usb, timeString);
}
});
//向主窗口发送采集结束以停止计时
ipcMain.on('fetch-over', (_, usb) => {
if (mainWindow && mainWindow.webContents !== null) {
mainWindow.webContents.send('fetch-over', usb);
}
});
//执行SQLite查询单位表
ipcMain.on('query-db', (_, ...args) => sqliteWindow.webContents.send('query-db', args));
//SQLite查询结果
ipcMain.on('query-db-result', (_, result) =>
mainWindow.webContents.send('query-db-result', result)
);
//发送进度消息
ipcMain.on('fetch-progress', (_, arg) => {
fetchRecordWindow.webContents.send('fetch-progress', arg);
mainWindow.webContents.send('fetch-progress', arg);
});
//采集完成发送USB号及日志数据
ipcMain.on('fetch-finish', (_, usb, log) =>
fetchRecordWindow.webContents.send('fetch-finish', usb, log)
);
//清除usb序号对应的采集记录
ipcMain.on('progress-clear', (_, usb) => fetchRecordWindow.webContents.send('progress-clear', usb));
//获取当前USB序号的采集进度数据
ipcMain.on('get-fetch-progress', (_, usb) =>
fetchRecordWindow.webContents.send('get-fetch-progress', usb)
);
//获取当前USB序号最新一条进度消息
ipcMain.on('get-last-progress', (_, usb) =>
fetchRecordWindow.webContents.send('get-last-progress', usb)
);
//消息发回LiveModal以显示采集进度
ipcMain.on('receive-fetch-progress', (_, fetchRecords) =>
mainWindow.webContents.send('receive-fetch-progress', fetchRecords)
);
//消息发回FetchInfo.tsx组件以显示最新一条进度
ipcMain.on('receive-fetch-last-progress', (_, fetchRecord) =>
mainWindow.webContents.send('receive-fetch-last-progress', fetchRecord)
);
//将FetchLog数据发送给入库
ipcMain.on('save-fetch-log', (_, log) => mainWindow.webContents.send('save-fetch-log', log));
//导出报告
ipcMain.on('report-export', (_, exportCondition, treeParams, msgId) => {
if (reportWindow === null) {
reportWindow = new BrowserWindow({
title: '报告导出',
width: 800,
height: 600,
show: false,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
javascript: true
}
});
reportWindow.loadFile(path.join(__dirname, './src/renderer/report/report.html'));
if (mode === 'development') {
reportWindow.webContents.openDevTools();
}
reportWindow.webContents.once('did-finish-load', () =>
reportWindow.webContents.send('report-export', exportCondition, treeParams, msgId)
);
} else {
reportWindow.webContents.send('report-export', exportCondition, treeParams, msgId);
}
});
//导出报告(批量)
ipcMain.on('report-batch-export', (_, batchExportTasks, isAttach, isZip, msgId) => {
if (reportWindow === null) {
reportWindow = new BrowserWindow({
title: '报告导出',
width: 800,
height: 600,
show: false,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
javascript: true
}
});
reportWindow.loadFile(path.join(__dirname, './src/renderer/report/report.html'));
reportWindow.webContents.openDevTools();
reportWindow.webContents.once('did-finish-load', () =>
reportWindow.webContents.send(
'report-batch-export',
batchExportTasks,
isAttach,
isZip,
msgId
)
);
} else {
reportWindow.webContents.send(
'report-batch-export',
batchExportTasks,
isAttach,
isZip,
msgId
);
}
});
ipcMain.on('update-export-msg', (_, args) =>
mainWindow.webContents.send('update-export-msg', args)
);
//导出报告完成
ipcMain.on('report-export-finish', (_, success, exportCondition, isBatch, msgId) => {
if (reportWindow !== null) {
reportWindow.destroy();
reportWindow = null;
}
shell.beep();
mainWindow.setProgressBar(0, {
mode: 'none'
});
mainWindow.webContents.send('report-export-finish', success, exportCondition, isBatch, msgId);
});
//显示阅读协议
ipcMain.on('show-protocol', (_, fetchData) => {
if (protocolWindow === null) {
protocolWindow = new BrowserWindow({
width: 800,
height: 350,
show: true,
frame: false,
resizable: false,
closable: false,
alwaysOnTop: true,
parent: mainWindow,
modal: true,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
javascript: true
}
});
protocolWindow.loadFile(path.join(__dirname, './src/renderer/protocol/protocol.html'));
protocolWindow.webContents.on('did-finish-load', () =>
protocolWindow.send('show-protocol', fetchData)
);
} else {
protocolWindow.show();
protocolWindow.send('show-protocol', fetchData);
}
});
//阅读协议同意反馈
ipcMain.on('protocol-read', (_, fetchData, agree) => {
mainWindow.send('protocol-read', fetchData, agree);
if (protocolWindow !== null) {
protocolWindow.destroy();
protocolWindow = null;
}
});
//左上角右键菜单
ipcMain.on('create-setting-menu', (_, position) => {
const menu = new Menu();
getConfigMenuConf(mainWindow.webContents).forEach((menuItem) => {
menu.append(new MenuItem({ ...menuItem }));
});
menu.popup(position);
});
//#endregion
//#region Nedb Handle
ipcMain.handle('db-all', all);
ipcMain.handle('db-find', find);
ipcMain.handle('db-find-one', findOne);
ipcMain.handle('db-find-by-page', findByPage);
ipcMain.handle('db-count', count);
ipcMain.handle('db-insert', insert);
ipcMain.handle('db-remove', remove);
ipcMain.handle('db-update', update);
//#endregion
ipcMain.handle('get-path', (_, type) => app.getPath(type));
ipcMain.handle('open-dialog', (_, options) => dialog.showOpenDialog(options));
ipcMain.handle('open-dialog-sync', (_, options) => dialog.showOpenDialogSync(options));
ipcMain.handle('write-net-json', (_, tcpPort, ocrPort) =>
writeNetJson(cwd, { apiPort: httpPort, tcpPort, ocrPort })
);