1 Star 0 Fork 0

carlmax_my/console-core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ldap.go 1.56 KB
一键复制 编辑 原始数据 按行查看 历史
carlmax_my 提交于 2024-12-02 21:32 . init project
package ldap
import (
"crypto/tls"
"net"
"strings"
"time"
"gitee.com/carlmax_my/console-core-go/pkg/errors"
"github.com/go-ldap/ldap/v3"
"github.com/randolphcyg/ldappool"
"github.com/samber/lo"
)
var (
DEF_LDAP_TIMEOUT time.Duration = 20 * time.Second
)
// 初始化连接池
func NewLdapPool(opts *LdapConnOptions) (ldappool.Pool, error) {
// 初始化ldap连接池
ldapPool, err := ldappool.NewChannelPool(1, 1000, "originalLdapPool",
func(s string) (ldap.Client, error) {
var dialer = &net.Dialer{
Timeout: lo.Ternary(opts.Timeout <= 0, DEF_LDAP_TIMEOUT, opts.Timeout),
}
var conn *ldap.Conn
var err error
if strings.HasPrefix(opts.ConnUrl, "ldaps://") {
conn, err = ldap.DialURL(opts.ConnUrl, ldap.DialWithDialer(dialer), ldap.DialWithTLSConfig(&tls.Config{InsecureSkipVerify: true}))
} else {
conn, err = ldap.DialURL(opts.ConnUrl, ldap.DialWithDialer(dialer))
}
if err != nil {
return nil, errors.Wrap(err, "Fail to dial ldap url")
}
if opts.SslEncryption {
// 重新连接TLS
if err = conn.StartTLS(&tls.Config{InsecureSkipVerify: true, ClientAuth: tls.NoClientCert}); err != nil {
return nil, errors.Wrap(err, "Fail to start ldap tls")
}
}
// 与只读用户绑定
if err = conn.Bind(opts.AdminAccount, opts.Password); err != nil {
return nil, errors.Wrap(err, "ldap admin user auth failed")
}
return conn, nil
}, []uint16{ldap.LDAPResultTimeLimitExceeded, ldap.ErrorNetwork})
if err != nil {
return nil, errors.Wrap(err, "ldap NewChannelPool error")
}
return ldapPool, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/carlmax_my/console-core-go.git
git@gitee.com:carlmax_my/console-core-go.git
carlmax_my
console-core-go
console-core-go
v0.0.10

搜索帮助

0d507c66 1850385 C8b1a773 1850385