Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
193 lines (182 loc) · 6.14 KB
/
Copy pathindex.js
File metadata and controls
193 lines (182 loc) · 6.14 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
const {
app,
Menu,
MenuItem,
Tray,
dialog
} = require('electron'),
path = require('path'),
os = require('os'),
child_proc = require('child_process');
const refresh_seconds = 10;
const
config = require('./config.json'),
booter = new (require('./libs/bootstrapper'))({
refresh_seconds: refresh_seconds
});
if (config.production_mode && config.packaged) {
const cdir = path.dirname(process.execPath);
if (process.cwd() !== cdir) {
process.chdir(cdir);
}
}
let tray = null;
let mainEntry = undefined;
if (!process.env.PRODUCTION_MODE) {
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit();
return;
} else {
app.on('second-instance', (cmdl, wkdir) => {
if (global.mainWindow) {
if (global.mainWindow.isMinimized())
global.mainWindow.restore();
global.mainWindow.focus();
}
});
}
}
const launcher = function (m, w, e) {
const gw = this;
if (!gw.serving)
return;
if (gw.auth_required) {
dialog.showErrorBox('Stop', 'The current browser does not support going through user authenticated V-NET gateway port!');
return;
}
const child_opts = {
cwd: process.cwd(),
detached: false,
shell: false,
env: {}
};
if (gw.name) {
child_opts.env.CONTEXT_TITLE = gw.name;
child_opts.env.SOCKS5_ADDRESS = gw.ip;
child_opts.env.SOCKS5_ADDRESS6 = gw.ip6;
child_opts.env.SOCKS5_PORT = gw.port;
};
const keys = Object.keys(process.env);
keys.forEach(k => {
child_opts.env[k] = process.env[k];
});
if (!config.production_mode) {
gw.proc = child_proc.spawn(path.join(process.cwd(), 'node_modules/electron/dist/electron' + (os.platform() === 'win32' ? '.exe' : '')), ['main-entry.js'], child_opts);
} else {
child_opts.env.PRODUCTION_MODE = true;
if (!config.packaged) {
gw.proc = child_proc.spawn(path.join(process.cwd(), 'node_modules/electron/dist/electron' + (os.platform() === 'win32' ? '.exe' : '')), ['index.js'], child_opts);
} else {
gw.proc = child_proc.spawn(path.join(process.cwd(), config.package_name + (os.platform() === 'win32' ? '.exe' : '')), [], child_opts);
}
}
gw.proc.on('error', err => {
console.log('error', err);
});
gw.proc.on('exit', function (code, sig) {
this.started = false;
this.proc = undefined;
console.log(`process exited with code ${code}, sig: ${sig}`);
}.bind(gw));
if (gw.proc.stdout) {
gw.proc.stdout.on('data', (data) => {
console.log(`local-app out: ${data}`);
});
gw.proc.stderr.on('data', (data) => {
console.error(`local-app err: ${data}`);
});
}
gw.started = true;
};
let gateway_ports = [];
let last_update = undefined;
app.on('window-all-closed', () => {
if (mainEntry) {
mainEntry.teardown();
}
});
const updater = () => {
return booter.update_ports().then(r => {
const old_ports = gateway_ports.map(p => p);
gateway_ports = [];
last_update = (new Date()).getTime();
r.ports.forEach(gwp => {
const old = old_ports.find(p => p.name === gwp.name);
if (old) {
gwp.proc = old.proc;
gwp.started = old.started;
}
gateway_ports.push(gwp);
});
setTimeout(function () {
booter.close();
}.bind(r), 10000);
});
};
const local_browser = {
started: false
};
app.on('ready', () => {
if (!process.env.PRODUCTION_MODE) {
booter.update_ports().then(r => {
last_update = (new Date()).getTime();
const getMenu = gw_lst => {
const contextMenu = new Menu();
contextMenu.append(new MenuItem({
icon: __dirname + '/images/blue-dot.png',
label: 'Local Browsing',
sublabel: 'Start browsing local resources ...',
click: launcher.bind(local_browser)
}));
contextMenu.append(new MenuItem({
type: 'separator'
}));
gw_lst.sort((a, b) => a.name > b.name ? 1 : -1).filter(gw => gw.serving).forEach(gw => {
contextMenu.append(new MenuItem({
icon: gw.auth_required ? __dirname + '/images/green-locked-dot.png' : __dirname + '/images/green-dot.png',
label: gw.name + (gw.netname ? ' in [' + gw.netname + ']' : ''),
sublabel: gw.descr || ' ... ',
click: launcher.bind(gw)
}));
});
contextMenu.append(new MenuItem({
type: 'separator'
}));
contextMenu.append(new MenuItem({
label: 'Exit',
click: (m, w, e) => {
app.quit();
}
}));
return contextMenu;
};
r.ports.forEach(gwp => {
gateway_ports.push(gwp);
});
tray = new Tray(__dirname + '/images/main-icon.png');
tray.on('click', (e, b) => {
const now = (new Date()).getTime();
if (now - last_update > refresh_seconds * 1000) {
updater().then(() => {
tray.setContextMenu(getMenu(gateway_ports));
tray.popUpContextMenu();
});
} else {
tray.setContextMenu(getMenu(gateway_ports));
tray.popUpContextMenu();
}
});
tray.on('right-click', e => {
e.preventDefault();
});
tray.setToolTip('V-NET Trans-LAN Browser');
setTimeout(function () {
booter.close();
}.bind(this), 10000);
});
} else {
mainEntry = require('./main-entry');
mainEntry.startup();
}
});