Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doge.js is redirecting to original site with no injection #35

Open
aribornstein opened this issue Mar 16, 2016 · 7 comments
Open

doge.js is redirecting to original site with no injection #35

aribornstein opened this issue Mar 16, 2016 · 7 comments

Comments

@aribornstein
Copy link

I tried running dodge.js locally (see code below). But when I navigate to localhost:8000 I am redirected from my proxy to https://nodejs.org/en/ and there is no image injection. Did something change that would break this code or am I doing something wrong?

var http = require('http'),
    https = require('https'),
    connect = require('connect'),
    httpProxy = require('http-proxy');
    harmon = require('harmon');

var selects = [];
var simpleselect = {};

//<img id="logo" src="/images/logo.svg" alt="node.js">
simpleselect.query = 'img';
simpleselect.func = function (node) {

    //Create a read/write stream wit the outer option 
    //so we get the full tag and we can replace it
    var stm = node.createStream({ "outer" : true });

    //variable to hold all the info from the data events
    var tag = '';

    //collect all the data in the stream
    stm.on('data', function (data) {
        tag += data;
    });

    //When the read side of the stream has ended..
    stm.on('end', function () {

        //Print out the tag you can also parse it or regex if you want
        process.stdout.write('tag:   ' + tag + '\n');
        process.stdout.write('end:   ' + node.name + '\n');

        //Now on the write side of the stream write some data using .end()
        //N.B. if end isn't called it will just hang.  
        stm.end('<img id="logo" src="http://i.imgur.com/LKShxfc.gif" alt="node.js">');

    });
}

selects.push(simpleselect);

//
// Basic Connect App
//
var app = connect();

var proxy = httpProxy.createProxyServer({
    target: 'https://nodejs.org',
    agent  : https.globalAgent, 
    headers: { host: 'nodejs.org' }
})


app.use(harmon([], selects, true));

app.use(
    function (req, res) {
        proxy.web(req, res);
    }
);

http.createServer(app).listen(8000);
@No9
Copy link
Owner

No9 commented Mar 16, 2016

So the origin is sending a 302 through the proxy to the browser
https://nodejs.org/en/ with status HTTP/1.1 302 Moved Temporarily
And the browser is honoring it and going straight to origin instead of through the proxy on localhost

You can intercept the response and format it as http://localhost/en/ and it should work but that is beyond the point of the example.
If you have a preferred site rather than nodejs.org for the example then please make a pull.

@costolo
Copy link

costolo commented Feb 2, 2017

Is there any way you could elaborate a little more on your above comment? I'm running into the same issue and have not been able to resolve it yet.

@No9
Copy link
Owner

No9 commented Feb 6, 2017

Hey @costolo
The example will work if you browse directly to http://localhost:8000/en/
If you wish to intercept the 302 redirect I would suggest starting with the http proxy examples
This should give you a starting place on how you work with a response object https://github.com/nodejitsu/node-http-proxy/blob/master/examples/middleware/modifyResponse-middleware.js

@nival999
Copy link

It doesn't work if you browse directly to that

@nival999
Copy link

I tried with the package.json from the original 2015 commit (removed "^"s in package.json). So I don't think it's a dependency problem.

What's weird is that I think http://localhost:8000/en is redirecting to http://localhost:8000 which then redirects to http://nodejs.org/en

@Emceelamb
Copy link

When I set proxy target to be https://nodejs.org/en/ the injection works as expected.
But when I change it to any other https website I get redirected to the origin. Probably because of 302 redirect described earlier.

Has anyone had any luck solving redirects? I'm having no success with modifying responses.

@Emceelamb
Copy link

After some digging I found out I had errors in the proxy configuration, I forgot the www in host.

Should look like:

var proxy = httpProxy.createProxyServer({
    target: 'https://google.com',
    agent  : https.globalAgent, 
    headers: { host: www.google.com' }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants