-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
293 lines (243 loc) · 8.26 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
/**
* @file Application main file
* @author zdying
*/
'use strict';
const log = require('electron-log');
const electron = require('electron');
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
const path = require('path');
// const http = require('http');
const url = require('url');
const hiproxy = require('hiproxy');
const Server = hiproxy.Server;
const pluginRoot = path.join(app.getAppPath(), 'node_modules');
const {autoUpdater} = require('electron-updater');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
let pluginWindow;
// Deep linked url
let deeplinkingUrl;
// let deeplinkingUrl = 'hiproxy://plugin/noah/26527';
let hiproxyServer;
const {ipcMain} = electron;
global.hiproxy = hiproxy;
log.transports.file.level = 'info';
autoUpdater.logger = log;
log.info('v1.0.2 is coming...');
const App = {
initialize: function () {
this.initAppEvent();
this.initIPCEvent();
},
initAppEvent: function () {
var self = this;
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', this.onready.bind(this));
app.on('will-finish-launching', function () {
log.info('will-finish-launching event');
});
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
log.info('quit application.');
app.quit();
}
});
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
self.createWindow();
}
});
// Define custom protocol handler. Deep linking works on packaged versions of the application!
app.setAsDefaultProtocolClient('hiproxy');
app.on('open-url', function (event, _url) {
log.info('open-url event.', _url);
event.preventDefault();
deeplinkingUrl = _url;
log.info('log', 'app.event#open-url', _url);
log.info('app is ready:', app.isReady());
if (app.isReady()) {
this.handleDeepLinkURL(_url);
}
}.bind(this));
},
initIPCEvent: function () {
var self = this;
ipcMain.on('close-main-window', function () {
app.quit();
});
ipcMain.on('load-url', function (eve, url) {
mainWindow.loadURL(url);
});
ipcMain.on('start-hiproxy', function (eve, workspace) {
log.info('在main中启动hiproxy', workspace);
// Start a hiproxy server
self.loadHiproxyPlugins()
.then(function () {
self.startHiproxyServer({
workspace: workspace
});
})
.then(function () {
mainWindow.loadURL('http://127.0.0.1:6636/?from=start-up');
mainWindow.setSize(1250, 750);
mainWindow.center();
});
});
},
onready: function () {
log.info('on ready event.', deeplinkingUrl);
autoUpdater.on('checking-for-update', () => {
log.info('checking-for-update');
});
autoUpdater.on('update-available', (info) => {
log.info('update-available', info);
});
autoUpdater.on('update-not-available', (info) => {
log.info('update-not-available');
});
autoUpdater.on('error', (err) => {
log.info('error', err);
});
autoUpdater.on('download-progress', (progressObj) => {
log.info('download-progress', progressObj);
});
autoUpdater.on('update-downloaded', (info) => {
// You could call autoUpdater.quitAndInstall(); immediately
console.log('downloaded..');
});
autoUpdater.checkForUpdatesAndNotify();
if (!deeplinkingUrl) {
this.createWindow();
return;
}
if (deeplinkingUrl) {
this.handleDeepLinkURL(deeplinkingUrl);
}
},
handleDeepLinkURL: function (deeplinkingUrl) {
var obj = url.parse(deeplinkingUrl);
var hostname = obj.hostname;
switch (hostname) {
case 'plugin':
log.info('load plugin from deep linking url.');
log.info('plugin url:' + 'http://127.0.0.1:6636' + obj.path);
log.info('is hiproxy server null :', !hiproxyServer);
if (!hiproxyServer) {
this.loadHiproxyPlugins()
.then(function () {
log.info('hiproxy plugins load success.');
this.startHiproxyServer();
}.bind(this))
.then(function () {
log.info('hiproxy proxy server start success.');
this.createWindow();
mainWindow.setSize(1250, 750);
mainWindow.loadURL('http://127.0.0.1:6636/?from=open-plugin');
pluginWindow = new BrowserWindow({width: 840, height: 515});
pluginWindow.on('closed', () => {
pluginWindow = null;
});
// Load a remote URL
pluginWindow.loadURL('http://127.0.0.1:6636' + obj.path);
}.bind(this))
.catch(function (err) {
log.error('[error]', err);
log.info('[error]', err);
});
} else if (pluginWindow) {
pluginWindow.loadURL('http://127.0.0.1:6636' + obj.path);
}
break;
default:
log.info('unknow hostname: ' + hostname);
break;
}
},
createWindow: function () {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 600,
height: 380,
center: true,
resizable: false,
maximizable: false
});
// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'app', 'welcome.html'),
protocol: 'file:',
slashes: true
}));
// mainWindow.loadURL(
// 'http://127.0.0.1:6636/?from=start-up'
// );
// Open the DevTools.
// mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
// hiproxyServer && hiproxyServer.stop();
// mainWindow = null;
app.quit();
});
},
/**
* Load hiproxy plugins
*/
loadHiproxyPlugins: function () {
return hiproxy.plugin.getInstalledPlugins(pluginRoot).then(function (plugins) {
if (plugins && plugins.length > 0) {
hiproxy.plugin.loadPlugins(plugins, {});
}
});
},
/**
* Start hiproxy server
*
* @returns Promise
*/
startHiproxyServer: function (options) {
var Proxy = Server;
var cliArgs = options || {};
// var https = cliArgs.https;
var port = cliArgs.port || 6636;
var httpsPort = cliArgs.middleManPort || 10011;
var workspace = cliArgs.workspace || app.getAppPath();
var proxy = new Proxy(port, httpsPort, workspace);
global.hiproxyServer = hiproxyServer = proxy;
// log format
proxy.logger.on('data', function (level) {
log.info.call(log, ['[hiproxy]', '[' + level + ']', [].slice.call(arguments, 1)].join(' '));
});
return proxy.start(cliArgs).then(function (servers) {
// proxy.showStartedMessage();
// var open = cliArgs.open;
// var browser = open === true ? 'chrome' : open;
// browser && proxy.openBrowser(browser, proxy.localIP + ':' + port, cliArgs.pacProxy);
// proxy.addRule('rewrite', ['test.abc.com => {', ' location / {', ' echo "it works";', ' }', '}'].join('\n'));
// proxy.addRule('hosts', ['127.0.0.1:8000 eight.hiproxy.org', '127.0.0.1 hiproxy.org'].join('\n'));
return servers;
});
}
};
App.initialize();
process.on('uncaughtException', function (error) {
// Handle the error
log.info('[uncaughtException]', error);
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.