-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
240 lines (198 loc) · 7.87 KB
/
server.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
"use strict"
/* ---------------------------------------------------------------------------
Log-HTTP2File - https://github.com/fabrom/log-http2file
Simple HTTP server to log POST request content to file
Author: Fabrice Romand <[email protected]>
README for more information
---------------------------------------------------------------------------- */
// Requirements ----
const http = require('http');
const fs = require('fs');
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
const tmpdir = require('os').tmpdir();
const path = require('path');
// Variables declaration ----
var prg_package = null;
var port = (process.env.npm_package_config_port) ? process.env.npm_package_config_port : 8142;
var nb_workers = (numCPUs/2)+1;
var output_file = path.join(tmpdir, 'message.log');
var output_file_pid = output_file + '.pid';
// Retrieve request source address
function getRemoteAddress(req) {
if (req.headers['x-forwarded-for']) {
return req.headers['x-forwarded-for'];
}
return req.connection.remoteAddress;
}
// Retrieve request user-agent
function getRemoteAgent(req) {
if (req.headers['user-agent']) {
return req.headers['user-agent'];
}
return null;
}
// Retrieve remote user
function getRemoteUser(req) {
if (req.headers['remote_user']) {
return req.headers['remote_user'];
}
return null;
}
// Return Object in string is JSON, null otherwise
function isJSON(jsonString) {
try {
var json = JSON.parse(jsonString);
} catch (exception) {
json = null;
}
return json;
}
// Go through all workers
function eachWorker(callback) {
for (var id in cluster.workers) {
callback(cluster.workers[id]);
}
}
// Fork cluster
function setWorkers() {
for (var i = 1; i <= nb_workers; i++) {
cluster.fork();
}
}
// Main -----------------------------------------------------------------------
function startServer(onReadyCallBack) {
const program = require('commander');
prg_package = JSON.parse(fs.readFileSync(path.join(__dirname,'package.json'), 'utf8'));
program
.version(prg_package.version)
.description(prg_package.description)
.usage('[options] <outputfile>')
.option('-w, --workers <n>', 'how many listening workers', parseInt)
.option('-p, --port <n>', 'listening port', parseInt)
.arguments('<outputfile>')
.action(function (outputfile) {
output_file = outputfile;
})
.parse(process.argv);
if (cluster.isMaster) {
console.log(prg_package.name + " v." + prg_package.version + ' - ' + prg_package.description);
console.log("Author: " + prg_package.author + " - Homepage: " + prg_package.homepage);
console.log("Report bugs on " + prg_package.bugs);
console.log(`Master process id: ${process.pid}`);
nb_workers = program.workers || nb_workers;
port = program.port || port;
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} exit with code: ${code} (signal: ${signal})`);
});
cluster.on('listening', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} now listening on port ${port} and writing to ${output_file}...`);
});
output_file_pid = output_file + '.pid';
fs.writeFileSync(output_file_pid, process.pid);
process.on('SIGHUP', function() {
eachWorker(function(worker) {
worker.kill('SIGHUP');
})
var d = new Date();
var d1 = d.toISOString();
var d2 = d1.substr(0,19).replace(/-/g,'').replace(/:/g,'').replace('T','');
var newdir = path.dirname(output_file);
var newname = path.basename(output_file, path.extname(output_file)) + '_' + d2 + path.extname(output_file);
fs.rename(output_file, path.join(newdir, newname), function() {
console.log(`${path.basename(output_file)} renamed to ${newname}`);
setWorkers();
});
});
process.on('SIGINT', function() {
eachWorker(function(worker) {
worker.kill('SIGINT');
});
});
process.on('SIGTERM', function() {
eachWorker(function(worker) {
worker.kill('SIGTERM');
});
});
process.on('SIGBREAK', function() {
eachWorker(function(worker) {
worker.kill('SIGBREAK');
});
});
setWorkers();
process.on('exit', (code) => {
fs.unlinkSync(output_file_pid);
});
if (onReadyCallBack) onReadyCallBack();
} else {
fs.open(output_file, 'a', function(err, flogd) {
if (err) {
console.error("can't open output file");
cluster.worker.kill();
} else {
const server = http.createServer((request, response) => {
if (request.method !== 'POST') {
response.statusCode = 405;
response.end();
} else {
var body = '';
request.on('data', function (data) {
body += data;
// 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
if (body.length > 1e6) {
// FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST
request.connection.destroy();
}
});
request.on('end', function () {
var remote_address = getRemoteAddress(request);
var remote_user = getRemoteUser(request);
var body_object = isJSON(body);
var remote_object = {
address: remote_address,
user: remote_user,
agent: getRemoteAgent(request)
};
if (body_object instanceof Object) {
body_object.remote = remote_object;
body = JSON.stringify(body_object);
};
fs.write(flogd, body + '\n', function(err, written, string) {
if (err) {
console.error("can't write to output file");
}
response.statusCode = 200;
response.end();
});
var continuation = (body.length > 128) ? "..." : "";
var message = body.substr(0, 128);
console.log(Date.now() +
" [" + remote_address +
"][" + cluster.worker.process.pid +
"] " + message + continuation
);
});
}
});
server.on('clientError', (err, socket) => {
socket.statusCode = 400;
socket.end();
});
server.on('close', (err, socket) => {
fs.close(flogd);
});
server.listen(port);
}
});
}
}
function stopServer() {
process.kill(process.pid, 'SIGTERM');
}
function renewServer() {
process.kill(process.pid, 'SIGHUP');
}
module.exports = exports
exports.start = startServer;
exports.stop = stopServer;
exports.renew = renewServer;