Skip to content

Commit 1cdcd87

Browse files
Create proxy.js
1 parent 5ea4deb commit 1cdcd87

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

src/proxy.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const Proxy = require('http-mitm-proxy').Proxy;
2+
// or using import/module (package.json -> "type": "module")
3+
// import { Proxy } from "http-mitm-proxy";
4+
const proxy = new Proxy();
5+
6+
proxy.onError(function(ctx, err) {
7+
console.error('proxy error:', err);
8+
});
9+
10+
proxy.onRequest(function(ctx, callback) {
11+
if (ctx.clientToProxyRequest.headers.host == 'reqres.in') {
12+
console.log('proxying request for:', ctx.clientToProxyRequest.headers.host);
13+
const chunks = [];
14+
ctx.use(Proxy.gunzip);
15+
16+
ctx.onResponseData(function(ctx, chunk, callback) {
17+
chunks.push(chunk);
18+
chunk = Buffer.from(JSON.stringify({"page":1,"per_page":6,"total":12,"total_pages":2,"data":[{"id":1,"email":"saikrishna.bluth@reqres.in","first_name":"George","last_name":"Bluth","avatar":"https://reqres.in/img/faces/1-image.jpg"},{"id":2,"email":"janet.weaver@reqres.in","first_name":"Janet","last_name":"Weaver","avatar":"https://reqres.in/img/faces/2-image.jpg"},{"id":3,"email":"emma.wong@reqres.in","first_name":"Emma","last_name":"Wong","avatar":"https://reqres.in/img/faces/3-image.jpg"},{"id":4,"email":"eve.holt@reqres.in","first_name":"Eve","last_name":"Holt","avatar":"https://reqres.in/img/faces/4-image.jpg"},{"id":5,"email":"charles.morris@reqres.in","first_name":"Charles","last_name":"Morris","avatar":"https://reqres.in/img/faces/5-image.jpg"},{"id":6,"email":"tracey.ramos@reqres.in","first_name":"Tracey","last_name":"Ramos","avatar":"https://reqres.in/img/faces/6-image.jpg"}],"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}));
19+
return callback(null, chunk);
20+
});
21+
ctx.onResponseEnd(function(ctx, callback) {
22+
const response = Buffer.concat(chunks).toString('utf8');
23+
callback(null, null)
24+
})
25+
}
26+
return callback();
27+
});
28+
29+
console.log('begin listening on 8082')
30+
proxy.listen({port: 8084, host: "::"});

0 commit comments

Comments
 (0)