diff --git a/docs/index.d.ts b/docs/index.d.ts index 5199f608..4a9b72a4 100644 --- a/docs/index.d.ts +++ b/docs/index.d.ts @@ -104,6 +104,9 @@ export interface WebSocket { /** Returns the remote IP address as text. See RecognizedString. */ getRemoteAddressAsText() : ArrayBuffer; + /** Returns the remote port number. */ + getRemotePort() : number; + /** Returns the UserData object. */ getUserData() : UserData; } @@ -172,12 +175,18 @@ export interface HttpResponse { /** Returns the remote IP address as text. */ getRemoteAddressAsText() : ArrayBuffer; + /** Returns the remote port number. */ + getRemotePort() : number; + /** Returns the remote IP address in binary format (4 or 16 bytes), as reported by the PROXY Protocol v2 compatible proxy. */ getProxiedRemoteAddress() : ArrayBuffer; /** Returns the remote IP address as text, as reported by the PROXY Protocol v2 compatible proxy. */ getProxiedRemoteAddressAsText() : ArrayBuffer; + /** Returns the remote port number, as reported by the PROXY Protocol v2 compatible proxy. */ + getProxiedRemotePort() : number; + /** Corking a response is a performance improvement in both CPU and network, as you ready the IO system for writing multiple chunks at once. * By default, you're corked in the immediately executing top portion of the route handler. In all other cases, such as when returning from * await, or when being called back from an async database request or anything that isn't directly executing in the route handler, you'll want diff --git a/src/HttpResponseWrapper.h b/src/HttpResponseWrapper.h index 340f5be0..23ea4522 100644 --- a/src/HttpResponseWrapper.h +++ b/src/HttpResponseWrapper.h @@ -160,6 +160,18 @@ struct HttpResponseWrapper { } } + /* Takes nothing, returns integer */ + template + static void res_getRemotePort(const FunctionCallbackInfo &args) { + Isolate *isolate = args.GetIsolate(); + auto *res = getHttpResponse(args); + if (res) { + unsigned int port = res->getRemotePort(); + + args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, port)); + } + } + /* Takes nothing, returns arraybuffer */ template static void res_getProxiedRemoteAddress(const FunctionCallbackInfo &args) { @@ -184,6 +196,18 @@ struct HttpResponseWrapper { } } + /* Takes nothing, returns number */ + template + static void res_getProxiedRemotePort(const FunctionCallbackInfo &args) { + Isolate *isolate = args.GetIsolate(); + auto *res = getHttpResponse(args); + if (res) { + unsigned int port = res->getProxiedRemotePort(); + + args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, port)); + } + } + template static void res_getX509Certificate(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); @@ -472,8 +496,10 @@ struct HttpResponseWrapper { resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "collect", 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, "getRemotePort", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getRemotePort)); 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)); + resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getProxiedRemotePort", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getProxiedRemotePort)); resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "pause", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_pause)); resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "resume", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_resume)); } diff --git a/src/WebSocketWrapper.h b/src/WebSocketWrapper.h index f476bd7c..47d4c97f 100644 --- a/src/WebSocketWrapper.h +++ b/src/WebSocketWrapper.h @@ -159,6 +159,17 @@ struct WebSocketWrapper { } } + /* Takes nothing, returns integer */ + template + static void uWS_WebSocket_getRemotePort(const FunctionCallbackInfo &args) { + Isolate *isolate = args.GetIsolate(); + auto *ws = getWebSocket(args); + if (ws) { + unsigned int port = ws->getRemotePort(); + args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, port)); + } + } + /* Takes nothing, returns integer */ template static void uWS_WebSocket_getBufferedAmount(const FunctionCallbackInfo &args) { @@ -335,6 +346,7 @@ struct WebSocketWrapper { 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)); + wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemotePort", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getRemotePort)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "isSubscribed", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_isSubscribed)); /* This one does not exist in C++ */