![](https://private-user-images.githubusercontent.com/1423657/255348645-36a8e515-ab0e-482b-bf49-2156e290c764.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU0OTA3MjAsIm5iZiI6MTcyNTQ5MDQyMCwicGF0aCI6Ii8xNDIzNjU3LzI1NTM0ODY0NS0zNmE4ZTUxNS1hYjBlLTQ4MmItYmY0OS0yMTU2ZTI5MGM3NjQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDkwNCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA5MDRUMjI1MzQwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NmE5ZmJjMDMxMjA1YTgzMGUxZjljMGNmMzdiNjM5OWM4YjU3NTQ5NTViZDhkMWRjMDlmMTkxNDQ2OWEzZmJkMSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.uepUbHnEpIXuDAaCWqQ39W8zEEm9w57dNSrSL9u2Hcw)
Generate PCAP files from Log Buffers & Co
const log2pcap = require("log2pcap");
var pcapFile = log2pcap.encodePcap([
{
srcIp: "127.0.0.1",
srcPort: 5060,
dstIp: "127.0.0.2",
dstPort: 5080,
proto: 17,
data: Buffer.from("HELLO"),
},
{
srcIp: "127.0.0.2",
srcPort: 5080,
dstIp: "127.0.0.1",
dstPort: 5060,
proto: 17,
data: Buffer.from("WORLD"),
},
]);
Build bundle to import
# _This will generate the bundle to use at your browser application_
npm run build
From this bundle we could use two functions:
-
writePcap that writes the object data into a ready to generate pcap data
-
generator that generates the pcap data into a blob
-
after this generation we need to write a download function at browser.
// import bundle
<script src="bundle.js"></script>
// process data
<script>
const bpc = require("browserPcap");
// sample data object
let pcapFile = [
{
srcIp: "127.0.0.1",
srcPort: 5060,
dstIp: "127.0.0.2",
dstPort: 5080,
proto: 17,
data: "HELLO",
},
{
srcIp: "127.0.0.2",
srcPort: 5080,
dstIp: "127.0.0.1",
dstPort: 5060,
proto: 17,
data: "WORLD",
},
];
// (...)
// input with id: file-name
const file = document.getElementById("file-name");
let fileName = file?.value;
if ( fileName && typeof fileName === "string" && fileName !== "") {
let pcap = bpc.writePcap(pcapFile);
if (pcap) {
let pcapData = bpc.generator(pcap);
var a = document.getElementById("a");
var blob = new Blob([pcapData], { type: "Buffer" });
a.href = URL.createObjectURL(blob);
a.download = fileName;
}
}
- Only supports TCP (6) and UDP (17) protocols and IPv4