🔗 net-keepalive
The Missing (TCP_KEEPINTVL
and TCP_KEEPCNT
) SO_KEEPALIVE
socket option setters and getters for Node using ffi-napi
module.
Tested on 🐧 linux
& 🍏 osx
(both amd64
and arm64
), should work on 😈 freebsd
and others. Does not work on 🐄 win32
(pull requests welcome).
Install
npm install --save net-keepalive
Documentation
You can find the full API Reference Document (JSDoc) published on our github pages.
The project includes TypeScript definitions file (index.d.ts
) which gives an overview of the API exposed.
Documentation gets generated from JSDoc comments, feel free to improve them by sending pull requests.
Demo
const Net = NetKeepAlive = // or // Create a TCP Serverconst srv = Net // Start on some portsrv // Connect to that serverconst s = Net
Now using iptables
add rule to drop all tcp
packets on INPUT
chain to port 1337
.
iptables -I INPUT -m tcp -p tcp --dport 1337 -j DROP
If you were monitoring packets on loopback
with tcp.srcport == 1337 || tcp.dstport == 1337
filter in wireshark
. You will see the following output:
Have fun!
More info about SO_KEEPALIVE
here: TCP Keepalive HOWTO
C
Code examples here: Examples
Sample
Note: For these methods to work you must enable SO_KEEPALIVE
and set the TCP_KEEPIDLE
options for socket using Net.Socket
-s built in method 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('custom-net-keepalive')// orimport * as NetKeepAlive from 'custom-net-keepalive' // ..... const enable = true // enable SO_KEEPALIVEconst initialDuration = 1000 // start probing after 1 second of inactivitysocket.setKeepAlive(enable, initialDuration) // sets SO_KEEPALIVE and TCP_KEEPIDLE const probeInterval = 1000 // after initialDuration send probes every 1 secondNetKeepAlive.setKeepAliveInterval(socket, probeInterval) //sets TCP_KEEPINTVL const maxProbesBeforeFail = 10 // after 10 failed probes connection will be droppedNetKeepAlive.setKeepAliveProbes(socket, maxProbesBeforeFail) // sets TCP_KEEPCNT // ....
Code of Conduct
Contributing
See CONTRIBUTING.md