#include "mainwindow.h" #include "channellist.h" #include "messagelist.h" #include "accountlist.h" #include "invitedialog.h" #include "joindialog.h" #include "settingsdialog.h" #include "creategroupdialog.h" #include "invite_link.h" #include "../db/db_manager.h" #include "../transport/utun_node.h" #include "../transport/gui_bridge.h" #include #include #include #include #include #include extern "C" { #include "../transport/chat_core.h" #include "../../lib/mem.h" #include "../../lib/debug_config.h" #include #include } static MainWindow* s_mainWindow = nullptr; static void onMsgReceivedCallback(const char* ch_id, int ch_id_len) { Q_UNUSED(ch_id_len); if (s_mainWindow) s_mainWindow->onMessageReceived(ch_id); } static void onChannelUpdatedCallback(const char* ch_id, int ch_id_len) { Q_UNUSED(ch_id_len); Q_UNUSED(ch_id); if (s_mainWindow) s_mainWindow->reloadChannels(); } MainWindow::MainWindow(QWidget *parent, DbManager* db, const QString& cfgPath, const QString& dbPath, const QString& debugFile, const QString& debugLevel, const QString& debugCategories) : QMainWindow(parent) , m_channelList(nullptr) , m_messageList(nullptr) , m_accountList(nullptr) , m_trayIcon(nullptr) , m_trayMenu(nullptr) , m_db(db) , m_cfgPath(cfgPath) , m_cfgDbPath(dbPath) , m_cfgDebugFile(debugFile) , m_cfgDebugLevel(debugLevel) , m_cfgDebugCategories(debugCategories) { setWindowTitle("Chat"); resize(900, 600); setupUi(); setupTray(); if (m_db && m_db->isOpen()) { s_mainWindow = this; gui_bridge_set_msg_received_cb(onMsgReceivedCallback); gui_bridge_set_channel_updated_cb(onChannelUpdatedCallback); setupNode(); setupMessaging(); connect(m_channelList, &ChannelList::channelSelected, this, [this](const QString& channelId) { m_db->setUiState("last_channel_id", channelId); m_messageList->loadChannel(channelId); }); connect(m_channelList, &ChannelList::inviteRequested, this, &MainWindow::showInviteDialog); connect(m_channelList, &ChannelList::joinRequested, this, &MainWindow::showJoinDialog); connect(m_channelList, &ChannelList::createGroupRequested, this, &MainWindow::onCreateGroupRequested); connect(m_channelList, &ChannelList::settingsRequested, this, &MainWindow::showSettings); m_channelList->loadChannels(); m_accountList->loadAccounts(); QString lastCid = m_db->getUiState("last_channel_id"); if (!lastCid.isEmpty()) m_channelList->selectChannel(lastCid); } } MainWindow::~MainWindow() { if (m_node) { m_node->stop(); delete m_node; m_node = nullptr; } } void MainWindow::setupUi() { auto *splitter = new QSplitter(Qt::Horizontal, this); m_channelList = new ChannelList(m_db, splitter); m_messageList = new MessageList(m_db, splitter); m_accountList = new AccountList(m_db, splitter); splitter->addWidget(m_channelList); splitter->addWidget(m_messageList); splitter->addWidget(m_accountList); splitter->setStretchFactor(0, 2); splitter->setStretchFactor(1, 5); splitter->setStretchFactor(2, 1); splitter->setSizes({220, 480, 180}); setCentralWidget(splitter); } void MainWindow::setupNode() { m_node = new UtunNode(this); connect(m_node, &UtunNode::error, this, [](const QString& msg) { qWarning("UtunNode error: %s", qPrintable(msg)); }); connect(m_node, &UtunNode::started, this, []() { qDebug("UtunNode started"); }); connect(m_node, &UtunNode::stopped, this, []() { qDebug("UtunNode stopped"); }); if (!m_cfgDebugFile.isEmpty()) m_node->setDebugFile(m_cfgDebugFile); if (!m_cfgDebugLevel.isEmpty()) m_node->setDebugLevel(m_cfgDebugLevel); if (!m_cfgDebugCategories.isEmpty()) m_node->setDebugCategories(m_cfgDebugCategories); if (!m_cfgDbPath.isEmpty()) m_node->setDbPath(m_cfgDbPath); if (!m_node->start(m_cfgPath)) qWarning("Failed to start UtunNode"); } void MainWindow::setupMessaging() { /* Initialize gui_bridge (GUI side) */ gui_bridge_init(); } void MainWindow::setupTray() { m_trayIcon = new QSystemTrayIcon(this); m_trayIcon->setIcon(style()->standardIcon(QStyle::SP_ComputerIcon)); m_trayIcon->setToolTip("Chat"); m_trayMenu = new QMenu(this); QAction *settingsAction = m_trayMenu->addAction("Settings"); connect(settingsAction, &QAction::triggered, this, &MainWindow::showSettings); m_trayMenu->addSeparator(); QAction *showAction = m_trayMenu->addAction("Show"); connect(showAction, &QAction::triggered, this, [this]() { show(); raise(); activateWindow(); }); QAction *quitAction = m_trayMenu->addAction("Quit"); connect(quitAction, &QAction::triggered, qApp, &QApplication::quit); m_trayIcon->setContextMenu(m_trayMenu); connect(m_trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::onTrayActivated); m_trayIcon->show(); } void MainWindow::closeEvent(QCloseEvent *event) { hide(); m_trayIcon->showMessage("Chat", "Application minimized to tray.", QSystemTrayIcon::Information, 2000); event->ignore(); } void MainWindow::onTrayActivated(QSystemTrayIcon::ActivationReason reason) { if (reason == QSystemTrayIcon::Trigger || reason == QSystemTrayIcon::DoubleClick) { show(); raise(); activateWindow(); } } void MainWindow::showInviteDialog(const QString& channelId) { if (!m_db || !m_db->isOpen()) return; QByteArray pubkey; if (m_node && m_node->isRunning()) pubkey = QByteArray::fromHex(m_node->pubKeyHex().toLatin1()); if (pubkey.size() != 32) { QMessageBox::warning(this, "Error", "Failed to get node public key."); return; } QList addrs = m_node->getInviteAddresses(m_db); QString link = encodeInviteLink(channelId.toULongLong(), pubkey, addrs); if (link.isEmpty()) { QMessageBox::warning(this, "Error", "Failed to create invite link."); return; } InviteDialog dlg(link, this); dlg.exec(); } void MainWindow::showJoinDialog() { if (!m_node || !m_node->isRunning()) { QMessageBox::warning(this, "Error", "Node is not running."); return; } JoinDialog dlg(m_node, m_db, this); if (dlg.exec() == QDialog::Accepted) { m_channelList->loadChannels(); } } void MainWindow::showSettings() { SettingsDialog dlg(m_cfgPath, this); dlg.exec(); } void MainWindow::onMessageReceived(const char* ch_id) { DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "MainWindow: msg received ch=%s", ch_id); if (m_messageList) m_messageList->refresh(); } void MainWindow::reloadChannels() { if (m_channelList) m_channelList->loadChannels(); } void MainWindow::onCreateGroupRequested() { if (!m_node || !m_node->isRunning()) { QMessageBox::warning(this, "Error", "Node is not running."); return; } if (!gui_bridge_is_uasync_ready()) { QMessageBox::warning(this, "Error", "uTun thread is not ready yet. Please wait and try again."); return; } CreateGroupDialog dlg(this); if (dlg.exec() != QDialog::Accepted) return; QString name = dlg.groupName(); if (name.isEmpty()) return; /* генерация X25519 ключей */ uint8_t x25519_pub[32] = {0}, x25519_priv[32] = {0}; { EVP_PKEY* pkey = nullptr; EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_X25519, nullptr); if (ctx) { EVP_PKEY_keygen_init(ctx); EVP_PKEY_generate(ctx, &pkey); EVP_PKEY_CTX_free(ctx); } if (pkey) { size_t pkLen = 32, skLen = 32; EVP_PKEY_get_raw_public_key(pkey, x25519_pub, &pkLen); EVP_PKEY_get_raw_private_key(pkey, x25519_priv, &skLen); EVP_PKEY_free(pkey); } } /* генерация Ed25519 ключей */ uint8_t ed25519_pub[32] = {0}, ed25519_priv[32] = {0}; EVP_PKEY* ed_pkey = nullptr; { EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_ED25519, nullptr); if (ctx) { EVP_PKEY_keygen_init(ctx); EVP_PKEY_generate(ctx, &ed_pkey); EVP_PKEY_CTX_free(ctx); } if (ed_pkey) { size_t pkLen = 32, skLen = 32; EVP_PKEY_get_raw_public_key(ed_pkey, ed25519_pub, &pkLen); EVP_PKEY_get_raw_private_key(ed_pkey, ed25519_priv, &skLen); } } /* случайный channel_id (uint64 → decimal string) */ uint64_t chId = 0; RAND_bytes((uint8_t*)&chId, sizeof(chId)); QString channelId = QString::number(chId); /* owner_node_id из instance */ uint64_t ownerNodeId = m_node->nodeIdHex().toULongLong(nullptr, 16); /* Ed25519 подпись: channel_id || name || owner_node_id(8 LE) || x25519_pub(32) || ed25519_pub(32) */ uint8_t sig[64] = {0}; if (ed_pkey) { QByteArray nameUtf8 = name.toUtf8(); QByteArray chIdUtf8 = channelId.toUtf8(); size_t msgLen = chIdUtf8.size() + 1 + nameUtf8.size() + 1 + 8 + 32 + 32; uint8_t* msg = (uint8_t*)malloc(msgLen); if (msg) { uint8_t* p = msg; memcpy(p, chIdUtf8.constData(), chIdUtf8.size()); p += chIdUtf8.size(); *p++ = '\0'; memcpy(p, nameUtf8.constData(), nameUtf8.size()); p += nameUtf8.size(); *p++ = '\0'; memcpy(p, &ownerNodeId, 8); p += 8; memcpy(p, x25519_pub, 32); p += 32; memcpy(p, ed25519_pub, 32); p += 32; EVP_MD_CTX* mdctx = EVP_MD_CTX_new(); if (mdctx) { EVP_DigestSignInit(mdctx, nullptr, nullptr, nullptr, ed_pkey); size_t siglen = 64; EVP_DigestSign(mdctx, sig, &siglen, msg, msgLen); EVP_MD_CTX_free(mdctx); } free(msg); } EVP_PKEY_free(ed_pkey); } /* отправляем в uasync-поток */ struct chat_channel_create* req = (struct chat_channel_create*)u_malloc(sizeof(*req)); memset(req, 0, sizeof(*req)); snprintf(req->channel_id, sizeof(req->channel_id), "%s", qPrintable(channelId)); snprintf(req->name, sizeof(req->name), "%s", qPrintable(name)); req->owner_node_id = ownerNodeId; req->is_dm = 0; memcpy(req->x25519_pubkey, x25519_pub, 32); memcpy(req->x25519_privkey, x25519_priv, 32); memcpy(req->ed25519_pubkey, ed25519_pub, 32); memcpy(req->ed25519_privkey, ed25519_priv, 32); memcpy(req->signature, sig, 64); gui_bridge_post_uasync_fn(chat_core_create_channel_trampoline, req); DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "MainWindow: create group request posted ch=%s name=%s", qPrintable(channelId), qPrintable(name)); }