Como o Libuv v1.35.0 (nó
v13.12.0ev12.17.0)TCP_KEEPINTVLeTCP_KEEPCNTtêm valores um tanto previsíveis que esse pacote permite ajustar esses valores por cada soquete, mas com um custo de ter que lidar com a Overhead e suas dependências. Verifique os documentos mais recentes do nó para soquete.
O ausente ( TCP_KEEPINTVL e TCP_KEEPCNT ) SO_KEEPALIVE Socket Setters e getters para nó usando FFI.
Testado no linux & osx ( amd64 e arm64 ), deve trabalhar no freebsd e em outros. Instala no win32 , mas os métodos não são operações (solicitações de puxar bem-vindas).
Também há suporte para obter e definir a opção TCP_USER_TIMEOUT (somente linux e osx ), que está intimamente relacionada ao Keep-Alive.
| Plataforma | Tcp_keepintvl | Tcp_keepcnt | Tcp_user_timeout |
|---|---|---|---|
linux | |||
osx | ( TCP_RXT_CONNDROPTIME ) | ||
freebsd | |||
win32 |
Lenda:
npm install --save net-keepaliveVocê pode encontrar o documento de referência da API (JSDOC) publicado em nossas páginas do Github.
O projeto inclui o arquivo de definições do TypeScript ( index.d.ts ), que fornece uma visão geral da API exposta.
A documentação é gerada a partir de comentários do JSDOC, sinta -se à vontade para melhorá -los enviando solicitações de tração.
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
} ) Agora, usando iptables adicione a regra para soltar todos os pacotes tcp na cadeia INPUT na porta 1337 .
iptables -I INPUT -m tcp -p tcp --dport 1337 -j DROP Se você estava monitorando pacotes em loopback com tcp.srcport == 1337 || tcp.dstport == 1337 filtro no wireshark . Você verá a seguinte saída:
Divirta-se!
Mais informações sobre SO_KEEPALIVE aqui: TCP Keepalive HowTO C Code Exemplos aqui: Exemplos
NOTA: Para que esses métodos funcionem, você deve ativar SO_KEEPALIVE e defina as opções TCP_KEEPIDLE para soquete usando Net.Socket -s embutido no 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
// .... Consulte Code_of_conduct.md
Consulte Contribuindo.md
Obrigado a essas pessoas maravilhosas (key emoji):
George Hertz | Alba Mendez | Paulo Castro | Jacob Jewell | Rmutharaju | Rafael Borges | Calvin |
GGSUBS | Mario Kozjak | Lukas Knuth | Ivan | Otávio Jacobi |
Este projeto segue a especificação de todos os contribuintes. Contribuições de qualquer tipo de boas -vindas!