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

Fixed issues with node v16 compatibility #10

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/build
/build
/node_modules
package-lock.json
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
17 changes: 0 additions & 17 deletions Makefile

This file was deleted.

9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
node-tuntap
===========

Forked for node v16 compatibility
---------------------------------

Node-tuntap is a node module that allows creating and using [`tun` and `tap`
interfaces](https://en.wikipedia.org/wiki/TUN/TAP) in javascript.

Expand Down Expand Up @@ -34,11 +37,7 @@ Simple sample :

Building/installing
-------------------

To build the module, just run `make`. *node-gyp* is required to build it.

There is currently no way to install it. You will have to copy the files in
the right places by hand.
`npm install @gtnoble/node-tuntap`

Available options
-----------------
Expand Down
25 changes: 15 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "node-tuntap",
"version": "1.0.6",
"name": "@gtnoble/node-tuntap",
"version": "2.1.0",
"description": "Tunnel interface management in node",
"author": "Maxime Ferrino",
"author": "Garret Noble",
"repository": {
"type": "git",
"url": "https://github.com/binarysec/node-tuntap.git"
"url": "https://github.com/gtnoble/node-tuntap.git"
},
"keywords": [
"tunnel",
Expand All @@ -14,15 +14,20 @@
"tuntap",
"interface"
],
"main": "./index.js",
"scripts": {
"install": "node-gyp rebuild"
"dependencies": {
"node-gyp": ">=9.1.0"
},
"devDependencies": {
"node-gyp": ">=9.1.0"
},
"main": "./index.js",
"gypfile": true,
"bugs": {
"url": "https://github.com/binarysec/node-tuntap/issues"
"url": "https://github.com/gtnoble/node-tuntap/issues"
},
"homepage": "https://github.com/binarysec/node-tuntap",
"homepage": "https://github.com/gtnoble/node-tuntap",
"license": "GPLv3",
"os" : [ "!win32" ]
"os": [
"!win32"
]
}
2 changes: 1 addition & 1 deletion src/module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

using namespace v8;

void InitAll(Handle<Object> exports, Handle<Object> module) {
void InitAll(Local<Object> exports, Local<Object> module) {
Tuntap::Init(module);
}

Expand Down
4 changes: 2 additions & 2 deletions src/module.hh
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
#include "tuntap.hh"

#define TT_THROW(str) \
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate, str)))
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate, str).ToLocalChecked()))

#define TT_THROW_TYPE(str) \
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, str)))
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, str).ToLocalChecked()))

#define NVM_NEW_INSTANCE(target, isolate, argc, argv) ( \
(target) \
Expand Down
62 changes: 35 additions & 27 deletions src/tuntap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ Tuntap::~Tuntap() {
delete[] this->read_buff;
}

