-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathsmpp.js
517 lines (482 loc) · 15.4 KB
/
smpp.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
var net = require('net'),
tls = require('tls'),
util = require('util'),
parse = require('url').parse,
defs = require('./defs'),
PDU = require('./pdu').PDU,
EventEmitter = require('events').EventEmitter,
proxy = require("findhit-proxywrap").proxy;
var proxyTransport = proxy(net, {
strict: false,
ignoreStrictExceptions: true
});
var proxyTlsTransport = proxy(tls, {
strict: false,
ignoreStrictExceptions: true
});
function Session(options) {
EventEmitter.call(this);
this.options = options || {};
var self = this;
var clientTransport = net;
var connectTimeout;
this._extractPDUs = this._extractPDUs.bind(self);
this.sequence = 0;
this.paused = false;
this.closed = false;
this.remoteAddress = null;
this.remotePort = null;
this.proxyProtocolProxy = null;
this._busy = false;
this._callbacks = {};
this._interval = 0;
this._command_length = null;
this._mode = null;
this._id = Math.floor(Math.random() * (999999 - 100000)) + 100000; // random session id
this._prevBytesRead = 0;
this.rootSocket = (function() {
if (self.socket._parent) return self.socket._parent;
return self.socket;
});
if (options.socket) {
// server mode / socket is already connected.
this._mode = "server";
this.socket = options.socket;
this.remoteAddress = self.rootSocket().remoteAddress || self.remoteAddress;
this.remotePort = this.rootSocket().remotePort;
this.proxyProtocolProxy = this.rootSocket().proxyAddress ? { address: this.rootSocket().proxyAddress, port: this.rootSocket().proxyPort } : false;
} else {
// client mode
this._mode = "client";
if (options.tls) {
clientTransport = tls;
}
if (options.hasOwnProperty("connectTimeout") && options.connectTimeout>0) {
connectTimeout = setTimeout(function () {
if (self.socket) {
var e = new Error("Timeout of " + options.connectTimeout + "ms while connecting to " +
self.options.host + ":" + self.options.port);
e.code = "ETIMEOUT";
e.timeout = options.connectTimeout;
self.socket.destroy(e);
}
}, options.connectTimeout);
}
this.socket = clientTransport.connect(this.options);
this.socket.on('connect', (function() {
clearTimeout(connectTimeout);
self.remoteAddress = self.rootSocket().remoteAddress || self.remoteAddress;
self.remotePort = self.rootSocket().remotePort || self.remoteAddress;
self.debug("server.connected", "connected to server", {secure: options.tls});
self.emitMetric("server.connected", 1);
self.emit('connect'); // @todo should emit the session, but it would break BC
if(self.options.auto_enquire_link_period) {
self._interval = setInterval(function() {
self.enquire_link();
}, self.options.auto_enquire_link_period);
}
}).bind(this));
this.socket.on('secureConnect', (function() {
self.emit('secureConnect'); // @todo should emit the session, but it would break BC
}).bind(this));
}
this.socket.on('readable', function() {
var bytesRead = self.socket.bytesRead - self._prevBytesRead;
if ( bytesRead > 0 ) {
// on disconnections the readable event receives 0 bytes, we do not want to debug that
self.debug("socket.data.in", null, {bytes: bytesRead});
self.emitMetric("socket.data.in", bytesRead, {bytes: bytesRead});
self._prevBytesRead = self.socket.bytesRead;
}
self._extractPDUs();
});
this.socket.on('close', function() {
self.closed = true;
clearTimeout(connectTimeout);
if (self._mode === "server") {
self.debug("client.disconnected", "client has disconnected");
self.emitMetric("client.disconnected", 1);
} else {
self.debug("server.disconnected", "disconnected from server");
self.emitMetric("server.disconnected", 1);
}
self.emit('close');
if(self._interval) {
clearInterval(self._interval);
self._interval = 0;
}
});
this.socket.on('error', function(e) {
clearTimeout(connectTimeout);
if (self._interval) {
clearInterval(self._interval);
self._interval = 0;
}
self.debug("socket.error", e.message, e);
self.emitMetric("socket.error", 1, {error: e});
self.emit('error', e); // Emitted errors will kill the program if they're not captured.
});
}
util.inherits(Session, EventEmitter);
Session.prototype.emitMetric = function(event, value, payload) {
this.emit('metrics', event || null, value || null, payload || {}, {
mode: this._mode || null,
remoteAddress: this.remoteAddress || null,
remotePort: this.remotePort || null,
remoteTls: this.options.tls || false,
sessionId: this._id || null,
session: this
});
}
Session.prototype.debug = function(type, msg, payload) {
if (type === undefined) type = null;
if (msg === undefined) msg = null;
if (this.options.debug) {
var coloredTypes = {
"reset": "\x1b[0m",
"dim": "\x1b[2m",
"client.connected": "\x1b[1m\x1b[34m",
"client.disconnected": "\x1b[1m\x1b[31m",
"server.connected": "\x1b[1m\x1b[34m",
"server.disconnected": "\x1b[1m\x1b[31m",
"pdu.command.in": "\x1b[36m",
"pdu.command.out": "\x1b[32m",
"pdu.command.error": "\x1b[41m\x1b[30m",
"socket.error": "\x1b[41m\x1b[30m",
"socket.data.in": "\x1b[2m",
"socket.data.out": "\x1b[2m",
"metrics": "\x1b[2m",
}
var now = new Date();
var logBuffer = now.toISOString() +
" - " + (this._mode === "server" ? "srv" : "cli") +
" - " + this._id +
" - " + (coloredTypes.hasOwnProperty(type) ? coloredTypes[type] + type + coloredTypes.reset : type) +
" - " + (msg !== null ? msg : "" ) +
" - " + coloredTypes.dim + (payload !== undefined ? JSON.stringify(payload) : "") + coloredTypes.reset;
if (this.remoteAddress) logBuffer += " - [" + this.remoteAddress + "]"
console.log( logBuffer );
}
if (this.options.debugListener instanceof Function) {
this.options.debugListener(type, msg, payload);
}
this.emit('debug', type, msg, payload);
}
Session.prototype.connect = function() {
this.sequence = 0;
this.paused = false;
this._busy = false;
this._callbacks = {};
this.socket.connect(this.options);
};
Session.prototype._extractPDUs = function() {
if (this._busy) {
return;
}
this._busy = true;
var pdu;
while (!this.paused) {
try {
if(!this._command_length) {
this._command_length = PDU.commandLength(this.socket);
if(!this._command_length) {
break;
}
}
if (!(pdu = PDU.fromStream(this.socket, this._command_length))) {
break;
}
this.debug("pdu.command.in", pdu.command, pdu);
this.emitMetric("pdu.command.in", 1, pdu);
} catch (e) {
this.debug("pdu.command.error", e.message, e);
this.emitMetric("pdu.command.error", 1, {error: e});
this.emit('error', e);
return;
}
this._command_length = null;
this.emit('pdu', pdu);
this.emit(pdu.command, pdu);
if (pdu.isResponse() && this._callbacks[pdu.sequence_number]) {
this._callbacks[pdu.sequence_number](pdu);
delete this._callbacks[pdu.sequence_number];
}
}
this._busy = false;
};
Session.prototype.send = function(pdu, responseCallback, sendCallback, failureCallback) {
if (!this.socket.writable) {
var errorObject = {
error: 'Socket is not writable',
errorType: 'socket_not_writable'
}
this.debug('socket.data.error', null, errorObject);
this.emitMetric("socket.data.error", 1, errorObject);
if (failureCallback) {
pdu.command_status = defs.errors.ESME_RSUBMITFAIL;
failureCallback(pdu);
}
return false;
}
if (!pdu.isResponse()) {
// when server/session pair is used to proxy smpp
// traffic, the sequence_number will be provided by
// client otherwise we generate it automatically
if (!pdu.sequence_number) {
if (this.sequence == 0x7FFFFFFF) {
this.sequence = 0;
}
pdu.sequence_number = ++this.sequence;
}
if (responseCallback) {
this._callbacks[pdu.sequence_number] = responseCallback;
}
} else if (responseCallback && !sendCallback) {
sendCallback = responseCallback;
}
this.debug('pdu.command.out', pdu.command, pdu);
this.emitMetric("pdu.command.out", 1, pdu);
var buffer = pdu.toBuffer();
this.socket.write(buffer, (function(err) {
if (err) {
this.debug('socket.data.error', null, {
error:'Cannot write command ' + pdu.command + ' to socket',
errorType: 'socket_write_error'
});
this.emitMetric("socket.data.error", 1, {
error: err,
errorType: 'socket_write_error',
pdu: pdu
});
if (!pdu.isResponse() && this._callbacks[pdu.sequence_number]) {
delete this._callbacks[pdu.sequence_number];
}
if (failureCallback) {
pdu.command_status = defs.errors.ESME_RSUBMITFAIL;
failureCallback(pdu, err);
}
} else {
this.debug("socket.data.out", null, {bytes: buffer.length, error: err});
this.emitMetric("socket.data.out", buffer.length, {bytes: buffer.length});
this.emit('send', pdu);
if (sendCallback) {
sendCallback(pdu);
}
}
}).bind(this));
return true;
};
Session.prototype.pause = function() {
this.paused = true;
};
Session.prototype.resume = function() {
this.paused = false;
this._extractPDUs();
};
Session.prototype.close = function(callback) {
if (callback) {
if (this.closed) {
callback();
} else {
this.socket.once('close', callback);
}
}
this.socket.end();
};
Session.prototype.destroy = function(callback) {
if (callback) {
if (this.closed) {
callback();
} else {
this.socket.once('close', callback);
}
}
this.socket.destroy();
};
var createShortcut = function(command) {
return function(options, responseCallback, sendCallback, failureCallback) {
if (typeof options == 'function') {
sendCallback = responseCallback;
responseCallback = options;
options = {};
}
var pdu = new PDU(command, options);
return this.send(pdu, responseCallback, sendCallback, failureCallback);
};
};
for (var command in defs.commands) {
Session.prototype[command] = createShortcut(command);
}
function Server(options, listener) {
var self = this,
transport;
this.sessions = [];
this.isProxiedServer = options.isProxiedServer == true;
if (typeof options == 'function') {
listener = options;
options = {};
} else {
options = options || {};
}
if (listener) {
this.on('session', listener);
}
this.tls = options.key && options.cert;
options.tls = this.tls != null; // standarized option for the session on both client & server
this.options = options;
self.on("proxiedConnection", function(socket) {
// The connection has successfully passed through the proxied server (event emitted by proxywrap)
socket.proxiedConnection = true;
});
// Fetch the right transport based on the current options
if (this.isProxiedServer) {
transport = this.tls ? proxyTlsTransport : proxyTransport;
} else {
transport = this.tls ? tls : net;
}
transport.Server.call(this, options, function(socket) {
var session = new Session({
socket: socket,
tls: self.options.tls,
debug: self.options.debug,
debugListener: self.options.debugListener || null
});
session.server = self;
if (socket.savedEmit) {
// Restore the saved emit to fix the proxywrap bug (on nodejs <=8)
socket.emit = socket.savedEmit;
socket.savedEmit = null;
}
session.debug("client.connected", "client has connected", {
secure: self.options.tls,
// Useful information for Proxy protocol debugging & testing
proxiedServer: self.isProxiedServer,
proxiedConnection: socket.proxiedConnection || (socket._parent ? socket._parent.proxiedConnection : false) || false,
remoteAddress: session.remoteAddress,
remotePort: session.remotePort,
proxyProtocolProxy: session.proxyProtocolProxy,
});
self.sessions.push(session);
socket.on('close', function() {
self.sessions.splice(self.sessions.indexOf(session), 1);
});
self.emit('session', session);
session.emitMetric("client.connected", 1);
});
if (this.isProxiedServer) {
// The proxied wrapper clears all connection listeners and adds their own.
// A new listener is added in order to catch socket error on the wrapper.
self.on("connection", function (socket) {
socket.on("error", function (e) {
self.emit("error", e);
});
if (self.options.autoPrependBuffer) {
// Allows to automatically prepend a buffer on the client socket. This feature is intended only for
// testing purposes and it's used to inject client simulated headers (Proxy protocol)
socket.unshift(self.options.autoPrependBuffer);
}
// There's a bug in the proxywrap server which tampers the emit method in nodejs <= 8 and makes the
// socket unable to emit the events. As a simple fix, save the emit method so it can be restored later.
socket.savedEmit = socket.emit;
});
}
}
function SecureServer(options, listener) {
Server.call(this, options, listener);
}
function ProxyServer(options, listener) {
options.isProxiedServer = true;
Server.call(this, options, listener);
}
function ProxySecureServer(options, listener) {
options.isProxiedServer = true;
Server.call(this, options, listener);
}
// Standard servers without proxy protocol support
util.inherits(Server, net.Server);
util.inherits(SecureServer, tls.Server);
// Servers with proxy protocol support
util.inherits(ProxyServer, proxyTransport.Server);
util.inherits(ProxySecureServer, proxyTlsTransport.Server);
exports.createServer = function(options, listener) {
if (typeof options == 'function') {
listener = options;
options = {};
} else {
options = options || {};
}
if (options.key && options.cert) {
if (options.enable_proxy_protocol_detection) {
return new ProxySecureServer(options, listener);
} else {
return new SecureServer(options, listener);
}
} else {
if (options.enable_proxy_protocol_detection) {
return new ProxyServer(options, listener);
} else {
return new Server(options, listener);
}
}
};
exports.connect = exports.createSession = function(options, listener) {
var clientOptions = {};
if (arguments.length > 1 && typeof listener != 'function') {
clientOptions = {
host: options,
port: listener
};
listener = arguments[3];
} else if (typeof options == 'string') {
clientOptions = parse(options);
clientOptions.host = clientOptions.slashes ? clientOptions.hostname : options;
clientOptions.tls = clientOptions.protocol === 'ssmpp:';
} else if (typeof options == 'function') {
listener = options;
} else {
clientOptions = options || {};
if (clientOptions.url) {
options = parse(clientOptions.url);
clientOptions.host = options.hostname;
clientOptions.port = options.port;
clientOptions.tls = options.protocol === 'ssmpp:';
}
}
if (clientOptions.tls && !clientOptions.hasOwnProperty("rejectUnauthorized")) {
clientOptions.rejectUnauthorized = false; // Allow self signed certificates by default
}
clientOptions.port = clientOptions.port || (clientOptions.tls ? 3550 : 2775);
clientOptions.debug = clientOptions.debug || false;
clientOptions.connectTimeout = clientOptions.connectTimeout || 30000;
var session = new Session(clientOptions);
if (listener) {
session.on(clientOptions.tls ? 'secureConnect' : 'connect', function() {
listener(session);
});
}
return session;
};
exports.addCommand = function(command, options) {
options.command = command;
defs.commands[command] = options;
defs.commandsById[options.id] = options;
Session.prototype[command] = createShortcut(command);
};
exports.addTLV = function(tag, options) {
options.tag = tag;
defs.tlvs[tag] = options;
defs.tlvsById[options.id] = options;
};
exports.Session = Session;
exports.Server = Server;
exports.SecureServer = SecureServer;
exports.PDU = PDU;
for (var key in defs) {
exports[key] = defs[key];
}
for (var error in defs.errors) {
exports[error] = defs.errors[error];
}
for (var key in defs.consts) {
exports[key] = defs.consts[key];
}