Füge SSLServer-Klasse hinzu und verbessere SSL/TLS-Unterstützung
- Integriere die `SSLServer`-Klasse zur Handhabung von SSL/TLS-Verbindungen. - Aktualisiere `main.cpp`, um die SSL-Server-Instanz zu erstellen und zu starten, wenn SSL aktiviert ist. - Modifiziere die `handleWebSocketMessage`-Methode in `ssl_server.cpp`, um die Benutzerverwaltung zu optimieren. - Ergänze die `generateToken`-Methode in `base.cpp`, um Token für Benutzer zu generieren. - Aktualisiere die Header-Dateien, um neue Methoden und Klassen zu berücksichtigen.
This commit is contained in:
@@ -15,6 +15,7 @@ message(STATUS "YC_DEBUG option: ${YC_DEBUG}")
|
|||||||
src/lib/base.cpp
|
src/lib/base.cpp
|
||||||
src/core/config.cpp
|
src/core/config.cpp
|
||||||
src/core/server.cpp
|
src/core/server.cpp
|
||||||
|
src/core/ssl_server.cpp
|
||||||
src/core/chat_room.cpp
|
src/core/chat_room.cpp
|
||||||
src/lib/tools.cpp
|
src/lib/tools.cpp
|
||||||
src/core/chat_user.cpp
|
src/core/chat_user.cpp
|
||||||
|
|||||||
@@ -212,10 +212,13 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa
|
|||||||
bool added = false;
|
bool added = false;
|
||||||
for (auto &roomObj: _rooms) {
|
for (auto &roomObj: _rooms) {
|
||||||
if (roomObj->name() == room) {
|
if (roomObj->name() == room) {
|
||||||
// Create ChatUser and add to room
|
// Add user to room (ChatUser will be created by addUser)
|
||||||
auto chatUser = std::make_shared<ChatUser>(name, color, token, lws_get_socket_fd(wsi));
|
|
||||||
if (roomObj->addUser(name, color, password, lws_get_socket_fd(wsi))) {
|
if (roomObj->addUser(name, color, password, lws_get_socket_fd(wsi))) {
|
||||||
|
// Find the created ChatUser
|
||||||
|
auto chatUser = roomObj->findUserByName(name);
|
||||||
|
if (chatUser) {
|
||||||
_users[token] = chatUser;
|
_users[token] = chatUser;
|
||||||
|
}
|
||||||
added = true;
|
added = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -244,7 +247,7 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa
|
|||||||
// Process message through room
|
// Process message through room
|
||||||
for (auto &room: _rooms) {
|
for (auto &room: _rooms) {
|
||||||
if (room->userIsInRoom(user->name())) {
|
if (room->userIsInRoom(user->name())) {
|
||||||
room->addMessage(user, msg);
|
room->addMessage(ChatUser::MsgType::message, msg, user->name(), user->color());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -310,13 +313,13 @@ void SSLServer::broadcastToRoom(const std::string& roomName, const std::string&
|
|||||||
|
|
||||||
void SSLServer::createRooms() {
|
void SSLServer::createRooms() {
|
||||||
// Load rooms from database or config
|
// Load rooms from database or config
|
||||||
// This is a simplified version - would need to be expanded
|
|
||||||
Json::Value roomList = _config->group("rooms");
|
Json::Value roomList = _config->group("rooms");
|
||||||
|
|
||||||
if (roomList.isArray()) {
|
if (roomList.isArray()) {
|
||||||
for (const auto& room : roomList) {
|
for (const auto& room : roomList) {
|
||||||
// Create room objects
|
// Create room objects using the same logic as the main server
|
||||||
// This would need the actual ChatRoom constructor
|
auto newRoom = std::make_shared<ChatRoom>(nullptr, room); // parent will be set later
|
||||||
|
_rooms.push_back(newRoom);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ public:
|
|||||||
|
|
||||||
// Message handling
|
// Message handling
|
||||||
void sendMessage(int socket, const std::string& message);
|
void sendMessage(int socket, const std::string& message);
|
||||||
|
void sendMessage(int socket, const Json::Value& message);
|
||||||
void broadcastToRoom(const std::string& roomName, const std::string& message);
|
void broadcastToRoom(const std::string& roomName, const std::string& message);
|
||||||
|
|
||||||
// WebSocket callbacks
|
// WebSocket callbacks
|
||||||
|
|||||||
@@ -242,5 +242,20 @@ namespace Yc {
|
|||||||
return inputTree;
|
return inputTree;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Base::generateToken() {
|
||||||
|
static const char charset[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||||
|
static const size_t charset_size = sizeof(charset) - 1;
|
||||||
|
|
||||||
|
std::string token;
|
||||||
|
token.reserve(32);
|
||||||
|
|
||||||
|
// Einfacher Zufallsgenerator für Token
|
||||||
|
for (int i = 0; i < 32; ++i) {
|
||||||
|
token += charset[rand() % charset_size];
|
||||||
|
}
|
||||||
|
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Lib
|
} // namespace Lib
|
||||||
} // namespace Yc
|
} // namespace Yc
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ protected:
|
|||||||
static void unmarkWebSocket(int socket);
|
static void unmarkWebSocket(int socket);
|
||||||
static bool isWebSocket(int socket);
|
static bool isWebSocket(int socket);
|
||||||
static std::string webSocketAcceptKey(const std::string& clientKey);
|
static std::string webSocketAcceptKey(const std::string& clientKey);
|
||||||
|
static std::string generateToken();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Lib
|
} // namespace Lib
|
||||||
|
|||||||
12
src/main.cpp
12
src/main.cpp
@@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
#include "core/config.h"
|
#include "core/config.h"
|
||||||
#include "core/server.h"
|
#include "core/server.h"
|
||||||
|
#include "core/ssl_server.h"
|
||||||
#include "lib/database.h"
|
#include "lib/database.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
@@ -9,19 +10,20 @@ int main(int, char **) {
|
|||||||
auto config = std::make_shared<Yc::Lib::Config>();
|
auto config = std::make_shared<Yc::Lib::Config>();
|
||||||
auto database = std::make_shared<Yc::Lib::Database>(config);
|
auto database = std::make_shared<Yc::Lib::Database>(config);
|
||||||
|
|
||||||
// Check if SSL is enabled (for future implementation)
|
// Check if SSL is enabled
|
||||||
bool sslEnabled = config->value("server", "ssl_enabled").asBool();
|
bool sslEnabled = config->value("server", "ssl_enabled").asBool();
|
||||||
|
|
||||||
if (sslEnabled) {
|
if (sslEnabled) {
|
||||||
std::cout << "[YourChat] SSL/TLS support is configured but not yet implemented" << std::endl;
|
std::cout << "[YourChat] Starting with SSL/TLS support" << std::endl;
|
||||||
std::cout << "[YourChat] Starting without SSL/TLS support" << std::endl;
|
auto sslServer = std::make_shared<Yc::Lib::SSLServer>(config, database);
|
||||||
|
sslServer->createRooms();
|
||||||
|
sslServer->run();
|
||||||
} else {
|
} else {
|
||||||
std::cout << "[YourChat] Starting without SSL/TLS support" << std::endl;
|
std::cout << "[YourChat] Starting without SSL/TLS support" << std::endl;
|
||||||
}
|
|
||||||
|
|
||||||
auto server = std::make_shared<Yc::Lib::Server>(config, database);
|
auto server = std::make_shared<Yc::Lib::Server>(config, database);
|
||||||
server->createRooms(config->group("rooms"));
|
server->createRooms(config->group("rooms"));
|
||||||
server->run();
|
server->run();
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user