void Tuntap::Init(Handle<Object> module) {
void Tuntap::Init(Local<Object> module) {
Isolate* isolate = module->GetIsolate();
Local<Context> context = isolate->GetCurrentContext();

// Prepare constructor template
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
tpl->SetClassName(String::NewFromUtf8(isolate, "tuntap"));
tpl->SetClassName(String::NewFromUtf8(isolate, "tuntap").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

// Prototype
Expand All @@ -62,14 +63,15 @@ void Tuntap::Init(Handle<Object> module) {

#undef SETFUNC

constructor.Reset(isolate, tpl->GetFunction());
constructor.Reset(isolate, tpl->GetFunction(context).ToLocalChecked());

module->Set(String::NewFromUtf8(isolate, "exports"), tpl->GetFunction());
module->Set(context, String::NewFromUtf8(isolate, "exports").ToLocalChecked(), tpl->GetFunction(context).ToLocalChecked());
}

void Tuntap::New(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
Local<Context> context = isolate->GetCurrentContext();
bool ret;
std::string err_str;
Local<Object> main_obj;
Expand All @@ -80,7 +82,7 @@ void Tuntap::New(const FunctionCallbackInfo<Value>& args) {
obj = new Tuntap();
obj->Wrap(args.This());
if(args[0]->IsObject()) {
main_obj = args[0]->ToObject();
main_obj = args[0]->ToObject(context).ToLocalChecked();
ret = obj->construct(main_obj, err_str);
if(ret == false) {
obj->fd = -1;
Expand All @@ -100,7 +102,7 @@ void Tuntap::New(const FunctionCallbackInfo<Value>& args) {
}
}

bool Tuntap::construct(Handle<Object> main_obj, std::string &error) {
bool Tuntap::construct(Local<Object> main_obj, std::string &error) {
Isolate* isolate = main_obj->GetIsolate();
HandleScope scope(isolate);
Local<Array> keys_arr;
Expand Down Expand Up @@ -195,6 +197,7 @@ void Tuntap::writeBuffer(const FunctionCallbackInfo<Value>& args) {
}
void Tuntap::open(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
HandleScope scope(isolate);
Local<Object> main_obj = Object::New(isolate);
std::string err_str;
Expand All @@ -212,7 +215,7 @@ void Tuntap::open(const FunctionCallbackInfo<Value>& args) {
return;
}

main_obj = args[0]->ToObject();
main_obj = args[0]->ToObject(context).ToLocalChecked();
}

ret = obj->construct(main_obj, err_str);
Expand Down Expand Up @@ -246,6 +249,7 @@ void Tuntap::close(const FunctionCallbackInfo<Value>& args) {

void Tuntap::set(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
HandleScope scope(isolate);
Tuntap *obj = ObjectWrap::Unwrap<Tuntap>(args.This());
std::vector<tuntap_itf_opts_t::option_e> options;
Expand All @@ -260,20 +264,20 @@ void Tuntap::set(const FunctionCallbackInfo<Value>& args) {
return;
}

main_obj = args[0]->ToObject();
main_obj = args[0]->ToObject(context).ToLocalChecked();

if(main_obj->Has(String::NewFromUtf8(isolate, "type")) || main_obj->Has(String::NewFromUtf8(isolate, "name"))) {
if(main_obj->Has(context, String::NewFromUtf8(isolate, "type").ToLocalChecked()).ToChecked() || main_obj->Has(context, String::NewFromUtf8(isolate, "name").ToLocalChecked()).ToChecked()) {
TT_THROW_TYPE("Cannot set name and type from this function!");
return;
}

obj->objset(main_obj);

keys_arr = main_obj->GetPropertyNames();
keys_arr = main_obj->GetPropertyNames(context).ToLocalChecked();
for (unsigned int i = 0, limiti = keys_arr->Length(); i < limiti; i++) {
key = keys_arr->Get(i);
val = main_obj->Get(key);
String::Utf8Value key_str(key->ToString());
key = keys_arr->Get(context, i).ToLocalChecked();
val = main_obj->Get(context, key).ToLocalChecked();
String::Utf8Value key_str(isolate, key);

if(strcmp(*key_str, "addr") == 0) {
if(obj->fd)
Expand Down Expand Up @@ -304,7 +308,7 @@ void Tuntap::set(const FunctionCallbackInfo<Value>& args) {
options.push_back(tuntap_itf_opts_t::OPT_RUNNING);
}
else if(strcmp(*key_str, "ethtype_comp") == 0) {
String::Utf8Value val_str(val->ToString());
String::Utf8Value val_str(isolate, val);

if(strcmp(*val_str, "none") == 0)
obj->itf_opts.ethtype_comp = TUNTAP_ETCOMP_NONE;
Expand All @@ -325,6 +329,7 @@ void Tuntap::set(const FunctionCallbackInfo<Value>& args) {

void Tuntap::unset(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
HandleScope scope(isolate);
Tuntap *obj = ObjectWrap::Unwrap<Tuntap>(args.This());
std::vector<tuntap_itf_opts_t::option_e> options;
Expand All @@ -340,8 +345,8 @@ void Tuntap::unset(const FunctionCallbackInfo<Value>& args) {
keys_arr = args[0].As<Array>();

for (unsigned int i = 0, limiti = keys_arr->Length(); i < limiti; i++) {
val = keys_arr->Get(i);
String::Utf8Value val_str(val->ToString());
val = keys_arr->Get(context, i).ToLocalChecked();
String::Utf8Value val_str(isolate, val);

if(strcmp(*val_str, "addr") == 0) {
obj->itf_opts.addr = "";
Expand Down Expand Up @@ -401,17 +406,20 @@ void Tuntap::startRead(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(args.This());
}

void Tuntap::objset(Handle<Object> obj) {
void Tuntap::objset(Local<Object> obj) {
Isolate* isolate = obj->GetIsolate();
Local<Context> context = isolate->GetCurrentContext();

Local<Array> keys_arr;
Local<Value> key;
Local<Value> val;

keys_arr = obj->GetPropertyNames();
keys_arr = obj->GetPropertyNames(context).ToLocalChecked();
for (unsigned int i = 0, limiti = keys_arr->Length(); i < limiti; i++) {
key = keys_arr->Get(i);
val = obj->Get(key);
String::Utf8Value key_str(key->ToString());
String::Utf8Value val_str(val->ToString());
key = keys_arr->Get(context, i).ToLocalChecked();
val = obj->Get(context, key).ToLocalChecked();
String::Utf8Value key_str(isolate, key);
String::Utf8Value val_str(isolate, val);

if(strcmp(*key_str, "type") == 0) {
if(strcmp(*val_str, "tun") == 0) {
Expand All @@ -434,21 +442,21 @@ void Tuntap::objset(Handle<Object> obj) {
this->itf_opts.dest = *val_str;
}
else if(strcmp(*key_str, "mtu") == 0) {
this->itf_opts.mtu = val->ToInteger()->Value();
this->itf_opts.mtu = val->ToInteger(context).ToLocalChecked()->Value();
if(this->itf_opts.mtu <= 50)
this->itf_opts.mtu = 50;
}
else if(strcmp(*key_str, "persist") == 0) {
this->itf_opts.is_persistant = val->ToBoolean()->Value();
this->itf_opts.is_persistant = val->ToBoolean(isolate)->Value();
}
else if(strcmp(*key_str, "up") == 0) {
this->itf_opts.is_up = val->ToBoolean()->Value();
this->itf_opts.is_up = val->ToBoolean(isolate)->Value();
}
else if(strcmp(*key_str, "running") == 0) {
this->itf_opts.is_running = val->ToBoolean()->Value();
this->itf_opts.is_running = val->ToBoolean(isolate)->Value();
}
else if(strcmp(*key_str, "ethtype_comp") == 0) {
String::Utf8Value val_str(val->ToString());
String::Utf8Value val_str(isolate, val);

if(strcmp(*val_str, "none") == 0)
this->itf_opts.ethtype_comp = TUNTAP_ETCOMP_NONE;
Expand Down
6 changes: 3 additions & 3 deletions src/tuntap.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class Tuntap : public node::ObjectWrap {
public:
static void Init(v8::Handle<v8::Object> module);
static void Init(v8::Local<v8::Object> module);

private:
Tuntap();
Expand Down Expand Up @@ -62,8 +62,8 @@ class Tuntap : public node::ObjectWrap {
int size;
};

bool construct(v8::Handle<v8::Object> main_obj, std::string &error);
void objset(v8::Handle<v8::Object> obj);
bool construct(v8::Local<v8::Object> main_obj, std::string &error);
void objset(v8::Local<v8::Object> obj);
static void writeBuffer(const v8::FunctionCallbackInfo<v8::Value>& args);
static void open(const v8::FunctionCallbackInfo<v8::Value>& args);
static void close(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down