var to let or const, better lints, code style and more
This commit is contained in:
+59
-62
@@ -1,11 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
var log = require('winston'),
|
||||
merge = require('utils-merge'),
|
||||
net = require('net'),
|
||||
tls = require('tls'),
|
||||
session = require('./session'),
|
||||
smppUtils = require('./utils');
|
||||
const topLogPrefix = 'larvitsmpp: lib/server.js: ',
|
||||
smppUtils = require(__dirname + '/utils'),
|
||||
session = require(__dirname + '/session'),
|
||||
merge = require('utils-merge'),
|
||||
log = require('winston'),
|
||||
net = require('net'),
|
||||
tls = require('tls');
|
||||
|
||||
/**
|
||||
* Try to log a connecting peer in
|
||||
@@ -13,22 +14,20 @@ var log = require('winston'),
|
||||
* @param {object} pduObj
|
||||
*/
|
||||
function login(pduObj) {
|
||||
var that = this;
|
||||
const logPrefix = topLogPrefix + 'login() - ',
|
||||
that = this;
|
||||
|
||||
log.debug('larvitsmpp: lib/server.js: login() - Data received and session is not loggedIn');
|
||||
log.debug(logPrefix + 'Data received and session is not loggedIn');
|
||||
|
||||
// Pause socket so we do not receive any other commands until we have processed the login
|
||||
this.sock.pause();
|
||||
that.sock.pause();
|
||||
|
||||
// Only bind_* is accepted when the client is not logged in
|
||||
if (pduObj.cmdName !== 'bind_transceiver' && pduObj.cmdName !== 'bind_receiver' && pduObj.cmdName !== 'bind_transmitter') {
|
||||
log.debug('larvitsmpp: lib/server.js: login() - Session is not loggedIn and no bind_* command is given. Return error "ESME_RINVBNDSTS');
|
||||
log.debug(logPrefix + 'Session is not loggedIn and no bind_* command is given. Return error "ESME_RINVBNDSTS');
|
||||
|
||||
smppUtils.pduReturn(pduObj, 'ESME_RINVBNDSTS', function(err, retPdu) {
|
||||
if (err) {
|
||||
that.closeSocket();
|
||||
return;
|
||||
}
|
||||
smppUtils.pduReturn(pduObj, 'ESME_RINVBNDSTS', function (err, retPdu) {
|
||||
if (err) return that.closeSocket();
|
||||
|
||||
that.sock.resume();
|
||||
that.sockWrite(retPdu);
|
||||
@@ -38,42 +37,36 @@ function login(pduObj) {
|
||||
}
|
||||
|
||||
// If there is a checkuserpass(), use it to check system_id and password from the PDU
|
||||
if (typeof this.options.checkuserpass === 'function') {
|
||||
this.options.checkuserpass(pduObj.params.system_id, pduObj.params.password, function(err, res, userData) {
|
||||
if (err) {
|
||||
that.closeSocket();
|
||||
return;
|
||||
}
|
||||
if (typeof that.options.checkuserpass === 'function') {
|
||||
return that.options.checkuserpass(pduObj.params.system_id, pduObj.params.password, function (err, res, userData) {
|
||||
if (err) return that.closeSocket();
|
||||
|
||||
if ( ! res) {
|
||||
log.info('larvitsmpp: lib/server.js: serverSession() - login() - Login failed! Connected host: ' + that.sock.remoteAddress + ':' + that.sock.remotePort + ' system_id: "' + pduObj.params.system_id + '"');
|
||||
log.info(logPrefix + 'Login failed! Connected host: ' + that.sock.remoteAddress + ':' + that.sock.remotePort + ' system_id: "' + pduObj.params.system_id + '"');
|
||||
|
||||
that.sock.resume();
|
||||
that.sendReturn(pduObj, 'ESME_RBINDFAIL');
|
||||
return;
|
||||
return that.sendReturn(pduObj, 'ESME_RBINDFAIL');
|
||||
}
|
||||
|
||||
log.verbose('larvitsmpp: lib/server.js: serverSession() - login() - Login successful! Connected host: ' + that.sock.remoteAddress + ':' + that.sock.remotePort + ' system_id: "' + pduObj.params.system_id + '"');
|
||||
that.loggedIn = true;
|
||||
log.verbose(logPrefix + 'Login successful! Connected host: ' + that.sock.remoteAddress + ':' + that.sock.remotePort + ' system_id: "' + pduObj.params.system_id + '"');
|
||||
that.loggedIn = true;
|
||||
|
||||
// Set additional user data to the session
|
||||
if (userData !== undefined) {
|
||||
that.userData = userData;
|
||||
that.userData = userData;
|
||||
}
|
||||
|
||||
that.sock.resume();
|
||||
that.emit('login');
|
||||
that.sendReturn(pduObj);
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// If we arrived here it means we are not logged in and that a bind_* event happened and no checkuserpass() method exists. Lets login!
|
||||
this.loggedIn = true;
|
||||
this.sock.resume();
|
||||
this.emit('login');
|
||||
this.sendReturn(pduObj);
|
||||
that.loggedIn = true;
|
||||
that.sock.resume();
|
||||
that.emit('login');
|
||||
that.sendReturn(pduObj);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,15 +74,16 @@ function login(pduObj) {
|
||||
* If this is not ran within options.timeout milliseconds, this session will self terminate
|
||||
*/
|
||||
function resetEnqLinkTimer() {
|
||||
var that = this;
|
||||
const logPrefix = topLogPrefix + 'resetEnqLinkTimer() - ',
|
||||
that = this;
|
||||
|
||||
log.silly('larvitsmpp: lib/server.js: resetEnqLinkTimer() - Resetting the kill timer');
|
||||
log.silly(logPrefix + 'Resetting the kill timer');
|
||||
if (that.enqLinkTimer) {
|
||||
clearTimeout(that.enqLinkTimer);
|
||||
}
|
||||
|
||||
that.enqLinkTimer = setTimeout(function() {
|
||||
log.info('larvitsmpp: lib/server.js: resetEnqLinkTimer() - Closing session from ' + that.sock.remoteAddress + ':' + that.sock.remotePort + ' due to timeout');
|
||||
that.enqLinkTimer = setTimeout(function () {
|
||||
log.info(logPrefix + 'Closing session from ' + that.sock.remoteAddress + ':' + that.sock.remotePort + ' due to timeout');
|
||||
that.closeSocket();
|
||||
}, that.options.timeout);
|
||||
}
|
||||
@@ -102,16 +96,17 @@ function resetEnqLinkTimer() {
|
||||
* @return {object} (returnObj)
|
||||
*/
|
||||
function serverSession(sock, options) {
|
||||
var returnObj = session(sock);
|
||||
const returnObj = session(sock);
|
||||
|
||||
returnObj.options = options;
|
||||
returnObj.login = login;
|
||||
returnObj.resetEnqLinkTimer = resetEnqLinkTimer;
|
||||
returnObj.options = options;
|
||||
returnObj.login = login;
|
||||
returnObj.resetEnqLinkTimer = resetEnqLinkTimer;
|
||||
|
||||
returnObj.resetEnqLinkTimer();
|
||||
|
||||
// Handle incoming Pdu Objects
|
||||
returnObj.on('incomingPduObj', function(pduObj) {
|
||||
returnObj.on('incomingPduObj', function (pduObj) {
|
||||
const logPrefix = topLogPrefix + 'serverSession() - returnObj.handleIncomingPdu() - ';
|
||||
// Call the appropriate handleCmd function
|
||||
|
||||
// Unbind is always ok
|
||||
@@ -120,18 +115,18 @@ function serverSession(sock, options) {
|
||||
|
||||
// If client is not logged in, always run the login function
|
||||
} else if (returnObj.loggedIn === false) {
|
||||
log.debug('larvitsmpp: lib/server.js: serverSession() - returnObj.handleIncomingPdu() - Not logged in, running login function');
|
||||
log.debug(logPrefix + ' Not logged in, running login function');
|
||||
returnObj.login(pduObj);
|
||||
|
||||
// Client is logged in, try to match a handling function
|
||||
} else if (typeof returnObj.handleCmd[pduObj.cmdName] === 'function') {
|
||||
log.debug('larvitsmpp: lib/server.js: serverSession() - returnObj.on(incomingPduObj) - Running cmd handling function returnObj.handleCmd.' + pduObj.cmdName + '()');
|
||||
log.debug(logPrefix + 'Running cmd handling function returnObj.handleCmd.' + pduObj.cmdName + '()');
|
||||
|
||||
returnObj.handleCmd[pduObj.cmdName](pduObj);
|
||||
|
||||
// No command handling function is registered, return error "invalid command"
|
||||
} else {
|
||||
log.info('larvitsmpp: lib/server.js: serverSession() - returnObj.on(incomingPduObj) - No handling function found for command: "' + pduObj.cmdName + '"');
|
||||
log.info(logPrefix + 'No handling function found for command: "' + pduObj.cmdName + '"');
|
||||
|
||||
returnObj.sendReturn(pduObj, 'ESME_RINVCMDID');
|
||||
}
|
||||
@@ -144,45 +139,47 @@ function serverSession(sock, options) {
|
||||
* Setup a server
|
||||
*
|
||||
* @param {object} options - host, port, checkuserpass() etc (OPTIONAL)
|
||||
* @param {function} callback(err, session)
|
||||
* @param {function} cb(err, session)
|
||||
*/
|
||||
function server(options, callback) {
|
||||
var tlsOrNet;
|
||||
function server(options, cb) {
|
||||
const logPrefix = topLogPrefix + 'server() - ';
|
||||
|
||||
let tlsOrNet;
|
||||
|
||||
if (typeof options === 'function') {
|
||||
callback = options;
|
||||
options = {};
|
||||
cb = options;
|
||||
options = {};
|
||||
}
|
||||
|
||||
// Set default options
|
||||
options = merge({
|
||||
'port': 2775,
|
||||
'tls': false,
|
||||
'timeout': 40000 // 40 sec
|
||||
'port': 2775,
|
||||
'tls': false,
|
||||
'timeout': 40000 // 40 sec
|
||||
}, options || {});
|
||||
|
||||
if (options && options.tls && options.tls === true) {
|
||||
tlsOrNet = tls;
|
||||
tlsOrNet = tls;
|
||||
} else {
|
||||
tlsOrNet = net;
|
||||
tlsOrNet = net;
|
||||
}
|
||||
|
||||
// Create a server instance, and chain the listen function to it
|
||||
// The function passed to net.createServer() becomes the event handler for the 'connection' event
|
||||
// The sock object the callback function receives UNIQUE for each connection
|
||||
tlsOrNet.createServer(options, function(sock) {
|
||||
var returnObj = serverSession(sock, options);
|
||||
// The sock object the cb function receives UNIQUE for each connection
|
||||
tlsOrNet.createServer(options, function (sock) {
|
||||
const returnObj = serverSession(sock, options);
|
||||
|
||||
// We have a connection - a socket object is assigned to the connection automatically
|
||||
log.verbose('larvitsmpp: lib/server.js: server() - Incoming connection! From: ' + sock.remoteAddress + ':' + sock.remotePort);
|
||||
log.verbose(logPrefix + 'Incoming connection! From: ' + sock.remoteAddress + ':' + sock.remotePort);
|
||||
|
||||
callback(null, returnObj);
|
||||
cb(null, returnObj);
|
||||
}).listen(options.port, options.host);
|
||||
|
||||
if (options.host !== undefined) {
|
||||
log.info('larvitsmpp: lib/server.js: server() - Up and listening at ' + options.host + ':' + options.port);
|
||||
log.info(logPrefix + 'Up and listening at ' + options.host + ':' + options.port);
|
||||
} else {
|
||||
log.info('larvitsmpp: lib/server.js: server() - Up and listening at *:' + options.port);
|
||||
log.info(logPrefix + 'Up and listening at *:' + options.port);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user