简体   繁体   English

Node.js代理,用于监视网络请求和响应

[英]Node.js proxy for monitor network requests and responses

I am using Nodejitsu's http proxy to build a tool to monitor web traffic. 我正在使用Nodejitsu的http代理来构建监视网络流量的工具。

httpProxy.createProxyServer({
    target: 'http://localhost:9000'
})
.listen(8000);

util.puts('proxy server listening on port 8000');


http.createServer(function targetServer(req, res) {
    res.writeHead(302, { 'Location': req.headers.host });
    util.puts('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));

    res.end();

})
.listen(9000);

util.puts('target server listening on port 9000');

What I want to do is 我想做的是

  1. Proxy outgoing requests from the client (browser) to my target server 从客户端(浏览器)到目标服务器的代理传出请求
  2. Send them to their original destination url from my target server 从我的目标服务器将它们发送到其原始目标URL
  3. Receive the response on my target server 在我的目标服务器上收到响应
  4. Send the response back to the client 将响应发送回客户端

Basically have a middleman placed between the client and the destination server so i can monitor traffic. 基本上在客户端和目标服务器之间放置了一个中间人,因此我可以监视流量。 However when I try to do this, as soon as I make the 302 request, I get an ECONN error. 但是,当我尝试执行此操作时,一旦发出302请求,就会收到ECONN错误。

Error: connect ECONNREFUSED
    at errnoException (net.js:901:11)
    at Object.afterConnect [as oncomplete] (net.js:892:19)

Can anyone help me figure out whats going on 谁能帮我弄清楚发生了什么事

UPDATE UPDATE

I changed the 302 Location argument like so: 我将302 Location参数更改为:

res.writeHead(302, { 'Location': '/' });

Now, when the browser tries hitting my proxy server, it enters a redirection loop. 现在,当浏览器尝试访问我的代理服务器时,它将进入重定向循环。 Why is this happening? 为什么会这样呢?

If you are going to replace dns queries by your server ip of your proxy it is possible - only works when not ssl, due to invalid certificate for every domain. 如果您打算用代理的服务器IP替换dns查询,则有可能-由于每个域的证书无效,因此仅在非ssl时有效。 Otherwise I don't have any clue how it should work. 否则我不知道它应该如何工作。 Here is an example: 这是一个例子:

const httpProxy = require('http-proxy');
const http = require('http');

var proxy = httpProxy.createProxyServer();

var server = http.createServer(function(req, res) {
    proxy.web(req, res, {
        target: 'http://' + req.headers.host + req.url
    });
    console.log('Yay, got it routed through my proxy.');
    console.log(req.connection.remoteAddress + '--->' + req.headers.host);
});

proxy.on('end', function(req, res) {
    console.log(req.connection.remoteAddress + '<---' + req.headers.host);
    // Do some nasty tracking and monitoring
});

server.listen(8000);

Dns queries must point to this server if you are trying to monitor outgoing requests. 如果您要监视传出的请求,则Dns查询必须指向此服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM