44 "bufio"
55 "crypto/tls"
66 "errors"
7+ "io"
8+ "log"
79 "net"
810 "strings"
911 "sync"
@@ -189,21 +191,25 @@ func (s *Server) goAcceptConnection(listener net.Listener) {
189191 }
190192 connection , err := listener .Accept ()
191193 if err != nil {
194+ log .Printf ("Failed to accept connection, " +
195+ "proto=%s, addr=%s, error=%v" ,
196+ listener .Addr ().Network (), listener .Addr (), err ,
197+ )
192198 continue
193199 }
200+ log .Printf ("Accepted connection, proto=%s, remote=%s, local=%s" ,
201+ listener .Addr ().Network (), connection .RemoteAddr (), connection .LocalAddr (),
202+ )
194203
195- s .goScanConnection (connection )
204+ s .goReadConnection (connection )
196205 }
197206
198207 s .wait .Done ()
199208 }(listener )
200209}
201210
202- func (s * Server ) goScanConnection (connection net.Conn ) {
203- scanner := bufio .NewScanner (connection )
204- if sf := s .format .GetSplitFunc (); sf != nil {
205- scanner .Split (sf )
206- }
211+ func (s * Server ) goReadConnection (connection net.Conn ) {
212+ reader := bufio .NewReader (connection )
207213
208214 remoteAddr := connection .RemoteAddr ()
209215 var client string
@@ -215,27 +221,36 @@ func (s *Server) goScanConnection(connection net.Conn) {
215221 if tlsConn , ok := connection .(* tls.Conn ); ok {
216222 // Handshake now so we get the TLS peer information
217223 if err := tlsConn .Handshake (); err != nil {
224+ log .Printf ("Failed to complete TLS handshake, closing connection, " +
225+ "remote=%s, local=%s, error=%v" ,
226+ tlsConn .RemoteAddr (), tlsConn .LocalAddr (), err ,
227+ )
218228 connection .Close ()
219229 return
220230 }
231+ log .Printf ("TLS handshake complete, remote=%s, local=%s" ,
232+ tlsConn .RemoteAddr (), tlsConn .LocalAddr (),
233+ )
221234 if s .tlsPeerNameFunc != nil {
222235 var ok bool
223236 tlsPeer , ok = s .tlsPeerNameFunc (tlsConn )
224237 if ! ok {
238+ log .Printf ("Failed to get TLS peer name, closing connection, " +
239+ "remote=%s, local=%s" , tlsConn .RemoteAddr (), tlsConn .LocalAddr (),
240+ )
225241 connection .Close ()
226242 return
227243 }
228244 }
229245 }
230246
231- var scanCloser * ScanCloser
232- scanCloser = & ScanCloser {scanner , connection }
247+ readCloser := & ReadCloser {reader , connection }
233248
234249 s .wait .Add (1 )
235- go s .scan ( scanCloser , client , tlsPeer )
250+ go s .read ( readCloser , client , tlsPeer )
236251}
237252
238- func (s * Server ) scan ( scanCloser * ScanCloser , client string , tlsPeer string ) {
253+ func (s * Server ) read ( readCloser * ReadCloser , client string , tlsPeer string ) {
239254loop:
240255 for {
241256 select {
@@ -244,15 +259,33 @@ loop:
244259 default :
245260 }
246261 if s .readTimeoutMilliseconds > 0 {
247- scanCloser .closer .SetReadDeadline (time .Now ().Add (time .Duration (s .readTimeoutMilliseconds ) * time .Millisecond ))
262+ readCloser .closer .SetReadDeadline (time .Now ().Add (time .Duration (s .readTimeoutMilliseconds ) * time .Millisecond ))
248263 }
249- if scanCloser .Scan () {
250- s .parser ([]byte (scanCloser .Text ()), client , tlsPeer )
251- } else {
264+ // Read up to and including first '<'
265+ token , err := readCloser .ReadString ('<' )
266+ if token != "" {
267+ // Re-add delimiter to start; remove from end
268+ token = "<" + token [:len (token )- 1 ]
269+ // Parse as syslog
270+ s .parser ([]byte (token ), client , tlsPeer )
271+ }
272+ // Break loop on error
273+ if err != nil {
274+ if err == io .EOF {
275+ log .Println ("EOF when reading token" )
276+ } else {
277+ log .Printf ("Error when reading token: %v" , err )
278+ }
252279 break loop
253280 }
254281 }
255- scanCloser .closer .Close ()
282+ // Close connection
283+ if conn , ok := readCloser .closer .(net.Conn ); ok {
284+ log .Printf ("Closing connection, proto=%s, remote=%s, local=%s" ,
285+ conn .LocalAddr ().Network (), conn .RemoteAddr (), conn .LocalAddr (),
286+ )
287+ }
288+ readCloser .closer .Close ()
256289
257290 s .wait .Done ()
258291}
@@ -318,8 +351,8 @@ type TimeoutCloser interface {
318351 SetReadDeadline (t time.Time ) error
319352}
320353
321- type ScanCloser struct {
322- * bufio.Scanner
354+ type ReadCloser struct {
355+ * bufio.Reader
323356 closer TimeoutCloser
324357}
325358
0 commit comments