简体   繁体   English

ldapjs连接在一段时间后超时

[英]ldapjs connection times out after certain period of time

I have some issues regarding searchEntry in LDAP repository using LDAPjs.我对使用 LDAPjs 的 LDAP 存储库中的 searchEntry 有一些问题。 I am not very familiar with LDAP and because of that I might be missing something in the client implementation.我对 LDAP 不是很熟悉,因此我可能在客户端实现中遗漏了一些东西。 The problem is that after some period of time LDAP there is no response from LDAP server, just nothing none of the callbacks are invoked.问题是在一段时间后 LDAP 没有来自 LDAP 服务器的响应,只是没有任何回调被调用。

  const ldapClient = ldap.createClient({
      url: 'ldap://some.ldap.server',
      timeout: 3000,
      connectTimeout: 6000
    });

  ldapClient.search('c=XX', opts, (err, res) => {
    if (err) {
      ldapClient.unbind(function(err) {
        if (err) {
          console.log(err)
        }
      });
      return next(null);
    }
  res.once('searchEntry', (entry) => {
    ldapClient.unbind(function(err) {
      if (err) {
      console.log(err)
      }
    });
    return next(entry);
  });

  res.on('error', (error) => {
    ldapClient.unbind(function(err) {
      if (err) {
      console.log(err)
      }
    });
    return next(null, new Error(error.message));
  });
});

Pass the reconnect flag as true while creating the LDAP client and don't unbind it in the way you have done.在创建 LDAP 客户端时将reconnect标志传递为true ,并且不要以您所做的方式解除绑定。 I guess it also hinders the reconnection.我想这也阻碍了重新连接。 Unbind it only after successful search operation.只有在搜索操作成功后才能解除绑定。

This code works for me: (The values are dummy of course)这段代码对我有用:(当然,这些值是虚拟​​的)

var ldap = require('ldapjs');
var tlsOptions = {
    host: 'example.com',
    port: '636',
    ca: [fs.readFileSync('./path/to/cert.pem')]
};
var client = ldap.createClient({
    url: 'ldaps://example.com:636',
    reconnect: true
    tlsOptions: tlsOptions
});

client.bind(username, password, function (err) {
    if (err) {
        console.log('Error occurred while binding');
    } else {
        var base = 'cn=admin,dc=example,dc=com';
        var search_options = {
            scope: 'sub',
            filter: '(&(objectClass=*)(CN=' + username + '))',
            attrs: 'attrs'
        };
        client.search(base, search_options, function (err, res) {
            if (err) {
                console.log('Error occurred while ldap search');
            } else {
                res.on('searchEntry', function (entry) {
                    console.log('Entry', JSON.stringify(entry.object));
                });
                res.on('searchReference', function (referral) {
                    console.log('Referral', referral);
                });
                res.on('error', function (err) {
                    console.log('Error is', err);
                });
                res.on('end', function (result) {
                    console.log('Result is', result);
                });
            }
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM