-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
129 lines (114 loc) · 3.47 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
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
const SystemProxy = require('./app/bin/www')
const { ipcMain } = require('electron')
const setCrt = require('./app/system-proxy/set-crt')
const installCrt = require('./app/system-proxy/set-crt/install')
const { MainSendMsgToRender, MainMsgListener } = require('./app/utils/index')
const env = require('./config/env.json');
const { SYSTEM_TIP_CRT,
SYSTEM_CLICK_BTN,
CHILD_MSG_TO_MAIN,
SYSTEM_CLICK_NET_BTN_SUCCESS,
SYSTEM_CLICK_NET_BTN } = require('./config')
const isDev = env.NODE_ENV === 'development';
class CreateProxyServerAndInitUI {
// SystemProxy
constructor() {
this.mainWindow = null;
this.pid = null;
this.renderPid = null;
this.initWinidow();
}
installCrt(mainWindow) {
ipcMain.on(SYSTEM_CLICK_BTN, async (event, arg) => {
let msg = null;
try {
// 设置证书
await setCrt()
msg = '证书安装完成';
} catch (error) {
msg = '证书安装失败,请手动安装证书';
}
mainWindow.webContents.send(SYSTEM_TIP_CRT, msg)
})
ipcMain.on(SYSTEM_CLICK_NET_BTN, async (event, arg) => {
let msg = '测试1下';
await installCrt();
// try {
// // 设置证书
// await setCrt()
// msg = '证书安装完成';
// } catch (error) {
// msg = '证书安装失败,请手动安装证书';
// }
mainWindow.webContents.send(SYSTEM_CLICK_NET_BTN_SUCCESS, msg)
})
ipcMain.on('SYSTEM_PID', (event, arg) => {
this.renderPid = arg;
})
}
initWinidow() {
this.readyServer();
}
async createSystemProxyConfig(mainWindow) {
new SystemProxy(mainWindow, this)
}
async hideProcessWindow(mainWindowChild, mainWindow) {
const portContent = await MainMsgListener(mainWindowChild, CHILD_MSG_TO_MAIN);
this.pid = portContent.pid;
MainSendMsgToRender(mainWindow, 'PORT_REDIY', portContent);
}
createWindow (type) {
const mainWindowChild = new BrowserWindow({
width: 800,
height: 800,
show: false,
devTools: isDev ? true : false,
webPreferences: {
contextIsolation:false,
nodeIntegration: true
}
});
mainWindowChild.webContents.openDevTools();
const mainWindow = new BrowserWindow({
width: 1200,
devTools: isDev ? true : false,
height: 1000,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation:false,
nodeIntegration: true,
nodeIntegrationInSubFrames: true
}
});
const html = isDev ? 'index.html' : 'index-prod.html';
this.onEventChange(mainWindow)
mainWindow.loadFile(html);
mainWindowChild.loadFile('./child.html');
this.mainWindow = mainWindow;
this.installCrt(mainWindow);
this.createSystemProxyConfig(mainWindow);
this.hideProcessWindow(mainWindowChild, mainWindow);
}
onEventChange(mainWindow) {
// 不会更改标题
mainWindow.on('page-title-updated', (event) => {
event.preventDefault();
});
mainWindow.on('close', (event) => {
event.preventDefault();
app.quit();
});
}
readyServer() {
app && app.whenReady().then(() => {
if (BrowserWindow.getAllWindows().length === 0) this.createWindow()
})
app && app.on('window-all-closed', () => {
app.quit()
})
}
}
new CreateProxyServerAndInitUI()