diff --git a/build.c b/build.c index 4b508e2..eb78fc7 100644 --- a/build.c +++ b/build.c @@ -57,7 +57,7 @@ void prepare() { /* Build for Unix systems */ void build(char *compiler, char *cpp_compiler, char *cpp_linker, char *os, char *arch) { char *c_shared = "-DLIBUS_USE_LIBUV -DLIBUS_USE_OPENSSL -flto -O3 -c -fPIC -I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c uWebSockets/uSockets/src/crypto/*.c"; - char *cpp_shared = "-DLIBUS_USE_LIBUV -DLIBUS_USE_OPENSSL -flto -O3 -c -fPIC -std=c++17 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.cpp"; + char *cpp_shared = "-DUWS_WITH_PROXY -DLIBUS_USE_LIBUV -DLIBUS_USE_OPENSSL -flto -O3 -c -fPIC -std=c++17 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.cpp"; for (unsigned int i = 0; i < sizeof(versions) / sizeof(struct node_version); i++) { run("%s %s -I targets/node-%s/include/node", compiler, c_shared, versions[i].name); diff --git a/src/HttpResponseWrapper.h b/src/HttpResponseWrapper.h index 196ad4a..f1a75a3 100644 --- a/src/HttpResponseWrapper.h +++ b/src/HttpResponseWrapper.h @@ -94,6 +94,45 @@ struct HttpResponseWrapper { } } + /* Takes nothing, returns arraybuffer */ + template + static void res_getRemoteAddressAsText(const FunctionCallbackInfo &args) { + Isolate *isolate = args.GetIsolate(); + auto *res = getHttpResponse(args); + if (res) { + std::string_view ip = res->getRemoteAddressAsText(); + + /* Todo: we need to pass a copy here */ + args.GetReturnValue().Set(ArrayBuffer::New(isolate, (void *) ip.data(), ip.length()/*, ArrayBufferCreationMode::kInternalized*/)); + } + } + + /* Takes nothing, returns arraybuffer */ + template + static void res_getProxiedRemoteAddress(const FunctionCallbackInfo &args) { + Isolate *isolate = args.GetIsolate(); + auto *res = getHttpResponse(args); + if (res) { + std::string_view ip = res->getProxiedRemoteAddress(); + + /* Todo: we need to pass a copy here */ + args.GetReturnValue().Set(ArrayBuffer::New(isolate, (void *) ip.data(), ip.length()/*, ArrayBufferCreationMode::kInternalized*/)); + } + } + + /* Takes nothing, returns arraybuffer */ + template + static void res_getProxiedRemoteAddressAsText(const FunctionCallbackInfo &args) { + Isolate *isolate = args.GetIsolate(); + auto *res = getHttpResponse(args); + if (res) { + std::string_view ip = res->getProxiedRemoteAddressAsText(); + + /* Todo: we need to pass a copy here */ + args.GetReturnValue().Set(ArrayBuffer::New(isolate, (void *) ip.data(), ip.length()/*, ArrayBufferCreationMode::kInternalized*/)); + } + } + /* Returns the current write offset */ template static void res_getWriteOffset(const FunctionCallbackInfo &args) { @@ -316,6 +355,9 @@ struct HttpResponseWrapper { resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddress", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getRemoteAddress)); resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "cork", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_cork)); resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "upgrade", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_upgrade)); + resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddressAsText", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getRemoteAddressAsText)); + resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getProxiedRemoteAddress", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getProxiedRemoteAddress)); + resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getProxiedRemoteAddressAsText", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getProxiedRemoteAddressAsText)); /* Create our template */ Local resObjectLocal = resTemplateLocal->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); diff --git a/src/WebSocketWrapper.h b/src/WebSocketWrapper.h index d3e8b58..6e44eb2 100644 --- a/src/WebSocketWrapper.h +++ b/src/WebSocketWrapper.h @@ -110,6 +110,19 @@ struct WebSocketWrapper { } } + /* Takes nothing returns arraybuffer */ + template + static void uWS_WebSocket_getRemoteAddressAsText(const FunctionCallbackInfo &args) { + Isolate *isolate = args.GetIsolate(); + auto *ws = getWebSocket(args); + if (ws) { + std::string_view ip = ws->getRemoteAddressAsText(); + + /* Todo: we need to pass a copy here */ + args.GetReturnValue().Set(ArrayBuffer::New(isolate, (void *) ip.data(), ip.length()/*, ArrayBufferCreationMode::kInternalized*/)); + } + } + /* Takes nothing, returns integer */ template static void uWS_WebSocket_getBufferedAmount(const FunctionCallbackInfo &args) { @@ -205,10 +218,11 @@ struct WebSocketWrapper { wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "unsubscribeAll", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_unsubscribeAll)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "cork", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_cork)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ping", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_ping)); + wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddressAsText", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getRemoteAddressAsText)); /* Create the template */ Local wsObjectLocal = wsTemplateLocal->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); - + return wsObjectLocal; } }; diff --git a/uWebSockets b/uWebSockets index 9e3a75b..025415d 160000 --- a/uWebSockets +++ b/uWebSockets @@ -1 +1 @@ -Subproject commit 9e3a75b19ac560ea03e5064043f25a2857cc7105 +Subproject commit 025415d1a0174cf581b481fd2b3f155241a1265b