From 292d8291133f0673bff73f0810f494b5c764c2f9 Mon Sep 17 00:00:00 2001 From: Jon Beniston Date: Thu, 15 Oct 2026 19:10:28 +0100 Subject: [PATCH] Qt5 CORS support. --- plugins/channelrx/demoddvb/dvbdemodgui.cpp | 2 +- plugins/channelrx/demoddvb/dvbdemodhbbtv.cpp | 59 ++++++- plugins/channelrx/demoddvb/dvbdemodhbbtv.h | 8 + plugins/channelrx/demoddvb/hbbtv/oipf.js | 50 +++++- sdrbase/dtv/dtvcarouselserver.cpp | 172 ++++++++++++++++++- sdrbase/dtv/dtvcarouselserver.h | 13 ++ 6 files changed, 291 insertions(+), 13 deletions(-) diff --git a/plugins/channelrx/demoddvb/dvbdemodgui.cpp b/plugins/channelrx/demoddvb/dvbdemodgui.cpp index edae39b22..3593ffe79 100644 --- a/plugins/channelrx/demoddvb/dvbdemodgui.cpp +++ b/plugins/channelrx/demoddvb/dvbdemodgui.cpp @@ -2158,7 +2158,7 @@ void DVBDemodGUI::tick() m_hbbtv->setSuppressed(m_guideOverlay->isShown()); // What the red button's waiting for, at the bottom right of the video, until there's nothing to wait for - const QString status = m_hbbtv->getStatus(); + const QString status = m_hbbtv->isStatusInPage() ? QString() : m_hbbtv->getStatus(); if (status != m_hbbtvStatus) { diff --git a/plugins/channelrx/demoddvb/dvbdemodhbbtv.cpp b/plugins/channelrx/demoddvb/dvbdemodhbbtv.cpp index 381253d0b..29c380b95 100644 --- a/plugins/channelrx/demoddvb/dvbdemodhbbtv.cpp +++ b/plugins/channelrx/demoddvb/dvbdemodhbbtv.cpp @@ -54,7 +54,7 @@ namespace { const QString m_messagePrefix = QStringLiteral("\u0001sdrangel:"); const QString m_scriptName = QStringLiteral("sdrangel-oipf"); -constexpr int m_checkMs = 500; +constexpr int m_checkMs = 250; constexpr int m_retryMs = 30000; //!< Before a failed AUTOSTART application's tried again constexpr int m_applicationTypeHbbTV = 0x10; constexpr int m_autostart = 1; @@ -174,7 +174,8 @@ DVBDemodHbbTV::DVBDemodHbbTV(DTVApps *apps, QObject *parent) : m_pendingRed(false), m_mediaId(0), m_appCarouselPid(-1), - m_loadsStarted(0) + m_loadsStarted(0), + m_keysetSinceLoad(false) { m_server->start(); connect(m_server, &DTVCarouselServer::served, this, [this](const QString& path, int status) { @@ -459,6 +460,7 @@ void DVBDemodHbbTV::setUserAgent(const QString& userAgent) { m_profile->setHttpUserAgent(userAgent); emit log(QString("User agent: %1").arg(userAgent)); + updateProxy(); return; } @@ -466,6 +468,7 @@ void DVBDemodHbbTV::setUserAgent(const QString& userAgent) QWebEngineProfile defaultProfile; m_profile->setHttpUserAgent(defaultProfile.httpUserAgent() + QString(" HbbTV/1.5.1 (; SDRangel; DVBDemod; %1; ; SDRangel; )").arg(QCoreApplication::applicationVersion())); + updateProxy(); } QString DVBDemodHbbTV::getUserAgent() const @@ -476,6 +479,16 @@ QString DVBDemodHbbTV::getUserAgent() const void DVBDemodHbbTV::setNetworkAllowed(bool allowed) { m_interceptor->m_networkAllowed = allowed; + updateProxy(); +} + +// Qt 5's WebEngine sends the page's origin whatever the interceptor says, which broadcasters' servers refuse: a +// carousel's application's requests to other sites go by the carousel server, which makes them with its TV origin +void DVBDemodHbbTV::updateProxy() +{ +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + m_server->setProxy(m_interceptor->m_networkAllowed, m_profile->httpUserAgent()); +#endif } bool DVBDemodHbbTV::keyCode(int qtKey, int& code, int& keysetBit) @@ -531,6 +544,11 @@ bool DVBDemodHbbTV::keyPress(QKeyEvent *event) } m_userRequest.start(); + + if (bit == m_keysetRed) { + m_redDelivered.start(); + } + m_page->runJavaScript(QString("window.__sdrangelHbbTV && window.__sdrangelHbbTV.key(%1, true)").arg(code)); return true; } @@ -554,6 +572,7 @@ bool DVBDemodHbbTV::pressRed() if (isRunning() && m_loaded && (m_keyset & m_keysetRed)) { m_page->runJavaScript("window.__sdrangelHbbTV && (window.__sdrangelHbbTV.key(403, true), window.__sdrangelHbbTV.key(403, false))"); + m_redDelivered.start(); return true; } @@ -593,7 +612,15 @@ QString DVBDemodHbbTV::getStatus() const return m_pendingRed && (m_userRequest.elapsed() < 30000) ? QString("Red button: starting...") : QString(); } - if (m_loaded) { + if (m_loaded) + { + // Loaded, but getting what it shows (as from the internet) until it's asked for keys; and just after red's + // given to it, before it starts what it's for + if ((!m_keysetSinceLoad && m_loadedAt.isValid() && (m_loadedAt.elapsed() < 8000) && m_visible) + || (m_redDelivered.isValid() && (m_redDelivered.elapsed() < 1500))) { + return "Red button: loading..."; + } + return QString(); } @@ -699,8 +726,23 @@ void DVBDemodHbbTV::stop() } // The service's AUTOSTART application started when there's none running; one the AIT kills, stopped +void DVBDemodHbbTV::showStatus() +{ + const QString status = isRunning() && m_loaded ? getStatus() : QString(); + + if (status != m_pageStatus) + { + m_pageStatus = status; + QString quoted = status; + quoted.replace('\\', "\\\\").replace('\'', "\\'"); + m_page->runJavaScript(QString("window.__sdrangelHbbTV && window.__sdrangelHbbTV.status('%1')").arg(quoted)); + } +} + void DVBDemodHbbTV::check() { + showStatus(); + if (m_channel.m_serviceId == 0) { return; } @@ -802,6 +844,8 @@ void DVBDemodHbbTV::navigate(const QString& url, const QString& name, uint32_t o m_appCarouselPid = -1; m_keyset = 0; m_loaded = false; + m_keysetSinceLoad = false; + m_pageStatus.clear(); if (m_mediaId != 0) { @@ -846,6 +890,7 @@ void DVBDemodHbbTV::loadFinished(bool ok) } m_loaded = true; + m_loadedAt.start(); makeTransparent(); showView(); } @@ -920,6 +965,11 @@ void DVBDemodHbbTV::message(const QJsonObject& message) if (type == "keyset") { m_keyset = message.value("v").toInt(); + + if (m_loaded) { + m_keysetSinceLoad = true; + } + emit log(QString("Keyset 0x%1").arg(m_keyset, 0, 16)); // Red pressed to start it: given now it's asked for, if that's soon after @@ -1174,6 +1224,9 @@ QJsonObject DVBDemodHbbTV::configuration() const config.insert("country", "GBR"); config.insert("version", QCoreApplication::applicationVersion()); config.insert("serverPort", m_server->getPort()); +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + config.insert("proxy", m_server->getProxyPath()); +#endif return config; } diff --git a/plugins/channelrx/demoddvb/dvbdemodhbbtv.h b/plugins/channelrx/demoddvb/dvbdemodhbbtv.h index b3b224d82..9cefb3a3f 100644 --- a/plugins/channelrx/demoddvb/dvbdemodhbbtv.h +++ b/plugins/channelrx/demoddvb/dvbdemodhbbtv.h @@ -112,6 +112,8 @@ public: //! What's being waited for, after the user's pressed red or a key the application has: the application starting, //! or loading (with how much of its carousel's been received). Empty when there's nothing to say QString getStatus() const; + //! The page's showing the status itself (over the application): it needn't be shown over the video + bool isStatusInPage() const { return !m_pageStatus.isEmpty(); } bool isVisible() const { return m_visible; } int getKeyset() const { return m_keyset; } QString getAppUrl() const { return m_appUrl; } @@ -163,6 +165,8 @@ private: void showView(); void loadFinished(bool ok); void updateScript(); + void showStatus(); //!< getStatus() in the page, over what the application shows + void updateProxy(); QJsonObject configuration() const; QJsonObject channelJson(const Channel& channel) const; QString appUrl(const DTVApps::App& app) const; @@ -202,6 +206,10 @@ private: QElapsedTimer m_failed; //!< Since an application last failed to load int m_appCarouselPid; //!< The carousel the running application's loaded from, -1 if none int m_loadsStarted; //!< Qt 5: page loads started, to tell a failed load from one replaced by another + QElapsedTimer m_loadedAt; //!< Since the running application's page loaded + bool m_keysetSinceLoad; //!< It's set its keyset since: it's ready + QString m_pageStatus; //!< Status last shown in the page + QElapsedTimer m_redDelivered; //!< Since red was last given to the application QElapsedTimer m_pendingRedTimer; }; diff --git a/plugins/channelrx/demoddvb/hbbtv/oipf.js b/plugins/channelrx/demoddvb/hbbtv/oipf.js index 34ee5ddc6..dfbf0bb5e 100644 --- a/plugins/channelrx/demoddvb/hbbtv/oipf.js +++ b/plugins/channelrx/demoddvb/hbbtv/oipf.js @@ -523,17 +523,39 @@ return new URL(path, location.href).href; } + // A carousel's page's request to another site, by SDRangel's proxy where it has one (with Qt 5), which makes it with + // the page's TV origin: of the page's own origin, so not cross-origin + function proxyUrl(url) { + if (!config.proxy || !/\.localhost$/.test(location.hostname)) { + return url; + } + + var target; + + try { + target = new URL(url, location.href); + } catch (e) { + return url; + } + + if (!/^https?:$/.test(target.protocol) || (target.origin === location.origin) || /\.localhost$/.test(target.hostname)) { + return url; + } + + return location.origin + config.proxy + '?url=' + encodeURIComponent(target.href); + } + var xhrOpen = XMLHttpRequest.prototype.open; XMLHttpRequest.prototype.open = function (method, url) { var args = Array.prototype.slice.call(arguments); - args[1] = mapUrl(String(url)); + args[1] = proxyUrl(mapUrl(String(url))); return xhrOpen.apply(this, args); }; if (window.fetch) { var fetchOriginal = window.fetch; window.fetch = function (input, init) { - return fetchOriginal.call(window, typeof input === 'string' ? mapUrl(input) : input, init); + return fetchOriginal.call(window, typeof input === 'string' ? proxyUrl(mapUrl(input)) : input, init); }; } @@ -847,7 +869,31 @@ window.confirm = function (text) { console.log('confirm: ' + text); return false; }; window.prompt = function (text) { console.log('prompt: ' + text); return null; }; + // SDRangel's status (as the red button loading), over everything the application shows + var statusElement = null; + + function showStatus(text) { + if (!text) { + if (statusElement && statusElement.parentNode) { + statusElement.parentNode.removeChild(statusElement); + } + statusElement = null; + return; + } + + if (!statusElement || !statusElement.isConnected) { + statusElement = document.createElement('div'); + statusElement.setAttribute('style', 'position: fixed; right: 64px; bottom: 48px; z-index: 2147483647; ' + + 'padding: 8px 16px; border-radius: 8px; background: rgba(16, 16, 16, 0.8); color: #f1efe8; ' + + 'font: 24px sans-serif; pointer-events: none'); + (document.body || document.documentElement).appendChild(statusElement); + } + + statusElement.textContent = text; + } + window.__sdrangelHbbTV = { + status: showStatus, // A key in the application's keyset, down or up key: function (code, down) { if (down) { diff --git a/sdrbase/dtv/dtvcarouselserver.cpp b/sdrbase/dtv/dtvcarouselserver.cpp index f3f80440d..8153e7d2b 100644 --- a/sdrbase/dtv/dtvcarouselserver.cpp +++ b/sdrbase/dtv/dtvcarouselserver.cpp @@ -19,6 +19,11 @@ #include #include +#include +#include +#include +#include +#include #include #include #include @@ -31,6 +36,8 @@ namespace { constexpr int m_maxHeader = 16384; +constexpr int m_maxBody = 1024 * 1024; //!< Of a request proxied +constexpr int m_proxyTimeoutMs = 30000; constexpr int m_retryMs = 250; const QString m_hostPrefix = "dvb-"; const QString m_hostSuffix = ".localhost"; @@ -40,8 +47,12 @@ const QString m_hostSuffix = ".localhost"; DTVCarouselServer::DTVCarouselServer(DTVApps *apps, QObject *parent) : QObject(parent), m_apps(apps), - m_server(new QTcpServer(this)) + m_server(new QTcpServer(this)), + m_network(new QNetworkAccessManager(this)), + m_proxyEnabled(false) { + m_proxyToken = QByteArray::number(QRandomGenerator::global()->generate64(), 16) + QByteArray::number(QRandomGenerator::global()->generate64(), 16); + connect(m_server, &QTcpServer::newConnection, this, &DTVCarouselServer::newConnection); } @@ -64,6 +75,17 @@ bool DTVCarouselServer::start() return true; } +void DTVCarouselServer::setProxy(bool enabled, const QString& userAgent) +{ + m_proxyEnabled = enabled; + m_userAgent = userAgent; +} + +QString DTVCarouselServer::getProxyPath() const +{ + return "/__sdrangel_proxy/" + QString::fromLatin1(m_proxyToken); +} + int DTVCarouselServer::getPort() const { return m_server->isListening() ? m_server->serverPort() : 0; @@ -217,27 +239,77 @@ void DTVCarouselServer::newConnection() void DTVCarouselServer::readRequest(QTcpSocket *socket) { QByteArray header = socket->property("header").toByteArray() + socket->readAll(); + const int end = header.indexOf("\r\n\r\n"); - if (header.size() > m_maxHeader) + if (((end < 0) && (header.size() > m_maxHeader)) || (end > m_maxHeader)) { reply(socket, 431, "Request Header Fields Too Large", "text/plain", QByteArray(), false); return; } - const int end = header.indexOf("\r\n\r\n"); - if (end < 0) { socket->setProperty("header", header); return; } - // One request a connection: it's closed after the reply - disconnect(socket, &QTcpSocket::readyRead, this, nullptr); const QList lines = header.left(end).split('\n'); const QList requestLine = lines[0].trimmed().split(' '); + int contentLength = 0; + + for (int i = 1; i < lines.size(); i++) + { + const QByteArray line = lines[i].trimmed(); + + if (line.toLower().startsWith("content-length:")) { + contentLength = line.mid(15).trimmed().toInt(); + } + } + + if ((contentLength < 0) || (contentLength > m_maxBody)) + { + reply(socket, 413, "Payload Too Large", "text/plain", QByteArray(), false); + return; + } + + // The body, all of it + QByteArray body = header.mid(end + 4); + + if (body.size() < contentLength) + { + socket->setProperty("header", header); + return; + } + + body.truncate(contentLength); + + // One request a connection: it's closed after the reply + disconnect(socket, &QTcpSocket::readyRead, this, nullptr); + + if (requestLine.size() < 2) + { + reply(socket, 400, "Bad Request", "text/plain", QByteArray(), false); + return; + } + + if (m_proxyEnabled && requestLine[1].startsWith(getProxyPath().toLatin1() + "?")) + { + QString proxyHost; + + for (int i = 1; i < lines.size(); i++) + { + const QByteArray line = lines[i].trimmed(); + + if (line.toLower().startsWith("host:")) { + proxyHost = QString::fromLatin1(line.mid(5).trimmed()); + } + } + + proxy(socket, requestLine[0], requestLine[1], lines, body, proxyHost); + return; + } - if ((requestLine.size() < 2) || ((requestLine[0] != "GET") && (requestLine[0] != "HEAD"))) + if ((requestLine[0] != "GET") && (requestLine[0] != "HEAD")) { reply(socket, 405, "Method Not Allowed", "text/plain", QByteArray(), false); return; @@ -325,6 +397,92 @@ void DTVCarouselServer::respond(Request *request) } } +// A request of a carousel's application to another site, made for it with its TV origin and the browser's user agent +void DTVCarouselServer::proxy(QTcpSocket *socket, const QByteArray& method, const QByteArray& target, const QList& lines, + const QByteArray& body, const QString& host) +{ + int serviceId, componentTag; + + if (!parseHost(host, serviceId, componentTag)) + { + reply(socket, 403, "Forbidden", "text/plain", QByteArray(), false); + return; + } + + const QUrlQuery query(QUrl("http://proxy" + QString::fromLatin1(target))); + const QUrl url(query.queryItemValue("url", QUrl::FullyDecoded)); + + if (!url.isValid() || ((url.scheme() != "http") && (url.scheme() != "https"))) + { + reply(socket, 400, "Bad Request", "text/plain", QByteArray(), false); + return; + } + + QNetworkRequest request(url); + request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy); +#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) + request.setTransferTimeout(m_proxyTimeoutMs); +#endif + + if (!m_userAgent.isEmpty()) { + request.setRawHeader("User-Agent", m_userAgent.toUtf8()); + } + + // dvb://onid.tsid.sid, from the carousel's host (dvb-onid-tsid-sid-ctag.localhost) + QString name = host.toLower(); + name = name.left(name.indexOf(':') >= 0 ? name.indexOf(':') : name.size()); + const QStringList parts = name.mid(4, name.size() - 4 - 10).split('-'); + + if ((parts.size() == 4) && (parts[0] != "x") && (parts[1] != "x")) { + request.setRawHeader("Origin", QString("dvb://%1.%2.%3").arg(parts[0]).arg(parts[1]).arg(parts[2]).toLatin1()); + } + + // What the request says of its content, and what it accepts + for (int i = 1; i < lines.size(); i++) + { + const QByteArray line = lines[i].trimmed(); + const int colon = line.indexOf(':'); + + if (colon <= 0) { + continue; + } + + const QByteArray name = line.left(colon).trimmed(); + const QByteArray lower = name.toLower(); + + if ((lower == "content-type") || (lower == "accept") || (lower == "accept-language") || (lower == "authorization")) { + request.setRawHeader(name, line.mid(colon + 1).trimmed()); + } + } + + QPointer client(socket); + QNetworkReply *networkReply = m_network->sendCustomRequest(request, method, body); + const bool head = method == "HEAD"; + + connect(networkReply, &QNetworkReply::finished, this, [this, networkReply, client, url, head]() { + networkReply->deleteLater(); + + if (!client) { + return; + } + + int status = networkReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + QByteArray reason = networkReply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray(); + + if (status == 0) + { + qDebug("DTVCarouselServer::proxy: %s: %s", qPrintable(url.toString()), qPrintable(networkReply->errorString())); + status = 502; + reason = "Bad Gateway"; + } + + const QByteArray type = networkReply->header(QNetworkRequest::ContentTypeHeader).toByteArray(); + reply(client, status, reason.isEmpty() ? QByteArray("OK") : reason, type.isEmpty() ? QByteArray("application/octet-stream") : type, + networkReply->readAll(), head); + emit served(QString("proxy %1").arg(url.toString()), status); + }); +} + void DTVCarouselServer::reply(QTcpSocket *socket, int status, const QByteArray& reason, const QByteArray& type, const QByteArray& body, bool head) { QByteArray response = "HTTP/1.1 " + QByteArray::number(status) + " " + reason + "\r\n"; diff --git a/sdrbase/dtv/dtvcarouselserver.h b/sdrbase/dtv/dtvcarouselserver.h index 7bca2b4a5..31a33fc13 100644 --- a/sdrbase/dtv/dtvcarouselserver.h +++ b/sdrbase/dtv/dtvcarouselserver.h @@ -28,6 +28,7 @@ #include "export.h" class DTVApps; +class QNetworkAccessManager; class QTcpServer; class QTcpSocket; @@ -53,6 +54,12 @@ public: //! Whether a URL's of the server bool isServerUrl(const QString& url) const; static QString contentType(const QString& path); + //! Requests a carousel's application makes to other sites, made for it with the origin a TV gives it, its + //! service's dvb: URL (dvb://onid.tsid.sid), which a browser can't always be made to (as Qt 5's WebEngine): to + //! getProxyPath()?url=..., of the carousel's own origin, so they aren't cross-origin. Off until enabled + void setProxy(bool enabled, const QString& userAgent); + //! As /__sdrangel_proxy/token: the token's random, so the proxy's only of pages given it + QString getProxyPath() const; static constexpr int m_waitMs = 20000; //!< For a file not received yet @@ -76,10 +83,16 @@ private: void readRequest(QTcpSocket *socket); void respond(Request *request); void reply(QTcpSocket *socket, int status, const QByteArray& reason, const QByteArray& type, const QByteArray& body, bool head); + void proxy(QTcpSocket *socket, const QByteArray& method, const QByteArray& target, const QList& lines, + const QByteArray& body, const QString& host); static bool parseHost(const QString& host, int& serviceId, int& componentTag); DTVApps *m_apps; QTcpServer *m_server; + QNetworkAccessManager *m_network; + QByteArray m_proxyToken; + bool m_proxyEnabled; + QString m_userAgent; }; #endif // INCLUDE_DTVCAROUSELSERVER_H