From 90610793616b3c469bedad671347d641d035f094 Mon Sep 17 00:00:00 2001 From: Andreas Neue Date: Mon, 25 Jul 2016 18:48:41 +0200 Subject: [PATCH 1/2] Authentication hook --- client.go | 5 +++++ server.go | 29 ++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/client.go b/client.go index 294723a..727f68a 100644 --- a/client.go +++ b/client.go @@ -16,6 +16,7 @@ import ( type Client interface { Name() string + Password() string Register(bool) Send(*irc.Message) Receive(*irc.Message) @@ -74,6 +75,10 @@ func (cl *RemoteClient) Name() string { return cl.name } +func (cl *RemoteClient) Password() string { + return cl.password +} + func (cl *RemoteClient) Register(success bool) { cl.registered <- success } diff --git a/server.go b/server.go index 30a3846..6c631ce 100644 --- a/server.go +++ b/server.go @@ -48,6 +48,8 @@ type Server struct { connectionsCurrent float64 connectionsCount float64 queueLen float64 + + authCallback func(name, pass string) bool } // Create a new server instance. @@ -82,6 +84,10 @@ func NewServer(configPath, software, version string) *Server { return sv } +func (sv *Server) SetAuthCallback(authCB func(name, pass string) bool) { + sv.authCallback = authCB +} + // Open the listening port and start the main server loop. func (sv *Server) Run() { xlog.Info("%s/%s", sv.software, sv.version) @@ -142,16 +148,21 @@ func (sv *Server) dispatcher() { clid := strings.ToLower(cl.Name()) if _, exists := sv.clients[clid]; exists { cl.Register(false) - xlog.Info("Client registration failed: '%s'", clid) - } else { - sv.clients[clid] = cl - sv.sendLogon(cl.Name()) - sv.connectionsCurrent = float64(len(sv.clients)) - cl.Register(true) - xlog.Info("Client registered: '%s'", clid) - xlog.Info("Server has %d client(s)", len(sv.clients)) - xlog.Debug("Goroutines running: %d", runtime.NumGoroutine()) + xlog.Info("Client registration failed: '%s' (client exists)", clid) + continue } + if !sv.authCallback(cl.Name(), cl.Password()) { + cl.Register(false) + xlog.Info("Client registration failed: '%s' (wrong password)", clid) + continue + } + sv.clients[clid] = cl + sv.sendLogon(cl.Name()) + sv.connectionsCurrent = float64(len(sv.clients)) + cl.Register(true) + xlog.Info("Client registered: '%s'", clid) + xlog.Info("Server has %d client(s)", len(sv.clients)) + xlog.Debug("Goroutines running: %d", runtime.NumGoroutine()) case cl := <-sv.delq: clid := strings.ToLower(cl.Name()) cl.Destroy() From 0becd1d9ac134f0ef80bf19baeff038e7e85a893 Mon Sep 17 00:00:00 2001 From: Andreas Neue Date: Tue, 26 Jul 2016 22:10:50 +0200 Subject: [PATCH 2/2] Changed dispacher's sleep strategy in order to reduce cpu load --- server.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/server.go b/server.go index 6c631ce..b8dc53c 100644 --- a/server.go +++ b/server.go @@ -38,9 +38,10 @@ type Server struct { clients map[string]Client - chUsers map[string]map[string]string - chTopics map[string]string - chModes map[string]map[string]bool + chUsers map[string]map[string]string + chTopics map[string]string + chModes map[string]map[string]bool + config *conf.ConfigFile configPath string @@ -62,6 +63,7 @@ func NewServer(configPath, software, version string) *Server { sv.delq = make(chan Client, 128) sv.clients = make(map[string]Client) + sv.chUsers = make(map[string]map[string]string) sv.chTopics = make(map[string]string) sv.chModes = make(map[string]map[string]bool) @@ -138,7 +140,6 @@ func (sv *Server) listenTls(laddr string) { func (sv *Server) dispatcher() { for { - time.Sleep(1 * time.Microsecond) sv.queueLen = float64(len(sv.queue)) select { case msg := <-sv.queue: @@ -178,6 +179,7 @@ func (sv *Server) dispatcher() { xlog.Info("Server has %d client(s)", len(sv.clients)) xlog.Debug("Goroutines running: %d", runtime.NumGoroutine()) default: + time.Sleep(100 * time.Millisecond) } } }