PupilTong / node-tuntap2

a opensource, asynchronized, napi-based, business friendly tuntap device addon for nodejs.
Apache License 2.0
12 stars 5 forks source link

How to use it ? #11

Open tchereau opened 1 year ago

tchereau commented 1 year ago

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 ?

tchereau commented 1 year ago

I readed the StreamAPI test unit, but there's no docs, I guess I have to use tap.pipe()/unpipe() and push/unshift, but how ?

PupilTong commented 1 year ago

I’ll give you an example in one week. If not, please ping me on next Monday. Sorry for late reply :D I’m overwhelmed these days

Get Outlook for iOShttps://aka.ms/o0ukef


From: thomas @.> Sent: Tuesday, August 2, 2022 12:39:55 AM To: PupilTong/node-tuntap2 @.> Cc: Subscribed @.***> Subject: Re: [PupilTong/node-tuntap2] How to use it ? (Issue #11)

I readed the StreamAPI test unit, but there's no docs, I guess I have to use tap.pipe()/unpipe() and push/unshift, but how ?

— Reply to this email directly, view it on GitHubhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FPupilTong%2Fnode-tuntap2%2Fissues%2F11%23issuecomment-1201447903&data=05%7C01%7C%7C3d5f24aed95b4fb0785f08da73dc77a5%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637949687983847988%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=mFc05mXnWNaTGU8yr793FcAltHmiLHRmoRVLUnC8Nmk%3D&reserved=0, or unsubscribehttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAC5YDX7EGS5O4EFKEEXAPM3VW74VXANCNFSM55IFVRLQ&data=05%7C01%7C%7C3d5f24aed95b4fb0785f08da73dc77a5%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637949687983847988%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=dcEGWAJuXG47d1EDvNY0el%2BQ1dfkxD6PNxtBq3RVR3E%3D&reserved=0. You are receiving this because you are subscribed to this thread.Message ID: @.***>

PupilTong commented 1 year ago

if you have another Stream, use pipe() If not, use tun.write tun.once('data', ()=>{}) tun.on('data',()=>{})

Docs will be improved, Thanks!

PupilTong commented 1 year ago

More specified examples will be posted soon.

tchereau commented 1 year ago

if you have another Stream, use pipe() If not, use tun.write tun.once('data', ()=>{}) tun.on('data',()=>{})

Docs will be improved, Thanks!

thanks I will try it :)

tchereau commented 1 year ago

It worked, using to machin on different network, and net library, both are connecting, and I can ping each other with the specified ipv4

client (under net.connect):

        client.on('data', (buf) => {
            console.log(`received: ${buf}`);
            tap.write(buf);
        });
        tap.on('data', (buf) => {
            console.log(`sent: ${buf}`);
            client.write(buf);
        });
    }

server (same, using net.connect):

        c.on('data', (buf) => {
            console.log(`received: ${buf}`);
            tap.write(buf);
        });
        tap.on('data', (buf) => {
            console.log(`sent: ${buf}`);
            c.write(buf);
        });
    }

image image

sosoba commented 1 month ago

Is there fixed rule: one call to tap.write(buf); are one network package?

PupilTong commented 1 month ago

Is there fixed rule: one call to tap.write(buf); are one network package?

I think the answer is no. This is implemented under Stream API of Node.js. So I guess it is not promised behavior.

sosoba commented 1 month ago

I think the answer is no. This is implemented under Stream API of Node.js. So I guess it is not promised behavior.

If so, can I send the write method twice to pass the datagram halves? How will tuntap2 know to glue it together and upload it to the network as signle frame?

I found simple Tun/Tap tutorial with simple example named "Tunells" which mappipng Tun/Tap device to TCP stream. Simple VPN. This example presents the same problem as tuntap2 - mapping sending and receiving a packet through a stream. They solved the problem by wrapping the packet or datagram with a header containing its size.

  • Since the program usese TCP, the receiver will see a single stream of data, which makes recognizing packet boundaries difficult. So when a packet or frame is written to the network, its length is prepended (2 bytes) to the actual packet.
  • When data comes in from the network, thanks to the aforementioned trick, we can know how long the next packet is going to be by reading the two-bytes length that precedes it in the stream. When we've read the packet, we write it to the tun/tap interface descriptor, where it will be received by the kernel as coming "from the wire".

I have a suspicion that tuntap2 is working because the datagram size (1500B) does not exceed the standard size of buffer in the Node stream (16kB).

PupilTong commented 1 month ago

I think the answer is no. This is implemented under Stream API of Node.js. So I guess it is not promised behavior.

If so, can I send the write method twice to pass the datagram halves? How will tuntap2 know to glue it together and upload it to the network as signle frame?

I found simple Tun/Tap tutorial with simple example named "Tunells" which mappipng Tun/Tap device to TCP stream. Simple VPN. This example presents the same problem as tuntap2 - mapping sending and receiving a packet through a stream. They solved the problem by wrapping the packet or datagram with a header containing its size.

  • Since the program usese TCP, the receiver will see a single stream of data, which makes recognizing packet boundaries difficult. So when a packet or frame is written to the network, its length is prepended (2 bytes) to the actual packet.
  • When data comes in from the network, thanks to the aforementioned trick, we can know how long the next packet is going to be by reading the two-bytes length that precedes it in the stream. When we've read the packet, we write it to the tun/tap interface descriptor, where it will be received by the kernel as coming "from the wire".

I have a suspicion that tuntap2 is working because the datagram size (1500B) does not exceed the standard size of buffer in the Node stream (16kB).

I think it doesn't change anything about the behavior of the Node API. The tun/tap APIs in linux could just create a file descriptor and it could be used as a normal file on disk. So we just create the tun/tap fd and tell the NodeJS to read/write it

PupilTong commented 1 month ago

I think the answer is no. This is implemented under Stream API of Node.js. So I guess it is not promised behavior.

If so, can I send the write method twice to pass the datagram halves? How will tuntap2 know to glue it together and upload it to the network as signle frame?

I found simple Tun/Tap tutorial with simple example named "Tunells" which mappipng Tun/Tap device to TCP stream. Simple VPN. This example presents the same problem as tuntap2 - mapping sending and receiving a packet through a stream. They solved the problem by wrapping the packet or datagram with a header containing its size.

  • Since the program usese TCP, the receiver will see a single stream of data, which makes recognizing packet boundaries difficult. So when a packet or frame is written to the network, its length is prepended (2 bytes) to the actual packet.
  • When data comes in from the network, thanks to the aforementioned trick, we can know how long the next packet is going to be by reading the two-bytes length that precedes it in the stream. When we've read the packet, we write it to the tun/tap interface descriptor, where it will be received by the kernel as coming "from the wire".

I have a suspicion that tuntap2 is working because the datagram size (1500B) does not exceed the standard size of buffer in the Node stream (16kB).

I think it doesn't change anything about the behavior of the Node API. The tun/tap APIs in linux could just create a file descriptor and it could be used as a normal file on disk. So we just create the tun/tap fd and tell the NodeJS to read/write it