-
Notifications
You must be signed in to change notification settings - Fork 47
/
rfc3164.go
346 lines (275 loc) · 6.1 KB
/
rfc3164.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package rfc3164
import (
"bytes"
"math"
"time"
"github.com/jeromer/syslogparser"
"github.com/jeromer/syslogparser/parsercommon"
)
const (
// according to https://tools.ietf.org/html/rfc3164#section-4.1
// "The total length of the packet MUST be 1024 bytes or less"
// However we will accept a bit more while protecting from exhaustion
MAX_PACKET_LEN = 2048
)
type Parser struct {
buff []byte
cursor int
l int
priority *parsercommon.Priority
version int
header *header
message *message
location *time.Location
hostname string
customTag string
customTimestampFormat string
}
type header struct {
timestamp time.Time
hostname string
}
type message struct {
tag string
content string
}
func NewParser(buff []byte) *Parser {
return &Parser{
buff: buff,
cursor: 0,
location: time.UTC,
l: int(
math.Min(
float64(len(buff)),
MAX_PACKET_LEN,
),
),
}
}
// Forces a priority for this parser. Priority will not be parsed.
func (p *Parser) WithPriority(pri *parsercommon.Priority) {
p.priority = pri
}
// Forces a location. UTC will be used otherwise.
func (p *Parser) WithLocation(l *time.Location) {
p.location = l
}
// Forces a hostname. Hostname will not be parsed
func (p *Parser) WithHostname(h string) {
p.hostname = h
}
// Forces a tag. Tag will not be parsed
func (p *Parser) WithTag(t string) {
p.customTag = t
}
// Forces a given time format.
// Refer to pkg/time layouts for more informations
// By default the following formats will be tried in order:
// Jan 02 15:04:05
// Jan 2 15:04:05
// The timezone MUST be specified using WithLocation() and
// not using WithTimestampFormat
func (p *Parser) WithTimestampFormat(s string) {
p.customTimestampFormat = s
}
// DEPRECATED. Use WithLocation() instead
func (p *Parser) Location(location *time.Location) {
p.WithLocation(location)
}
// DEPRECATED. Use WithHostname() instead
func (p *Parser) Hostname(hostname string) {
p.WithHostname(hostname)
}
func (p *Parser) Parse() error {
p.version = parsercommon.NO_VERSION
pri, err := p.parsePriority()
if err != nil {
return err
}
p.priority = pri
hdr, err := p.parseHeader()
if err != nil {
return err
}
p.header = hdr
if p.buff[p.cursor] == ' ' {
p.cursor++
}
msg, err := p.parsemessage()
if err != parsercommon.ErrEOL {
return err
}
p.message = msg
return nil
}
func (p *Parser) Dump() syslogparser.LogParts {
return syslogparser.LogParts{
"timestamp": p.header.timestamp,
"hostname": p.header.hostname,
"tag": p.message.tag,
"content": p.message.content,
"priority": p.priority.P,
"facility": p.priority.F.Value,
"severity": p.priority.S.Value,
}
}
func (p *Parser) parsePriority() (*parsercommon.Priority, error) {
if p.priority != nil {
return p.priority, nil
}
return parsercommon.ParsePriority(
p.buff, &p.cursor, p.l,
)
}
// HEADER: TIMESTAMP + HOSTNAME (or IP)
// https://tools.ietf.org/html/rfc3164#section-4.1.2
func (p *Parser) parseHeader() (*header, error) {
var err error
if p.buff[p.cursor] == ' ' {
p.cursor++
}
ts, err := p.parseTimestamp()
if err != nil {
return nil, err
}
h, err := p.parseHostname()
if err != nil {
return nil, err
}
hdr := &header{
timestamp: ts,
hostname: h,
}
return hdr, nil
}
// MSG: TAG + CONTENT
// https://tools.ietf.org/html/rfc3164#section-4.1.3
func (p *Parser) parsemessage() (*message, error) {
var err error
tag, err := p.parseTag()
if err != nil {
return nil, err
}
content, err := p.parseContent()
if err != parsercommon.ErrEOL {
return nil, err
}
msg := &message{
tag: tag,
content: content,
}
return msg, err
}
// https://tools.ietf.org/html/rfc3164#section-4.1.2
func (p *Parser) parseTimestamp() (time.Time, error) {
var ts time.Time
var err error
var tsFmtLen int
var sub []byte
tsFmts := []string{
"Jan 02 15:04:05",
"Jan 2 15:04:05",
}
if p.customTimestampFormat != "" {
tsFmts = []string{
p.customTimestampFormat,
}
}
found := false
for _, tsFmt := range tsFmts {
tsFmtLen = len(tsFmt)
if p.cursor+tsFmtLen > p.l {
continue
}
sub = p.buff[p.cursor : tsFmtLen+p.cursor]
ts, err = time.ParseInLocation(
tsFmt, string(sub), p.location,
)
if err == nil {
found = true
break
}
}
if !found {
p.cursor = tsFmtLen
// XXX : If the timestamp is invalid we try to push the cursor one byte
// XXX : further, in case it is a space
if (p.cursor < p.l) && (p.buff[p.cursor] == ' ') {
p.cursor++
}
return ts, parsercommon.ErrTimestampUnknownFormat
}
fixTimestampIfNeeded(&ts)
p.cursor += tsFmtLen
if (p.cursor < p.l) && (p.buff[p.cursor] == ' ') {
p.cursor++
}
return ts, nil
}
func (p *Parser) parseHostname() (string, error) {
if p.hostname != "" {
return p.hostname, nil
}
return parsercommon.ParseHostname(
p.buff, &p.cursor, p.l,
)
}
// http://tools.ietf.org/html/rfc3164#section-4.1.3
func (p *Parser) parseTag() (string, error) {
if p.customTag != "" {
return p.customTag, nil
}
var b byte
var tag []byte
var err error
var enough bool
previous := p.cursor
// "The TAG is a string of ABNF alphanumeric characters that MUST NOT exceed 32 characters."
to := int(
math.Min(
float64(p.l),
float64(p.cursor+32),
),
)
for p.cursor < to {
b = p.buff[p.cursor]
if b == ' ' {
p.cursor++
break
}
if b == '[' || b == ']' || b == ':' || enough {
enough = true
p.cursor++
continue
}
tag = append(tag, b)
p.cursor++
}
if len(tag) == 0 {
p.cursor = previous
}
return string(tag), err
}
func (p *Parser) parseContent() (string, error) {
if p.cursor > p.l {
return "", parsercommon.ErrEOL
}
content := bytes.Trim(
p.buff[p.cursor:p.l], " ",
)
p.cursor += len(content)
return string(content), parsercommon.ErrEOL
}
func fixTimestampIfNeeded(ts *time.Time) {
now := time.Now()
y := ts.Year()
if ts.Year() == 0 {
y = now.Year()
}
newTs := time.Date(
y, ts.Month(), ts.Day(),
ts.Hour(), ts.Minute(), ts.Second(), ts.Nanosecond(),
ts.Location(),
)
*ts = newTs
}