Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I suppress 301 redirects from the target with node-http-proxy?

When the target of the proxy issues a redirect (301 in this case), the proxy forwards that to the client. The client is then redirected to the actual domain and no longer using the proxy. Is there a way to prevent this redirection from affecting the client? I want the proxy to follow the redirect, but I don't want the client to leave the proxy while following this redirect.

Example case:

Assume I have a proxy running on fake-proxy.example. When setting the target to a server ip and then setting the host to a domain on that server, the proxy tries to connect via http. The domain is set up to redirect http traffic to https and issues a 301 redirect to the same domain with https. This redirect is sent back to the client, and the client leaves fake-proxy.example and goes to the actual domain (for the sake of this question, let's call it real-domain.example). My configuration:

var options = {
    target: {
        host: '1.1.1.1'
    },
    headers: {
        host: 'real-domain.example'
    },
    autoRewrite: false,
    hostRewrite: false,
    protocalRewrite: false
};

I appreciate the help!

like image 317
David R. Myers Avatar asked Apr 11 '16 19:04

David R. Myers


1 Answers

It's a bit late, but a solution like the following one could help those that stumble on this post :

var http = require('http');
var url = require('url');
var request = require('request');

var host = 'http://real-domain.example';

http.createServer(onRequest).listen(3080);

function onRequest(req, res) {
    var url_parts = url.parse(req.url);
    request({
        url: host + url_parts.pathname
    }).on('error', function(e) {
        res.end(e);
    }).pipe(res);
}

Of course, cookies and the like won't get preserved and you will likely lose some functionality I guess, but at least, it's stable, as in you will not get redirected out of your proxy domain.

like image 196
tchap Avatar answered Oct 08 '22 22:10

tchap