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

2

8

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?

Leelah answered 10/8, 2020 at 3:49 Comment(1)
I've done this before with any-proxy, for https you have to generate a cert and --disable-web-security or add the cert to your trusted certs.Hackworth
E
3

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.

Eyesight answered 18/8, 2020 at 15:0 Comment(0)
C
0

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

First install node if not installed and then install AnyProxy npm install -g anyproxy

Write a rule to intercept responses that start with cat. It 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);
    }
  },
};

Same way AnyProxy also supports the beforeSendRequest method too for intercepting requests.

Cathcart answered 19/8, 2020 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.