Socket
Socket
Sign inDemoInstall

rai

Package Overview
Dependencies
0
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.0 to 0.1.3

jsdoc/symbols/mockup.html

56

lib/rai.js
/**
* @fileOverview This is the main file for the RAI library to create text based servers
* @author <a href="mailto:andris@node.ee">Andris Reinman</a>
* @version 0.1.0
* @version 0.1.3
*/

@@ -10,3 +10,3 @@

EventEmitter = require('events').EventEmitter,
starttls = require("./starttls"),
starttls = require("./starttls").starttls,
tlslib = require("tls"),

@@ -24,2 +24,3 @@ crypto = require("crypto"),

module.exports.RAIServer = RAIServer;
module.exports.runClientMockup = require("./mockup");

@@ -33,2 +34,3 @@ /**

* <li><b>debug</b> - if set to true print traffic to console</li>
* <li><b>disconnectOnTimeout</b> - if set to true close the connection on disconnect</li>
* <li><b>timeout</b> - timeout in milliseconds for disconnecting the client,

@@ -52,3 +54,3 @@ * defaults to 0 (no timeout)</li>

this.options = options || {};
this.options = options || {};

@@ -116,2 +118,3 @@ this._createServer();

*
* @event
* @param {Object} err Error object

@@ -168,3 +171,4 @@ */

* <li><b>'timeout'</b> - emitted when a timeout occurs. Connection to the
* client is disconnected automatically.</l>
* client is disconnected automatically if disconnectOnTimeout option
* is set to true.</l>
* <li><b>'end'</b> - emitted when the client disconnects</l>

@@ -181,3 +185,3 @@ * </ul>

this.socket = socket;
this.options = options || {};
this.options = options || {};

@@ -190,3 +194,3 @@ this.remoteAddress = socket.remoteAddress;

this._secureConnection = false;
this.secureConnection = false;
this._destroyed = false;

@@ -204,3 +208,3 @@ this._remainder = "";

/**
* <p>Sends some data to the client. <code>&lt;CR&gt;&lt;LF&gt; is automatically appended to
* <p>Sends some data to the client. <code>&lt;CR&gt;&lt;LF&gt;</code> is automatically appended to
* the data</p>

@@ -212,3 +216,3 @@ *

var buffer;
if(data instanceof Buffer || (typeof SlowBuffer != "undefined" && data instanceof SlowBuffer)){
if(data instanceof Buffer || (typeof SlowBuffer != "undefined" && data instanceof SlowBuffer)){
buffer = new Buffer(data.length+2);

@@ -219,3 +223,3 @@ buffer[buffer.length-2] = 0xD;

}else{
buffer = new Buffer((data || "").toString()+"\r\n", "binary");
buffer = new Buffer((data || "").toString()+"\r\n", "binary");
}

@@ -227,3 +231,7 @@

this.socket.write(buffer);
if(this.socket && this.socket.writable){
this.socket.write(buffer);
}else{
this.socket.end();
}
};

@@ -260,3 +268,3 @@

RAISocket.prototype.startTLS = function(credentials, callback){
if(this._secureConnection){
if(this.secureConnection){
this._onError(new Error("Secure connection already established"));

@@ -283,3 +291,3 @@ }

this._secureConnection = true;
this.secureConnection = true;

@@ -317,2 +325,3 @@ this.socket = ssl_socket;

*
* @event
* @param {Buffer|String} chunk Data sent by the client

@@ -402,2 +411,7 @@ */

