Desde Libuv V1.35.0 (nodo
v13.12.0yv12.17.0) TantoTCP_KEEPINTVLcomoTCP_KEEPCNTtienen valores algo predecibles, este paquete le permite ajustar esos valores por cada enchufe, pero a un costo de tener que lidiar con las dependencias de FFI y sus dependencias. Verifique los últimos documentos de nodo para Socket.SetkeepAliveEnable, si los valores predeterminados son lo suficientemente buenos para usted, entonces no necesita usar este paquete.
Falta ( TCP_KEEPINTVL y TCP_KEEPCNT ) SO_KEEPALIVE socket setters y getters para el nodo usando FFI.
Probado en linux y osx (tanto amd64 como arm64 ), debe funcionar en freebsd y otros. Instala en win32 pero los métodos son OPS (solicitudes de extracción de bienvenida).
También hay soporte para obtener y configurar la opción TCP_USER_TIMEOUT (solo linux y osx ), que está estrechamente relacionada con Keep-Alive.
| Plataforma | Tcp_keepintvl | Tcp_keepcnt | Tcp_user_timeout |
|---|---|---|---|
linux | |||
osx | ( TCP_RXT_CONNDROPTIME ) | ||
freebsd | |||
win32 |
Leyenda:
npm install --save net-keepalivePuede encontrar el documento de referencia de API completo (JSDOC) publicado en nuestras páginas GitHub.
El proyecto incluye el archivo de definiciones de TypeScript ( index.d.ts ) que proporciona una descripción general de la API expuesta.
La documentación se genera a partir de comentarios de JSDOC, no dude en mejorarlos enviando solicitudes de extracción.
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
} ) Ahora, usando iptables agregue la regla para soltar todos los paquetes tcp en la cadena INPUT al puerto 1337 .
iptables -I INPUT -m tcp -p tcp --dport 1337 -j DROP Si estaba monitoreando los paquetes en loopback con tcp.srcport == 1337 || tcp.dstport == 1337 Filtro en wireshark . Verá la siguiente salida:
¡Divertirse!
Más información sobre SO_KEEPALIVE aquí: TCP Keepalive Howto C Code Ejemplos aquí: Ejemplos
Nota: Para que estos métodos funcionen, debe habilitar SO_KEEPALIVE y establecer las opciones TCP_KEEPIDLE para el socket utilizando Net.Socket -s incorporado método socket.setKeepAlive([enable][, initialDelay]) !
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
// .... Ver code_of_conduct.md
Ver contribuyente.md
Gracias a estas maravillosas personas (Key Emoji):
George Hertz | Alba méndez | Paulo Castro | Jacob Jewell | Rmutharaju | Rafael Borges | Calvin |
ggsubs | Mario Kozjak | Lukas Knuth | Iván | Otávio Jacobi |
Este proyecto sigue la especificación de todos los contribuyentes. ¡Contribuciones de cualquier tipo bienvenido!