@@ -40,6 +40,7 @@ type Server struct {
40
40
readTimeoutMilliseconds int64
41
41
tlsPeerNameFunc TlsPeerNameFunc
42
42
datagramPool sync.Pool
43
+ errChannel chan error
43
44
}
44
45
45
46
//NewServer returns a new Server
@@ -61,6 +62,11 @@ func (s *Server) SetHandler(handler Handler) {
61
62
s .handler = handler
62
63
}
63
64
65
+ //Sets a channel for errors
66
+ func (s * Server ) SetErrChannel (c chan error ) {
67
+ s .errChannel = c
68
+ }
69
+
64
70
//Sets the connection timeout for TCP connections, in milliseconds
65
71
func (s * Server ) SetTimeout (millseconds int64 ) {
66
72
s .readTimeoutMilliseconds = millseconds
@@ -181,6 +187,9 @@ func (s *Server) goAcceptConnection(listener net.Listener) {
181
187
}
182
188
connection , err := listener .Accept ()
183
189
if err != nil {
190
+ if s .errChannel != nil {
191
+ s .errChannel <- & ListenerError {err }
192
+ }
184
193
continue
185
194
}
186
195
@@ -207,6 +216,9 @@ func (s *Server) goScanConnection(connection net.Conn) {
207
216
if tlsConn , ok := connection .(* tls.Conn ); ok {
208
217
// Handshake now so we get the TLS peer information
209
218
if err := tlsConn .Handshake (); err != nil {
219
+ if s .errChannel != nil {
220
+ s .errChannel <- & HandshakeError {err , remoteAddr , tlsConn .ConnectionState ()}
221
+ }
210
222
connection .Close ()
211
223
return
212
224
}
@@ -241,6 +253,9 @@ loop:
241
253
if scanCloser .Scan () {
242
254
s .parser ([]byte (scanCloser .Text ()), client , tlsPeer )
243
255
} else {
256
+ if err := scanCloser .Err (); err != nil && s .errChannel != nil {
257
+ s .errChannel <- & ScannerError {err , client , tlsPeer }
258
+ }
244
259
break loop
245
260
}
246
261
}
@@ -254,6 +269,9 @@ func (s *Server) parser(line []byte, client string, tlsPeer string) {
254
269
err := parser .Parse ()
255
270
if err != nil {
256
271
s .lastError = err
272
+ if s .errChannel != nil {
273
+ s .errChannel <- & ParserError {err }
274
+ }
257
275
}
258
276
259
277
logParts := parser .Dump ()
@@ -376,3 +394,56 @@ func (s *Server) goParseDatagrams() {
376
394
}
377
395
}()
378
396
}
397
+
398
+ // Error types
399
+ type ListenerError struct {
400
+ wrappedError error
401
+ }
402
+
403
+ func (l * ListenerError ) Error () string {
404
+ return l .wrappedError .Error ()
405
+ }
406
+
407
+ func (l * ListenerError ) Unwrap () error {
408
+ return l .wrappedError
409
+ }
410
+
411
+ type HandshakeError struct {
412
+ wrappedError error
413
+ RemoteAddr net.Addr
414
+ ConnectionState tls.ConnectionState
415
+ }
416
+
417
+ func (l * HandshakeError ) Error () string {
418
+ return l .wrappedError .Error ()
419
+ }
420
+
421
+ func (l * HandshakeError ) Unwrap () error {
422
+ return l .wrappedError
423
+ }
424
+
425
+ type ScannerError struct {
426
+ wrappedError error
427
+ Client string
428
+ TLSPeer string
429
+ }
430
+
431
+ func (l * ScannerError ) Error () string {
432
+ return l .wrappedError .Error ()
433
+ }
434
+
435
+ func (l * ScannerError ) Unwrap () error {
436
+ return l .wrappedError
437
+ }
438
+
439
+ type ParserError struct {
440
+ wrappedError error
441
+ }
442
+
443
+ func (l * ParserError ) Error () string {
444
+ return l .wrappedError .Error ()
445
+ }
446
+
447
+ func (l * ParserError ) Unwrap () error {
448
+ return l .wrappedError
449
+ }
0 commit comments