GH-10: Client not interacting with AD, and has bad socket.connect() calls
This commit is contained in:
parent
1ec7a76b66
commit
0be6fc9dcb
106
lib/client.js
106
lib/client.js
|
@ -104,8 +104,7 @@ function Client(options) {
|
||||||
throw new TypeError('options.socketPath must be a string');
|
throw new TypeError('options.socketPath must be a string');
|
||||||
if (options.log4js && typeof(options.log4js) !== 'object')
|
if (options.log4js && typeof(options.log4js) !== 'object')
|
||||||
throw new TypeError('options.log4s must be an object');
|
throw new TypeError('options.log4s must be an object');
|
||||||
if (options.numConnections && typeof(options.numConnections) !== 'number')
|
|
||||||
throw new TypeError('options.numConnections must be a number');
|
|
||||||
if (!xor(options.url, options.socketPath))
|
if (!xor(options.url, options.socketPath))
|
||||||
throw new TypeError('options.url ^ options.socketPath required');
|
throw new TypeError('options.url ^ options.socketPath required');
|
||||||
|
|
||||||
|
@ -119,9 +118,6 @@ function Client(options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.log4js = options.log4js || logStub;
|
this.log4js = options.log4js || logStub;
|
||||||
this.numConnections = Math.abs(options.numConnections) || 1;
|
|
||||||
this.connections = [];
|
|
||||||
this.currentConnection = 0;
|
|
||||||
this.connectOptions = {
|
this.connectOptions = {
|
||||||
port: self.url ? self.url.port : options.socketPath,
|
port: self.url ? self.url.port : options.socketPath,
|
||||||
host: self.url ? self.url.hostname : undefined
|
host: self.url ? self.url.hostname : undefined
|
||||||
|
@ -168,33 +164,16 @@ function Client(options) {
|
||||||
self.emit('connect', c.ldap.id);
|
self.emit('connect', c.ldap.id);
|
||||||
});
|
});
|
||||||
c.on('end', function() {
|
c.on('end', function() {
|
||||||
self.log.trace('%s end', c.ldap.id);
|
self.emit('end');
|
||||||
c.ldap.connected = false;
|
|
||||||
if (!self.shutdown)
|
|
||||||
c.connect();
|
|
||||||
});
|
});
|
||||||
c.addListener('close', function(had_err) {
|
c.addListener('close', function(had_err) {
|
||||||
self.log.trace('%s close; had_err=%j', c.ldap.id, had_err);
|
self.emit('close', had_err);
|
||||||
c.ldap.connected = false;
|
|
||||||
if (!self.shutdown)
|
|
||||||
c.connect();
|
|
||||||
});
|
});
|
||||||
c.on('error', function(err) {
|
c.on('error', function(err) {
|
||||||
self.log.warn('%s unexpected connection error %s', c.ldap.id, err);
|
self.emit('error', err);
|
||||||
self.emit('error', err, c.ldap.id);
|
|
||||||
c.ldap.connected = false;
|
|
||||||
if (!self.shutdown) {
|
|
||||||
c.end();
|
|
||||||
c.connect();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
c.on('timeout', function() {
|
c.on('timeout', function() {
|
||||||
self.log.trace('%s timed out', c.ldap.id);
|
self.emit('timeout');
|
||||||
c.ldap.connected = false;
|
|
||||||
if (!self.shutdown) {
|
|
||||||
c.end();
|
|
||||||
c.connect();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
c.on('data', function(data) {
|
c.on('data', function(data) {
|
||||||
if (self.log.isTraceEnabled())
|
if (self.log.isTraceEnabled())
|
||||||
|
@ -208,8 +187,8 @@ function Client(options) {
|
||||||
|
|
||||||
var callback = c.ldap.messages[message.messageID];
|
var callback = c.ldap.messages[message.messageID];
|
||||||
if (!callback) {
|
if (!callback) {
|
||||||
self.log.error('%s: received unsolicited message: %j',
|
self.log.error('%s: received unsolicited message: %j', c.ldap.id,
|
||||||
c.ldap.id, message.json);
|
message.json);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,9 +197,7 @@ function Client(options) {
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < this.numConnections; i++) {
|
self.connection = newConnection();
|
||||||
self.connections.push(newConnection());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
util.inherits(Client, EventEmitter);
|
util.inherits(Client, EventEmitter);
|
||||||
module.exports = Client;
|
module.exports = Client;
|
||||||
|
@ -258,26 +235,7 @@ Client.prototype.bind = function(name, credentials, controls, callback) {
|
||||||
controls: controls
|
controls: controls
|
||||||
});
|
});
|
||||||
|
|
||||||
var cbIssued = false;
|
return self._send(req, [errors.LDAP_SUCCESS], callback);
|
||||||
var finished = 0;
|
|
||||||
function _callback(err, res) {
|
|
||||||
if (err) {
|
|
||||||
if (!cbIssued) {
|
|
||||||
cbIssued = true;
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (++finished >= self.connections.length && !cbIssued) {
|
|
||||||
cbIssued = true;
|
|
||||||
self.emit('bind');
|
|
||||||
return callback(null, res);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.connections.forEach(function(c) {
|
|
||||||
return self._send(req, [errors.LDAP_SUCCESS], _callback, c);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -646,60 +604,20 @@ Client.prototype.unbind = function(callback) {
|
||||||
if (!callback)
|
if (!callback)
|
||||||
callback = function defUnbindCb() { self.log.trace('disconnected'); };
|
callback = function defUnbindCb() { self.log.trace('disconnected'); };
|
||||||
|
|
||||||
this.shutdown = true;
|
|
||||||
var req = new UnbindRequest();
|
var req = new UnbindRequest();
|
||||||
var finished = 0;
|
return self._send(req, 'unbind', callback);
|
||||||
var cbIssued = false;
|
|
||||||
|
|
||||||
function _callback(err, res) {
|
|
||||||
if (err) {
|
|
||||||
if (!cbIssued) {
|
|
||||||
cbIssued = true;
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (++finished >= self.connections.length && !cbIssued) {
|
|
||||||
cbIssued = true;
|
|
||||||
self.emit('unbind');
|
|
||||||
return callback(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.connections.forEach(function(c) {
|
|
||||||
return self._send(req, 'unbind', _callback, c);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Client.prototype._send = function(message, expect, callback, conn) {
|
Client.prototype._send = function(message, expect, callback) {
|
||||||
assert.ok(message);
|
assert.ok(message);
|
||||||
assert.ok(expect);
|
assert.ok(expect);
|
||||||
assert.ok(callback);
|
assert.ok(callback);
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
// First select a connection
|
var conn = self.connection;
|
||||||
// Note bind and unbind are special in that they will pass in the
|
|
||||||
// connection since they iterate over the whole pool
|
|
||||||
if (!conn) {
|
|
||||||
function nextConn() {
|
|
||||||
if (++self.currentConnection >= self.connections.length)
|
|
||||||
self.currentConnection = 0;
|
|
||||||
|
|
||||||
return self.connections[self.currentConnection];
|
|
||||||
}
|
|
||||||
|
|
||||||
var save = this.currentConnection;
|
|
||||||
while ((conn = nextConn()) && save !== this.currentConnection);
|
|
||||||
|
|
||||||
if (!conn) {
|
|
||||||
self.emit('error', new DisconnectedError('No connections available'));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert.ok(conn);
|
|
||||||
|
|
||||||
// Now set up the callback in the messages table
|
// Now set up the callback in the messages table
|
||||||
message.messageID = conn.ldap.nextMessageID;
|
message.messageID = conn.ldap.nextMessageID;
|
||||||
|
|
|
@ -127,13 +127,13 @@ SearchRequest.prototype._toBer = function(ber) {
|
||||||
var f = this.filter || new filters.PresenceFilter({attribute: 'objectclass'});
|
var f = this.filter || new filters.PresenceFilter({attribute: 'objectclass'});
|
||||||
ber = f.toBer(ber);
|
ber = f.toBer(ber);
|
||||||
|
|
||||||
|
ber.startSequence(Ber.Sequence | Ber.Constructor);
|
||||||
if (this.attributes && this.attributes.length) {
|
if (this.attributes && this.attributes.length) {
|
||||||
ber.startSequence(Ber.Sequence | Ber.Constructor);
|
|
||||||
this.attributes.forEach(function(a) {
|
this.attributes.forEach(function(a) {
|
||||||
ber.writeString(a);
|
ber.writeString(a);
|
||||||
});
|
});
|
||||||
ber.endSequence();
|
|
||||||
}
|
}
|
||||||
|
ber.endSequence();
|
||||||
|
|
||||||
return ber;
|
return ber;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue