由於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) | 伊万 | 奧塔維奧·雅各比 |
該項目遵循全企業規範。歡迎任何形式的貢獻!