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

modifyGoogle.js from the examples fails with "Content Encoding Error"

Open OnkelTem opened this issue 2 years ago • 10 comments

I'm using Firefox 99.0 (64-bit) on Linux Kubuntu 20.04

Running the example fails with this error: image when I open an URL like this one

Ideas?

OnkelTem avatar May 01 '22 11:05 OnkelTem

I added console.log() in the onResponseData() handler:

 ctx.onResponseData(function (ctx, chunk, callback) {
      console.log(chunk.toString());
      ...

and it prints this:

Y͘��J�#���1�8�!�:
�&�[��>�h����ɩm3w����|�m�N���Jc`}<}�~�X�P�Q�@���`FpE�Xr���zLf��q�}��fߖ8~ջij�X4�*�� ����...

So maybe it's the Proxy.gunzip middleware that doesn't work properly?

OnkelTem avatar May 01 '22 12:05 OnkelTem

I should say I installed only ca.pem certificate. Not sure what else should I install.

OnkelTem avatar May 01 '22 12:05 OnkelTem

try https://www.npmjs.com/package/pms-proxy

wawahuy avatar May 07 '22 14:05 wawahuy

Thanks @wawahuy I switched to https://www.npmjs.com/package/mockttp

OnkelTem avatar May 08 '22 15:05 OnkelTem

Thanks @wawahuy I switched to https://www.npmjs.com/package/mockttp

Could you please give a short piece of code how to use it? I have the same issue

vtulin avatar May 15 '22 10:05 vtulin

Could you please give a short piece of code how to use it? I have the same issue

Sure! I had the same question :)

https://github.com/httptoolkit/mockttp/issues/76

And also, I've almost finished a standalone proxying script which is using mockttp. I'm gonna upload a fresh version in an hour or two.

OnkelTem avatar May 15 '22 14:05 OnkelTem

httptoolkit/mockttp#76

It looks like mockttp depends on a module called vm2 which has a binary, this is not equal replacement as http-mitm-proxy is script only. For me that's important as I'm using webvpack.

vtulin avatar May 15 '22 16:05 vtulin

So maybe it's the Proxy.gunzip middleware that doesn't work properly?

I found the problem, that was so simple, we should take a look on content-encoding in response, there we may see that Google not using gzip, it uses br (brotli). So the partial solution is:

import zlib from 'zlib';
...
ctx.addResponseFilter(zlib.createBrotliDecompress());

instead of:

ctx.use(Proxy.gunzip);

Check node_modules\http-mitm-proxy\lib\middleware\gunzip.js. The best solution would be to check content-encoding and add required decompressor if needed.

vtulin avatar May 15 '22 17:05 vtulin

Ok, I see you're solving a different task. But since I promised to share, here is my proxy code: https://github.com/OnkelTem/wmod-proxy/blob/master/src/proxy.ts (not finished or well tested)

OnkelTem avatar May 15 '22 21:05 OnkelTem

Ok, I see you're solving a different task. But since I promised to share, here is my proxy code: https://github.com/OnkelTem/wmod-proxy/blob/master/src/proxy.ts (not finished or well tested)

Thank you

vtulin avatar May 16 '22 05:05 vtulin