Git Product home page Git Product logo

node-tuntap2's Introduction

node-tuntap2

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

Node.js CI Node.js Package

Package quality

TL; DR

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.on('data', (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);
}

install

npm i tuntap2

Properties

interface TuntapI extends Duplex {
    /**
     * the name of this tun/tap device. 
     * This will be generated.
     * @type {string}
     * @memberof TuntapI
     * @since 0.0.1
     */
    readonly name: string;
    /**
     * returns `true` if this is a Tap device
     * @type {boolean}
     * @memberof TuntapI
     * @since 0.0.1
     */
    readonly isTap: boolean;
    /**
     * returns `true` if this is a Tun device
     * @type {boolean}
     * @memberof TuntapI
     * @since 0.0.1
     */
    readonly isTun: boolean;
    /**
     * the mac address of this interface
     * @example
     * ```js
     * this.mac = '00:11:22:33:44:55';
     * ```
     * @type {string}
     * @memberof TuntapI
     * @since 0.1.1
     */
    mac: string;
    /**
     * mtu of this interface
     * @example
     * ```js
     * this.mtu = 1500;
     * ```
     * @type {number}
     * @memberof TuntapI
     * @since 0.0.1
     */
    mtu: number;
    /**
     * ipv4 address/subnet in cidr format of this interface
     * @example
     * ```js
     * this.ipv4='127.0.0.1/24';
     * ```
     * @type {string}
     * @memberof TuntapI
     * @since 0.0.1
     */
    ipv4: string;
    /**
     * ipv6 address/subnet in cidr format of this interface
     * @example
     * ```js
     * this.ipv6='abcd:0:1::/64';
     * ```
     * @type {string}
     * @memberof TuntapI
     * @since 0.0.1
     */
    ipv6: string;
    /**
     * get/set the interface to up/down status
     * @example
     * ```js
     * this.isUp = true; //set this interface up
     * ```
     * @type {boolean}
     * @memberof TuntapI
     * @since 0.0.1
     */
    isUp: boolean;
    /**
     * release this interface
     * @memberof TuntapI
     */
    release: ()=>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.

Streams API

The tuntap object supports Node.js Duplex interface. However, almost all streams api methods is a wrapper of fs.ReadStream or fs.WriteStream. The writing and events are based on them.

Performance

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.

Test

npm test

Contribute

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

I REALLY APPRECIATE IT

TODO

  • add BSD os support
  • add tap devices support for osx, using utun.
  • add demos

node-tuntap2's People

Contributors

pupiltong avatar stefanwerfling avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

node-tuntap2's Issues

Windows support

Would it be possible to add Windows support using something like WinTun?

Class 'Tuntap' incorrectly implements interface 'TuntapI'

With the update to the latest 18.x version of nodejs ("@types/node": "^18.19.33",) there were many new methods in the interface. Could you clarify this issue?

3.066   SOLINK_MODULE(target) Release/obj.target/tuntap2Addon.node
3.170   COPY Release/tuntap2Addon.node
3.175 make: Leaving directory '/opt/flyingfish/node_modules/tuntap2/build'
3.177 gyp info ok 
8.059 src/ts/Tuntap.ts(4,14): error TS2420: Class 'Tuntap' incorrectly implements interface 'TuntapI'.
8.059   Type 'Tuntap' is missing the following properties from type 'TuntapI': iterator, map, filter, forEach, and 10 more.

image

These come from the duplex interface:
export interface TuntapI extends Duplex

How to use it ?

I created the tap
now how can I push input and read output ?
in example, if I have a serveur and a client, I've create a server, and both script can connect, now I can read data from one tap, and send it to the other ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.