0

I'm trying to set up a proxy server that should return the http requests from any page that I'm accessing.

Basically, if I navigate to www.google.com then I'm expecting to get the following requests:

enter image description here

Is this achievable using node-http-proxy module?

I've tried the following code, but can't figure out how to get the requests..

var http = require('http'),  
    httpProxy = require('http-proxy');

//
// Create a proxy server with custom application logic
//
httpProxy.createServer(function (req, res, proxy) {  
  //
  // Put your custom server logic here
  //

  proxy.proxyRequest(req, res, {
    host: 'localhost',
    port: 9000
  });
}).listen(8000);

http.createServer(function (req, res) {  
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(9000);

UPDATE:
I configured your browser to use my proxy server, and changed the code as follows:

var http = require('http'),
    httpProxy = require('http-proxy');

//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createServer(function (req, res, proxy) {  
  //
  // Put your custom server logic here
  //
  proxy.proxyRequest(req, res, {
    host: 'localhost',
    port: 9000
  });
})

proxy.listen(8000);

proxy.on('proxyReq', function(proxyReq, req, res, options) {
  console.log(req.url);
  console.log(proxyReq.url);
});

http.createServer(function(req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(9000);

But there are no logs in the console when I'm accessing different websites

Valip
  • 4,440
  • 19
  • 79
  • 150

1 Answers1

1

You will probably have to configure your browser to use your proxy server:

For chrome,

  1. go to settings,
  2. search for "proxy",
  3. click "change proxy settings",
  4. click LAN settings,
  5. and then add information about your proxy server.

I am not sure if you need to restart the browser, but your request should be sent to the proxy after that.

Here is a an example of what I got working for myself:

var httpProxy = require('http-proxy');
//
// Http Proxy Server with bad target
//
var proxy = httpProxy.createServer({
  target:'http://localhost:9005' 
});

proxy.listen(8005);

var http = require('http');
//
// Create your target server
//
http.createServer(function (req, res) {
  var options = {
    target: 'http://'+req.headers.host
  };
  console.log(req.url)
  console.log(req.headers.host)
  req.host = req.headers.host;
  proxy.web(req, res, options, function(err){console.log('err', err)}); // errorCallback is optional
}).listen(9005);

proxy.on('proxyReq', function (proxyReq, req, res) {
  console.log('request url', JSON.stringify(req.url, true, 2));
});

It works only for http for now.

Vladimir M
  • 4,403
  • 1
  • 19
  • 24
  • This works, but how can I get the HTTP request urls? If I do `console.log(req.url)` then it logs: `http://detectportal.firefox.com/success.txt http://google.com/ http://detectportal.firefox.com/success.txt http://detectportal.firefox.com/success.txt http://detectportal.firefox.com/success.txt and so on` – Valip May 10 '17 at 09:31
  • I'm expecting to get: `http://google.com/`, `?gws_rd=cr&ei=ed4SWeCbNIKYsAHxmIvACw`, `googlelogo_color_120x44dp.png` etc. – Valip May 10 '17 at 09:34
  • I think you need to see the [documentation on request rewriting](https://github.com/nodejitsu/node-http-proxy#setup-a-stand-alone-proxy-server-with-proxy-request-header-re-writing). here you should have access to the original request – Vladimir M May 10 '17 at 09:51
  • For this one I should set the `HTTP Proxy` = 127.0.0.1 and `Port` = 5050 ? If I do this I'm getting `The proxy server is refusing connections` when trying to access a website... – Valip May 10 '17 at 10:00
  • You're right, but I was referring to the code from here https://github.com/nodejitsu/node-http-proxy#setup-a-stand-alone-proxy-server-with-proxy-request-header-re-writing – Valip May 10 '17 at 10:32
  • In the last line of their code they are opening the server that listens on that port. – Vladimir M May 10 '17 at 10:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143866/discussion-between-valip-and-vladimir-m). – Valip May 10 '17 at 10:42