1

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
}
...
1
  • Have similar issue with Java SDK, found this stuff which useless for me but might help you :) tekloon.medium.com/… Commented Nov 22, 2022 at 9:24

2 Answers 2

1

You have to generate the auth header

var username = 'Test';
var password = '123';
var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');

// auth is: 'Basic VGVzdDoxMjM='

var header = {'Host': 'www.example.com', 'Authorization': auth};
var request = client.request('GET', '/', header);
Sign up to request clarification or add additional context in comments.

Comments

1

DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the

    var username = 'Test';
    var password = '123';
    // Deprecated
    // var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');

    var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
    // auth is: 'Basic VGVzdDoxMjM='

    var header = {'Host': 'www.example.com', 'Authorization': auth};
    var request = client.request('GET', '/', header);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.