Dependency updates and new API for logging

This commit is contained in:
2018-08-22 17:05:29 +02:00
parent d9d3babe3b
commit 81be0a9aea
9 changed files with 198 additions and 172 deletions
+19 -16
View File
@@ -3,8 +3,9 @@
const topLogPrefix = 'larvitsmpp: lib/server.js: ',
smppUtils = require(__dirname + '/utils'),
session = require(__dirname + '/session'),
LUtils = require('larvitutils'),
lUtils = new LUtils(),
merge = require('utils-merge'),
log = require('winston'),
net = require('net'),
tls = require('tls');
@@ -17,14 +18,14 @@ function login(pduObj) {
const logPrefix = topLogPrefix + 'login() - ',
that = this;
log.debug(logPrefix + 'Data received and session is not loggedIn');
that.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
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(logPrefix + 'Session is not loggedIn and no bind_* command is given. Return error "ESME_RINVBNDSTS');
that.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) return that.closeSocket();
@@ -42,13 +43,13 @@ function login(pduObj) {
if (err) return that.closeSocket();
if ( ! res) {
log.info(logPrefix + 'Login failed! Connected host: ' + that.sock.remoteAddress + ':' + that.sock.remotePort + ' system_id: "' + pduObj.params.system_id + '"');
that.log.info(logPrefix + 'Login failed! Connected host: ' + that.sock.remoteAddress + ':' + that.sock.remotePort + ' system_id: "' + pduObj.params.system_id + '"');
that.sock.resume();
return that.sendReturn(pduObj, 'ESME_RBINDFAIL');
}
log.verbose(logPrefix + 'Login successful! Connected host: ' + that.sock.remoteAddress + ':' + that.sock.remotePort + ' system_id: "' + pduObj.params.system_id + '"');
that.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
@@ -77,13 +78,13 @@ function resetEnqLinkTimer() {
const logPrefix = topLogPrefix + 'resetEnqLinkTimer() - ',
that = this;
log.silly(logPrefix + 'Resetting the kill timer');
that.log.silly(logPrefix + 'Resetting the kill timer');
if (that.enqLinkTimer) {
clearTimeout(that.enqLinkTimer);
}
that.enqLinkTimer = setTimeout(function () {
log.info(logPrefix + 'Closing session from ' + that.sock.remoteAddress + ':' + that.sock.remotePort + ' due to timeout');
that.log.info(logPrefix + 'Closing session from ' + that.sock.remoteAddress + ':' + that.sock.remotePort + ' due to timeout');
that.closeSocket();
}, that.options.timeout);
}
@@ -96,11 +97,12 @@ function resetEnqLinkTimer() {
* @return {object} (returnObj)
*/
function serverSession(sock, options) {
const returnObj = session(sock);
const returnObj = session({'sock': sock, 'log': options.log});
returnObj.options = options;
returnObj.login = login;
returnObj.resetEnqLinkTimer = resetEnqLinkTimer;
returnObj.log = options.log;
returnObj.resetEnqLinkTimer();
@@ -115,18 +117,18 @@ function serverSession(sock, options) {
// If client is not logged in, always run the login function
} else if (returnObj.loggedIn === false) {
log.debug(logPrefix + ' Not logged in, running login function');
options.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(logPrefix + 'Running cmd handling function returnObj.handleCmd.' + pduObj.cmdName + '()');
options.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(logPrefix + 'No handling function found for command: "' + pduObj.cmdName + '"');
options.log.info(logPrefix + 'No handling function found for command: "' + pduObj.cmdName + '"');
returnObj.sendReturn(pduObj, 'ESME_RINVCMDID');
}
@@ -138,7 +140,7 @@ function serverSession(sock, options) {
/**
* Setup a server
*
* @param {object} options - host, port, checkuserpass() etc (OPTIONAL)
* @param {object} options - host, port, checkuserpass() etc (OPTIONAL), log
* @param {function} cb(err, session)
*/
function server(options, cb) {
@@ -155,7 +157,8 @@ function server(options, cb) {
options = merge({
'port': 2775,
'tls': false,
'timeout': 40000 // 40 sec
'timeout': 40000, // 40 sec
'log': new lUtils.Log()
}, options || {});
if (options && options.tls && options.tls === true) {
@@ -171,15 +174,15 @@ function server(options, cb) {
const returnObj = serverSession(sock, options);
// We have a connection - a socket object is assigned to the connection automatically
log.verbose(logPrefix + 'Incoming connection! From: ' + sock.remoteAddress + ':' + sock.remotePort);
options.log.verbose(logPrefix + 'Incoming connection! From: ' + sock.remoteAddress + ':' + sock.remotePort);
cb(null, returnObj);
}).listen(options.port, options.host);
if (options.host !== undefined) {
log.info(logPrefix + 'Up and listening at ' + options.host + ':' + options.port);
options.log.info(logPrefix + 'Up and listening at ' + options.host + ':' + options.port);
} else {
log.info(logPrefix + 'Up and listening at *:' + options.port);
options.log.info(logPrefix + 'Up and listening at *:' + options.port);
}
}