Search code examples
node.jshttpspuppeteerhttp-proxy

How to set up a node http proxy to intercept a particular request/response?


I am making a nodejs pupeteer app that loads a web page. In that page, many resources are loaded. The pupeteer API for intercepting requets/response seems to not work for all resources, so I want to use an http proxy.

I want to intercept a particular request/response in my proxy. If the remote server sends back a response with the first line of the content being the word "cat", then I want to console.log that resource URL before I forward the response back to the client. The request may use https. How can I achieve something like that?


Solution

  • https://anyproxy.io/en/#use-anyproxy-as-an-npm-module

    First install AnyProxy

    For Debian / Ubuntu Users, first do:

    sudo apt-get install nodejs-legacy
    

    Then install AnyProxy:

    npm install -g anyproxy
    

    You need to write a rule to intercept responses which start with cat, and console.log. So maybe something like this:

    // file: sample.js
    module.exports = {
      summary: 'a rule to log responses starting with cat',
      *beforeSendResponse(requestDetail, responseDetail) {
        if responseDetail.body.startsWith("cat "){
             console.log(responseDetail.body);
        }
      },
    };
    

    AnyProxy does not intercept https request by default. To view decryptd info, you have to config the CA certificate.

    anyproxy-ca #generate root CA. manually trust it after that.
    anyproxy --intercept --rule sample.js #launch anyproxy and intercept all https traffic, and use sample.js rule
    

    You may have to do other configuration stuff depending on your setup, but once you set it up writing rules for intercepting responses seems straightforward.