Is there a way to set the source port for a node js https request?

后端 未结 2 821
暖寄归人
暖寄归人 2020-12-21 06:43

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.

T

相关标签:
2条回答
  • 2020-12-21 07:21

    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.

    0 讨论(0)
  • 2020-12-21 07:26

    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();
    
    0 讨论(0)
提交回复
热议问题