由于libuv v1.35.0(节点
v13.12.0&v12.17.0)TCP_KEEPINTVL和TCP_KEEPCNT均具有有点可预测的值,因此该包允许您每个套接字进行调整这些值,但要花费成本,但必须与FFI盖帽交易且相关。检查最新的节点文档中的socket.setKeepaliveEnable,如果默认值对您足够好,则无需使用此软件包。
缺少( TCP_KEEPINTVL和TCP_KEEPCNT ) SO_KEEPALIVE套接字选项设置器和使用FFI的节点的Getters。
在linux & osx ( amd64和arm64 )上进行了测试,应在freebsd和其他方面工作。在win32上安装,但方法是无op的(欢迎拉动请求)。
还支持获得和设置TCP_USER_TIMEOUT (仅linux和osx )选项,该选项与Keep-Alive密切相关。
| 平台 | tcp_keepintvl | tcp_keepcnt | tcp_user_timeout |
|---|---|---|---|
linux | |||
osx | ( TCP_RXT_CONNDROPTIME ) | ||
freebsd | |||
win32 |
传奇:
npm install --save net-keepalive您可以在我们的GitHub页面上找到完整的API参考文档(JSDOC)。
该项目包括打字条定义文件( index.d.ts ),其中概述了API暴露。
文档从JSDOC评论中产生,可以通过发送拉动请求来随意改进它们。
const Net = require ( 'net' ) ,
NetKeepAlive = require ( 'net-keepalive' )
// or
import * as Net from 'net'
import * as NetKeepAlive from 'net-keepalive'
// Create a TCP Server
const srv = Net . createServer ( ( s ) => {
console . log ( 'Connected %j' , s . address ( ) )
// Doesn't matter what it does
s . pipe ( s )
} )
// Start on some port
srv . listen ( 1337 , ( ) => {
console . log ( 'Listening on %j' , srv . address ( ) )
} )
// Connect to that server
const s = Net . createConnection ( { port : 1337 } , ( ) => {
console . log ( 'Connected to %j' , s . address ( ) )
//IMPORTANT: KeepAlive must be enabled for this to work
s . setKeepAlive ( true , 1000 )
// Set TCP_KEEPINTVL for this specific socket
NetKeepAlive . setKeepAliveInterval ( s , 1000 )
// Get TCP_KEEPINTVL for this specific socket
NetKeepAlive . getKeepAliveInterval ( s ) // 1000
// Set TCP_KEEPCNT for this specific socket
NetKeepAlive . setKeepAliveProbes ( s , 1 )
// Get TCP_KEEPCNT for this specific socket
NetKeepAlive . getKeepAliveProbes ( s ) // 1
} )现在,使用iptables添加规则将INPUT链上的所有tcp数据包放到端口1337 。
iptables -I INPUT -m tcp -p tcp --dport 1337 -j DROP如果您使用tcp.srcport == 1337 || tcp.dstport == 1337 loopback数据包。 tcp.srcport == 1337 || tcp.dstport == 1337 wireshark中的过滤器。您将看到以下输出:
玩得开心!
有关SO_KEEPALIVE的更多信息,这里:TCP keepalive howto C代码示例此处:示例
注意:要使用这些方法,您必须启用SO_KEEPALIVE ,并使用Net.Socket -s构建的方法socket.setKeepAlive([enable][, initialDelay])设置套接字的TCP_KEEPIDLE选项!
TCP_KEEPIDLE (since Linux 2.4) The time (in seconds) the connection needs to remain idle before TCP starts sending keepalive probes, if the socket option SO_KEEPALIVE has been set on this socket. This option should not be used in code intended to be portable.
const NetKeepAlive = require ( 'net-keepalive' )
// or
import * as NetKeepAlive from 'net-keepalive'
// .....
const enable = true // enable SO_KEEPALIVE
const initialDuration = 1000 // start probing after 1 second of inactivity
socket . setKeepAlive ( enable , initialDuration ) // sets SO_KEEPALIVE and TCP_KEEPIDLE
const probeInterval = 1000 // after initialDuration send probes every 1 second
NetKeepAlive . setKeepAliveInterval ( socket , probeInterval ) //sets TCP_KEEPINTVL
const maxProbesBeforeFail = 10 // after 10 failed probes connection will be dropped
NetKeepAlive . setKeepAliveProbes ( socket , maxProbesBeforeFail ) // sets TCP_KEEPCNT
// .... 请参阅Code_of_conduct.md
参见贡献
谢谢这些好人(表情符号钥匙):
乔治·赫兹 | 阿尔巴·门德斯(Alba Mendez) | 保罗·卡斯特罗(Paulo Castro) | 雅各布·杰尔(Jacob Jewell) | rmutharaju | 拉斐尔·博尔赫斯(Rafael Borges) | 加尔文 |
ggsubs | Mario Kozjak | 卢卡斯·诺斯(Lukas Knuth) | 伊万 | 奥塔维奥·雅各比 |
该项目遵循全企业规范。欢迎任何形式的贡献!