Permalink
Browse files

Merge pull request #60 from garbetjie/master

Added limiting of max client connections.
  • Loading branch information...
2 parents c40a0cd + f84c7b5 commit 98870656c13b84cf4e635127c47880887f8d2dd0 @andris9 committed Dec 23, 2013
Showing with 28 additions and 4 deletions.
  1. +11 −2 lib/server.js
  2. +17 −2 test/server.js
View
@@ -38,6 +38,7 @@ module.exports = function(options){
* <li><b>disableEHLO</b> - if set, support HELO only</li>
* <li><b>ignoreTLS</b> - if set, allow client do not use STARTTLS</li>
* <li><b>disableDNSValidation</b> - if set, do not validate sender domains</li>
+ * <li><b>maxClients</b> - if set, limit the number of simultaneous connections to the server</li>
* </ul>
*
* @constructor
@@ -47,6 +48,7 @@ module.exports = function(options){
function SMTPServer(options){
EventEmitter.call(this);
+ this.connectedClients = 0;
this.options = options || {};
this.options.name = this.options.name || (oslib.hostname && oslib.hostname()) ||
(oslib.getHostname && oslib.getHostname()) ||
@@ -116,6 +118,7 @@ function SMTPServerConnection(server, client){
this.client = client;
this.init();
+ this.server.connectedClients++;
if(this.server.options.debug){
console.log("Connection from", this.client.remoteAddress);
@@ -129,8 +132,13 @@ function SMTPServerConnection(server, client){
this.client.on("data", this._onData.bind(this));
this.client.on("ready", this._onDataReady.bind(this));
- // Send the greeting banner. Force ESMTP notice
- this.client.send("220 "+this.server.options.name + " ESMTP " + (this.server.options.SMTPBanner || "node.js simplesmtp"));
+ // Too many clients. Disallow processing
+ if (this.server.options.maxClients && this.server.connectedClients > this.server.options.maxClients) {
+ this.end("421 " + this.server.options.name + " ESMTP - Too many connections. Please try again later.");
+ } else {
+ // Send the greeting banner. Force ESMTP notice
+ this.client.send("220 "+this.server.options.name + " ESMTP " + (this.server.options.SMTPBanner || "node.js simplesmtp"));
+ }
}
/**
@@ -184,6 +192,7 @@ SMTPServerConnection.prototype._onEnd = function(){
if(this.server.options.debug){
console.log("Connection closed to", this.client.remoteAddress);
}
+ this.server.connectedClients--;
this.server.emit("close", this.envelope);
};
View
@@ -16,7 +16,8 @@ exports["General tests"] = {
this.smtp = new simplesmtp.createServer({
SMTPBanner: "SCORPIO",
name: "MYRDO",
- maxSize: 1234
+ maxSize: 1234,
+ maxClients: 1,
});
this.smtp.listen(PORT_NUMBER, function(err){
if(err){
@@ -157,7 +158,21 @@ exports["General tests"] = {
test.ok(resp.toString("utf-8").trim().match(/^250[\- ]SIZE 1234$/mi));
test.done();
});
- }
+ },
+ "Max Incoming Connections": function(test) {
+ var maxClients = this.smtp.options.maxClients,
+ name = this.smtp.options.name;
+
+ for (var i = 0; i <= maxClients; i++) {
+ runClientMockup(PORT_NUMBER, "localhost", [], (function(i) {
+ return function(resp) {
+ if (i < maxClients) return;
+ test.ok((new RegExp("^421\\s+" + name)).test(resp.toString("utf-8").trim()));
+ test.done();
+ }
+ })(i));
+ }
+ },
};
exports["EHLO setting"] = {

0 comments on commit 9887065

Please sign in to comment.