forked from neex/http2smugl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_writer.go
74 lines (65 loc) · 1.16 KB
/
log_writer.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
package main
import (
"encoding/csv"
"os"
"sync"
)
type CSVLogWriter struct {
m sync.Mutex
headerWritten bool
f *os.File
w *csv.Writer
}
func NewCSVLogWriter(filename string) (*CSVLogWriter, error) {
f, err := os.Create(filename)
if err != nil {
return nil, err
}
w := csv.NewWriter(f)
return &CSVLogWriter{
f: f,
w: w,
}, nil
}
func (w *CSVLogWriter) Log(d *DetectParams, result DetectResult) error {
w.m.Lock()
defer w.m.Unlock()
if !w.headerWritten {
if err := w.w.Write([]string{
"target",
"http_method",
"detect_method",
"padding_method",
"smuggling_method",
"smuggling_variant",
"result",
}); err != nil {
return err
}
w.headerWritten = true
}
if err := w.w.Write([]string{
d.Target,
d.RequestMethod,
d.DetectMethod.String(),
d.PaddingMethod.String(),
d.SmugglingMethod.String(),
d.SmugglingVariant.String(),
result.String(),
}); err != nil {
return err
}
return nil
}
func (w *CSVLogWriter) Close() error {
w.m.Lock()
defer w.m.Unlock()
w.w.Flush()
if err := w.w.Error(); err != nil {
return err
}
if err := w.f.Close(); err != nil {
return err
}
return nil
}