Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Server struct {
readTimeoutMilliseconds int64
tlsPeerNameFunc TlsPeerNameFunc
datagramPool sync.Pool
errChannel chan error
}

//NewServer returns a new Server
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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()
Expand Down Expand Up @@ -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
}