【问题标题】:Node.js http-proxy traffic monitoringNode.js http-proxy 流量监控
【发布时间】:2017-05-10 07:50:14
【问题描述】:

我正在尝试设置一个代理服务器,它应该从我正在访问的任何页面返回 http requests

基本上,如果我导航到www.google.com,那么我预计会收到以下请求:

这可以使用node-http-proxy 模块实现吗?

我尝试了以下代码,但无法弄清楚如何获取请求..

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);

更新:
我将你的浏览器配置为使用我的代理服务器,并将代码更改如下:

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);

但是当我访问不同的网站时,控制台中没有日志

【问题讨论】:

    标签: javascript node.js proxy node-http-proxy


    【解决方案1】:

    您可能需要配置您的浏览器以使用您的代理服务器:

    对于铬,

    1. 进入设置,
    2. 搜索“代理”,
    3. 点击“更改代理设置”,
    4. 点击局域网设置,
    5. 然后添加有关您的代理服务器的信息。

    我不确定您是否需要重新启动浏览器,但您的请求应该在此之后发送到代理。

    这是我为自己工作的一个例子:

    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));
    });
    

    它目前仅适用于 http。

    【讨论】:

    • 这行得通,但我怎样才能得到 HTTP 请求的 url?如果我这样做console.log(req.url),那么它会记录: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
    • 我希望得到:http://google.com/?gws_rd=cr&ei=ed4SWeCbNIKYsAHxmIvACwgooglelogo_color_120x44dp.png
    • 我认为您需要查看documentation on request rewriting。在这里您应该可以访问原始请求
    • 对于这个我应该设置HTTP Proxy = 127.0.0.1 和Port = 5050 ?如果我这样做,我会在尝试访问网站时收到 The proxy server is refusing connections...
    • 你是对的,但我指的是这里的代码github.com/nodejitsu/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多