Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 698
Thomas Avatar asked Aug 10 '20 03:08

Thomas


People also ask

What is the use of HTTP-proxy-middleware?

External WebSocket upgrade In the previous WebSocket examples, http-proxy-middleware relies on a initial http request in order to listen to the http upgrade event. If you need to proxy WebSockets without the initial http request, you can subscribe to the server's http upgrade event manually.

What is interceptor in node JS?

An interceptor is a class annotated with the @Injectable() decorator and implements the NestInterceptor interface. Interceptors have a set of useful capabilities which are inspired by the Aspect Oriented Programming (AOP) technique. They make it possible to: bind extra logic before / after method execution.


1 Answers

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.

like image 176
Rahul Iyer Avatar answered Sep 27 '22 22:09

Rahul Iyer