Skip to content

Commit 7b66e59

Browse files
loggers: simplify structed data conversions
- unify conversion for rfc3164 and rfc5424 formats.
1 parent 98b8b06 commit 7b66e59

File tree

3 files changed

+39
-60
lines changed

3 files changed

+39
-60
lines changed

daemon/log/formats/formats.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import (
44
"log/syslog"
55
"os"
66
"strconv"
7+
"strings"
8+
9+
"github.com/evilsocket/opensnitch/daemon/core"
10+
"github.com/evilsocket/opensnitch/daemon/ui/protocol"
711
)
812

913
// LoggerFormat is the common interface that every format must meet.
@@ -23,3 +27,36 @@ func init() {
2327
ourPid = strconv.FormatUint(uint64(os.Getpid()), 10)
2428
syslogLevel = strconv.FormatUint(uint64(syslog.LOG_NOTICE|syslog.LOG_DAEMON), 10)
2529
}
30+
31+
// transform protocol.Connection to Structured Data format.
32+
func connToSD(out string, val interface{}) string {
33+
checksums := ""
34+
tree := ""
35+
con := val.(*protocol.Connection)
36+
37+
for k, v := range con.ProcessChecksums {
38+
checksums = core.ConcatStrings(checksums, k, ":", v)
39+
}
40+
for _, y := range con.ProcessTree {
41+
tree = core.ConcatStrings(tree, y.Key, ",")
42+
}
43+
44+
// TODO: allow to configure this via configuration file.
45+
return core.ConcatStrings(out,
46+
" SRC=\"", con.SrcIp, "\"",
47+
" SPT=\"", strconv.FormatUint(uint64(con.SrcPort), 10), "\"",
48+
" DST=\"", con.DstIp, "\"",
49+
" DSTHOST=\"", con.DstHost, "\"",
50+
" DPT=\"", strconv.FormatUint(uint64(con.DstPort), 10), "\"",
51+
" PROTO=\"", con.Protocol, "\"",
52+
" PID=\"", strconv.FormatUint(uint64(con.ProcessId), 10), "\"",
53+
" UID=\"", strconv.FormatUint(uint64(con.UserId), 10), "\"",
54+
//" COMM=", con.ProcessComm, "\"",
55+
" PATH=\"", con.ProcessPath, "\"",
56+
" CMDLINE=\"", strings.Join(con.ProcessArgs, " "), "\"",
57+
" CWD=\"", con.ProcessCwd, "\"",
58+
" CHECKSUMS=\"", checksums, "\"",
59+
" PROCTREE=\"", tree, "\"",
60+
// TODO: envs
61+
)
62+
}

daemon/log/formats/rfc3164.go

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ package formats
22

33
import (
44
"fmt"
5-
"strconv"
6-
"strings"
75
"time"
86

9-
"github.com/evilsocket/opensnitch/daemon/core"
107
"github.com/evilsocket/opensnitch/daemon/ui/protocol"
118
)
129

@@ -38,32 +35,7 @@ func (r *Rfc3164) Transform(args ...interface{}) (out string) {
3835
for n, val := range values {
3936
switch val.(type) {
4037
case *protocol.Connection:
41-
checksums := ""
42-
tree := ""
43-
con := val.(*protocol.Connection)
44-
45-
for k, v := range con.ProcessChecksums {
46-
checksums = core.ConcatStrings(checksums, k, ":", v)
47-
}
48-
for _, y := range con.ProcessTree {
49-
tree = core.ConcatStrings(tree, y.Key, ",")
50-
}
51-
out = core.ConcatStrings(out,
52-
" SRC=\"", con.SrcIp, "\"",
53-
" SPT=\"", strconv.FormatUint(uint64(con.SrcPort), 10), "\"",
54-
" DST=\"", con.DstIp, "\"",
55-
" DSTHOST=\"", con.DstHost, "\"",
56-
" DPT=\"", strconv.FormatUint(uint64(con.DstPort), 10), "\"",
57-
" PROTO=\"", con.Protocol, "\"",
58-
" PID=\"", strconv.FormatUint(uint64(con.ProcessId), 10), "\"",
59-
" UID=\"", strconv.FormatUint(uint64(con.UserId), 10), "\"",
60-
//" COMM=", con.ProcessComm, "\"",
61-
" PATH=\"", con.ProcessPath, "\"",
62-
" CMDLINE=\"", strings.Join(con.ProcessArgs, " "), "\"",
63-
" CWD=\"", con.ProcessCwd, "\"",
64-
" CHECKSUMS=\"", checksums, "\"",
65-
" PROCTREE=\"", tree, "\"",
66-
)
38+
out = connToSD(out, val)
6739
default:
6840
out = fmt.Sprint(out, " ARG", n, "=\"", val, "\"")
6941
}

daemon/log/formats/rfc5424.go

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ package formats
22

33
import (
44
"fmt"
5-
"strconv"
6-
"strings"
75
"time"
86

9-
"github.com/evilsocket/opensnitch/daemon/core"
107
"github.com/evilsocket/opensnitch/daemon/ui/protocol"
118
)
129

@@ -40,35 +37,8 @@ func (r *Rfc5424) Transform(args ...interface{}) (out string) {
4037
for n, val := range values {
4138
switch val.(type) {
4239
case *protocol.Connection:
43-
tree := ""
44-
checksums := ""
45-
con := val.(*protocol.Connection)
4640
event = "CONNECTION"
47-
48-
for k, v := range con.ProcessChecksums {
49-
checksums = core.ConcatStrings(checksums, k, ":", v)
50-
}
51-
for _, y := range con.ProcessTree {
52-
tree = core.ConcatStrings(tree, y.Key, ",")
53-
}
54-
55-
// TODO: allow to configure this via configuration file.
56-
out = core.ConcatStrings(out,
57-
" SRC=\"", con.SrcIp, "\"",
58-
" SPT=\"", strconv.FormatUint(uint64(con.SrcPort), 10), "\"",
59-
" DST=\"", con.DstIp, "\"",
60-
" DSTHOST=\"", con.DstHost, "\"",
61-
" DPT=\"", strconv.FormatUint(uint64(con.DstPort), 10), "\"",
62-
" PROTO=\"", con.Protocol, "\"",
63-
" PID=\"", strconv.FormatUint(uint64(con.ProcessId), 10), "\"",
64-
" UID=\"", strconv.FormatUint(uint64(con.UserId), 10), "\"",
65-
//" COMM=", con.ProcessComm, "\"",
66-
" PATH=\"", con.ProcessPath, "\"",
67-
" CMDLINE=\"", strings.Join(con.ProcessArgs, " "), "\"",
68-
" CWD=\"", con.ProcessCwd, "\"",
69-
" CHECKSUMS=\"", checksums, "\"",
70-
" PROCTREE=\"", tree, "\"",
71-
)
41+
out = connToSD(out, val)
7242
default:
7343
out = fmt.Sprint(out, " ARG", n, "=\"", val, "\"")
7444
}

0 commit comments

Comments
 (0)