Skip to content
Browse files

Merge branch 'master' of github.com:andris9/simplesmtp

  • Loading branch information...
2 parents 647c249 + 9887065 commit 15142f9e6d6b014fd3065d81c6fcce868df044c2 @andris9 committed Jan 5, 2014
Showing with 28 additions and 4 deletions.
  1. +11 −2 lib/server.js
  2. +17 −2 test/server.js
View
13 lib/server.js
@@ -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.client.remoteAddress){
if(this.server.options.debug){
@@ -137,8 +140,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"));
+ }
}
/**
@@ -192,6 +200,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
19 test/server.js
@@ -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 15142f9

Please sign in to comment.
Something went wrong with that request. Please try again.