@@ -3,54 +3,115 @@ const Proxy = require('http-mitm-proxy').Proxy;
33// import { Proxy } from "http-mitm-proxy";
44const proxy = new Proxy ( ) ;
55
6- proxy . onError ( function ( ctx , err ) {
6+ proxy . onError ( function ( ctx , err ) {
77 console . error ( 'proxy error:' , err ) ;
88} ) ;
99
10- proxy . onRequest ( function ( ctx , callback ) {
10+ proxy . onRequest ( function ( ctx , callback ) {
1111 if ( ctx . clientToProxyRequest . headers . host == 'reqres.in' ) {
12- console . log ( 'proxying request for:' , ctx . clientToProxyRequest . headers . host ) ;
13- const chunks = [ ] ;
12+ console . log ( 'proxying request for:' , ctx . clientToProxyRequest . headers . host ) ;
13+ const chunks = [ ] ;
1414 ctx . use ( Proxy . gunzip ) ;
1515
1616 /* Replace url */
17- if ( ctx . proxyToServerRequestOptions ?. path ) {
18- ctx . proxyToServerRequestOptions . path = ctx . proxyToServerRequestOptions . path . replace ( "page=1" , "page=2" )
17+ if ( ctx . proxyToServerRequestOptions ?. path ) {
18+ ctx . proxyToServerRequestOptions . path = ctx . proxyToServerRequestOptions . path . replace (
19+ 'page=1' ,
20+ 'page=2'
21+ ) ;
1922 }
2023
21- if ( ctx . proxyToServerRequestOptions ?. path ) {
22- ctx . proxyToServerRequestOptions . path = ctx . proxyToServerRequestOptions . path . replace ( "page=1" , "page=2" )
24+ if ( ctx . proxyToServerRequestOptions ?. path ) {
25+ ctx . proxyToServerRequestOptions . path = ctx . proxyToServerRequestOptions . path . replace (
26+ 'page=1' ,
27+ 'page=2'
28+ ) ;
2329 }
24-
30+
2531 /* Replace request body */
26- const requestChunks : any = [ ]
27- ctx . onRequestData ( function ( ctx , chunk , callback ) {
32+ const requestChunks : any = [ ] ;
33+ ctx . onRequestData ( function ( ctx , chunk , callback ) {
2834 // console.log(chunk.toString("utf-8").replace(/Dashboard/g, '<h3>Pwned!</h3>'))
29- //chunk = Buffer.from(chunk.toString().replace(/Dashboard/g, '<h3>Pwned!</h3>'));
30- requestChunks . push ( chunk ) ;
31- return callback ( null , undefined ) ;
32- } ) ;
35+ //chunk = Buffer.from(chunk.toString().replace(/Dashboard/g, '<h3>Pwned!</h3>'));
36+ requestChunks . push ( chunk ) ;
37+ return callback ( null , undefined ) ;
38+ } ) ;
3339
34- ctx . onRequestEnd ( function ( ctx , callback ) {
40+ ctx . onRequestEnd ( function ( ctx , callback ) {
3541 let bodyOld = Buffer . concat ( requestChunks ) . toString ( 'utf8' ) ;
36- bodyOld = bodyOld . replace ( " login=abc" , " login=someusername" )
37- bodyOld = bodyOld . replace ( " password=123" , " password=somepassword" )
38- ctx . proxyToServerRequest ?. write ( bodyOld )
42+ bodyOld = bodyOld . replace ( ' login=abc' , ' login=someusername' ) ;
43+ bodyOld = bodyOld . replace ( ' password=123' , ' password=somepassword' ) ;
44+ ctx . proxyToServerRequest ?. write ( bodyOld ) ;
3945 callback ( ) ;
40- } ) ;
46+ } ) ;
4147
42- ctx . onResponseData ( function ( ctx , chunk , callback ) {
48+ ctx . onResponseData ( function ( ctx , chunk , callback ) {
4349 chunks . push ( chunk ) ;
44- 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!" } } ) ) ;
50+ chunk = Buffer . from (
51+ JSON . stringify ( {
52+ page : 1 ,
53+ per_page : 6 ,
54+ total : 12 ,
55+ total_pages : 2 ,
56+ data : [
57+ {
58+ id : 1 ,
59+ email : 'saikrishna.bluth@reqres.in' ,
60+ first_name : 'George' ,
61+ last_name : 'Bluth' ,
62+ avatar : 'https://reqres.in/img/faces/1-image.jpg' ,
63+ } ,
64+ {
65+ id : 2 ,
66+ email : 'janet.weaver@reqres.in' ,
67+ first_name : 'Janet' ,
68+ last_name : 'Weaver' ,
69+ avatar : 'https://reqres.in/img/faces/2-image.jpg' ,
70+ } ,
71+ {
72+ id : 3 ,
73+ email : 'emma.wong@reqres.in' ,
74+ first_name : 'Emma' ,
75+ last_name : 'Wong' ,
76+ avatar : 'https://reqres.in/img/faces/3-image.jpg' ,
77+ } ,
78+ {
79+ id : 4 ,
80+ email : 'eve.holt@reqres.in' ,
81+ first_name : 'Eve' ,
82+ last_name : 'Holt' ,
83+ avatar : 'https://reqres.in/img/faces/4-image.jpg' ,
84+ } ,
85+ {
86+ id : 5 ,
87+ email : 'charles.morris@reqres.in' ,
88+ first_name : 'Charles' ,
89+ last_name : 'Morris' ,
90+ avatar : 'https://reqres.in/img/faces/5-image.jpg' ,
91+ } ,
92+ {
93+ id : 6 ,
94+ email : 'tracey.ramos@reqres.in' ,
95+ first_name : 'Tracey' ,
96+ last_name : 'Ramos' ,
97+ avatar : 'https://reqres.in/img/faces/6-image.jpg' ,
98+ } ,
99+ ] ,
100+ support : {
101+ url : 'https://reqres.in/#support-heading' ,
102+ text : 'To keep ReqRes free, contributions towards server costs are appreciated!' ,
103+ } ,
104+ } )
105+ ) ;
45106 return callback ( null , chunk ) ;
46107 } ) ;
47- ctx . onResponseEnd ( function ( ctx , callback ) {
48- const response = Buffer . concat ( chunks ) . toString ( 'utf8' ) ;
49- callback ( null , null )
50- } )
108+ ctx . onResponseEnd ( function ( ctx , callback ) {
109+ const response = Buffer . concat ( chunks ) . toString ( 'utf8' ) ;
110+ callback ( null , null ) ;
111+ } ) ;
51112 }
52113 return callback ( ) ;
53114} ) ;
54115
55- console . log ( 'begin listening on 8082' )
56- proxy . listen ( { port : 8084 , host : "::" } ) ;
116+ console . log ( 'begin listening on 8082' ) ;
117+ proxy . listen ( { port : 8084 , host : '::' } ) ;
0 commit comments