-
Notifications
You must be signed in to change notification settings - Fork 112
/
socketQueue.js
318 lines (257 loc) · 7.97 KB
/
socketQueue.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
var socketServer = require('./lib/socketServer');
var helpers = require('./lib/helpers');
var argv = require('optimist').argv;
var cfgParams = require('./lib/cfgParams');
var winston = require('winston');
require('winston-logstash');
var fs = require('fs');
var logger = false;
var fileTransport = null;
// Available runtime parameters configuration
global.validParams = require('./parameters.js').params;
// This one displays configuration help
String.prototype.repeat = function(num) {
return new Array( num + 1 ).join( this );
}
var c = {};
var defaults = {};
// Loading command line parameters
for (var name in argv) {
if (name == '_' || name.substring(0,1) == '$') continue;
var val = cfgParams.read(name, argv[name]);
if (val !== null) {
c[name] = val;
} else {
console.log("Configuration error: invalid parameter " + name + ". See node socketQueue.js --help to see available parameters\n");
process.exit(0);
}
}
// Read file config
if (c.c) {
if (!fs.existsSync(c.c)) {
console.log("Configuration file " + c.c + " does not exist!");
process.exit(0);
}
var cfgData = JSON.parse(fs.readFileSync(c.c, 'utf8'));
for (var name in cfgData) {
var val = cfgParams.read(name, cfgData[name]);
if (val !== null) {
c[name] = val;
} else {
console.log("Configuration file: invalid parameter " + name + ". See node socketQueue.js --help to see available parameters\n");
process.exit(0);
}
}
}
// Applying defaults
for (var name in validParams) {
if (validParams[name]['default'] && !c.hasOwnProperty(name)) {
c[name] = cfgParams.read(name, validParams[name]['default']);
defaults[name] = true;
}
}
global.c = c;
global.dd = dd;
global.defaultSyntax = c.hostConfig;
// Display help screen
if (c.help) {
cfgParams.showHelp();
process.exit(0);
}
// Display help in JSON format
if (c.helpJson) {
cfgParams.showHelpJSON();
process.exit(0);
}
// Checking parameters
if ((!c.upstreamHost && !c.upstreamListenPort && !c.testClients && !c.echoServerPort) || c.upstreamHost && (!c.upstreamPort || (!c.listenPort && !c.listenHttpPort))) {
console.log("Usage socketQueue.js [options]");
console.log("Run socketQueue.js --help to see help");
process.exit(0);
} else if (c.testClients && !c.testTargetHost) {
console.log("No test target host specified. Aborting");
process.exit(0);
} else if (c.testClients && !c.testTargetPort) {
console.log("No test target port specified. Aborting");
process.exit(0);
} else if (c.logstashHost && !c.logstashPort) {
console.log("No Logstash port specified. Aborting");
process.exit(0);
}
// Overrides
if (c.overrides) {
params = c.overrides.split(',');
global.overrides = {};
for (var i in params) {
var parts = params[i].split(':');
var item = {type: parts[1]};
if (parts[2] !== 'undefined') item['length'] = parseInt(parts[2]);
global.overrides[parts[0]] = item;
}
}
// Header format
if (c.headerFormat) {
params = c.headerFormat.split(',');
global.headerFormat = [];
for (var i in params) {
var parts = params[i].split(':');
var item = {name: parts[0], type: parts[1], length: parseInt(parts[2])};
if (parts[3] !== 'undefined') item['default'] = parts[3];
global.headerFormat.push(item);
}
}
// Setting up Winston debug
if (c.v) c.debugLevel = 'info';
if (c.vv) c.debugLevel = 'verbose';
if (c.debugLevel) {
var customLevels = {
common: 0,
private: 1
};
var transports = [];
var dateFunc = function() { var d = new Date(); return d.getFullYear() + '-' + ('0' + (d.getMonth() + 1)).substr(-2) + '-' + ('0' + d.getDate()).substr(-2) + ' ' + ('0' + d.getHours()).substr(-2) + ':' + ('0' + d.getMinutes()).substr(-2) + ':' + ('0' + d.getSeconds()).substr(-2)}
// Set up console logging
if (!c.silent) {
transports.push(new (winston.transports.Console)(
{
timestamp: dateFunc,
level: c.debugLevel
}));
}
winston.level = c.debugLevel;
winston.showLevel = false;
// Set up logging into file
if (c.logFile) {
fileTransport = new (winston.transports.File) (
{
filename: c.logFile,
json: false,
showLevel: false,
colorize: false,
timestamp: dateFunc,
level: c.debugLevel
});
transports.push(fileTransport);
}
// Logstash
if (c.logstashHost) {
ls = new (winston.transports.Logstash)(
{
host: c.logstashHost,
port: c.logstashPort,
node_name: c.logstashNode,
level: c.debugLevel
});
ls.on('error', function (error) {
dd('Winston Logstash error: ' + error);
});
transports.push(ls);
}
logger = new (winston.Logger) ({
transports: transports
});
logger.exitOnError = false;
}
// c.debugLevel output
function dd(msg, level) {
if (!logger) return;
if (!msg) return;
if (!level) level = 'info';
msg = helpers.safeLog(msg);
// Here goes a sort of hidden multiline message delimiter
msg += " ";
logger.log(level, msg);
}
dd('Important: starting...'.green);
dd('Remote host configuration name: ' + c.hostConfig);
// Start the upstream server
if (c.upstreamHost || c.upstreamListenPort) {
if (!c.upstreamListenPort) {
var upstreamServer = new socketServer.upstream(c);
} else {
var upstreamServer = new socketServer.upstreamListen(c);
}
if (c.listenPort) {
var serverSocket = new socketServer.clientSocket(upstreamServer, c).listen(c.listenPort);
dd("Relay raw ISO-8583 server is now running on port " + c.listenPort);
}
if (c.listenHttpPort) {
var serverHttp = new socketServer.clientHttpServer(upstreamServer, c).listen(c.listenHttpPort);
dd("Relay HTTP server is now running on port " + c.listenHttpPort);
}
if (upstreamServer.statServer && c.statDumpFile && fs.existsSync(c.statDumpFile)) {
upstreamServer.statServer.restore(JSON.parse(fs.readFileSync(c.statDumpFile)));
}
} else {
var upstreamServer = null;
var serverSocket = null;
var serverHttp = null;
}
if (c.echoServerPort || c.testClients) var testSuite = require('./lib/testSuite');
// Start test server and perform tests
if (c.echoServerPort) {
// Run local echo server
var echoServer = new testSuite.echoServer(c, dd).listen(c.echoServerPort);
dd("Echo server is now running on port " + c.echoServerPort);
}
// Run local clients
if (c.testClients) {
setTimeout(function () {
var testClients = [];
for (var i = 1; i < c.testClients + 1; i++) {
testClients.push(new testSuite.testClient(dd).start(c, i));
}
}, 1000);
}
// Pidfile
if (c.pidFile) {
fs.writeFile(c.pidFile, process.pid, function (err) {
if (err) dd(err);
});
}
// Signals
process.on('SIGINT', function() {
dd('Warning: got SIGINT.');
gracefulQuit();
});
process.on('SIGTERM', function() {
dd('Warning: got SIGTERM.');
gracefulQuit();
});
process.on('SIGHUP', function() {
dd('Warning: got SIGHUP.');
if (upstreamServer.statServer) upstreamServer.statServer.reset();
if (fileTransport) helpers.winstonRotate(fileTransport);
});
// On Exit
process.on('exit', function(code) {
if (c.pidFile) {
// Clear pid file
fs.writeFile(c.pidFile, '', function (err) {
if (err) dd(err);
});
}
// Dump stats
if (upstreamServer && upstreamServer.statServer && c.statDumpFile) {
fs.writeFileSync(c.statDumpFile, JSON.stringify(upstreamServer.statServer.dump()));
}
});
function gracefulQuit() {
// If no upstream server running -- just quit
if (!upstreamServer) {
dd('Quitting...');
global.flushAndQuit();
// If there is upstream server -- let it pick a proper moment then quit
} else {
if (serverSocket) serverSocket.deny();
if (serverHttp) serverHttp.deny();
upstreamServer.emit('terminate');
}
}
global.flushAndQuit = function() {
if (fileTransport) {
helpers.winstonFlushAndQuit(fileTransport);
} else {
process.exit(0);
}
}