// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // KOutNet - One peer's profile banner, exchanged over the pinned TLS channel #include "BannerExchange.h" #include "Protocol.h" #include #include #include #include #include #include #include namespace koutnet { namespace { constexpr int kMaxBannerBytes = 4 * 1024 * 1024; } BannerExchange::BannerExchange(ChannelWriter channelWriter, Seats seats, PeerInfoRecorder peerInfoRecorder, QObject *parent) : QObject(parent) , m_channelWriter(std::move(channelWriter)) , m_seats(std::move(seats)) , m_peerInfoRecorder(std::move(peerInfoRecorder)) { } void BannerExchange::setBanner(const QString &bannerPath) { if (m_bannerPath == bannerPath) return; m_bannerPath = bannerPath; m_bannerData.clear(); m_bannerSha.clear(); if (!bannerPath.isEmpty()) { QFile file(bannerPath); if (file.exists() && file.open(QIODevice::ReadOnly)) { m_bannerData = file.readAll(); if (m_bannerData.size() > kMaxBannerBytes) { m_bannerData.clear(); } else { m_bannerSha = QString::fromLatin1(QCryptographicHash::hash(m_bannerData, QCryptographicHash::Sha256).toHex()); } } } // A sha the seats have not seen is sent again; everything else is skipped by // the per-seat dedup below. broadcastBanner(); } void BannerExchange::onPeerAuthenticated(const QString &address) { // The peer just proved who it is, and every channel-up is a moment a push can be missed. if (m_lastBannerSent.value(address) != m_bannerSha) sendBannerToPeer(address); } void BannerExchange::sendBannerToPeer(const QString &targetAddress) { if (!m_channelWriter) return; QJsonObject payload; payload[QStringLiteral("type")] = protocol::kMsgBanner; payload[QStringLiteral("sha")] = m_bannerSha; payload[QStringLiteral("ext")] = QFileInfo(m_bannerPath).suffix().toLower(); payload[QStringLiteral("data")] = QString::fromLatin1(m_bannerData.toBase64()); if (!m_channelWriter(targetAddress, payload)) return; // no channel to that seat; do not record a push that never landed m_lastBannerSent[targetAddress] = m_bannerSha; } void BannerExchange::broadcastBanner() { if (!m_seats) return; const QSet seats = m_seats(); for (const QString &seat : seats) { if (m_lastBannerSent.value(seat) != m_bannerSha) sendBannerToPeer(seat); } } void BannerExchange::onFrame(const QString &host, const QJsonObject &msg) { const QString sha = msg.value(QStringLiteral("sha")).toString(); const QByteArray data = QByteArray::fromBase64(msg.value(QStringLiteral("data")).toString().toLatin1()); if (data.size() > kMaxBannerBytes) return; QString source; if (!data.isEmpty()) { if (sha.size() != 64) { return; // a sha is the integrity collar the file name is cut from } // A local file, not a URL: the banner is bytes on the wire, and the name // carries the sha so an unchanged banner resolves to the same entry. QString ext = msg.value(QStringLiteral("ext")).toString().toLower(); static const QStringList kAllowed = {QStringLiteral("png"), QStringLiteral("jpg"), QStringLiteral("jpeg"), QStringLiteral("webp"), QStringLiteral("bmp")}; if (!kAllowed.contains(ext)) ext = QStringLiteral("png"); const QString fileName = QStringLiteral("peer-banner-%1-%2.%3").arg(host, sha.left(8), ext); const QString dir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/peer-banners"); QDir().mkpath(dir); const QString path = dir + QLatin1Char('/') + fileName; QSaveFile out(path); if (out.open(QIODevice::WriteOnly)) { out.write(data); if (out.commit()) source = QUrl::fromLocalFile(path).toString(); } if (source.isEmpty()) return; // the writer has gone read-only: keep the old banner } if (m_peerInfoRecorder) m_peerInfoRecorder(host, source); Q_EMIT peerBanner(host, source); } } // namespace koutnet