This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcp-server.go
375 lines (268 loc) · 9.53 KB
/
tcp-server.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"net"
"net/rpc"
"math"
"time"
"sync"
"os/exec"
"github.com/io-digital/nprobe-collector/structure"
"github.com/io-digital/nprobe-collector/function"
)
var (
debugMode bool
dataBuffer []map[string][]byte
dataTemplates = make(map[uint16]map[string][]uint16)
optionTemplates = make(map[uint16]map[string][]uint16)
packetCount = 0
flowSetHeaderReadDone = make(chan bool)
flowSetRecordReadDone = make(chan bool)
dataBufferFull = make(chan bool)
optionDataBufferFull = make(chan bool)
flowSetRecord []byte
dataBufferLock sync.Mutex
rpcClients []*rpc.Client
)
func readFlowSetHeader(reader *bufio.Reader, flowSetHeaderSlice []byte, flowSetHeaderStruct *structure.FlowSetHeader/*, done chan bool*/) {
flowSetHeaderSlice = nil
for i := 0; i < 20; {
newByte, err := reader.ReadByte()
if err == nil {
flowSetHeaderSlice = append(flowSetHeaderSlice, newByte)
i++
} else if err == io.EOF {
fmt.Printf("Number of Packets: %d\n", packetCount)
log.Fatalf("Unexpected Error: %s", err)
} else {
fmt.Println("Waiting for data")
}
}
flowSetHeaderStruct.Version = function.ReadUint16(flowSetHeaderSlice[0:2])
flowSetHeaderStruct.Records = function.ReadUint16(flowSetHeaderSlice[2:4])
flowSetHeaderStruct.SystemUptime = function.ReadUint32(flowSetHeaderSlice[4:8])
flowSetHeaderStruct.Timestamp = time.Unix(int64(function.ReadUint32(flowSetHeaderSlice[8:12])), 0)
if debugMode {
fmt.Printf("FlowSet Header: NetFlow Version = %d, Number of Packets = %d, Time = %s \n", flowSetHeaderStruct.Version, flowSetHeaderStruct.Records, flowSetHeaderStruct.Timestamp)
}
packetCount++
if(math.Remainder(float64(packetCount), 1000) == 0){
fmt.Printf("Records processed: %d\n", packetCount)
}
flowSetHeaderReadDone <- true
}
func readFlowSetRecord(reader *bufio.Reader, flowSetRecord *[]byte) {
flowSetIdLength, err := reader.Peek(4)
if err != nil {
log.Fatalf("Unexpected Error: %s", err)
}
flowSetLength := int(function.ReadUint16(flowSetIdLength[2:4]))
if debugMode {
fmt.Printf("FlowSet Record: FlowSetId = %d, Length = %d\n", function.ReadUint16(flowSetIdLength[0:2]), flowSetLength)
}
for i := 0; i < flowSetLength; {
newByte, err := reader.ReadByte()
if err == nil {
*flowSetRecord = append(*flowSetRecord, newByte)
i++
} else if debugMode {
fmt.Println("Waiting for data")
}
}
//done <- true
flowSetRecordReadDone <- true
}
func parseFlowSetTemplate(flowSetTemplate []byte) {
templates := flowSetTemplate[4:]
for len(templates) > 0 {
templateId := function.ReadUint16(templates[0:2])
fieldCount := int(function.ReadUint16(templates[2:4]))
templates = templates[4:]
fieldTypeIndex := make([]uint16, 0)
fieldLength := make([]uint16, 0)
if debugMode {
fmt.Printf("Parsing FlowSet Template: TemplateId = %d, Number of Fields = %d\n", templateId, fieldCount)
}
for i := 0; i < fieldCount; i++ {
fieldTypeIndex = append(fieldTypeIndex, function.ReadUint16(templates[0:2]))
fieldLength = append(fieldLength, function.ReadUint16(templates[2:4]))
templates = templates[4:]
}
dataTemplates[templateId] = map[string][]uint16{
"fieldTypeIndex": fieldTypeIndex,
"fieldLength": fieldLength,
}
}
}
func parseFlowSetOptions(flowSetOptions []byte) {
template := flowSetOptions[4:]
templateId := function.ReadUint16(template[0:2])
optionScopeLength := int(function.ReadUint16(template[2:4]))/4
optionLength := int(function.ReadUint16(template[4:6]))/4
template = template[6:]
scopeFieldTypeIndex := make([]uint16, 0)
scopeFieldLength := make([]uint16, 0)
optionFieldTypeIndex := make([]uint16, 0)
optionFieldLength := make([]uint16, 0)
if debugMode {
fmt.Printf("Parsing FlowSet Options Template: TemplateId = %d, Option Scope Length = %d, Option Length = %d\n", templateId, optionScopeLength, optionLength)
}
for i := 0; i < optionScopeLength; i++ {
scopeFieldTypeIndex = append(scopeFieldTypeIndex, function.ReadUint16(template[0:2]))
scopeFieldLength = append(scopeFieldLength, function.ReadUint16(template[2:4]))
template = template[4:]
}
for i := 0; i < optionLength; i++ {
optionFieldTypeIndex = append(optionFieldTypeIndex, function.ReadUint16(template[0:2]))
optionFieldLength = append(optionFieldLength, function.ReadUint16(template[2:4]))
template = template[4:]
}
optionTemplates[templateId] = map[string][]uint16{
"scopeFieldTypeIndex": scopeFieldTypeIndex,
"scopeFieldLength": scopeFieldLength,
"optionFieldTypeIndex": optionFieldTypeIndex,
"optionFieldLength": optionFieldLength,
}
}
func parseFlowSetData(flowSetData []byte) {
templateId := function.ReadUint16(flowSetData[0:2])
dataTemplate, dataTemplateExists := dataTemplates[templateId]
optionTemplate, optionTemplateExists := optionTemplates[templateId]
if dataTemplateExists {
if debugMode {
fmt.Printf("Parsing FlowSet Data: TemplateId = %d, Packet Length = %d\n", templateId, len(flowSetData))
fmt.Println("------------------------------------------------------------------------")
}
fieldValues := flowSetData[4:]
dataBufferLock.Lock()
dataBuffer = nil
for len(fieldValues) >= 4 {
packet := make(map[string][]byte, 0)
for index, fieldLength := range dataTemplate["fieldLength"] {
fieldMapIndex := dataTemplate["fieldTypeIndex"][index]
fieldName := structure.NetFlowFieldTypes[fieldMapIndex]
fieldValue := fieldValues[0:fieldLength]
packet[fieldName] = fieldValue
if debugMode {
fmt.Printf("Name: %s, Length: %d, Value: %d\n", fieldName, fieldLength, fieldValue)
}
fieldValues = fieldValues[fieldLength:]
}
dataBuffer = append(dataBuffer, packet)
if debugMode {
fmt.Println("------------------------------------------------------------------------")
}
}
dataBufferLock.Unlock()
//data buffer is full for this flowset
dataBufferFull <- true
} else if optionTemplateExists {
if debugMode {
fmt.Printf("Parsing FlowSet Options Data: TemplateId = %d, Packet Length = %d\n", templateId, len(flowSetData))
fmt.Println("------------------------------------------------------------------------")
}
fieldValues := flowSetData[4:]
for index, fieldLength := range optionTemplate["scopeFieldLength"] {
fieldMapIndex := optionTemplate["scopeFieldTypeIndex"][index]
fieldName := structure.NetFlowScopeFields[fieldMapIndex]
fieldValue := fieldValues[0:fieldLength]
if debugMode {
fmt.Printf("Name: %s, Length: %d, Value: %d\n", fieldName, fieldLength, fieldValue)
}
fieldValues = fieldValues[fieldLength:]
}
for index, fieldLength := range optionTemplate["optionFieldLength"] {
fieldMapIndex := optionTemplate["optionFieldTypeIndex"][index]
fieldName := structure.NetFlowFieldTypes[fieldMapIndex]
fieldValue := fieldValues[0:fieldLength]
if debugMode {
fmt.Printf("Name: %s, Length: %d, Value: %d\n", fieldName, fieldLength, fieldValue)
}
fieldValues = fieldValues[fieldLength:]
}
if debugMode {
fmt.Println("------------------------------------------------------------------------")
}
optionDataBufferFull <- true
} else {
fmt.Printf("templateId %d not found\n", templateId)
}
}
func init(){
debugPtr := flag.Bool("debug", false, "Outputs data as it is received")
flag.Parse()
debugMode = *debugPtr
fmt.Println("Launching TCP server...")
//Initialize processors
processorConfig := function.GetProcessorConfig()
for _, processor := range processorConfig.Processors {
fmt.Println("Processor Found:", processor.Name, "| Location:", processor.Location)
var processStarted = false
processorCmd := exec.Command(processor.Location)
err := processorCmd.Start()
if err != nil {
log.Fatal(err)
}
fmt.Println("Processor started, connecting...")
for i := 0; i < processor.ConnectionAttempts; i++ {
time.Sleep(1000 * time.Millisecond)
fmt.Println("attempt ", i + 1)
client, err := rpc.Dial("tcp", "localhost:"+processor.TcpPort)
if err == nil {
rpcClients = append(rpcClients, client)
processStarted = true
fmt.Println("Communication with Processor Successful")
break
}
}
if !processStarted {
fmt.Println("Could not establish communication with processor", processor.Name)
}
}
}
func main() {
flowSetHeaderSlice := make([]byte, 20)
flowSetHeader := structure.FlowSetHeader{}
ln, err := net.Listen("tcp", ":2055")
if err != nil {
log.Fatalf("TCP Error: %s", err)
}
conn, err := ln.Accept()
if err != nil {
log.Fatalf("Connection Accept Error: %s", err)
}
reader := bufio.NewReader(conn)
fmt.Println("Listening on and accepting connections on port 2055...")
tempRes := make(chan *rpc.Call, 100)
for {
go readFlowSetHeader(reader, flowSetHeaderSlice, &flowSetHeader)
<-flowSetHeaderReadDone
for flowSetRecordCounter := 0; flowSetRecordCounter < int(flowSetHeader.Records); flowSetRecordCounter++ {
flowSetRecord = nil
go readFlowSetRecord(reader, &flowSetRecord)
<-flowSetRecordReadDone
flowSetId := function.ReadUint16(flowSetRecord)
switch flowSetId {
case 0:
parseFlowSetTemplate(flowSetRecord)
case 1:
parseFlowSetOptions(flowSetRecord)
default:
go parseFlowSetData(flowSetRecord)
select {
case <-dataBufferFull:
for _, client := range rpcClients {
var reply bool
client.Go("Processor.ProcessData", structure.ProcessingFuncArgs{FlowSetHeaderStruct: flowSetHeader, DataBuffer: dataBuffer}, &reply, tempRes)
}
case <-optionDataBufferFull:
//skryf data
}
}
}
}
}