Skip to content

Commit

Permalink
flakpak: fix broken autostart feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilledheart committed Aug 22, 2024
1 parent 7ee9cdf commit 20bee24
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 2 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ if (OFFICIAL_BUILD)
add_definitions(-DOFFICIAL_BUILD)
endif()

if (FLATPAK_BUILD)
list(APPEND YASS_APP_FEATURES "flatpak-build")
add_definitions(-DFLATPAK_BUILD)
endif()

if(MSVC)
# On Windows, prefer cl over gcc if both are available. By default most of
# the CMake generators prefer gcc, even on Windows.
Expand Down Expand Up @@ -536,6 +541,10 @@ cmake_dependent_option(
INSTALL_FLATHUB_METAINFO "Build with flathub metainfo" OFF
"GUI AND LINUX" OFF)

cmake_dependent_option(
FLATPAK_BUILD "Build with flatpak support" OFF
"GUI AND LINUX" OFF)

# Dynamic users are supported from version 235
# see https://0pointer.net/blog/dynamic-users-with-systemd.html
if (USE_OLD_SYSTEMD_SERVICE)
Expand Down
23 changes: 23 additions & 0 deletions flatpak/dconf-override.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
diff --git a/engine/dconf-engine-source-user.c b/engine/dconf-engine-source-user.c
index 1657875..e4f8786 100644
--- a/engine/dconf-engine-source-user.c
+++ b/engine/dconf-engine-source-user.c
@@ -39,11 +39,17 @@ dconf_engine_source_user_open_gvdb (const gchar *name)
{
GvdbTable *table;
gchar *filename;
+ const gchar *override;
+
+ override = g_getenv ("DCONF_USER_CONFIG_DIR");
+ if (override == NULL)
+ filename = g_build_filename (g_get_user_config_dir (), "dconf", name, NULL);
+ else
+ filename = g_build_filename (g_get_home_dir (), override, name, NULL);

/* This can fail in the normal case of the user not having any
* settings. That's OK and it shouldn't be considered as an error.
*/
- filename = g_build_filename (g_get_user_config_dir (), "dconf", name, NULL);
table = gvdb_table_new (filename, FALSE, NULL);
g_free (filename);

36 changes: 36 additions & 0 deletions flatpak/io.github.chilledheart.yass.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,47 @@ finish-args:
- --talk-name=org.kde.StatusNotifierWatcher
- --talk-name=com.canonical.indicator.application
- --talk-name=org.ayatana.indicator.application
# For Freedesktop autostart
- --filesystem=xdg-config/autostart:create
# For GNOME proxy resolution
- --filesystem=xdg-run/dconf
- --filesystem=~/.config/dconf
- --talk-name=ca.desrt.dconf
- --env=DCONF_USER_CONFIG_DIR=.config/dconf
- --env=GIO_EXTRA_MODULES=/app/lib/gio/modules
- --env=GSETTINGS_BACKEND=dconf
# For KDE proxy resolution (KDE5/KDE6 only)
- --filesystem=xdg-config/kioslaverc
modules:
- name: dconf
buildsystem: meson
config-opts:
- -Dbash_completion=false
- -Dman=false
- -Dvapi=false
cleanup:
- /etc
- /include
- ca.desrt.dconf.service
- dconf.service
- dconf-service
- '*.pc'
sources:
- type: archive
url: https://download.gnome.org/sources/dconf/0.40/dconf-0.40.0.tar.xz
sha256: cf7f22a4c9200421d8d3325c5c1b8b93a36843650c9f95d6451e20f0bcb24533
x-checker-data:
type: gnome
name: dconf
stable-only: true
# From: https://src.fedoraproject.org/rpms/dconf
- type: patch
path: dconf-override.patch
- name: yass
buildsystem: cmake-ninja
builddir: true
config-opts:
- -DFLATPAK_BUILD=on
- -DCMAKE_BUILD_TYPE=RelWithDebInfo
- -DINSTALL_FLATHUB_METAINFO=on
- -DUSE_ZLIB=on
Expand Down
61 changes: 59 additions & 2 deletions src/freedesktop/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ using namespace yass;

static constexpr const char kDefaultAutoStartName[] = "io.github.chilledheart.yass";

#ifdef FLATPAK_BUILD
static constexpr const std::string_view kAutoStartFileContent =
"[Desktop Entry]\n"
"Type=Application\n"
"Name=yass\n"
"Comment=Yet Another Shadow Socket is a lightweight and secure http/socks4/socks5 proxy for embedded devices and "
"low end boxes.\n"
"Icon=io.github.chilledheart.yass\n"
"Exec=/usr/bin/flatpak run --command=yass io.github.chilledheart.yass --background\n"
"Terminal=false\n"
"Categories=Network;GTK;Utility\n"
"X-Flatpak=io.github.chilledheart.yass\n";
#else
static constexpr const std::string_view kAutoStartFileContent =
"[Desktop Entry]\n"
"Version=1.0\n"
Expand All @@ -31,6 +44,7 @@ static constexpr const std::string_view kAutoStartFileContent =
"Exec=\"%s\" --background\n"
"Terminal=false\n"
"Categories=Network;GTK;Utility\n";
#endif

namespace {

Expand All @@ -48,6 +62,19 @@ std::string GetConfigDir() {
return config_dir;
}

// https://github.com/qt/qtbase/blob/7fe1198f6edb40de2299272c7523d85d7486598b/src/corelib/io/qstandardpaths_unix.cpp#L201
std::string GetDataDir() {
const char* data_dir_ptr = getenv("XDG_DATA_HOME");
std::string data_dir;
// spec says relative paths should be ignored
if (data_dir_ptr == nullptr || data_dir_ptr[0] != '/') {
data_dir = ExpandUser("~/.local/share");
} else {
data_dir = data_dir_ptr;
}
return data_dir;
}

std::string GetAutostartDirectory() {
return absl::StrCat(GetConfigDir(), "/", "autostart");
}
Expand All @@ -61,8 +88,12 @@ bool IsKDE() {
// see https://userbase.kde.org/KDE_System_Administration/Environment_Variables
std::string GetKDESessionVersion() {
DCHECK(IsKDE());
#ifdef FLATPAK_BUILD
return "5"s;
#else
const char* kde_session_ptr = getenv("KDE_SESSION_VERSION");
return kde_session_ptr ? std::string(kde_session_ptr) : "5"s;
#endif
}
} // namespace

Expand All @@ -89,9 +120,13 @@ void Utils::EnableAutoStart(bool on) {
}

// write to target
#ifdef FLATPAK_BUILD
auto desktop_entry = std::string(kAutoStartFileContent);
#else
std::string executable_path = "yass"s;
GetExecutablePath(&executable_path);
std::string desktop_entry = absl::StrFormat(kAutoStartFileContent, executable_path);
#endif
if (!WriteFileWithBuffer(autostart_desktop_path, desktop_entry)) {
PLOG(WARNING) << "Internal error: unable to create autostart file";
}
Expand All @@ -101,7 +136,7 @@ void Utils::EnableAutoStart(bool on) {

// Update Desktop Database
std::string _;
std::vector<std::string> params = {"update-desktop-database"s, ExpandUser("~/.local/share/applications"s)};
std::vector<std::string> params = {"update-desktop-database"s, absl::StrCat(GetDataDir(), "/applications"s)};
if (ExecuteProcess(params, &_, &_) != 0) {
PLOG(WARNING) << "update-desktop-database failed";
} else {
Expand All @@ -116,7 +151,7 @@ bool Utils::GetSystemProxy() {
if (!QuerySystemProxy_KDE(&enabled, &server_addr, &bypass_addr)) {
return false;
}
return enabled && server_addr == GetLocalAddr();
return enabled && (server_addr == GetLocalAddrKDE() || server_addr == GetLocalAddr());
}
bool enabled;
std::string server_host, server_port, bypass_addr;
Expand Down Expand Up @@ -173,6 +208,28 @@ std::string Utils::GetLocalAddr() {
return ss.str();
}

std::string Utils::GetLocalAddrKDE() {
std::ostringstream ss;
auto local_host = absl::GetFlag(FLAGS_local_host);
auto local_port = absl::GetFlag(FLAGS_local_port);

asio::error_code ec;
auto addr = asio::ip::make_address(local_host.c_str(), ec);
bool host_is_ip_address = !ec;
if (host_is_ip_address && addr.is_v6()) {
if (addr.is_unspecified()) {
local_host = "::1"s;
}
ss << "http://[" << local_host << "] " << local_port;
} else {
if (host_is_ip_address && addr.is_unspecified()) {
local_host = "127.0.0.1"s;
}
ss << "http://" << local_host << " " << local_port;
}
return ss.str();
}

bool QuerySystemProxy(bool* enabled, std::string* server_host, std::string* server_port, std::string* bypass_addr) {
std::string output, _;
std::vector<std::string> params = {"gsettings"s, "get"s, "org.gnome.system.proxy"s, "mode"s};
Expand Down
1 change: 1 addition & 0 deletions src/freedesktop/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Utils {
static bool GetSystemProxy();
static bool SetSystemProxy(bool on);
static std::string GetLocalAddr();
static std::string GetLocalAddrKDE();
};

bool QuerySystemProxy(bool* enabled, std::string* server_host, std::string* server_port, std::string* bypass_addr);
Expand Down

0 comments on commit 20bee24

Please sign in to comment.