2017-05-10 7 views
0

は、私がアクセスしています任意のページからhttp requestsを返す必要があり、プロキシサーバーをセットアップしようとしています。Node.jsのHTTPプロキシトラフィック監視

私はwww.google.comに移動した場合、基本的に、私は次の要求を取得するために期待している:

enter image description here

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

UPDATE:私は私のプロキシサーバーを使用するには、ブラウザを設定して、コードを変更し

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

をしかし、私は別のウェブサイト

答えて

1

おそらく肝炎になるにアクセスしていたときに、コンソールにはログが存在しない次のように

  1. は、設定に "プロキシ" のための
  2. 検索、
  3. クリックして "変更プロキシの設定"、
  4. を行く、Chromeの

    :Eプロキシサーバーを使用するようにブラウザを設定するには、LANの設定]をクリックします

  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でのみ動作します。ここでは

は、私が自分のために働いて得たものの一例です。

+0

これは動作しますが、どのように私は、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 など – Valip

+0

取得予定: http: // google.com/'、'?gws_rd = cr&ei = ed4SWeCbNIKYsAHxmIvACw'、 'googlelogo_color_120x44dp.png'など – Valip

+0

[リクエストの書き換えに関するドキュメント](https://github.com/nodejitsu/node)を参照する必要があると思います-http-proxy#setup-a-stand-alone-proxy-server-with-proxy-request-header-re-writing)を使用します。ここで元のリクエストにアクセスする必要があります –