此软件包仍在开发中。
ldapserver 是一个辅助库,用于构建能够使用 LDAP 协议进行通信的服务器软件。它可以是 LDAP 的替代实现、自定义 LDAP 代理,甚至是能够将其 API “伪装”成 LDAP 服务器的完全不同的后端。
该软件包支持:
如果您没有设置处理 AbandonRequest 的路由,该软件包将为您处理它(信号发送到 message.Done 通道)。
当没有路由匹配请求时,服务器将首先尝试调用特殊的 NotFound 路由,如果未指定任何内容,它将返回一个 UnwillingToResponse 错误代码(53)。
欢迎贡献、评论 :)
// Listen to 10389 port for LDAP Request
// and route bind request to the handleBind func
package main
import (
"log"
"os"
"os/signal"
"syscall"
ldap "github.com/vjeantet/ldapserver"
)
func main() {
//ldap logger
ldap.Logger = log.New(os.Stdout, "[server] ", log.LstdFlags)
//Create a new LDAP Server
server := ldap.NewServer()
routes := ldap.NewRouteMux()
routes.Bind(handleBind)
server.Handle(routes)
// listen on 10389
go server.ListenAndServe("127.0.0.1:10389")
// When CTRL+C, SIGINT and SIGTERM signal occurs
// Then stop server gracefully
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
close(ch)
server.Stop()
}
// handleBind return Success if login == mysql
func handleBind(w ldap.ResponseWriter, m *ldap.Message) {
r := m.GetBindRequest()
res := ldap.NewBindResponse(ldap.LDAPResultSuccess)
if string(r.Name()) == "myLogin" {
w.Write(res)
return
}
log.Printf("Bind failed User=%s, Pass=%s", string(r.Name()), string(r.AuthenticationSimple()))
res.SetResultCode(ldap.LDAPResultInvalidCredentials)
res.SetDiagnosticMessage("invalid credentials")
w.Write(res)
}
请查看 "examples" 文件夹