Browse Source

Merge pull request #537 from paullouisageneau/ws-server-cert-string

Add support for PEM certificate and key as string in WebSocketServer
Paul-Louis Ageneau 3 years ago
parent
commit
0b8b5bad02
3 changed files with 23 additions and 15 deletions
  1. 3 3
      DOC.md
  2. 7 4
      pages/content/pages/reference.md
  3. 13 8
      src/impl/websocketserver.cpp

+ 3 - 3
DOC.md

@@ -791,9 +791,9 @@ Arguments:
 - `config`: a structure with the following parameters:
   - `uint16_t port`: the port to listen on (if 0, automatically select an available port)
   - `bool enableTls`: if true, enable the TLS layer (WSS)
-  - `const char *certificatePemFile`: path of the file containing the TLS PEM certificate (`NULL` for an autogenerated certificate)
-  - `const char *keyPemFile`: path of the file containing the TLS PEM key (`NULL` for an autogenerated certificate)
-  - `const char *keyPemPass`: the TLS PEM key passphrase (NULL if no passphrase)
+  - `const char *certificatePemFile`: PEM certificate or path of the file containing the PEM certificate (`NULL` for an autogenerated certificate)
+  - `const char *keyPemFile`: PEM key or path of the file containing the PEM key (`NULL` for an autogenerated certificate)
+  - `const char *keyPemPass`: PEM key file passphrase (NULL if no passphrase)
 - `cb`: the callback for incoming client WebSocket connections (must not be `NULL`)
 
 `cb` must have the following signature: `void rtcWebSocketClientCallbackFunc(int wsserver, int ws, void *user_ptr)`

+ 7 - 4
pages/content/pages/reference.md

@@ -57,7 +57,6 @@ An optional call to `rtcCleanup` unloads the global resources used by the librar
 
 Warning: This function requires all Peer Connections, Data Channels, Tracks, and WebSockets to be destroyed before returning, meaning all callbacks must return before this function returns. Therefore, it must never be called from a callback.
 
-
 #### rtcSetUserPointer
 
 ```
@@ -460,6 +459,10 @@ int rtcGetBufferedAmount(int id)
 
 Retrieves the current buffered amount, i.e. the total size of currently buffered messages waiting to be actually sent in the channel. This does not account for the data buffered at the transport level.
 
+Arguments:
+
+- `id`: the channel identifier
+
 Return value: the buffered amount or a negative error code
 
 #### rtcSetBufferedAmountLowThreshold
@@ -791,9 +794,9 @@ Arguments:
 - `config`: a structure with the following parameters:
   - `uint16_t port`: the port to listen on (if 0, automatically select an available port)
   - `bool enableTls`: if true, enable the TLS layer (WSS)
-  - `const char *certificatePemFile`: path of the file containing the TLS PEM certificate (`NULL` for an autogenerated certificate)
-  - `const char *keyPemFile`: path of the file containing the TLS PEM key (`NULL` for an autogenerated certificate)
-  - `const char *keyPemPass`: the TLS PEM key passphrase (NULL if no passphrase)
+  - `const char *certificatePemFile`: PEM certificate or path of the file containing the PEM certificate (`NULL` for an autogenerated certificate)
+  - `const char *keyPemFile`: PEM key or path of the file containing the PEM key (`NULL` for an autogenerated certificate)
+  - `const char *keyPemPass`: PEM key file passphrase (NULL if no passphrase)
 - `cb`: the callback for incoming client WebSocket connections (must not be `NULL`)
 
 `cb` must have the following signature: `void rtcWebSocketClientCallbackFunc(int wsserver, int ws, void *user_ptr)`

+ 13 - 8
src/impl/websocketserver.cpp

@@ -27,6 +27,8 @@ namespace rtc::impl {
 
 using namespace std::placeholders;
 
+const string PemBeginCertificateTag = "-----BEGIN CERTIFICATE-----";
+
 WebSocketServer::WebSocketServer(Configuration config_)
     : config(std::move(config_)), tcpServer(std::make_unique<TcpServer>(config.port)),
       mStopped(false) {
@@ -34,16 +36,19 @@ WebSocketServer::WebSocketServer(Configuration config_)
 
 	if (config.enableTls) {
 		if (config.certificatePemFile && config.keyPemFile) {
-			mCertificate = std::make_shared<Certificate>(Certificate::FromFile(
-			    *config.certificatePemFile, *config.keyPemFile, config.keyPemPass.value_or("")));
-
-		} else if (!config.certificatePemFile && !config.keyPemFile) {
 			mCertificate = std::make_shared<Certificate>(
-			    Certificate::Generate(CertificateType::Default, "localhost"));
-		} else {
-			throw std::invalid_argument(
-			    "Either none or both certificate and key PEM files must be specified");
+			    config.certificatePemFile->find(PemBeginCertificateTag) != string::npos
+			        ? Certificate::FromString(*config.certificatePemFile, *config.keyPemFile)
+			        : Certificate::FromFile(*config.certificatePemFile, *config.keyPemFile,
+			                                config.keyPemPass.value_or("")));
 		}
+
+	} else if (!config.certificatePemFile && !config.keyPemFile) {
+		mCertificate = std::make_shared<Certificate>(
+		    Certificate::Generate(CertificateType::Default, "localhost"));
+	} else {
+		throw std::invalid_argument(
+		    "Either none or both certificate and key PEM files must be specified");
 	}
 
 	mThread = std::thread(&WebSocketServer::runLoop, this);