From 4fbe73dc8a8d7e79787f9b8cb0cea804670a1088 Mon Sep 17 00:00:00 2001 From: Mike Luu Date: Tue, 10 Aug 2021 12:10:19 -0700 Subject: [PATCH] Allow errors to be captured Added Server.SetErrChannel which will be used to send errors fixes #78 --- server.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/server.go b/server.go index 352597b..06fdd4e 100644 --- a/server.go +++ b/server.go @@ -40,6 +40,7 @@ type Server struct { readTimeoutMilliseconds int64 tlsPeerNameFunc TlsPeerNameFunc datagramPool sync.Pool + errChannel chan error } //NewServer returns a new Server @@ -61,6 +62,11 @@ func (s *Server) SetHandler(handler Handler) { s.handler = handler } +//Sets a channel for errors +func (s *Server) SetErrChannel(c chan error) { + s.errChannel = c +} + //Sets the connection timeout for TCP connections, in milliseconds func (s *Server) SetTimeout(millseconds int64) { s.readTimeoutMilliseconds = millseconds @@ -181,6 +187,9 @@ func (s *Server) goAcceptConnection(listener net.Listener) { } connection, err := listener.Accept() if err != nil { + if s.errChannel != nil { + s.errChannel <- &ListenerError{err} + } continue } @@ -207,6 +216,9 @@ func (s *Server) goScanConnection(connection net.Conn) { if tlsConn, ok := connection.(*tls.Conn); ok { // Handshake now so we get the TLS peer information if err := tlsConn.Handshake(); err != nil { + if s.errChannel != nil { + s.errChannel <- &HandshakeError{err, remoteAddr, tlsConn.ConnectionState()} + } connection.Close() return } @@ -241,6 +253,9 @@ loop: if scanCloser.Scan() { s.parser([]byte(scanCloser.Text()), client, tlsPeer) } else { + if err := scanCloser.Err(); err != nil && s.errChannel != nil { + s.errChannel <- &ScannerError{err, client, tlsPeer} + } break loop } } @@ -254,6 +269,9 @@ func (s *Server) parser(line []byte, client string, tlsPeer string) { err := parser.Parse() if err != nil { s.lastError = err + if s.errChannel != nil { + s.errChannel <- &ParserError{err} + } } logParts := parser.Dump() @@ -376,3 +394,56 @@ func (s *Server) goParseDatagrams() { } }() } + +// Error types +type ListenerError struct { + wrappedError error +} + +func (l *ListenerError) Error() string { + return l.wrappedError.Error() +} + +func (l *ListenerError) Unwrap() error { + return l.wrappedError +} + +type HandshakeError struct { + wrappedError error + RemoteAddr net.Addr + ConnectionState tls.ConnectionState +} + +func (l *HandshakeError) Error() string { + return l.wrappedError.Error() +} + +func (l *HandshakeError) Unwrap() error { + return l.wrappedError +} + +type ScannerError struct { + wrappedError error + Client string + TLSPeer string +} + +func (l *ScannerError) Error() string { + return l.wrappedError.Error() +} + +func (l *ScannerError) Unwrap() error { + return l.wrappedError +} + +type ParserError struct { + wrappedError error +} + +func (l *ParserError) Error() string { + return l.wrappedError.Error() +} + +func (l *ParserError) Unwrap() error { + return l.wrappedError +}