Skip to content

Instantly share code, notes, and snippets.

@cvle
Last active May 23, 2018 21:37
Show Gist options
  • Save cvle/59951eb065f23bf2031493be9d4375bf to your computer and use it in GitHub Desktop.
Save cvle/59951eb065f23bf2031493be9d4375bf to your computer and use it in GitHub Desktop.
http-mitm-proxy example
const Proxy = require('http-mitm-proxy');
const proxy = Proxy();
const port = 8081;
const host = '0.0.0.0';
function appendToBody(chunk, s) {
return new Buffer(chunk.toString().replace(/<\/body>/g, s + '</body>'));
}
proxy.onError(function(ctx, err) {
console.error('proxy error:', err);
});
proxy.onRequest(function(ctx, callback) {
if (ctx.clientToProxyRequest.url.indexOf('/embed/stream') == 0) {
ctx.use(Proxy.gunzip);
ctx.onResponseData(function(ctx, chunk, callback) {
chunk = appendToBody(chunk, "<script>document.body.addEventListener('touchstart', () => {});</script>");
console.log(chunk.toString());
return callback(null, chunk);
});
}
return callback();
});
proxy.onResponse(function(ctx, callback) {
// Disable caching
ctx.serverToProxyResponse.headers['cache-control'] = 'no-cache, no-store, must-revalidate';
ctx.serverToProxyResponse.headers['pragma'] = 'no-cache';
ctx.serverToProxyResponse.headers['expires'] = '0';
delete ctx.serverToProxyResponse.headers['age'];
delete ctx.serverToProxyResponse.headers['etag'];
delete ctx.serverToProxyResponse.headers['date'];
return callback(null);
});
console.log(`Start listening on ${host}:${port}`);
// Once ran, you can find the CA certificate in .http-mitm-proxy/certs/ca.pem
// that you can install into your local machine and don't forget to trust it.
// Then change your settings to use this as a http proxy.
proxy.listen({port, host});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment