Skip to content

Commit

Permalink
chore: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
hertzg committed Jul 19, 2020
1 parent 6d5a128 commit 1057127
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 46 deletions.
21 changes: 10 additions & 11 deletions .github/workflows/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ name: Mark stale issues and pull requests

on:
schedule:
- cron: "0 0 * * *"
- cron: '0 0 * * *'

jobs:
stale:

runs-on: ubuntu-latest

steps:
- uses: actions/stale@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: 'This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.'
stale-pr-message: 'This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.'
stale-issue-label: 'no-issue-activity'
stale-pr-label: 'no-pr-activity'
days-before-stale: 25
days-before-close: 7
- uses: actions/stale@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: 'This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.'
stale-pr-message: 'This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.'
stale-issue-label: 'no-issue-activity'
stale-pr-label: 'no-pr-activity'
days-before-stale: 25
days-before-close: 7
13 changes: 3 additions & 10 deletions .jsdoc.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
{
"source": {
"include": [
"./lib",
"./index.js"
],
"exclude": [
"node_modules"
]
"include": ["./lib", "./index.js"],
"exclude": ["node_modules"]
},
"recurseDepth": 10,
"plugins": [
Expand All @@ -24,9 +19,7 @@
},
"tags": {
"allowUnknownTags": true,
"dictionaries": [
"jsdoc"
]
"dictionaries": ["jsdoc"]
},
"templates": {
"cleverLinks": false,
Expand Down
49 changes: 27 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
[codecov_url]: https://codecov.io/gh/hertzg/node-net-keepalive
[codecov_shield]: https://codecov.io/gh/hertzg/node-net-keepalive/branch/master/graph/badge.svg


# 🔗 net-keepalive

[![NPM](https://nodei.co/npm/net-keepalive.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/net-keepalive/)

The Missing (`TCP_KEEPINTVL` and `TCP_KEEPCNT`) `SO_KEEPALIVE` socket option setters and getters for Node using [`ffi-napi`](https://www.npmjs.com/package/ffi-napi) module.
Expand All @@ -46,6 +46,7 @@ You can find the [full API Reference](https://hertzg.github.io/node-net-keepaliv
Documentation gets generated from JSDoc comments, feel free to improve them by sending pull requests.

## Demo

```Javascript
var Net = require('net')
, NetKeepAlive = require('net-keepalive')
Expand All @@ -66,29 +67,31 @@ srv.listen(1337, function(){
// Connect to that server
var s = Net.createConnection({port:1337}, function(){
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
// Set TCP_KEEPCNT for this specific socket
NetKeepAlive.setKeepAliveProbes(s, 1)
// Get TCP_KEEPCNT for this specific socket

// Get TCP_KEEPCNT for this specific socket
NetKeepAlive.getKeepAliveProbes(s) // 1
});
```

Now using `iptables` add rule to drop all `tcp` packets on `INPUT` chain to port `1337`.

```bash
$ 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:
```

If you were monitoring packets on `loopback` with `tcp.srcport == 1337 || tcp.dstport == 1337` filter in `wireshark`. You will see the following output:

[![Wireshark screenshot KEEPALIVE](http://hertzg.github.io/node-net-keepalive/images/wireshark.jpg)](http://hertzg.github.io/node-net-keepalive/images/wireshark.jpg)

Expand All @@ -99,7 +102,7 @@ More info about `SO_KEEPALIVE` here: [TCP Keepalive HOWTO](http://tldp.org/HOWTO

## API

***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])`](https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay) !***
**_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])`](https://nodejs.org/api/net.html#net_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.

Expand All @@ -117,44 +120,46 @@ socket.setKeepAlive(enable, initialDuration) // sets SO_KEEPALIVE
var probeInterval = 1000 // after initialDuration send probes every 1 second
NetSocket.setKeepAliveInterval(socket, probeInterval) //sets TCP_KEEPINTVL

var maxProbesBeforeFail = 10 // after 10 failed probes connection will be dropped
var maxProbesBeforeFail = 10 // after 10 failed probes connection will be dropped
NetSocket.setKeepAliveProbes(socket, maxProbesBeforeFail) // sets TCP_KEEPCNT

// ....
// ....
```

### setKeepAliveInterval(socket, msecs)
* `socket` - `instanceof Net.Socket`- Socket to modify
* `msecs` - `Number` - Time in milliseconds between KeepAlive probes.
* Returns `true` on success

- `socket` - `instanceof Net.Socket`- Socket to modify
- `msecs` - `Number` - Time in milliseconds between KeepAlive probes.
- Returns `true` on success

Sets `TCP_KEEPINTVL` to `msecs` miliseconds (converted to seconds `int` internally) for the `socket` based on its file descriptor (`fd`)

TCP_KEEPINTVL (since Linux 2.4) The time (in seconds) between individual keepalive probes. This option should not be used in code intended to be portable.

### getKeepAliveInterval(socket)
* `socket` - `instanceof Net.Socket`- Socket to modify
* Returns `msecs` - `Number` - Time in milliseconds between KeepAlive probes on success

- `socket` - `instanceof Net.Socket`- Socket to modify
- Returns `msecs` - `Number` - Time in milliseconds between KeepAlive probes on success

Gets `TCP_KEEPINTVL`. The `msecs` miliseconds (converted from seconds `int` internally) set for the `socket` based on its file descriptor (`fd`)

TCP_KEEPINTVL (since Linux 2.4) The time (in seconds) between individual keepalive probes. This option should not be used in code intended to be portable.

### setKeepAliveProbes(socket, count)

### setKeepAliveProbes(socket, count)
* `socket` - `instanceof Net.Socket`- Socket to modify
* `count` - `Number` - Number of probes to send before dropping the connection
* Returns `true` on success
- `socket` - `instanceof Net.Socket`- Socket to modify
- `count` - `Number` - Number of probes to send before dropping the connection
- Returns `true` on success

Sets `TCP_KEEPCNT` to `count` number of probes for the `socket` based on its file descriptor (`fd`)

TCP_KEEPCNT (since Linux 2.4) - The maximum number of keepalive probes TCP should send before dropping the connection. This option should not be used in code intended to be portable.

### getKeepAliveProbes(socket)

### getKeepAliveProbes(socket)
* `socket` - `instanceof Net.Socket`- Socket to modify
* Returns `count` - `Number` - Number of probes to send before dropping the connection on success.
- `socket` - `instanceof Net.Socket`- Socket to modify
- Returns `count` - `Number` - Number of probes to send before dropping the connection on success.

Gets `TCP_KEEPCNT`. The `count` number of probes set for the `socket` based on its file descriptor (`fd`)

Expand Down
4 changes: 1 addition & 3 deletions test/test-probes.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ describe('keep-alive probes', () => {
const oldHandle = socket._handle

socket._handle = { fd: -99999 }
;(() => Lib.getKeepAliveProbes(socket)).should.throw(
'getsockopt EBADF'
)
;(() => Lib.getKeepAliveProbes(socket)).should.throw('getsockopt EBADF')

socket._handle = oldHandle
socket.destroy()
Expand Down

0 comments on commit 1057127

Please sign in to comment.