From 8e4e4c95965dc2eb8453e2e741984a5d8c9e02c1 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sat, 4 Oct 2025 21:52:38 +0200 Subject: [PATCH] Add ufw detection and setup --- dashboard/CMakeLists.txt | 1 + dashboard/firewall.cpp | 98 ++++++++++++++++++++++++++++ dashboard/firewall.h | 51 +++++++++++++++ dashboard/qml/Main.qml | 14 ++++ flatpak/io.github.wivrn.wivrn.yml.in | 2 + 5 files changed, 166 insertions(+) create mode 100644 dashboard/firewall.cpp create mode 100644 dashboard/firewall.h diff --git a/dashboard/CMakeLists.txt b/dashboard/CMakeLists.txt index b224366e..d741f89e 100644 --- a/dashboard/CMakeLists.txt +++ b/dashboard/CMakeLists.txt @@ -16,6 +16,7 @@ add_executable(wivrn-dashboard rectangle_partitionner.cpp adb.cpp apk_installer.cpp + firewall.cpp vulkan_info.cpp ) diff --git a/dashboard/firewall.cpp b/dashboard/firewall.cpp new file mode 100644 index 00000000..7a874710 --- /dev/null +++ b/dashboard/firewall.cpp @@ -0,0 +1,98 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "firewall.h" + +#include "escape_sandbox.h" +#include "utils/flatpak.h" +#include "wivrn_config.h" + +#include +#include +#include +#include + +firewall::type_t firewall::detect_type() +{ + if (wivrn::is_flatpak()) + { + if (not QStandardPaths::findExecutable( + "ufw", + QStringList({"/run/host/usr/sbin", "/run/host/usr/bin"})) + .isEmpty()) + return type_t::ufw; + } + else + { + if (not QStandardPaths::findExecutable("ufw").isEmpty()) + return type_t::ufw; + } + + return type_t::none; +} + +firewall::firewall() : + type(detect_type()) +{ +} + +const std::filesystem::path ufw_conf = "etc/ufw/applications.d/wivrn"; + +static bool need_setup_ufw() +{ + return not std::filesystem::exists( + (wivrn::is_flatpak() ? "/run/host" : "/") / ufw_conf); +} + +bool firewall::needSetup() +{ + switch (type) + { + case type_t::none: + return false; + case type_t::ufw: + return need_setup_ufw(); + } + assert(false); + __builtin_unreachable(); +} + +void firewall::doSetup() +{ + switch (type) + { + case type_t::none: + return; + case type_t::ufw: { + pkexec = escape_sandbox("pkexec", + "sh", + "-c", + "printf '[WiVRn]\\ntitle=WiVRn server\ndescription=WiVRn OpenXR streaming server\nports=" + std::to_string(wivrn::default_port) + "' > /" + ufw_conf.string() + " && ufw allow wivrn"); + pkexec->setProcessChannelMode(QProcess::MergedChannels); + pkexec->start(); + + QObject::connect(pkexec.get(), &QProcess::finished, this, [this](int exit_code, QProcess::ExitStatus exit_status) { + if (exit_status != QProcess::NormalExit or exit_code) + qWarning() << "ufw configuration exitec with status" << exit_status << "and code" << exit_code; + needSetupChanged(needSetup()); + pkexec.release()->deleteLater(); + }); + return; + } + } +} diff --git a/dashboard/firewall.h b/dashboard/firewall.h new file mode 100644 index 00000000..6da751a8 --- /dev/null +++ b/dashboard/firewall.h @@ -0,0 +1,51 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include +#include +#include + +class firewall : public QObject +{ + Q_OBJECT + QML_NAMED_ELEMENT(Firewall) + QML_SINGLETON + + Q_PROPERTY(bool needSetup READ needSetup NOTIFY needSetupChanged) + + enum class type_t + { + none, + ufw, + }; + + type_t type; + std::unique_ptr pkexec; + + static type_t detect_type(); + +public: + firewall(); + bool needSetup(); + Q_INVOKABLE void doSetup(); + +Q_SIGNALS: + void needSetupChanged(bool); +}; diff --git a/dashboard/qml/Main.qml b/dashboard/qml/Main.qml index 4854befe..5a1ae841 100644 --- a/dashboard/qml/Main.qml +++ b/dashboard/qml/Main.qml @@ -117,6 +117,20 @@ Kirigami.ApplicationWindow { ] } + Kirigami.InlineMessage { + Layout.fillWidth: true + text: i18n("Firewall may not allow port 9757.") + type: Kirigami.MessageType.Warning + showCloseButton: true + visible: Firewall.needSetup + actions: [ + Kirigami.Action { + text: i18n("Fix it") + onTriggered: Firewall.doSetup() + } + ] + } + Kirigami.InlineMessage { id: restart_capsysnice Layout.fillWidth: true diff --git a/flatpak/io.github.wivrn.wivrn.yml.in b/flatpak/io.github.wivrn.wivrn.yml.in index abb34416..6f7070a3 100644 --- a/flatpak/io.github.wivrn.wivrn.yml.in +++ b/flatpak/io.github.wivrn.wivrn.yml.in @@ -38,6 +38,8 @@ finish-args: # Let outside application access the WiVRn socket - --filesystem=xdg-run/wivrn:create - --own-name=io.github.wivrn.Server + # Check firewall configuration + - --filesystem=host-etc # QSystemTrayIcon - --talk-name=org.kde.StatusNotifierWatcher # Run processes outside of sandbox