forked from shinylightdev/web-proxy-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
147 lines (112 loc) · 3.83 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
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
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);
const { spawn } = require('child_process');
//learning model for predicting sites that are not on the whitelist
function predictUsingMLModel(domain) {
const process = spawn('python3', ['ml/model_prediction.py', domain]);
process.stdout.on('data', function (data) {
console.log('Pipe data from python script ...');
console.log(data.toString());
});
}
// =============================================
// 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) => {
clientToProxySocket.once("data", (buffer) => {
const details = proxyHandler.GetDetailsFromBuffer(buffer);
const domain = details.domain;
predictUsingMLModel(domain);
proxyHandler.RunForEachConnection(clientToProxySocket, buffer, whiteListDomains);
});
});
function RunForEachConnection(clientToProxySocket, buffer, whiteListDomains) {
let buff = GetDetailsFromBuffer(buffer);
let proxyToServerSocket = net.createConnection(
{
host: buff.domain,
port: buff.port,
},
() => {
let isOnWhiteList = Utils.IsFoundInWildCardEntries(whiteListDomains, buff.domain);
if (isOnWhiteList) {
if (buff.isHttps) {
AllowSecureRequest(clientToProxySocket, proxyToServerSocket);
}
else {
AllowUnsecureRequest(clientToProxySocket,proxyToServerSocket,buffer);
}
}
else {
BlockRequest(clientToProxySocket, proxyToServerSocket);
}
let logEntry = GetDetailsFromBuffer(buffer, isOnWhiteList);
DB().insert("LogEntry", logEntry);
}
);
// Rest of the code...
}
// =============================================
// 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
}));