for(var i=0, len = lines.length; i<len; i++){
if(this._ignore_data){
// If TLS upgrade is initiated do not process current buffer
this._remainder = "";
break;
}
if(!this._dataMode){

@@ -433,2 +447,4 @@ if((match = lines[i].match(/\s*[\S]+\s?/))){

* <p>Called when the connection is ended. Emits <code>'end'</code></p>
*
* @event
*/

@@ -444,2 +460,3 @@ RAISocket.prototype._onEnd = function(){

*
* @event
* @param {Object} err Error object

@@ -455,9 +472,15 @@ */

* <code>'timeout'</code> is emitted.</p>
*
* @event
*/
RAISocket.prototype._onTimeout = function(){
if(this.socket && !this.socket.destroyed){
this.socket.end();
if(this.options.disconnectOnTimeout){
if(this.socket && !this.socket.destroyed){
this.socket.end();
}
this.emit("timeout");
this._destroy();
}else{
this.emit("timeout");
}
this.emit("timeout");
this._destroy();
};

@@ -468,2 +491,3 @@

*
* @event
* @param {Boolean} hadError did the connection end because of an error?

@@ -470,0 +494,0 @@ */

@@ -19,3 +19,17 @@ // SOURCE: https://gist.github.com/848444

module.exports = function starttls(socket, options, callback) {
/**
* @namespace STARTTLS module
* @name starttls
*/
module.exports.starttls = starttls;
/**
* <p>Upgrades a socket to a secure TLS connection</p>
*
* @memberOf starttls
* @param {Object} socket Plaintext socket to be upgraded
* @param {Object} options Certificate data for the server
* @param {Function} callback Callback function to be run after upgrade
*/
function starttls(socket, options, callback) {
var sslcontext, pair, cleartext;

@@ -43,3 +57,3 @@

return pair;
};
}

@@ -46,0 +60,0 @@ function forwardEvents(events, emitterSource, emitterDestination) {

{
"name": "rai",
"description": "Request-Answer-Interface for generating text based command servers (SMTP, POP etc)",
"version": "0.1.0",
"version": "0.1.3",
"author" : "Andris Reinman",

@@ -6,0 +6,0 @@ "maintainers":[

@@ -120,4 +120,27 @@ # RAI - Request-Answer-Interface

## Testing
There is a possibility to set up a mockup client which sends a batch of commands
one by one to the server and returns the last response.
var runClientMockup = require("rai").runClientMockup;
var cmds = ["EHLO FOOBAR", "STARTTLS", "QUIT"];
runClientMockup(25, "mail.hot.ee", cmds, function(resp){
console.log("Final:", resp.toString("utf-8").trim());
});
`runClientMockup` has he following parameters in the following order:
* **port** - Port number
* **host** - Hostname to connect to
* **commands** - Command list (an array) to be sent to server
* **callback** - Callback function to run on completion
* **debug** - if set to true log all input/output
Response from the callback function is a Buffer and contains the
last data received from the server
## License
**MIT**

@@ -216,2 +216,62 @@ var RAIServer = require("../lib/rai").RAIServer,

});
},
"STARTTLS clears command buffer": function(test){
var server = new RAIServer();
server.listen(PORT_NUMBER, function(err){
test.expect(2);
server.on("connection", function(socket){
socket.on("command", function(command){
if(command == "STARTTLS"){
socket.startTLS();
socket.send("OK");
}else if(command == "KILL"){
test.ok(0, "Should not occur");
}else if(command == "OK"){
test.ok(1, "OK");
}
});
socket.on("tls", function(){
test.ok(1, "Secure connection opened");
socket.send("TEST");
});
socket.on("end", function(){
server.end(function(){
test.done();
});
});
socket.on("error", function(err){
test.isError(err);
});
});
var client = netlib.connect(PORT_NUMBER, function(){
client.write("STARTTLS\r\nKILL\r\n");
client.on("data", function(chunk){
if(chunk.toString("utf-8").trim() == "OK"){
var sslcontext = crypto.createCredentials();
var pair = tlslib.createSecurePair(sslcontext, false);
pair.encrypted.pipe(client);
client.pipe(pair.encrypted);
pair.fd = client.fd;
pair.on("secure", function(){
pair.cleartext.write("OK\r\n");
pair.cleartext.end();
});
}
});
});
});
}

@@ -402,7 +462,7 @@ };

}
}
};
exports["Timeout tests"] = {
"Timeout": function(test){
var server = new RAIServer({timeout: 300});
var server = new RAIServer({timeout: 300, disconnectOnTimeout: true});
test.expect(3);

@@ -434,3 +494,3 @@ server.listen(PORT_NUMBER, function(err){

"Timeout with TLS": function(test){
var server = new RAIServer({timeout: 300});
var server = new RAIServer({timeout: 300, disconnectOnTimeout: true});
server.listen(PORT_NUMBER, function(err){

@@ -437,0 +497,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc