libzt icon indicating copy to clipboard operation
libzt copied to clipboard

Node.js Integration

Open laduke opened this issue 8 years ago • 16 comments

😎 Not sure how one goes about hooking a c library to javascript.

All the projects in my node_modules that use external c libraries use https://github.com/nodejs/node-gyp

and there's this thing which may or may not help https://github.com/charto/nbind

I'm going to give it a shot on the weekend and see how far I get.

laduke avatar Jun 22 '17 00:06 laduke

This would be really interesting. We'd wan't to expose the functions in include/libzt.h to the user. For now you'd want to use the experimental branch, but soon that will be merged back into master.

joseph-henry avatar Jun 22 '17 01:06 joseph-henry

getting somewhere, but it fails to compile.

git clone https://github.com/laduke/libzt.git libzt-laduke git checkout laduke-gyp npm install npm run -s -- node-gyp rebuild

make: *** No rule to make target `zto/controller/EmbeddedNetworkController.o', needed by `static_lib'.  Stop.

which is the same error I get from just running make static_lib (on Darwin and Linux)

laduke avatar Jun 22 '17 17:06 laduke

We have something that compiles now. Manual process to get it running:

git clone [email protected]:laduke/libzt.git 
git fetch origin laduke-gyp
git checkout laduke-gyp
git submodule init
git submodule update
make static_lib
npm install  //automatically compiles binding.cc
node test.js

Important files:

binding.cc
binding.gyp
test.js

rebuild binding.cc: npm run -- node-gyp rebuild

This branch^ has a compiled libzt.a in ./darwin, which is not ideal, but I saw that was how that go-lang repo was doing it. Getting node-gyp to compile picotcp and libzt correctly would be better, but I tried and it was quite hard.

All it does right now:

running 0
EXTRA[0] :  pico_protocol.c:  225:       pico_protocol_init: Protocol ethernet registered (layer: 2).
EXTRA[0] :  pico_protocol.c:  225:       pico_protocol_init: Protocol ipv4 registered (layer: 3).
EXTRA[0] :  pico_protocol.c:  225:       pico_protocol_init: Protocol ipv6 registered (layer: 3).
EXTRA[0] :  pico_protocol.c:  225:       pico_protocol_init: Protocol icmp4 registered (layer: 4).
EXTRA[0] :  pico_protocol.c:  225:       pico_protocol_init: Protocol icmp6 registered (layer: 4).
EXTRA[0] :  pico_protocol.c:  225:       pico_protocol_init: Protocol igmp registered (layer: 4).
EXTRA[0] :  pico_protocol.c:  225:       pico_protocol_init: Protocol udp registered (layer: 4).
EXTRA[0] :  pico_protocol.c:  225:       pico_protocol_init: Protocol tcp registered (layer: 4).
INFO [0] :        libzt.cpp: 1867:        zts_start_service: homeDir=./networks/8056c2e21c000001
INFO [0] :    SocketTap.cpp:  105:                SocketTap: set device name to: libzt0
EXTRA[0] :      pico_ipv4.c: 1321:       pico_ipv4_link_add: Assigned ipv4 29.18.59.241 to device libzt0
INFO [0] :      picoTCP.cpp:  114:      pico_init_interface: addr  = 29.18.59.241/7
INFO [0] :      picoTCP.cpp:  127:      pico_init_interface: addr6 = fd80:56c2:e21c::199:9342:d912:3aac
running 1

laduke avatar Aug 15 '17 00:08 laduke

note to self: https://github.com/mafintosh/prebuildify https://github.com/mafintosh/turbo-net/blob/master/package.json

prebuild the libs

laduke avatar Mar 02 '18 18:03 laduke

What happened to the merged pull request? Why are the nodejs examples removed?

heri16 avatar Aug 02 '20 09:08 heri16

Notes to self on creating a ztnet.createConnection() that returns a custom NodeJs Socket which _write() and _read() calls zts_send and zts_recv, to work with any existing npm libraries that use https://nodejs.org/docs/latest-v12.x/api/net.html#net_net_createconnection

Monkey-patching may be applied manually on a per-project basis to cause ztnet.createConnection to be called instead of net.createConnection.

  • PR https://github.com/zerotier/libzt/pull/18

  • https://github.com/Geode-solutions/genepi

  • https://github.com/zerotier/libzt/blob/master/examples/cpp/nonblockingclient.cpp

  • https://nodejs.org/docs/latest-v12.x/api/stream.html#stream_api_for_stream_implementers

  • https://www.derpturkey.com/extending-tcp-socket-in-node-js/

  • https://github.com/altangent/lntools/tree/master/packages/noise

  • https://github.com/nodejs/node/blob/master/lib/internal/js_stream_socket.js

  • https://github.com/nodejs/node/issues/18795#issuecomment-649882045

  • https://www.audero.it/blog/2016/12/05/monkey-patching-javascript/

heri16 avatar Aug 02 '20 17:08 heri16

I never got it into any kind of production state and libzt has changed a little since then. gonna check out your PR!

laduke avatar Aug 07 '20 18:08 laduke

Are there any questions I can answer about the library in order to help this effort, @heri16?

joseph-henry avatar Sep 04 '20 22:09 joseph-henry

@laduke did you manage to get this working?

heri16 avatar May 14 '21 01:05 heri16

Hi @heri16 I thought your PR got merged in? I haven't been able to follow this closely lately.

Did you end up using it for anything?

laduke avatar May 14 '21 16:05 laduke

It did get merged in, but enough changed since its merge that I cant guarantee that it works hence it being moved to the attic. If there's enough interest in bringing it up to feature-parity with the C API I'm not opposed to it being brought back.

joseph-henry avatar May 14 '21 17:05 joseph-henry

Really looking forward to see js binding and use zt in our JavaScript/NodeJS based projects.

I'm looking for underlying module/solutions for building a new semi-private p2p application.

bluet avatar Jul 04 '21 02:07 bluet

Hi all (@laduke @joseph-henry @heri16 @bluet ),

So far any solution to integrate zerotier into JavaScript/Node.js based application?

Thanks!

jerrychong25 avatar Aug 04 '22 16:08 jerrychong25

@jerrychong25 I have no idea. There's no update here.

bluet avatar Aug 08 '22 22:08 bluet

@jerrychong25 I have no idea. There's no update here.

Ok thanks @bluet !

Let's hope for good news soon =)

jerrychong25 avatar Aug 09 '22 16:08 jerrychong25

I've opened a new issue at #234

janvanbouwel avatar Jul 24 '23 16:07 janvanbouwel