Füge Unterstützung für SSL/TLS in die Konfiguration und das Build-System ein

- Integriere die libwebsockets-Bibliothek für SSL/TLS WebSocket-Unterstützung in `CMakeLists.txt`.
- Aktualisiere `chatconfig.json`, um SSL-Optionen wie `ssl_enabled`, `ssl_cert_path` und `ssl_key_path` hinzuzufügen.
- Ergänze das `deploy.sh`-Skript um einen Schritt zur optionalen Einrichtung von SSL/TLS.
- Modifiziere `update_config.sh`, um die SSL-Konfiguration in die Servereinstellungen zu integrieren.
- Implementiere eine Überprüfung in `main.cpp`, um den SSL-Status zu prüfen und entsprechende Meldungen auszugeben.
This commit is contained in:
Torsten Schulz (local)
2025-09-04 12:05:22 +02:00
parent ec939bb506
commit d619d70a76
9 changed files with 929 additions and 2 deletions

100
src/core/ssl_server.h Normal file
View File

@@ -0,0 +1,100 @@
#ifndef YC_LIB_SSL_SERVER_H
#define YC_LIB_SSL_SERVER_H
#include <libwebsockets.h>
#include <memory>
#include <string>
#include <thread>
#include <atomic>
#include <mutex>
#include <shared_mutex>
#include <unordered_map>
#include <queue>
#include <condition_variable>
#include <vector>
namespace Yc {
namespace Lib {
class Config;
class Database;
class ChatRoom;
class ChatUser;
struct WebSocketUserData {
std::string token;
std::string userName;
std::string userColor;
std::string currentRoom;
bool authenticated = false;
std::string pendingMessage;
};
class SSLServer {
public:
SSLServer(std::shared_ptr<Config> config, std::shared_ptr<Database> database);
~SSLServer();
void run();
void stop();
void createRooms();
// Room management
std::vector<std::string> roomList();
bool roomAllowed(const std::string& roomName, const std::string& userName, const std::string& password);
bool changeRoom(std::shared_ptr<ChatUser> user, const std::string& newRoom, const std::string& password);
// User management
bool userExists(const std::string& userName);
void initUser(const std::string& token, const std::string& name, const std::string& room, const std::string& color, const std::string& password);
// Message handling
void sendMessage(int socket, const std::string& message);
void broadcastToRoom(const std::string& roomName, const std::string& message);
// WebSocket callbacks
static int wsCallback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len);
private:
void startServer();
void processMessageQueue();
void handleWebSocketMessage(struct lws *wsi, const std::string& message);
void addConnection(const std::string& token, struct lws *wsi);
void removeConnection(const std::string& token);
std::shared_ptr<ChatUser> getUserByToken(const std::string& token);
std::shared_ptr<Config> _config;
std::shared_ptr<Database> _database;
std::vector<std::shared_ptr<ChatRoom>> _rooms;
// SSL/TLS settings
bool _useSSL;
std::string _certPath;
std::string _keyPath;
int _port;
// Server state
std::atomic<bool> _running{false};
struct lws_context* _context = nullptr;
std::thread _serverThread;
std::thread _messageThread;
// Message queue
std::mutex _queueMutex;
std::condition_variable _queueCV;
std::queue<std::string> _messageQueue;
// Connections
std::shared_mutex _connectionsMutex;
std::unordered_map<std::string, struct lws*> _connections;
std::unordered_map<std::string, std::shared_ptr<ChatUser>> _users;
// Static instance for callbacks
static SSLServer* _instance;
static struct lws_protocols _protocols[];
};
} // namespace Lib
} // namespace Yc
#endif // YC_LIB_SSL_SERVER_H