0

I have been working with node.js to set up a proxy server that will handle incoming client request and will verify that they have the proper certificates to get connected to the server.

What I want to do is to be able to add the client's certificate to their header to craft a user name, that I will pass on to the server.

function (req, res) {

//Here is the client certificate in a variable 
var clientCertificate = req.socket.getPeerCertificate();

// Proxy a web request
return this.handle_proxy('web', req, res);

};

What I want to be able to do is this : req.setHeader('foo','foo')

I know that the proxy.on('proxyReq) exist, but the way the code is set up, I need to be able to use the req parameter.

Is there a way to do this?

Please let me know if I need to clarify my question.

1 Answer 1

1

You can craft your own http request with the headers provided in the original request plus any extra headers that you'd like by using http.request. Just receive the original request, copy the headers into the new request headers, add the new headers and send the new request.

var data = [];
var options = {
  hostname: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': postData.length
  }
};
var req = http.request(options, function(res) {

  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    data.push(chunk);
  });
  res.on('end', function() {
    console.log(data.join(""));

    //send the response to your original request

  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// Set headers here i.e. req.setHeader('Content-Type', originalReq.getHeader('Content-Type'));

// write data to request body

req.write(/*original request data goes here*/);
req.end();
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for this! Could you explain a little more on how you are copying the header into a new request header? Im just still a little confused how you are doing that in the above code.
Sorry, I missed the spot for setting the headers, you would do it before you call write, I'll update the location in the code.
A few more questions: what are in the options that you are passing to the request? Why can this be done on the proxy?
This is the proxy. I added the options from the http.request sample code to clarify. You can add the headers in the options or anytime before calling "write". If I need custom headers, I normally add them outside of the options so that I can reuse the crafted http.request, but it's your choice where to put them.
So I want to make the headers be the the client certificate, which I do by req.socket.getPeerCertificate. How could I add that to the header?
|

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.