diff --git a/docs/index.d.ts b/docs/index.d.ts index 5199f608..791d1558 100644 --- a/docs/index.d.ts +++ b/docs/index.d.ts @@ -178,6 +178,9 @@ export interface HttpResponse { /** Returns the remote IP address as text, as reported by the PROXY Protocol v2 compatible proxy. */ getProxiedRemoteAddressAsText() : ArrayBuffer; + /** Returns the SSL cipher used for connection */ + getSSLCipher(): string + /** 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 ec7b848c..ef78379c 100644 --- a/src/HttpResponseWrapper.h +++ b/src/HttpResponseWrapper.h @@ -196,6 +196,18 @@ struct HttpResponseWrapper { } } + template + static void res_getSSLCipher(const FunctionCallbackInfo &args) { + Isolate *isolate = args.GetIsolate(); + auto *res = getHttpResponse(args); + if (res) { + void* sslHandle = res->getNativeHandle(); + SSL* ssl = static_cast(sslHandle); + std::string ciphers = getSSLCipher(ssl); + args.GetReturnValue().Set(String::NewFromUtf8(isolate, ciphers.c_str(), NewStringType::kNormal).ToLocalChecked()); + } + } + /* Returns the current write offset */ template static void res_getWriteOffset(const FunctionCallbackInfo &args) { @@ -481,6 +493,7 @@ struct HttpResponseWrapper { if constexpr (SSL == 1) { resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getX509Certificate", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getX509Certificate)); + resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getSSLCipher", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getSSLCipher)); } /* Create our template */ diff --git a/src/Utilities.h b/src/Utilities.h index 3713f602..ccbbee58 100644 --- a/src/Utilities.h +++ b/src/Utilities.h @@ -202,4 +202,19 @@ std::string extractX509PemCertificate(SSL* ssl) { return pemCertificate; } +std::string getSSLCipher(SSL* ssl) { + std::string cipher; + if (!ssl) { + return cipher; + } + const SSL_CIPHER *peerCipher = SSL_get_current_cipher(ssl); + if (!peerCipher) { + return cipher; + } + const char *cipher_name = SSL_CIPHER_get_name(peerCipher); + cipher = std::string(cipher_name); + return cipher; +} + + #endif