3

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

Community
  • 1
  • 1
user3356715
  • 181
  • 2
  • 12
  • you want to change the system default port , or just want to change the port for your application ? – mugabits Apr 11 '16 at 16:52
  • Add `:80` to the end of the link the client is accessing. (replace the 80 with whatever port you want) – Oisin Apr 11 '16 at 16:53
  • I want to set the source port for a specific https request. My https server is listening on 8443, since it isn't root. In some cases my server needs to make https requests to a remote server. In those cases I want the requests to be sent from local port 8444. How do I make that happen? I do not see a parameter for the source port. There is a "default" but it doesn't seem to do anything. – user3356715 Apr 11 '16 at 17:00
  • This is a total guess: perhaps try setting the localAddress property of the https.request object to include a port. https://nodejs.org/api/https.html#https_https_request_options_callback – binarymax Apr 11 '16 at 17:25
  • Was trying that as you were typing. So far no luck. The param localAddress is not well documented, so not sure how to use it. – user3356715 Apr 11 '16 at 17:30
  • Indeed, it doesnt work. I get `throw new TypeError('localAddress must be a valid IP: ' + localAddress);` for `192.168.0.6:8444`, but it works fine for `192.168.0.6` without the port – binarymax Apr 11 '16 at 18:14

3 Answers3

3

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();
binarymax
  • 3,147
  • 3
  • 20
  • 16
1

Unfortunately it looks like Node does not support binding a client port. Apparently this isn't a feature that is used much, but it is possible. This link explains port binding fairly well. https://blog.cloudflare.com/cloudflare-now-supports-websockets/ Not sure how to get the nodejs people to consider this change.

user3356715
  • 181
  • 2
  • 12
0

You can use the localPort option in http.Request options to configure a source port. Seems like it's available since v14.x LTS. I tested out with Node v18.12.0 and it works. :)

https://nodejs.org/docs/latest-v14.x/api/http.html#http_http_request_url_options_callback

options.localPort <number> Local port to connect from.
Suhas V
  • 11
  • 1