-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (46 loc) · 1.14 KB
/
index.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
/*
log2pcap exporter
(c) qxip bv 2023
*/
const ip = require('ip-packet');
const tcp = require('tcp-packet');
const udp = require('udp-packet');
const generator = require('pcap-generator');
function encodePcap(packetArray) {
const pcapData = [];
packetArray.forEach(packet => {
// Create TCP or UDP packet
let packet_data;
if (packet.proto === 6) { // TCP
packet_data = tcp.encode({
sourcePort: packet.srcPort,
destinationPort: packet.dstPort,
data: Buffer.from(packet.data)
})
} else if (packet.proto === 17) { // UDP
packet_data = udp.encode({
sourcePort: packet.srcPort,
destinationPort: packet.dstPort,
data: Buffer.from(packet.data)
})
}
let ipv4_packet = ip.encode({
version: packet.version || 4,
protocol: packet.proto,
sourceIp: packet.srcIp,
destinationIp: packet.dstIp,
data: packet_data
})
// Combine IP and transport packets
pcapData.push({
timestamp: packet.ts || Date.now(),
buffer: ipv4_packet
});
});
// Generate the pcap file
const pcapFileBuffer = generator(pcapData);
return pcapFileBuffer;
}
module.exports = {
encodePcap
};