DEV Community

Cover image for Packet Sniffing tool implemented in Node.js (alternatives to Charles, hoxy, etc...)
Kohei Ueno
Kohei Ueno

Posted on

Packet Sniffing tool implemented in Node.js (alternatives to Charles, hoxy, etc...)

Illustration by Stories by Freepik

GitHub logo cola119 / ESniffer

Modern network analyze tool. Alternatives to Hoxy, Charles, etc.

ESniffer 🔬

Modern network analyze tool. Alternatives to Hoxy, Charles, etc.

Install

npm i esniffer
yarn add esniffer
Enter fullscreen mode Exit fullscreen mode

Example

// index.js
import ESniffer from "esniffer";
import fs from "fs";
// Required if you want to monitor over HTTPS
const key = fs.readFileSync(`path/to/root-key.pem`);
const cert = fs.readFileSync(`path/to/root-cert.pem`);

const proxy = ESniffer.createServer({ secure: { key, cert } });
proxy.listen(8080);

proxy.on("request", (req) => {
  req.pipe(process.stdout);
});
proxy.on("response", (res) => {
  res.pipe(process.stdout);
});
proxy.on("info", (info) => {
  console.log(info);
}
…
Enter fullscreen mode Exit fullscreen mode

Charles is an HTTP proxy / HTTP monitor / Reverse Proxy that enables a developer to view all of the HTTP and SSL / HTTPS traffic between their machine and the Internet. I'm a heavy user of Charles for debugging but sometimes struggle with its lack of customization.

If we can monitor HTTP/HTTPS traffic using Node.js script, we can do even more useful debugging and testing. So I've developed a simple module that allows for packet monitoring.

import ESniffer from "esniffer";
import fs from "fs";

// Required if you want to monitor over HTTPS
const key = fs.readFileSync(`path/to/root-key.pem`);
const cert = fs.readFileSync(`path/to/root-cert.pem`);

const proxy = ESniffer.createServer({ secure: { key, cert } });
proxy.listen(8080);

proxy.on("request", (req) => {
  req.pipe(process.stdout);
});
proxy.on("response", (res) => {
  res.pipe(process.stdout);
});
proxy.on("info", (info) => {
  console.log(info);
});
proxy.on("error", (e) => {
  console.error(e.message);
});
Enter fullscreen mode Exit fullscreen mode

This is inheriting the EventEmitter so we can access request to the server and response from the server via custom events. By installing and trusting a self-signed certificate, we can also monitor over HTTPS communication. See examples for more information.

Finally, I welcome bug reports and requests for additional features. Github Star, please!

GitHub logo cola119 / ESniffer

Modern network analyze tool. Alternatives to Hoxy, Charles, etc.

ESniffer 🔬

Modern network analyze tool. Alternatives to Hoxy, Charles, etc.

Install

npm i esniffer
yarn add esniffer
Enter fullscreen mode Exit fullscreen mode

Example

// index.js
import ESniffer from "esniffer";
import fs from "fs";
// Required if you want to monitor over HTTPS
const key = fs.readFileSync(`path/to/root-key.pem`);
const cert = fs.readFileSync(`path/to/root-cert.pem`);

const proxy = ESniffer.createServer({ secure: { key, cert } });
proxy.listen(8080);

proxy.on("request", (req) => {
  req.pipe(process.stdout);
});
proxy.on("response", (res) => {
  res.pipe(process.stdout);
});
proxy.on("info", (info) => {
  console.log(info);
}
…
Enter fullscreen mode Exit fullscreen mode

Top comments (0)