-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain-entry.js
187 lines (173 loc) · 6.61 KB
/
main-entry.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
const {
BrowserWindow,
app,
ipcMain
} = require('electron'),
path = require('path'),
mdns_client = require('./libs/mdns-client'),
app_register = require('./libs/app-register'),
mainDbApi = require('./libs/main-db-api'),
winStateUpdator = require('./libs/state-updater');
const remote_main = require('@electron/remote/main');
global.mainWindow = null;
const mainWindowId = 'main-window';
let mainDB, stateUpdator;
let gateway_resolve_task;
if (process.env.SOCKS5_ADDRESS) {
const onenet_port_url_pattern = /\.gw-port\.local\s*$/i;
if (process.env.SOCKS5_ADDRESS.match(onenet_port_url_pattern)) {
const dns = new (require('./libs/mdns-client'))();
gateway_resolve_task = dns.find(process.env.SOCKS5_ADDRESS).then(result => {
//incomplete, mostly unused ...
if (result) {
app.commandLine.appendSwitch('proxy-server', 'socks5://' + result.socks5_address + ':' + result.socks5_port);
if (!process.env.SOCKS5_LOCAL_DNS) {
app.commandLine.appendSwitch('host-resolver-rules', 'MAP * 0.0.0.0, EXCLUDE ' + result.socks5_address);
}
} else {
app.commandLine.appendSwitch('proxy-server', 'socks5://' + process.env.SOCKS5_ADDRESS + ':' + process.env.SOCKS5_PORT);
if (!process.env.SOCKS5_LOCAL_DNS) {
app.commandLine.appendSwitch('host-resolver-rules', 'MAP * 0.0.0.0, EXCLUDE ' + process.env.SOCKS5_ADDRESS);
}
}
});
} else {
app.commandLine.appendSwitch('proxy-server', 'socks5://' + process.env.SOCKS5_ADDRESS + ':' + process.env.SOCKS5_PORT);
if (!process.env.SOCKS5_LOCAL_DNS) {
app.commandLine.appendSwitch('host-resolver-rules', 'MAP * 0.0.0.0, EXCLUDE ' + process.env.SOCKS5_ADDRESS);
}
gateway_resolve_task = Promise.resolve();
}
} else {
gateway_resolve_task = Promise.resolve();
}
const windowAllClosed = () => {
if (!process.env.PRODUCTION_MODE) {
app_register.close();
}
stateUpdator.flush().then(() => {
return mainDB.close().then(() => {
if (process.platform != 'darwin') {
app.quit();
}
});
});
};
const startup = () => {
return gateway_resolve_task.then(() => {
try {
const flashPath = app.getPath('pepperFlashSystemPlugin');
if (flashPath) {
app.commandLine.appendSwitch('ppapi-flash-path', flashPath);
}
} catch (err) {
console.log(err);
console.log('\nSystem wide pepper flash plugin failed to be initialized, flash will not be available on web pages ...');
}
ipcMain.on('mdns-query', (e, q) => {
const dns = new mdns_client();
dns.find(q.hostname, 'SRV').then(resp => {
e.sender.send('mdns-query-ack', {
ok: true,
response: resp
});
}).catch(err => {
e.sender.send('mdns-query-ack', {
ok: false,
error: err
});
});
});
ipcMain.on('full-screen-mode', (e, d) => {
global.mainWindow.setFullScreen(d.on);
});
const createWindow = initBounds => {
const wopts = {
width: initBounds ? initBounds.width : 1530,
height: initBounds ? initBounds.height : 920,
frame: false,
icon: __dirname + '/images/v-net-browser.png',
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false,
webviewTag: true
}
};
if (initBounds) {
wopts.x = initBounds.loc_x;
wopts.y = initBounds.loc_y;
}
remote_main.initialize();
global.mainWindow = new BrowserWindow(wopts);
//global.mainWindow.webContents.openDevTools();
remote_main.enable(global.mainWindow.webContents);
//global.mainWindow.loadURL('file://' + path.join(__dirname, 'browser.html'));
global.mainWindow.loadFile(path.join(__dirname, 'browser.html'));
global.mainWindow.webContents.on('did-finish-load', () => {
const copts = {
has_context: !!process.env.SOCKS5_ADDRESS
};
if (copts.has_context) {
copts.context_title = process.env.CONTEXT_TITLE;
copts.start_url = process.env.START_URL;
copts.socks5_address = process.env.SOCKS5_ADDRESS;
copts.socks5_port = process.env.SOCKS5_PORT;
}
global.mainWindow.webContents.send('runtime-context-update', copts);
});
global.mainWindow.on('resize', () => {
stateUpdator.updateWindowState(mainWindowId, {
bounds: global.mainWindow.getBounds()
})
});
global.mainWindow.on('move', () => {
stateUpdator.updateWindowState(mainWindowId, {
bounds: global.mainWindow.getBounds()
})
});
global.mainWindow.on('enter-full-screen', () => {
});
global.mainWindow.on('leave-full-screen', () => {
});
global.mainWindow.on('closed', () => {
global.mainWindow = null;
});
};
const run = () => {
mainDB = new mainDbApi({
home: app.getPath('appData'),
path: app.getName() + '/databases-2'
});
mainDB.open().then(() => {
stateUpdator = new winStateUpdator(mainDB);
mainDB.find({
table: 'window-states',
predicate: '"window_id"=\'' + mainWindowId + '\''
}).then((wstate) => {
createWindow(wstate);
});
});
};
if (!process.env.PRODUCTION_MODE) {
app.on('ready', () => {
app.on('window-all-closed', windowAllClosed);
app_register.regist(app).then(ok => {
if (ok) {
run();
}
})
});
} else {
run();
}
});
};
if (!process.env.PRODUCTION_MODE) {
startup();
} else {
module.exports = {
startup: startup,
teardown: windowAllClosed
};
}