https://github.com/qxip/log2pcap
Generate PCAP files from Buffers & Co
https://github.com/qxip/log2pcap
Last synced: 12 months ago
JSON representation
Generate PCAP files from Buffers & Co
- Host: GitHub
- URL: https://github.com/qxip/log2pcap
- Owner: QXIP
- License: agpl-3.0
- Created: 2023-08-26T17:42:20.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-08-28T09:53:28.000Z (almost 3 years ago)
- Last Synced: 2025-03-01T15:16:44.086Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 112 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# log2pcap
Generate PCAP files from Log Buffers & Co
## Usage
```javascript
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"),
},
]);
```
## Browser version
_**Build bundle to import**_
```bash
# _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.
### Sample usage (incluided at **index.html** file from this repo):
```js
// import bundle
// process data
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;
}
}
```
### Limitations
- Only supports TCP (6) and UDP (17) protocols and IPv4