Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions viewer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import logging
import sys
from builtins import range
from os import getenv, path
from os import getenv, path, environ
import socket
from signal import SIGALRM, SIGUSR1, signal
from time import sleep

Expand Down Expand Up @@ -46,10 +47,12 @@
connect_to_redis,
get_balena_device_info,
get_node_ip,
get_node_mac_address,
is_balena_app,
string_to_bool,
url_fails,
)
from lib import diagnostics
from viewer.scheduling import Scheduler
from viewer.zmq import ZMQ_HOST_PUB_URL, ZmqSubscriber
except Exception:
Expand Down Expand Up @@ -154,7 +157,36 @@ def load_browser():
global browser
logging.info('Loading browser...')

browser = sh.Command('ScreenlyWebview')(_bg=True, _err_to_out=True)
# Prepare headers data for WebView via environment variables
try:
anthias_hostname = socket.gethostname()
except Exception:
anthias_hostname = ''

try:
git_branch = diagnostics.get_git_branch()
git_short_hash = diagnostics.get_git_short_hash()
anthias_version = (
f"{git_branch}@{git_short_hash}"
if git_branch and git_short_hash else ''
)
except Exception:
anthias_version = ''

try:
anthias_mac = get_node_mac_address()
except Exception:
anthias_mac = ''

env = dict(environ)
if anthias_hostname:
env['ANTHIAS_HOSTNAME'] = anthias_hostname
if anthias_version:
env['ANTHIAS_VERSION'] = anthias_version
if anthias_mac:
env['ANTHIAS_MAC'] = anthias_mac

browser = sh.Command('ScreenlyWebview')(_bg=True, _err_to_out=True, _env=env)
while (
'Screenly service start' not in browser.process.stdout.decode('utf-8')
):
Expand Down
6 changes: 4 additions & 2 deletions webview/ScreenlyWebview.pro
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ CONFIG += c++11

SOURCES += src/main.cpp \
src/mainwindow.cpp \
src/view.cpp
src/view.cpp \
src/requestinterceptor.cpp

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
Expand All @@ -15,4 +16,5 @@ include(src/deployment.pri)

HEADERS += \
src/mainwindow.h \
src/view.h
src/view.h \
src/requestinterceptor.h
29 changes: 29 additions & 0 deletions webview/src/requestinterceptor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "requestinterceptor.h"

#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
#include <QProcessEnvironment>

RequestInterceptor::RequestInterceptor(QObject* parent)
: QWebEngineUrlRequestInterceptor(parent)
{
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
hostname = env.value("ANTHIAS_HOSTNAME").toUtf8();
version = env.value("ANTHIAS_VERSION").toUtf8();
mac = env.value("ANTHIAS_MAC").toUtf8();
}

void RequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{
if (!hostname.isEmpty()) {
info.setHttpHeader("X-Anthias-hostname", hostname);
}
if (!version.isEmpty()) {
info.setHttpHeader("X-Anthias-version", version);
}
if (!mac.isEmpty()) {
info.setHttpHeader("X-Anthias-mac", mac);
}
}
#endif


23 changes: 23 additions & 0 deletions webview/src/requestinterceptor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <QtGlobal>
#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
#include <QObject>
#include <QByteArray>
#include <QWebEngineUrlRequestInfo>
#include <QWebEngineUrlRequestInterceptor>

class RequestInterceptor : public QWebEngineUrlRequestInterceptor
{
public:
explicit RequestInterceptor(QObject* parent = nullptr);
void interceptRequest(QWebEngineUrlRequestInfo &info) override;

private:
QByteArray hostname;
QByteArray version;
QByteArray mac;
};
#endif


22 changes: 22 additions & 0 deletions webview/src/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#include <QPainter>
#include <QMovie>
#include <QBuffer>
#include <QWebEngineProfile>

#include "view.h"
#include "requestinterceptor.h"


View::View(QWidget* parent) : QWidget(parent)
Expand Down Expand Up @@ -44,6 +46,13 @@ View::View(QWidget* parent) : QWidget(parent)
isAnimatedImage = false;

connect(animationTimer, &QTimer::timeout, this, &View::updateMovieFrame);

#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
// Attach request interceptor to inject headers
RequestInterceptor* interceptor = new RequestInterceptor(this);
webView1->page()->profile()->setUrlRequestInterceptor(interceptor);
webView2->page()->profile()->setUrlRequestInterceptor(interceptor);
#endif
}

View::~View()
Expand Down Expand Up @@ -135,6 +144,19 @@ void View::loadImage(const QString &preUri)

// Start loading the next image
QNetworkRequest request(src);
// Inject the same headers for image fetches
QByteArray hostname = qgetenv("ANTHIAS_HOSTNAME");
QByteArray version = qgetenv("ANTHIAS_VERSION");
QByteArray mac = qgetenv("ANTHIAS_MAC");
if (!hostname.isEmpty()) {
request.setRawHeader("X-Anthias-hostname", hostname);
}
if (!version.isEmpty()) {
request.setRawHeader("X-Anthias-version", version);
}
if (!mac.isEmpty()) {
request.setRawHeader("X-Anthias-mac", mac);
}
QNetworkReply* reply = networkManager->get(request);

connect(reply, &QNetworkReply::finished, this, [=]() {
Expand Down
Loading