windows - Node.js proxy server with https support -


i'm trying write proxy server proxy (almost) http/s requests. because need catch requests specific https url's , response send file hdd instead of real response web.

whole solution should works proxy in browser , have work on windows 7. started own proxy based on express.js. works great ... unfortunately not via https. trying use several existing node.js proxy servers github (https://github.com/horaci/node-mitm-proxy, https://github.com/hypermediaisobar/hyperproxy , few other) of them worked in windows environment on https (or don't know how congiure them).

finally found somewhere in internet code (don't have link source) works via https (see code below). problems code is, can't find right way check incoming request url , depending on request url handle them in different ways. grateful if me that.

var http = require('http'); var net = require('net');  var debugging = 0;  var regex_hostport = /^([^:]+)(:([0-9]+))?$/;  function gethostportfromstring(hoststring, defaultport) {   var host = hoststring;   var port = defaultport;    var result = regex_hostport.exec(hoststring);   if (result != null) {     host = result[1];     if (result[2] != null) {       port = result[3];     }   }    return( [ host, port ] ); }  // handle http proxy request function httpuserrequest(userrequest, userresponse) {   var httpversion = userrequest['httpversion'];   var hostport = gethostportfromstring(userrequest.headers['host'], 80);    // have extract path requested url   var path = userrequest.url;   result = /^[a-za-z]+:\/\/[^\/]+(\/.*)?$/.exec(userrequest.url);   if (result) {     if (result[1].length > 0) {       path = result[1];     } else {       path = "/";     }   }    var options = {     'host': hostport[0],     'port': hostport[1],     'method': userrequest.method,     'path': path,     'agent': userrequest.agent,     'auth': userrequest.auth,     'headers': userrequest.headers   };    var proxyrequest = http.request(     options,     function (proxyresponse) {       userresponse.writehead(proxyresponse.statuscode, proxyresponse.headers);        proxyresponse.on('data', function (chunk) {           userresponse.write(chunk);         }       );        proxyresponse.on('end',         function () {           userresponse.end();         }       );     }   );    proxyrequest.on('error', function (error) {       userresponse.writehead(500);       userresponse.write(         "<h1>500 error</h1>\r\n<p>error <pre>" + error + "</pre></p>\r\n</body></html>\r\n";       );       userresponse.end();     }   );    userrequest.addlistener('data', function (chunk) {       proxyrequest.write(chunk);     }   );    userrequest.addlistener('end', function () {       proxyrequest.end();     }   ); }      function main() {       var port = 5555; // default port if none on command line        // check command line arguments       (var argn = 2; argn < process.argv.length; argn++) {         if (process.argv[argn] === '-p') {           port = parseint(process.argv[argn + 1]);           argn++;           continue;         }          if (process.argv[argn] === '-d') {           debugging = 1;           continue;         }       }        if (debugging) {         console.log('server listening on port ' + port);       }        // start http server custom request handler callback function       var server = http.createserver(httpuserrequest).listen(port);        server.addlistener('checkcontinue', function (request, response){         console.log(request);         response.writecontinue();       });       // add handler https (which issues connect proxy)       server.addlistener(         'connect',         function (request, socketrequest, bodyhead) {           var url = request['url'];           var httpversion = request['httpversion'];           var hostport = gethostportfromstring(url, 443);            // set tcp connection           var proxysocket = new net.socket();            proxysocket.connect(             parseint(hostport[1]), hostport[0],             function () {                    console.log("proxysocket: " + hostport[1] + " | " + hostport[0]);                   proxysocket.write(bodyhead);                // tell caller connection established               socketrequest.write("http/" + httpversion + " 200 connection established\r\n\r\n");             }           );            proxysocket.on('data', function (chunk) {               socketrequest.write(chunk);             }           );            proxysocket.on('end', function () {               socketrequest.end();             }           );            socketrequest.on('data', function (chunk) {               proxysocket.write(chunk);             }           );            socketrequest.on('end', function () {               proxysocket.end();             }           );            proxysocket.on('error', function (err) {               socketrequest.write("http/" + httpversion + " 500 connection error\r\n\r\n");               socketrequest.end();             }           );            socketrequest.on('error', function (err) {               proxysocket.end();         }           );         }       ); // https connect listener     }      main(); 

are asking

http://expressjs.com/4x/api.html#req.secure req.secure -> https

http://expressjs.com/4x/api.html#req.protocol req.protocol -> http

http://expressjs.com/4x/api.html#req.host req.host

req.url

this should on userrequest

i did not understand question correctly.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -