Füge Unterstützung für SSL/TLS in den WebSocket-Server hinzu. Aktualisiere die Konfigurationsdatei, um SSL-Optionen zu ermöglichen, und passe die WebSocketServer-Klasse an, um Zertifikat- und Schlüsselpfade zu akzeptieren. Verbessere die Serverstartlogik, um SSL korrekt zu initialisieren und entsprechende Meldungen auszugeben.

This commit is contained in:
Torsten Schulz (local)
2025-09-03 14:50:07 +02:00
committed by Torsten (PC)
parent 92e17a9f43
commit 8212e906a3
5 changed files with 143 additions and 6 deletions

View File

@@ -46,8 +46,12 @@ int main() {
10
);
int websocketPort = std::stoi(config.get("WEBSOCKET_PORT"));
bool sslEnabled = config.get("WEBSOCKET_SSL_ENABLED") == "true";
std::string certPath = sslEnabled ? config.get("WEBSOCKET_SSL_CERT_PATH") : "";
std::string keyPath = sslEnabled ? config.get("WEBSOCKET_SSL_KEY_PATH") : "";
MessageBroker broker;
WebSocketServer websocketServer(websocketPort, pool, broker);
WebSocketServer websocketServer(websocketPort, pool, broker, sslEnabled, certPath, keyPath);
// Use C++23 features for better performance
std::vector<std::unique_ptr<Worker>> workers;
workers.reserve(9); // Pre-allocate for better performance

View File

@@ -18,8 +18,9 @@ struct lws_protocols WebSocketServer::protocols[] = {
{ nullptr, nullptr, 0, 0 }
};
WebSocketServer::WebSocketServer(int port, ConnectionPool &pool, MessageBroker &broker)
: port(port), pool(pool), broker(broker) {}
WebSocketServer::WebSocketServer(int port, ConnectionPool &pool, MessageBroker &broker,
bool useSSL, const std::string& certPath, const std::string& keyPath)
: port(port), pool(pool), broker(broker), useSSL(useSSL), certPath(certPath), keyPath(keyPath) {}
WebSocketServer::~WebSocketServer() {
stop();
@@ -57,8 +58,21 @@ void WebSocketServer::startServer() {
info.port = port;
info.protocols = protocols;
// SSL/TLS Konfiguration
if (useSSL) {
if (certPath.empty() || keyPath.empty()) {
throw std::runtime_error("SSL enabled but certificate or key path not provided");
}
info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
info.ssl_cert_filepath = certPath.c_str();
info.ssl_private_key_filepath = keyPath.c_str();
std::cout << "WebSocket SSL Server starting on port " << port << " with certificates: "
<< certPath << " / " << keyPath << std::endl;
} else {
std::cout << "WebSocket Server starting on port " << port << " (no SSL)" << std::endl;
}
// Reduziere Log-Level um weniger Debug-Ausgaben zu haben
// Setze Umgebungsvariable für Log-Level
setenv("LWS_LOG_LEVEL", "0", 1); // 0 = nur Fehler
context = lws_create_context(&info);

View File

@@ -25,7 +25,8 @@ class Worker; // forward
class WebSocketServer {
public:
WebSocketServer(int port, ConnectionPool &pool, MessageBroker &broker);
WebSocketServer(int port, ConnectionPool &pool, MessageBroker &broker,
bool useSSL = false, const std::string& certPath = "", const std::string& keyPath = "");
~WebSocketServer();
void run();
@@ -46,6 +47,9 @@ private:
int port;
ConnectionPool &pool;
MessageBroker &broker;
bool useSSL;
std::string certPath;
std::string keyPath;
std::atomic<bool> running{false};
struct lws_context *context = nullptr;