diff --git a/lib/client.js b/lib/client.js index 98b0896..1112488 100644 --- a/lib/client.js +++ b/lib/client.js @@ -3,6 +3,7 @@ var log = require('winston'), merge = require('utils-merge'), net = require('net'), + tls = require('tls'), session = require('./session'); function login() { @@ -11,10 +12,10 @@ function login() { loginPdu = { 'cmdName': 'bind_transceiver', - 'seqNr': this.ourSeqNr, + 'seqNr': this.ourSeqNr, 'params': { 'system_id': this.options.username, - 'password': this.options.password + 'password': this.options.password } }; @@ -94,7 +95,7 @@ function clientSession(sock, options) { * @param func callback(err, session) */ function client(options, callback) { - var sock = new net.Socket(); + var sock; if (typeof options === 'function') { callback = options; @@ -107,11 +108,18 @@ function client(options, callback) { 'port': 2775, 'username': 'user', 'password': 'pass', + 'tls': false, 'enqLinkTiming': 20000 // 20 sec }, options || {}); + if (options && options.tls && option.tls === true) { + sock = new tls.Socket(); + } else { + sock = new net.Socket(); + } + log.debug('larvitsmpp: lib/client.js: client() - Connecting to ' + options.host + ':' + options.port); - sock.connect(options.port, options.host, function() { + sock.connect(options, function() { var session = clientSession(sock, options); log.info('larvitsmpp: lib/client.js: client() - Connected to ' + sock.remoteAddress + ':' + sock.remotePort); diff --git a/lib/server.js b/lib/server.js index f4aac11..df63b8c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,8 +1,9 @@ 'use strict'; var log = require('winston'), - merge = require('utils-merge'), + merge = require('utils-merge'), net = require('net'), + tls = require('tls'), session = require('./session'), smppUtils = require('./utils'); @@ -117,18 +118,18 @@ function serverSession(sock, options) { if (pduObj.cmdName === 'unbind') { returnObj.sendReturn(pduObj, 'ESME_ROK', undefined, true); - // If client is not logged in, always run the login function + // 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'); returnObj.login(pduObj); - // Client is logged in, try to match a handling function + // 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 + '()'); returnObj.handleCmd[pduObj.cmdName](pduObj); - // No command handling function is registered, return error "invalid command" + // 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 + '"'); @@ -146,6 +147,8 @@ function serverSession(sock, options) { * @param func callback(err, session) */ function server(options, callback) { + var tlsOrNet; + if (typeof options === 'function') { callback = options; options = {}; @@ -153,14 +156,21 @@ function server(options, callback) { // Set default options options = merge({ - 'port': 2775, + 'port': 2775, + 'tls': false, 'timeout': 40000 // 40 sec }, options || {}); + if (options && options.tls && option.tls === true) { + tlsOrNet = tls; + } else { + 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 - net.createServer(function(sock) { + tlsOrNet.createServer(options, function(sock) { var returnObj = serverSession(sock, options); // We have a connection - a socket object is assigned to the connection automatically @@ -177,4 +187,4 @@ function server(options, callback) { } // Expose some functions -exports = module.exports = server; \ No newline at end of file +exports = module.exports = server;