npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mitmproxy

v2.1.2

Published

NodeJS mitmproxy adapter.

Downloads

27,523

Readme

mitmproxy-node 2.1.1

A bridge between Python's mitmproxy and Node.JS programs. Rewrite network requests using Node.JS!

Why?

It is far easier to rewrite JavaScript/HTML/etc using JavaScript than Python, but mitmproxy only accepts Python plugins. There are no decent alternatives to mitmproxy, so this package lets me use mitmproxy with Node.js-based rewriting code.

What can I use this for?

For transparently rewriting HTTP/HTTPS responses. The mitmproxy plugin lets every HTTP request go through to the server uninhibited, and then passes it to Node.js via a WebSocket for rewriting. You can optionally specify a list of paths that should be directly intercepted without being passed to the server.

If you want to add additional functionality, such as filtering or whatnot, I'll accept pull requests so long as they do not noticeably hinder performance.

How does it work?

A Python plugin for mitmproxy starts a WebSocket server, and mitmproxy-node talks with it over WebSocket messages. The two communicate via binary messages to reduce marshaling-related overhead.

Your Python plugin is bad and you should feel bad

I have no idea what I am doing. PRs to improve my Python code are appreciated!

Pre-requisites

  • mitmproxy V4 must be installed and runnable from the terminal. The install method cannot be a prebuilt binary or homebrew, since those packages are missing the Python websockets module. Install via pip or from source.
  • Python 3.6, since I use the new async/await syntax in the mitmproxy plugin
  • npm install to pull in Node and PIP dependencies.

Using

You can either start mitmproxy manually with mitmdump --anticache -s scripts/proxy.py, or mitmproxy-node will do so automatically for you. mitmproxy-node auto-detects if mitmproxy is already running. If you frequently start/stop the proxy, it may be best to start it manually.

import MITMProxy from 'mitmproxy-node';

// Returns Promise<MITMProxy>
async function makeProxy() {
  // Note: Your interceptor can also be asynchronous and return a Promise!
  return MITMProxy.Create(function(interceptedMsg) {
    const req = interceptedMsg.request;
    const res = interceptedMsg.response;
    if (req.rawUrl.contains("target.js") && res.getHeader('content-type').indexOf("javascript") !== -1) {
      interceptedMsg.setResponseBody(Buffer.from(`Hacked!`, 'utf8'));
    }
  }, ['/eval'] /* list of paths to directly intercept -- don't send to server */,
  true /* Be quiet; turn off for debug messages */,
  true /* Only intercept text or potentially-text requests (all mime types with *application* and *text* in them, plus responses with no mime type) */
  );
}

async function main() {
  const proxy = await makeProxy();
  // when done:
  await proxy.shutdown();
}

Without fancy async/await:

import MITMProxy from 'mitmproxy-node';

// Returns Promise<MITMProxy>
function makeProxy() {
  return MITMProxy.Create(function(interceptedMsg) {
    const req = interceptedMsg.request;
    const res = interceptedMsg.response;
    if (req.rawUrl.contains("target.js") && res.getHeader('content-type').indexOf("javascript") !== -1) {
      interceptedMsg.setResponseBody(Buffer.from(`Hacked!`, 'utf8'));
    }
  }, ['/eval'], true, true);
}

function main() {
  makeProxy().then((proxy) => {
    // when done
    proxy.shutdown.then(() => {
      // Proxy is closed!
    });
  });
}

Building

npm run build