Skip to content

Commit

Permalink
Export Logger to customize ldapserver output.
Browse files Browse the repository at this point in the history
use ldap.Logger = ldap.DiscardingLogger to discard ldapserver logs
  • Loading branch information
vjeantet committed May 21, 2016
1 parent 5057261 commit 77354ea
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 27 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The package supports
* Unbind request is implemented, but is handled internally to close the connection.
* Graceful stopping
* Basic request routing inspired by [net/http ServeMux](http://golang.org/pkg/net/http/#ServeMux)
* Logger customisation (log interface)

# Default behaviors
## Abandon request
Expand All @@ -38,6 +39,9 @@ import (
)

func main() {
//ldap logger
ldap.Logger = log.New(os.Stdout, "[server] ", log.LstdFlags)

//Create a new LDAP Server
server := ldap.NewServer()

Expand Down
25 changes: 12 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ldapserver

import (
"bufio"
"log"
"net"
"sync"
"time"
Expand Down Expand Up @@ -51,7 +50,7 @@ func (c *client) serve() {
c.closing = make(chan bool)
if onc := c.srv.OnNewConnection; onc != nil {
if err := onc(c.rwc); err != nil {
log.Printf("Erreur OnNewConnection: %s", err)
Logger.Printf("Erreur OnNewConnection: %s", err)
return
}
}
Expand Down Expand Up @@ -106,9 +105,9 @@ func (c *client) serve() {
messagePacket, err := readMessagePacket(c.br)
if err != nil {
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
log.Printf("Sorry client %d, i can not wait anymore (reading timeout) ! %s", c.Numero, err)
Logger.Printf("Sorry client %d, i can not wait anymore (reading timeout) ! %s", c.Numero, err)
} else {
log.Printf("Error readMessagePacket: %s", err)
Logger.Printf("Error readMessagePacket: %s", err)
}
return
}
Expand All @@ -117,10 +116,10 @@ func (c *client) serve() {
message, err := messagePacket.readMessage()

if err != nil {
log.Printf("Error reading Message : %s\n\t%x", err.Error(), messagePacket.bytes)
Logger.Printf("Error reading Message : %s\n\t%x", err.Error(), messagePacket.bytes)
continue
}
log.Printf("<<< %d - %s - hex=%x", c.Numero, message.ProtocolOpName(), messagePacket)
Logger.Printf("<<< %d - %s - hex=%x", c.Numero, message.ProtocolOpName(), messagePacket)

// TODO: Use a implementation to limit runnuning request by client
// solution 1 : when the buffered output channel is full, send a busy
Expand Down Expand Up @@ -158,36 +157,36 @@ func (c *client) serve() {
// * close client connection
// * signal to server that client shutdown is ok
func (c *client) close() {
log.Printf("client %d close()", c.Numero)
Logger.Printf("client %d close()", c.Numero)
close(c.closing)

// stop reading from client
c.rwc.SetReadDeadline(time.Now().Add(time.Millisecond))
log.Printf("client %d close() - stop reading from client", c.Numero)
Logger.Printf("client %d close() - stop reading from client", c.Numero)

// signals to all currently running request processor to stop
c.mutex.Lock()
for messageID, request := range c.requestList {
log.Printf("Client %d close() - sent abandon signal to request[messageID = %d]", c.Numero, messageID)
Logger.Printf("Client %d close() - sent abandon signal to request[messageID = %d]", c.Numero, messageID)
go request.Abandon()
}
c.mutex.Unlock()
log.Printf("client %d close() - Abandon signal sent to processors", c.Numero)
Logger.Printf("client %d close() - Abandon signal sent to processors", c.Numero)

c.wg.Wait() // wait for all current running request processor to end
close(c.chanOut) // No more message will be sent to client, close chanOUT
log.Printf("client [%d] request processors ended", c.Numero)
Logger.Printf("client [%d] request processors ended", c.Numero)

<-c.writeDone // Wait for the last message sent to be written
c.rwc.Close() // close client connection
log.Printf("client [%d] connection closed", c.Numero)
Logger.Printf("client [%d] connection closed", c.Numero)

c.srv.wg.Done() // signal to server that client shutdown is ok
}

func (c *client) writeMessage(m *ldap.LDAPMessage) {
data, _ := m.Write()
log.Printf(">>> %d - %s - hex=%x", c.Numero, m.ProtocolOpName(), data.Bytes())
Logger.Printf(">>> %d - %s - hex=%x", c.Numero, m.ProtocolOpName(), data.Bytes())
c.bw.Write(data.Bytes())
c.bw.Flush()
}
Expand Down
6 changes: 5 additions & 1 deletion examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
)

func main() {

//ldap logger
ldap.Logger = log.New(os.Stdout, "[server] ", log.LstdFlags)

//Create a new LDAP Server
server := ldap.NewServer()

Expand All @@ -38,7 +42,7 @@ func handleBind(w ldap.ResponseWriter, m *ldap.Message) {
res := ldap.NewBindResponse(ldap.LDAPResultSuccess)

if string(r.Name()) == "login" {
w.Write(res)
// w.Write(res)
return
}

Expand Down
33 changes: 33 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ldapserver

import (
"io/ioutil"
"log"
"os"
)

var Logger logger

// Logger represents log.Logger functions from the standard library
type logger interface {
Fatal(v ...interface{})
Fatalf(format string, v ...interface{})
Fatalln(v ...interface{})

Panic(v ...interface{})
Panicf(format string, v ...interface{})
Panicln(v ...interface{})

Print(v ...interface{})
Printf(format string, v ...interface{})
Println(v ...interface{})
}

func init() {
Logger = log.New(os.Stdout, "", log.LstdFlags)
}

var (
// DiscardingLogger can be used to disable logging output
DiscardingLogger = log.New(ioutil.Discard, "", 0)
)
9 changes: 4 additions & 5 deletions route.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ldapserver

import (
"log"
"strings"

ldap "github.com/vjeantet/goldap/message"
Expand Down Expand Up @@ -149,10 +148,10 @@ func (h *RouteMux) ServeLDAP(w ResponseWriter, r *Message) {
}

if route.label != "" {
log.Printf("")
log.Printf(" ROUTE MATCH ; %s", route.label)
log.Printf("")
// log.Printf(" ROUTE MATCH ; %s", runtime.FuncForPC(reflect.ValueOf(route.handler).Pointer()).Name())
Logger.Printf("")
Logger.Printf(" ROUTE MATCH ; %s", route.label)
Logger.Printf("")
// Logger.Printf(" ROUTE MATCH ; %s", runtime.FuncForPC(reflect.ValueOf(route.handler).Pointer()).Name())
}

route.handler(w, r)
Expand Down
15 changes: 7 additions & 8 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ldapserver

import (
"bufio"
"log"
"net"
"sync"
"time"
Expand Down Expand Up @@ -55,7 +54,7 @@ func (s *Server) ListenAndServe(addr string, options ...func(*Server)) error {
if e != nil {
return e
}
log.Printf("Listening on %s\n", addr)
Logger.Printf("Listening on %s\n", addr)

for _, option := range options {
option(s)
Expand All @@ -69,15 +68,15 @@ func (s *Server) serve() error {
defer s.Listener.Close()

if s.Handler == nil {
log.Panicln("No LDAP Request Handler defined")
Logger.Panicln("No LDAP Request Handler defined")
}

i := 0

for {
select {
case <-s.chDone:
log.Print("Stopping server")
Logger.Print("Stopping server")
s.Listener.Close()
return nil
default:
Expand All @@ -95,7 +94,7 @@ func (s *Server) serve() error {
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
continue
}
log.Println(err)
Logger.Println(err)
}

cli, err := s.newClient(rw)
Expand All @@ -106,7 +105,7 @@ func (s *Server) serve() error {

i = i + 1
cli.Numero = i
log.Printf("Connection client [%d] from %s accepted", cli.Numero, cli.rwc.RemoteAddr().String())
Logger.Printf("Connection client [%d] from %s accepted", cli.Numero, cli.rwc.RemoteAddr().String())
s.wg.Add(1)
go cli.serve()
}
Expand Down Expand Up @@ -138,7 +137,7 @@ func (s *Server) newClient(rwc net.Conn) (c *client, err error) {
// In either case, when the LDAP session is terminated.
func (s *Server) Stop() {
close(s.chDone)
log.Print("gracefully closing client connections...")
Logger.Print("gracefully closing client connections...")
s.wg.Wait()
log.Print("all clients connection closed")
Logger.Print("all clients connection closed")
}

0 comments on commit 77354ea

Please sign in to comment.