-
Notifications
You must be signed in to change notification settings - Fork 159
/
utils.go
125 lines (105 loc) · 2.66 KB
/
utils.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
/*
* NETCAP - Traffic Analysis Framework
* Copyright (c) 2017 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package main
import (
"bufio"
"compress/gzip"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/dreadl0ck/netcap"
"github.com/dreadl0ck/netcap/encoder"
"github.com/dreadl0ck/netcap/types"
"github.com/google/kythe/kythe/go/platform/delimited"
)
// AuditRecordHandle wraps a file handle of a netcap audit record file
// contains the original file handle and writers to compress and buffer the data
type AuditRecordHandle struct {
gWriter *gzip.Writer
bWriter *bufio.Writer
f *os.File
}
// NewAuditRecordHandle creates a new netcap audit record file
func NewAuditRecordHandle(b *types.Batch, path string) *AuditRecordHandle {
err := os.MkdirAll(b.ClientID, 0755)
if err != nil {
panic(err)
}
f, err := os.Create(path)
if err != nil {
panic(err)
}
fmt.Println("new audit record handle", path)
conf := encoder.Config{
Source: b.ClientID,
Version: netcap.Version,
}
var (
// create buffered writer that writes into the file handle
bWriter = bufio.NewWriter(f)
// create gzip writer that writes into the buffered writer
gWriter = gzip.NewWriter(bWriter)
)
// add file header
err = delimited.NewWriter(gWriter).PutProto(encoder.NewHeader(b.MessageType, conf))
if err != nil {
fmt.Println("failed to write header")
panic(err)
}
return &AuditRecordHandle{
bWriter: bWriter,
gWriter: gWriter,
f: f,
}
}
func cleanup() {
fmt.Println("cleanup")
// cleanup
for p, a := range files {
// flush and close gzip writer
err := a.gWriter.Flush()
if err != nil {
panic(err)
}
err = a.gWriter.Close()
if err != nil {
panic(err)
}
// flush buffered writer
err = a.bWriter.Flush()
if err != nil {
panic(err)
}
// sync and close file handle
fmt.Println("closing file", p)
err = a.f.Sync()
if err != nil {
panic(err)
}
err = a.f.Close()
if err != nil {
panic(err)
}
}
}
func handleSignals() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
// start signal handler and cleanup routine
go func() {
_ = <-sigs
cleanup()
os.Exit(0)
}()
}