Fixed a crash and using properly the Nan::HandlerScropes in async callbacks

This commit is contained in:
WolverinDEV
2019-07-24 01:03:01 +02:00
parent 8d3cbbdc3a
commit 2a03857955
16 changed files with 201 additions and 109 deletions
@@ -92,12 +92,22 @@ void ServerConnection::initialize() {
this->voice_connection->_ref = this->voice_connection;
this->voice_connection->initialize_js_object();
this->execute_pending_commands = Nan::async_callback([&]{ this->_execute_callback_commands(); });
this->execute_pending_voice = Nan::async_callback([&]{ this->_execute_callback_voice(); });
this->execute_callback_disconnect = Nan::async_callback([&](std::string reason){ this->_execute_callback_disconnect(reason); });
this->execute_pending_commands = Nan::async_callback([&]{
Nan::HandleScope scope;
this->_execute_callback_commands();
});
this->execute_pending_voice = Nan::async_callback([&]{
Nan::HandleScope scope;
this->_execute_callback_voice();
});
this->execute_callback_disconnect = Nan::async_callback([&](std::string reason){
Nan::HandleScope scope;
this->_execute_callback_disconnect(reason);
});
this->call_connect_result = Nan::async_callback([&](ErrorHandler::error_id error_id) {
Nan::HandleScope scope;
/* lets update the server type */
{
auto js_this = this->handle();
@@ -117,6 +127,7 @@ void ServerConnection::initialize() {
this->call_disconnect_result = Nan::async_callback([&](ErrorHandler::error_id error_id) {
Nan::HandleScope scope;
v8::Local<v8::Value> argv[1];
argv[0] = Nan::New<v8::Number>(error_id);
@@ -188,7 +199,7 @@ NAN_METHOD(ServerConnection::connect) {
return;
}
v8::Local arguments = info[0]->ToObject();
v8::Local arguments = info[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
if(!arguments->IsObject()) {
Nan::ThrowError(tr("Invalid argument"));
@@ -231,7 +242,7 @@ NAN_METHOD(ServerConnection::connect) {
this->protocol_handler->reset();
if(identity_key->IsString()) {
auto& identity = this->protocol_handler->get_identity_key();
auto key = base64::decode(*Nan::Utf8String(identity_key->ToString()));
auto key = base64::decode(*Nan::Utf8String(identity_key->ToString(Nan::GetCurrentContext()).ToLocalChecked()));
if(ecc_import((u_char*) key.data(), key.length(), &identity) != CRYPT_OK) {
Nan::ThrowError(tr("failed to import identity"));
return;
@@ -256,7 +267,7 @@ NAN_METHOD(ServerConnection::connect) {
hints.ai_family = AF_UNSPEC;
auto _remote_host = Nan::Utf8String(remote_host->ToString());
auto _remote_host = Nan::Utf8String(remote_host->ToString(Nan::GetCurrentContext()).ToLocalChecked());
if(getaddrinfo(*_remote_host, nullptr, &hints, &result) != 0 || !result) {
this->call_connect_result(this->errors.register_error(tr("failed to resolve hostname")));
return;
@@ -267,9 +278,9 @@ NAN_METHOD(ServerConnection::connect) {
}
switch(remote_address.ss_family) {
case AF_INET:
((sockaddr_in*) &remote_address)->sin_port = htons(remote_port->Int32Value());
((sockaddr_in*) &remote_address)->sin_port = htons(remote_port->Int32Value(Nan::GetCurrentContext()).FromMaybe(0));
case AF_INET6:
((sockaddr_in6*) &remote_address)->sin6_port = htons(remote_port->Int32Value());
((sockaddr_in6*) &remote_address)->sin6_port = htons(remote_port->Int32Value(Nan::GetCurrentContext()).FromMaybe(0));
default:break;
}
@@ -283,7 +294,7 @@ NAN_METHOD(ServerConnection::connect) {
this->socket->on_data = [&](const pipes::buffer_view& buffer) { this->protocol_handler->progress_packet(buffer); };
if(teamspeak->IsBoolean() && teamspeak->BooleanValue())
if(teamspeak->IsBoolean() && teamspeak->BooleanValue(info.GetIsolate()))
this->protocol_handler->server_type = server_type::TEAMSPEAK;
this->protocol_handler->connect();
}
@@ -333,7 +344,7 @@ NAN_METHOD(ServerConnection::error_message) {
return;
}
auto error = this->errors.get_message(info[0]->IntegerValue());
auto error = this->errors.get_message(info[0]->IntegerValue(Nan::GetCurrentContext()).FromMaybe(0));
info.GetReturnValue().Set(Nan::New<v8::String>(error).ToLocalChecked());
}
@@ -364,7 +375,7 @@ NAN_METHOD(ServerConnection::send_command) {
}
auto begin = chrono::system_clock::now();
auto command = info[0]->ToString();
auto command = info[0]->ToString(Nan::GetCurrentContext()).ToLocalChecked();
auto arguments = info[1].As<v8::Array>();
auto switches = info[2].As<v8::Array>();
@@ -376,20 +387,20 @@ NAN_METHOD(ServerConnection::send_command) {
return;
}
v8::Local<v8::Array> properties = object->ToObject()->GetOwnPropertyNames();
v8::Local<v8::Array> properties = object->ToObject(Nan::GetCurrentContext()).ToLocalChecked()->GetOwnPropertyNames(Nan::GetCurrentContext()).ToLocalChecked();
for(uint32_t i = 0; i < properties->Length(); i++) {
auto key = properties->Get(i)->ToString();
auto value = object->ToObject()->Get(key);
auto key = properties->Get(i)->ToString(Nan::GetCurrentContext()).ToLocalChecked();
auto value = object->ToObject(Nan::GetCurrentContext()).ToLocalChecked()->Get(Nan::GetCurrentContext(), key).ToLocalChecked();
string key_string = *Nan::Utf8String(key);
if(value->IsInt32())
cmd[index][key_string] = value->Int32Value();
cmd[index][key_string] = value->Int32Value(Nan::GetCurrentContext()).FromMaybe(0);
else if(value->IsNumber() || value->IsNumberObject())
cmd[index][key_string] = _to_string<double>(value->NumberValue()); /* requires our own conversation because node overrides stuff to 0,0000*/
cmd[index][key_string] = _to_string<double>(value->NumberValue(Nan::GetCurrentContext()).FromMaybe(0)); /* requires our own conversation because node overrides stuff to 0,0000*/
else if(value->IsString())
cmd[index][key_string] = *Nan::Utf8String(value->ToString());
cmd[index][key_string] = *Nan::Utf8String(value->ToString(Nan::GetCurrentContext()).ToLocalChecked());
else if(value->IsBoolean() || value->IsBooleanObject())
cmd[index][key_string] = value->BooleanValue();
cmd[index][key_string] = value->BooleanValue(info.GetIsolate());
else if(value->IsNullOrUndefined())
cmd[index][key_string] = "";
else {
@@ -442,7 +453,7 @@ NAN_METHOD(ServerConnection::send_voice_data) {
auto voice_data = info[0].As<v8::Uint8Array>()->Buffer();
this->send_voice_data(voice_data->GetContents().Data(), voice_data->GetContents().ByteLength(), (uint8_t) info[1]->Int32Value(), info[2]->BooleanValue());
this->send_voice_data(voice_data->GetContents().Data(), voice_data->GetContents().ByteLength(), (uint8_t) info[1]->Int32Value(Nan::GetCurrentContext()).FromMaybe(0), info[2]->BooleanValue(info.GetIsolate()));
}
@@ -463,9 +474,9 @@ NAN_METHOD(ServerConnection::send_voice_data_raw) {
return;
}
auto channels = info[1]->Int32Value();
auto sample_rate = info[2]->Int32Value();
auto flag_head = info[2]->BooleanValue();
auto channels = info[1]->Int32Value(Nan::GetCurrentContext()).FromMaybe(0);
auto sample_rate = info[2]->Int32Value(Nan::GetCurrentContext()).FromMaybe(0);
auto flag_head = info[2]->BooleanValue(info.GetIsolate());
auto voice_data = info[0].As<v8::Float32Array>()->Buffer();
auto vs = this->voice_connection->voice_sender();
@@ -488,6 +499,7 @@ void ServerConnection::send_voice_data(const void *buffer, size_t buffer_length,
if(head) /* head packet */
packet->enable_flag(ts::protocol::PacketFlag::Compressed);
#define FUZZ_VOICE
#ifdef FUZZ_VOICE
if((rand() % 10) < 2) {
log_info(category::connection, tr("Dropping voice packet"));
@@ -572,7 +584,7 @@ void ServerConnection::_execute_callback_commands() {
}
arguments[2] = switched;
callback->Call(Nan::Undefined(), 3, arguments);
callback->Call(Nan::GetCurrentContext(), Nan::Undefined(), 3, arguments);
}
}
@@ -614,7 +626,7 @@ void ServerConnection::_execute_callback_voice() {
arguments[2] = Nan::New<v8::Integer>(next_packet->codec_id);
arguments[3] = Nan::New<v8::Boolean>(next_packet->flag_head);
arguments[4] = Nan::New<v8::Integer>(next_packet->packet_id);
callback->Call(Nan::Undefined(), 5, arguments);
callback->Call(Nan::GetCurrentContext(), Nan::Undefined(), 5, arguments);
}
}
@@ -627,6 +639,6 @@ void ServerConnection::_execute_callback_disconnect(const std::string &reason) {
v8::Local<v8::Value> arguments[1];
arguments[0] = Nan::New<v8::String>(reason).ToLocalChecked();
callback->Call(Nan::Undefined(), 1, arguments);
callback->Call(Nan::GetCurrentContext(), Nan::Undefined(), 1, arguments);
}