node-mitm icon indicating copy to clipboard operation
node-mitm copied to clipboard

Intercept http call and change the path in URL

Open amar02041988 opened this issue 5 years ago • 1 comments

First of all, thanks for this wonderful utility. I wanted to intercept the http call and update host,port and path(uri) in the url and then let the request flow to this other http service. I am able to change the host and port but not able to change the path. Below is the code:

Wanted to re-write the url from http://api-gateway:8080/service/compression to http://localhost:3000/local/service/compression. Not able to change the path from service/compression to local/service/compression

mitm.on('connect', (socket, opts) => { opts.host = "localhost"; opts.port = 3000; opts.pathname = "local/" + opts.pathname; // Do thing is not updating the uri path socket.bypass(); });

Please let me know what should be done to change the path as well

amar02041988 avatar Apr 11 '20 12:04 amar02041988

Hey,

You're welcome.

Changing the path in this situation is going to be a tad more difficult as then you're not merely bypassing the intercept, but actually reading its body, changing it to your liking and retransmitting it in a new connection. Perhaps this thread could help and direct you to a working solution: https://github.com/moll/node-mitm/issues/51.

Depending on how much control you have over the code you're trying to intercept, you could also just overwrite require("http").request yourself and skip using Mitm.js entirely. That would permit you to change the outgoing request at a higher level than Mitm.js would permit.

moll avatar Apr 11 '20 13:04 moll

I'll close the issue for now as there's nothing particularly actionable, but we can continue chatting on how to implement proxying.

moll avatar Aug 26 '24 05:08 moll