-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
95 lines (72 loc) · 2.41 KB
/
app.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
const net = require("net");
const proxyServer = net.createServer();
const constants = require('./proxy/constants').Constants;
const proxyHandler = require('./proxy/proxy.handlers');
const utils = require('./proxy/proxy.utils');
const dirPath = __dirname;
const whiteListDomains = utils.GetWhitelistDomains(dirPath);
// =============================================
// Let's open port and run server.
// =============================================
proxyServer.listen(constants.PROXY_PORT_NUMBER, () => {
console.log("Server running at http://localhost:" + constants.PROXY_PORT_NUMBER);
console.log("\n");
});
// =============================================
// For each connection.
// =============================================
proxyServer.on("connection", (clientToProxySocket) => {
proxyHandler.RunForEachConnection(clientToProxySocket, whiteListDomains);
});
// =============================================
// Problems with proxy server.
// =============================================
proxyServer.on("error", exitHandler.bind(null, {
exit: true, cleanup: true
}));
// =============================================
// Close connection.
// =============================================
proxyServer.on("close", () => {
console.log("Client Disconnected");
});
// =============================================
// Problems with this process.
// =============================================
process.on("uncaughtException", exitHandler.bind(null, {
exit: true, cleanup: true }
));
// So the program will not close instantly.
process.stdin.resume();
function exitHandler(options, exitCode) {
// Cleaning up.
if (options.cleanup) {
proxyServer.close();
// CLEAN UP HERE!! kill port?
// console.log("CLEANUP");
}
if (exitCode || exitCode === 0) {
console.log(exitCode);
// console.log("EXITCODE");
}
// Exiting...
if (options.exit) {
process.exit();
}
}
// https://stackoverflow.com/questions/14031763/doing-a-cleanup-action-just-before-node-js-exits
// do something when app is closing
process.on("exit", exitHandler.bind(null, {
cleanup: true
}));
// catches ctrl+c event
process.on("SIGINT", exitHandler.bind(null, {
exit: true, cleanup: true
}));
// catches "kill pid" (for example: nodemon restart)
process.on("SIGUSR1", exitHandler.bind(null, {
exit: true
}));
process.on("SIGUSR2", exitHandler.bind(null, {
exit: true
}));