简体   繁体   中英

Catch all http requests and send urls to parent

server.js

var http = require('http');
var url = require('url');
var request = require('request');
var fs = require('fs');
http.createServer(onRequest).listen(85);

function onRequest(req, res) {
   if (req.url === "/"){
         fs.readFile('/index.html',function(data,err){
            if (err){
                console.log(err);
                return res.end(err);
            }

            res.write(data);
            res.end();
        });
    }
    var queryData = url.parse(req.url, true).query;
    if (queryData.url) {
        request({
            url: queryData.url
        }).on('error', function(e) {
            res.end(e);
        }).pipe(res);
    }
    else {
        res.end("no url found");
    }
}

What this program does is get a file from a remote location of the user's choosing by adding a query parameter to the end of the url as follows

www.myprogramdomain:85/?url=http://www.domain.com/index.html

If I try to do the above bet set url=http://us.battle.net It shows a webpage without the css. I go into the browser console and see that the page does not have any css or javascript because the scripts are included with local tags such as '/scripts/myscript.js' or something. What I plan to do is embed the webdata from the server inside of an iframe and intercept the requests for files as they come.

The question is, inside of the iframe, how do I intercept http requests and get the url the request is trying to send to the server. Preferably as a string. I searched around for a long time. I even attempted to replace all instances of 'http' with ' http://www.myprogramdomain.com:85/?url=http ' but that did not work as expected as some of the urls such as '/scripts/js.js' do not have 'http' in it. That's why I want to be able to intercept requests and get the urls from the request. Or maybe I want to run a script every time an http request is made from the iframe.

What I want to do is intercept the http request inside of the iframe and replace it with my own request.

If you need some more explanation DO NOT BE AFRAID TO ASK FOR MORE INFORMATION!

What I understand is that you're trying to create a "Man in the Middle" (MITM) Proxy, that will intercept all requests, maybe modify them a bit, then forward them to another url.

In this case, I suggest you use a library that does that already. You can google "node mitm". I found these libraries, you can review their api and see if they provide what you need

This will make you able to forward http://myprogramdomain:85/index.html

to http://www.domain.com/index.html

but you won't be able to use a query string parameter like ?url=http://www.domain.com/index.html

because while this will work to serve the first page (index.html) any requests with a relative path (images, css) will not have that url= which is why it's not working in your code. The path on your server and the end server should be exactly the same.

You could inline the script and css files in the document!

NodeJS:

1) Request the document 2) Search the documents for script/css 3) Request the script/css files 4) Inline the script/css files into the document 5) Send the document to your client

Can you give a try of the following modified code and refactor it further as required..

var http = require('http');
var url = require('url');
var request = require('request');
var fs = require('fs');
http.createServer(onRequest).listen(85);

function parseCookies (req) {
    var list = {},
        rc = req.headers.cookie;

    rc && rc.split(';').forEach(function( cookie ) {
        var parts = cookie.split('=');
        list[parts.shift().trim()] = decodeURI(parts.join('='));
    });

    return list;
}

function onRequest(req, res) {
   if (req.url === "/"){
         fs.readFile('/index.html',function(data,err){
            if (err){
                console.log(err);
                return res.end(err);
            }

            res.write(data);
            res.end();
        });
    }

    var getExternal = function(extUrl) {
       request({
            url: extUrl
        }).on('error', function(e) {
            res.end(e);
        }).pipe(res);
    };

    var queryData = url.parse(req.url, true).query;
    if (queryData.url) {
        var externalUrl = url.parse(queryData.url);
        //res.cookie('externalBaseUrl', externalUrl.protocol + '//' + externalUrl.host);
        res.writeHead(200, {
        'Set-Cookie': 'externalBaseUrl=' + externalUrl.protocol + '//' + externalUrl.host
    });
        getExternal(queryData.url);
    }
    else {
        var cookies = parseCookies(req);
        if (cookies && cookies.externalBaseUrl) {
           //var urlPath = url.parse(req.url).pathname;
            getExternal(cookies.externalBaseUrl + req.url);
        } else {
           res.end("no url found");
        }
    }
}

I checked this as http://localhost:85/?url=https://iiwebi.com which works as cool as a cucumber :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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