do not choke on long reverse DNS call

This commit is contained in:
Andris Reinman 2017-11-28 10:14:52 +02:00
parent ccaff93eb8
commit 4e952ee9a6
2 changed files with 61 additions and 17 deletions

View file

@ -117,21 +117,16 @@ class IMAPConnection extends EventEmitter {
// Setup event handlers for the socket // Setup event handlers for the socket
this._setListeners(); this._setListeners();
// Resolve hostname for the remote IP // make sure we have a session set up
// we do not care for errors as we consider the ip as unresolved in this case, no big deal this._startSession();
dns.reverse(this.remoteAddress, (err, hostnames) => {
if (err) {
//ignore, no big deal
}
// eslint-disable-line handle-callback-err let now = Date.now();
if (this._closing || this._closed) { let greetingSent = false;
let sendGreeting = () => {
if (greetingSent) {
return; return;
} }
greetingSent = true;
this.clientHostname = (hostnames && hostnames.shift()) || '[' + this.remoteAddress + ']';
this._startSession();
this.logger.info( this.logger.info(
{ {
@ -141,7 +136,7 @@ class IMAPConnection extends EventEmitter {
'[%s] %s from %s to %s:%s', '[%s] %s from %s to %s:%s',
this.id, this.id,
this.secure ? 'Secure connection' : 'Connection', this.secure ? 'Secure connection' : 'Connection',
this.clientHostname, this.session.clientHostname,
this._socket && this._socket.localAddress, this._socket && this._socket.localAddress,
this._socket && this._socket.localPort this._socket && this._socket.localPort
); );
@ -154,7 +149,53 @@ class IMAPConnection extends EventEmitter {
' ' + ' ' +
this.id this.id
); );
}); };
// do not wait with initial response too long
let resolveTimer = setTimeout(() => {
clearTimeout(resolveTimer);
sendGreeting();
}, 1000);
let reverseCb = (err, hostnames) => {
clearTimeout(resolveTimer);
if (err) {
//ignore, no big deal
}
let clientHostname = hostnames && hostnames.shift();
this.session.clientHostname = this.clientHostname = clientHostname || '[' + this.remoteAddress + ']';
if (greetingSent && clientHostname) {
this.logger.info(
{
tnx: 'connect',
cid: this.id
},
'[%s] Resolved %s as %s in %ss',
this.id,
this.remoteAddress,
clientHostname,
((Date.now() - now) / 1000).toFixed(3)
);
}
// eslint-disable-line handle-callback-err
if (this._closing || this._closed) {
return;
}
sendGreeting();
};
// Resolve hostname for the remote IP
// we do not care for errors as we consider the ip as unresolved in this case, no big deal
try {
dns.reverse(this.remoteAddress, reverseCb);
} catch (E) {
// happens on invalid remote address
reverseCb(E);
}
} }
/** /**
@ -397,7 +438,7 @@ class IMAPConnection extends EventEmitter {
selected: this.selected, selected: this.selected,
remoteAddress: this.remoteAddress, remoteAddress: this.remoteAddress,
clientHostname: this.clientHostname, clientHostname: this.clientHostname || '[' + this.remoteAddress + ']',
writeStream: this.writeStream, writeStream: this.writeStream,
socket: this._socket, socket: this._socket,

View file

@ -8,7 +8,10 @@
"test": "mongo --eval 'db.dropDatabase()' wildduck-test && redis-cli -n 13 flushdb && NODE_ENV=test grunt", "test": "mongo --eval 'db.dropDatabase()' wildduck-test && redis-cli -n 13 flushdb && NODE_ENV=test grunt",
"apidoc": "apidoc -i lib/api/ -o docs/" "apidoc": "apidoc -i lib/api/ -o docs/"
}, },
"keywords": ["imap", "mail server"], "keywords": [
"imap",
"mail server"
],
"author": "Andris Reinman", "author": "Andris Reinman",
"license": "EUPL-1.1", "license": "EUPL-1.1",
"devDependencies": { "devDependencies": {
@ -57,7 +60,7 @@
"qrcode": "1.0.0", "qrcode": "1.0.0",
"restify": "6.3.4", "restify": "6.3.4",
"seq-index": "1.1.0", "seq-index": "1.1.0",
"smtp-server": "3.3.0", "smtp-server": "3.3.1",
"speakeasy": "2.0.0", "speakeasy": "2.0.0",
"tlds": "1.199.0", "tlds": "1.199.0",
"u2f": "0.1.3", "u2f": "0.1.3",