My task is to forward incoming requests to a https URL while adding some headers and adding a client certificate
I'm trying to solve this in nodejs and am running into some issues.
This is the code I am currently playing around with:
http.createServer(onRequest).listen(cnf.port);
function onRequest(client_req, client_res) {
console.log('serve: ' + client_req.url);
let options = {
hostname: 'some.host.name',
port: 443,
path: 'some/path',
method: 'POST',
cert: fs.readFileSync('./resources/cert.pem'),
key: fs.readFileSync('./resources/key.pem'),
timeout: 30000,
headers: {
'Accept': 'text/xml',
'Content-Type': 'application/soap+xml',
'charset': 'UTF-8',
'KD_NR': 'XXXXX',
'Z-Version': '1.8',
// .. lots of other headers .. //
}
};
let proxy = http.request(options, (res) => {
console.log('Forwarding request request');
res.pipe(client_res, {
end: true
});
});
proxy.on('error', (e) => {
console.error("Error during forwarding", e);
});
client_req.pipe(proxy, {
end: true
});
}
Executing this code I am getting:
Error during forwarding { Error: socket hang up
at createHangUpError (_http_client.js:253:15)
at Socket.socketOnEnd (_http_client.js:345:23)
at emitNone (events.js:91:20)
at Socket.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9) code: 'ECONNRESET' }
I am thinking the problem might be that the source host is a https url and the proxy is currently listening for http. Could anyone help me out here?
I have been playing around with the node-http-proxy module as well, but what bugs me is that the options object is different in that module, so I don't know how to add the client certificate and key.
via Jakob Abfalter
No comments:
Post a Comment