Sunday 28 May 2017

NodeJs HTTP proxy basic auth

I am trying to implement a simple HTTP proxy that will only try to perform basic auth on the target host.

So far I have the following:

var http = require('http');

const my_proxy =  http.createServer(function(request, response) {
    console.log(request.connection.remoteAddress + ": " + request.method + " " + request.url);

    const options = {
            port: 80
            , host: request.headers['host']
            , method: request.method
            , path: request.url
            , headers: request.headers
            , auth : 'real_user:real_password'
            }
        };

    var proxy_request = http.request(options);

    proxy_request.on('response', function (proxy_response) {
        proxy_response.on('data', function(chunk) {
            response.write(chunk, 'binary');
        });
        proxy_response.on('end', function() {
            response.end();
        });
        response.writeHead(proxy_response.statusCode, proxy_response.headers);
    });

    request.on('data', function(chunk) {
        proxy_request.write(chunk, 'binary');
    });

    request.on('end', function() {
        proxy_request.end();
    });
});
my_proxy.listen(8080);

However, "auth : 'real_user:real_password'" doesn't seem to do anything. Also have tried:

...
auth: {
  user: real_user,
  pass: real_pass
}
...



via danizgod

No comments:

Post a Comment