Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The first workable version :) #6

Merged
merged 13 commits into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm install -g nyc
# - run: sudo npm install -g nyc
- run: sudo npm install -g mocha
- run: npm ci
- run: npm run build --if-present
- run: sudo iptables -P FORWARD ACCEPT
- run: sudo sysctl net.ipv4.ip_forward=1
- run: npm test
34 changes: 34 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Node.js Package

on:
release:
types: [published]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
- run: npm ci
# - run: sudo npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm version ${{github.event.release.tag_name}}
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/build/
/node_modules/
dcdcd*
*node_modules*
index.json
.nyc_output
/coverage
Expand Down
75 changes: 74 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,76 @@
# node-tuntap2

a opensource, asynchronized, napi-based, business friendly tuntap device driver addon for nodejs.
still under development.

## TL; DR

```javascript
const {Tun, Tap} = require('tuntap2');

try {
const tun = new Tun();
tun.mtu = 1400;
tun.ipv4 = '10.0.0.100/24';
tun.ipv6 = 'abcd:1:2:3::/64';
tun.onReceive = (buf) => {
console.log('received:', buf);
}
tun.isUp = true;
console.log(`created tun: ${tun.name}, ip: ${tun.ipv4}, ${tun.ipv6}, mtu: ${tun.mtu}`);
tun.release();

}
catch(e) {
console.log('error: ', e);
process.exit(0);
}
```

## Properties

```typescript
interface Tuntap {
readonly name: string;
readonly isTap: boolean;
readonly isTun: boolean;
mac: string;
mtu: number;
ipv4: string;
ipv6: string;
isUp: boolean;
onReceive: (packet: Buffer) => void;
release: () => Promise<void>;
writePacket: (
data: Buffer,
callback: (
err: NodeJS.ErrnoException,
written: number,
buffer: Buffer,
) => void,
) => void;
}
```

**Note:** Reading properties requires your interface in `up` status.

**Note:** For a `Tun` device, `mac` property is readonly.

**Node:** Please make sure the first 8bits is `00` for setting the mac address of a Tap device.

## Performace

The writing and reading of tuntap is based on nodejs `fs` and `readingStream`. This means these methods is running on libuv threads pool. Please set the threads pool size properly for your application.

## Contribute

Please feel free to create issues, Prs and tell me your ideas!

**I REALLY APPRECIATE IT**

## TODO

* [ ] make it compatible with `node-tuntap`
* [ ] make it compatible with `pipe()`
* [ ] add BSD os support
* [ ] add tap devices support for osx, using utun.
* [ ] add demos
8 changes: 5 additions & 3 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"src/cpp/tuntap-napi.cpp",
"src/cpp/tuntap-linux.cpp"
],
'cflags!': [ '-fno-exceptions', '--std=c++17' ],
'cflags_cc!': [ '-fno-exceptions' ],
'cflags!': [ '-fno-exceptions', '--std=c++17', '-Wno-stringop-truncation' ],
'cflags_cc!': [ '-fno-exceptions', '--std=c++17', '-Wno-stringop-truncation' ],
"cflags+": [
"-fvisibility=hidden"
"-fvisibility=hidden",
'--std=c++17',
'-Wno-stringop-truncation'
],
"include_dirs": [
"<!(node -p \"require('node-addon-api').include_dir\")"
Expand Down
19 changes: 19 additions & 0 deletions demos/readme/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const {Tun, Tap} = require('tuntap2');

try {
const tun = new Tun();
tun.mtu = 1400;
tun.ipv4 = '10.0.0.100/24';
tun.ipv6 = 'abcd:1:2:3::/64';
tun.onReceive = (buf) => {
console.log('received:', buf);
}
tun.isUp = true;
console.log(`created tun: ${tun.name}, ip: ${tun.ipv4}, ${tun.ipv6}, mtu: ${tun.mtu}`);
tun.release();

}
catch(e) {
console.log('error: ', e);
process.exit(0);
}
Loading