forked from XiaohuaCN/Cloud-Radar-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-parse-pcapfile.js
86 lines (79 loc) · 2.34 KB
/
test-parse-pcapfile.js
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
// Usage: LOGLEVEL=info node test-parse-pcapfile.js '/DIR/pubg-game1.pcap' | pino
const PacketByPacket = require('p-by-p')
const PacketParser = require('./backend/packetparser')
const utils = require('./backend/utils')
const logger = require('./backend/logger')
const CONSTS = require('./backend/constants')
const parser = PacketParser()
const optionSummary = {}
function addToSummary (options) {
for (let key in options) {
if (options[key] === true) {
if (optionSummary[key] == null) {
optionSummary[key] = 1
} else {
optionSummary[key]++
}
}
}
}
async function main () {
const args = process.argv.slice(2)
if (args.length !== 1) {
console.log(`Usage: test-parse-pcapfile.js <pcapfile>`)
console.log(
` e.g.: test-parse-pcapfile.js /DIR/pubg-game1.pcap`
)
}
const pbyp = PacketByPacket(args[0])
let eventCount = 0
let packetNumber = 0
let invalidEvent = 0
let channelSet = new Set()
let errCount = 0
let totalBunchCount = 0
pbyp.on('packet', packet => {
// packet contains { header, data }
packetNumber++
let result = null
try {
result = parser.parse(packet.data, packet.header.timestamp, packetNumber)
} catch (err) {
// console.log(err)
errCount++
}
// parser would only return the event that we are interested in, otherwise, it returns null
if (result != null) {
eventCount++
if (result.valid === false) {
invalidEvent++ // for those malformatted bunch udp packet
logger.warn({ packetNumber, ...result }, 'Got Invalid Event')
} else {
logger.debug({ packetNumber, ...result }, 'Got Event')
}
for (let bunch of result.data) {
totalBunchCount++
addToSummary(bunch.options)
if (bunch.chIndex === 0) {
console.log(bunch)
}
channelSet.add(bunch.chIndex)
}
}
})
pbyp.on('error', err => {
logger.error(err, 'Error happend')
})
pbyp.on('end', result => {
logger.warn({ parserResult: result, eventCount, invalidEvent, totalChannel: channelSet.size, totalBunchCount }, 'pcap processing finished')
logger.warn(optionSummary, 'bunch options summary')
logger.warn({ errCount }, 'errCount')
})
pbyp.resume()
}
main().catch(err => {
console.log('unexpected error', err)
cleanup.then(() => {
process.exit(2)
})
})