引言
我想测试一个内部会发起请求的函数,于是想到能否在测试期间启动一个服务器。
后来我意识到,发出请求本身不需要先开服务器,转而好奇请求会使用哪个源端口。
发出和接收请求可以用同一个端口吗?
📓 说明: 技术上可行,但不常见。
有些协议(例如 ISAKMP)确实这么做,但灵活性不足,所以并不普遍。
这里有两个限制:
若两端地址、端口都固定,就只有一条对应的数据流。浏览器通常会与同一个 Web 服务器建立多个 TCP 连接来提高性能,所以有时网页文字先出现,图片后加载。单条数据流无法利用这种连接并行性。
一台主机也可能同时充当客户端和服务器。若把两端端口都限定为同一号码,会限制这两种角色可以建立的连接。 一般来说,目的端使用知名端口、源端使用临时端口最为灵活。
- Ron Trunk(2016 年 7 月 13 日 11:10)答:技术上能用同一个端口,ISAKMP 就是例子,但并不灵活。固定一条流无法像常见浏览器那样向同一服务器开多条 TCP 连接,让页面文字和图片并行加载。Network Engineering Stack Exchange。https://networkengineering.stackexchange.com/questions/33061/why-cant-a-single-port-be-used-for-both-incoming-and-outgoing-traffic/33063#33063
Node.js 请求的源端口由谁决定?
默认选取临时端口的是内核,而非 Node.js。
- Henningsen [@addaleax](2021 年 1 月 4 日)答:隐式分配端口的是内核,而非 libuv。端口也非完全随机,通常在 49152–65535 范围内,以避开服务监听端口。axios 使用内置 http 模块,后者通过 net.Socket 或 libuv 通信。连接建立后,客户端与服务端套接字在 I/O 层没有区别。文件操作可以交给 libuv 线程池异步执行;epoll 可用于网络和许多其他非文件系统 I/O。GitHub 回复。https://github.com/nodejs/help/issues/3160#issuecomment-754295674
再想一步:这也是个网络问题
处于 NAT 后方时,本机选定的端口还可能被映射到 NAT 设备分配的外部端口。
NAT 设备会按自身的实现为连接或会话选择外部端口,返回的数据也会到达同一外部端口。
- Brad(2013 年 3 月 29 日 18:01)答:外部端口取决于 NAT 实现,回复会回到该端口。如果局域网中的两台计算机以相同源端口向同一网站发送 HTTP 请求,NAT 可为两条会话分配不同的外部端口,并保存映射。数据返回时,它便能按各自的端口将数据送回正确的内网地址和端口。Stack Overflow。https://stackoverflow.com/questions/15708011/how-is-source-port-for-http-determined-is-there-ever-collision-in-nat/15708801#15708801
如何给请求绑定源端口?
Node.js 服务向外部服务器(例如某个 API)发送 HTTP 请求时,源端口通常由操作系统自动分配。若想指定端口,可以从以下途径入手。
-
显式指定本地端口
- 创建 HTTP 客户端请求(例如使用 http 或 axios)时,可在请求设置里指定本地端口。
- 用
localPort属性配置源端口。 - 使用 http 模块的示例:
const http = require('http'); const options = { hostname: 'example.com', port: 80, localPort: 52000, // Specify your desired local port method: 'GET', }; const req = http.request(options, (res) => { // Handle the response }); req.end();
-
访问套接字属性
- Node.js 的 HTTP 请求可以访问底层套接字。
- 套接字上的
localAddress和localPort可以用来查看连接使用的本地地址和端口。 - 例如:
const http = require('http'); const req = http.request('http://example.com', (res) => { // Handle the response }); req.on('socket', (socket) => { socket.localPort = 52000; // Set your desired local port }); req.end();
-
用底层套接字自定义 HTTP 客户端
- 需要更精细的控制时,可以直接用 Node.js 套接字编写 HTTP 客户端。
- 这样就能直接处理套接字的参数。
- 底层实现示例:
const net = require('net'); const client = new net.Socket(); client.connect(80, 'example.com', () => { client.localPort = 52000; // Set your desired local port client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n'); }); client.on('data', (data) => { // Handle the response client.end(); });
指定端口在测试、调试或特定网络配置下很有用。可以按实际需要选择做法。🚀
更多细节见 Node.js HTTP 文档。¹²³
来源:与 Bing 的对话,2024 年 3 月 14 日。 (1) node.js - Specify client port Node js request - Stack Overflow. https://stackoverflow.com/questions/51454221/specify-client-port-node-js-request. (2) How to run Node/Express on a specific port? - GeeksforGeeks. https://www.geeksforgeeks.org/how-to-run-node-express-on-a-specific-port/. (3) Is there a way to set the source port for a node js https request?. https://stackoverflow.com/questions/36554680/is-there-a-way-to-set-the-source-port-for-a-node-js-https-request. (4) Bind your node.js/express apps to localhost - Joe Flateau. https://joeflateau.net/posts/bind-your-node-js-express-apps-to-localhost. (5) Specify port number in HTTP request (node.js) - Stack Overflow. https://stackoverflow.com/questions/31662411/specify-port-number-in-http-request-node-js. (6) undefined. http://example.com. (7) How does Node.js choose random ports? - Stack Overflow. https://stackoverflow.com/questions/9901043/how-does-node-js-choose-random-ports. (8) node.js - Nodejs random free tcp ports - Stack Overflow. https://stackoverflow.com/questions/28050171/nodejs-random-free-tcp-ports. (9) Assigning a Random Port in Node - Brian Childress. https://www.brianchildress.co/assign-random-port-in-node/. (10) Assign a Random Port to Node.js Server - Maxim Orlov. https://maximorlov.com/tips/assign-a-random-port-to-nodejs-server/. (11) HTTP | Node.js v21.7.1 Documentation. https://nodejs.org/api/http.html. (12) undefined. https://github.com/joyent/node/blob/v0.6.11/lib/net.js. (13) node.js - Specify local port for TCP socket client connection - Stack .... https://stackoverflow.com/questions/25642583/specify-local-port-for-tcp-socket-client-connection.
这一功能当时似乎没有出现在文档中,但在 https.request 的 options 参数中同时设置 localAddress 和 localPort 就可以做到。
相关实现见:https://github.com/nodejs/node/blob/b85a50b6da5bbd7e9c8902a13dfbe1a142fd786a/lib/net.js#L916
一个基本示例如下:
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(2016 年 4 月 11 日 18:23)答:在 https.request 的 options 中同时设置 localAddress 和 localPort 即可。Stack Overflow。https://stackoverflow.com/questions/36554680/is-there-a-way-to-set-the-source-port-for-a-node-js-https-request/36556341#36556341