Is there a way to set the source port for a node js https request? I am not asking about the destination, rather the source, ie the port used to send the request.
The context is I am trying to send https requests from a specific port, rather than random ports, thus allowing for locking down iptables. Node is not running as root, thus the port is not 443.
Update : It appears there is a bug in Node. The options localAddress and localPort do not work, at least with a TLS socket.
Update : Found a similar question from last year. The answers were "don't do that", which seems dumb given that node is suppose to be a generic tool. Nodejs TCP connection client port assignment
The feature appears to be undocumented, but you can achieve this by setting BOTH the localAddress and localPort parameters for the options argument in https.request.
For more information, see the code here: https://github.com/nodejs/node/blob/b85a50b6da5bbd7e9c8902a13dfbe1a142fd786a/lib/net.js#L916
A basic example follows:
var https = require('https');
var options = {
hostname: 'example.com',
port: 8443,
localAddress : '192.168.0.1',
localPort: 8444
};
var req = https.request(options, function(res) {
console.log(res);
});
req.end();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With