Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added limiting of max client connections.

  • Loading branch information...
commit bcd5c0b3b0195b1a95def5caeee54bda2a4961d3 1 parent c40a0cd
@garbetjie garbetjie authored
Showing with 11 additions and 2 deletions.
  1. +11 −2 lib/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.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);
};
Please sign in to comment.
Something went wrong with that request. Please try again.