forked from CN-TU/go-flows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
162 lines (137 loc) · 4.55 KB
/
application.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
package custom
import (
"strings"
"github.com/CN-TU/go-flows/flows"
"github.com/CN-TU/go-flows/packet"
ipfix "github.com/CN-TU/go-ipfix"
"github.com/google/gopacket/layers"
)
type _payload struct {
flows.BaseFeature
}
func (f *_payload) Event(new interface{}, context *flows.EventContext, src interface{}) {
tl := new.(packet.Buffer).TransportLayer()
if tl == nil {
return
}
payload := tl.LayerPayload()
if len(payload) == 0 {
return
}
f.SetValue(payload, context, f)
}
func init() {
flows.RegisterTemporaryFeature("_payload", "application layer of a packet", ipfix.OctetArrayType, 0, flows.PacketFeature, func() flows.Feature { return &_payload{} }, flows.RawPacket)
}
////////////////////////////////////////////////////////////////////////////////
type _HTTPLines struct {
flows.BaseFeature
buffer []byte
status uint8
statusNext uint8
current string
src string
}
const _None uint8 = 0
const _Request uint8 = 1
const _Response uint8 = 2
const _Header uint8 = 3
const _Body uint8 = 4
const _Error string = "-1"
func (f *_HTTPLines) Start(context *flows.EventContext) {
f.BaseFeature.Start(context)
f.status = _Request
f.statusNext = _None
}
func (f *_HTTPLines) extractLine(ignore bool) (ret string) {
data := string(f.buffer) // TODO maybe it's better to keep it as bytes and not use strings?
if ignore {
data = strings.TrimLeft(data, " \t\n\r")
}
dataSplit := strings.SplitN(data, "\n", 2)
if len(dataSplit) != 2 {
return _Error
}
f.buffer = []byte(dataSplit[1]) // TODO here maybe there's unnecessary allocations
return strings.TrimSpace(dataSplit[0])
}
func (f *_HTTPLines) extractRequest(line string) (ret string) {
// TODO implement proper request parsing
lineSplit := strings.Split(line, " ")
return lineSplit[0]
}
func (f *_HTTPLines) parsePart(context *flows.EventContext, src interface{}) (ret bool) {
// TODO nothing is implemented except for extracting headers in simple http sessions
switch f.status {
case _Request:
line := f.extractLine(true)
if line == _Error {
return false
}
f.SetValue(line, context, src)
f.statusNext = _Response
f.status = _Header
f.current = f.extractRequest(line)
if f.current == "" {
f.status = _Request
}
case _Header:
line := f.extractLine(false)
if line == _Error {
return false
}
if line == "" { // end of header
return false // FIXME this should continue, and do more stuff
}
f.SetValue(line, context, src)
default:
}
return true
}
func (f *_HTTPLines) Event(new interface{}, context *flows.EventContext, src interface{}) {
f.buffer = append(f.buffer, []byte(new.(string))...)
for f.parsePart(context, src) == true {
continue
}
}
func (f *_HTTPLines) Stop(reason flows.FlowEndReason, context *flows.EventContext) {
}
func init() {
flows.RegisterTemporaryFeature("httpLines", "returns headers from a http session", ipfix.StringType, 0, flows.PacketFeature, func() flows.Feature { return &_HTTPLines{} }, flows.PacketFeature)
flows.RegisterTemporaryCompositeFeature("_HTTPLines", "returns headers from a http session", ipfix.StringType, 0, "httpLines", "_tcpReorderPayload")
}
type httpRequestHost struct {
flows.BaseFeature
}
func (f *httpRequestHost) Event(new interface{}, context *flows.EventContext, src interface{}) {
header := strings.SplitN(new.(string), ":", 2)
if header[0] == "Host" {
f.SetValue(strings.TrimSpace(header[1]), context, src)
}
}
func init() {
flows.RegisterTemporaryFeature("__httpRequestHost", "extract the host header from lines of text", ipfix.StringType, 0, flows.FlowFeature, func() flows.Feature { return &httpRequestHost{} }, flows.PacketFeature)
flows.RegisterStandardCompositeFeature("httpRequestHost", "__httpRequestHost", "_HTTPLines")
}
////////////////////////////////////////////////////////////////////////////////
type _DNSDomain struct {
flows.BaseFeature
}
func (f *_DNSDomain) Start(context *flows.EventContext) {
f.BaseFeature.Start(context)
}
func (f *_DNSDomain) Event(new interface{}, context *flows.EventContext, src interface{}) {
layer := new.(packet.Buffer).Layer(layers.LayerTypeDNS)
if layer != nil {
dns := layer.(*layers.DNS)
for _, dnsQuestion := range dns.Questions {
f.SetValue(string(dnsQuestion.Name), context, src)
}
}
}
func (f *_DNSDomain) Stop(reason flows.FlowEndReason, context *flows.EventContext) {
}
func init() {
flows.RegisterTemporaryFeature("_DNSDomain", "returns domains from DNS packets.", ipfix.StringType, 0, flows.PacketFeature, func() flows.Feature { return &_DNSDomain{} }, flows.RawPacket)
}
////////////////////////////////////////////////////////////////////////////////