Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't try to parse hostname for RFC3164 over unix socket. #43

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
14 changes: 11 additions & 3 deletions format/automatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func detect(data []byte) (detected int, err error) {
return detectedUnknown, nil
}

func (f *Automatic) GetParser(line []byte) LogParser {
func (f *Automatic) getAnyParser(line []byte, isUnixSocket bool) LogParser {
switch format, _ := detect(line); format {
case detectedRFC3164:
return &parserWrapper{rfc3164.NewParser(line)}
return &parserWrapper{rfc3164.NewParser(line, isUnixSocket)}
case detectedRFC5424:
return &parserWrapper{rfc5424.NewParser(line)}
default:
Expand All @@ -69,10 +69,18 @@ func (f *Automatic) GetParser(line []byte) LogParser {
// will return detectedRFC6587. The line may also simply be malformed after the length in
// which case we will have detectedUnknown. In this case we return the simplest parser so
// the illegally formatted line is properly handled
return &parserWrapper{rfc3164.NewParser(line)}
return &parserWrapper{rfc3164.NewParser(line, isUnixSocket)}
}
}

func (f *Automatic) GetParser(line []byte) LogParser {
return f.getAnyParser(line, false)
}

func (f *Automatic) GetParserUnixSocket(line []byte) LogParser {
return f.getAnyParser(line, true)
}

func (f *Automatic) GetSplitFunc() bufio.SplitFunc {
return f.automaticScannerSplit
}
Expand Down
1 change: 1 addition & 0 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type LogParser interface {

type Format interface {
GetParser([]byte) LogParser
GetParserUnixSocket([]byte) LogParser
GetSplitFunc() bufio.SplitFunc
}

Expand Down
6 changes: 5 additions & 1 deletion format/rfc3164.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import (
type RFC3164 struct{}

func (f *RFC3164) GetParser(line []byte) LogParser {
return &parserWrapper{rfc3164.NewParser(line)}
return &parserWrapper{rfc3164.NewParser(line, false)}
}

func (f *RFC3164) GetParserUnixSocket(line []byte) LogParser {
return &parserWrapper{rfc3164.NewParser(line, true)}
}

func (f *RFC3164) GetSplitFunc() bufio.SplitFunc {
Expand Down
4 changes: 4 additions & 0 deletions format/rfc5424.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func (f *RFC5424) GetParser(line []byte) LogParser {
return &parserWrapper{rfc5424.NewParser(line)}
}

func (f *RFC5424) GetParserUnixSocket(line []byte) LogParser {
return f.GetParser(line)
}

func (f *RFC5424) GetSplitFunc() bufio.SplitFunc {
return nil
}
4 changes: 4 additions & 0 deletions format/rfc6587.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func (f *RFC6587) GetParser(line []byte) LogParser {
return &parserWrapper{rfc5424.NewParser(line)}
}

func (f *RFC6587) GetParserUnixSocket(line []byte) LogParser {
return f.GetParser(line)
}

func (f *RFC6587) GetSplitFunc() bufio.SplitFunc {
return rfc6587ScannerSplit
}
Expand Down
33 changes: 19 additions & 14 deletions internal/syslogparser/rfc3164/rfc3164.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (
)

type Parser struct {
buff []byte
cursor int
l int
priority syslogparser.Priority
version int
header header
message rfc3164message
location *time.Location
skipTag bool
buff []byte
cursor int
l int
priority syslogparser.Priority
version int
header header
message rfc3164message
location *time.Location
skipTag bool
skipHostname bool
}

type header struct {
Expand All @@ -29,12 +30,13 @@ type rfc3164message struct {
content string
}

func NewParser(buff []byte) *Parser {
func NewParser(buff []byte, skipHostname bool) *Parser {
return &Parser{
buff: buff,
cursor: 0,
l: len(buff),
location: time.UTC,
buff: buff,
cursor: 0,
l: len(buff),
location: time.UTC,
skipHostname: skipHostname,
}
}

Expand Down Expand Up @@ -186,6 +188,9 @@ func (p *Parser) parseTimestamp() (time.Time, error) {
}

func (p *Parser) parseHostname() (string, error) {
if p.skipHostname {
return "", nil
}
return syslogparser.ParseHostname(p.buff, &p.cursor, p.l)
}

Expand Down
9 changes: 8 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,14 @@ loop:
}

func (s *Server) parser(line []byte, client string, tlsPeer string) {
parser := s.format.GetParser(line)
var parser format.LogParser

if client == "" {
parser = s.format.GetParserUnixSocket(line)
} else {
parser = s.format.GetParser(line)
}

err := parser.Parse()
if err != nil {
s.lastError = err
Expand Down