opensnitch icon indicating copy to clipboard operation
opensnitch copied to clipboard

[Feature Request] Enable/disable autostart in tray

Open munix9 opened this issue 3 years ago • 12 comments

Enable/disable autostart in tray.

Summary:

I have created a patch to be able to enable/disable autostart in the tray. It is a proof of concept (hack!) and some things can certainly be implemented better.

The following paths and files are defined as prerequisites for the checks and activation: /etc/xdg/autostart/opensnitch_ui.desktop (optional) ~/.config/autostart/opensnitch_ui.desktop /usr/share/applications/opensnitch_ui.desktop

This should all be standard, it works fine on openSUSE Tumbleweed (KDE Plasma, X11).

Maybe it can be used as a template to implement the functionality.

--- a/ui/opensnitch/service.py
+++ b/ui/opensnitch/service.py
@@ -181,11 +181,57 @@
         self._menu_enable_fw = self._menu.addAction(self.MENU_ENTRY_FW_DISABLE)
         self._menu_enable_fw.setEnabled(False)
         self._menu_enable_fw.triggered.connect(self._on_enable_interception_clicked)
+
+        self._menu.addSeparator()
+        self._menu_autostart = self._menu.addAction("Autostart")
+        self._menu_autostart.setCheckable(True)
+        self._menu_autostart.setChecked(self._is_autostart_enabled())
+        self._menu_autostart.triggered.connect(self._on_switch_autostart)
+        self._menu.addSeparator()
+
         self._menu.addAction(self.MENU_ENTRY_HELP).triggered.connect(
                 lambda: QtGui.QDesktopServices.openUrl(QtCore.QUrl(Config.HELP_CONFIG_URL))
                 )
         self._menu.addAction(self.MENU_ENTRY_CLOSE).triggered.connect(self._on_close)
 
+    def _is_autostart_enabled(self):
+        desktopFile = "opensnitch_ui.desktop"
+        systemAutostart = "/etc/xdg/autostart/" + desktopFile
+        if not os.path.isfile(systemAutostart) and os.path.isfile("/usr" + systemAutostart):
+            systemAutostart = "/usr" + systemAutostart
+        userAutostart = os.environ['HOME'] + "/.config/autostart/" + desktopFile
+        if os.path.isfile(userAutostart):
+            retVal = os.system("grep -qsi \"^Hidden=true\" " + userAutostart)
+            if retVal != 0:
+                return True
+        elif os.path.isfile(systemAutostart):
+            return True
+
+        return False
+
+    def _on_switch_autostart(self):
+        desktopFile = "opensnitch_ui.desktop"
+        systemDesktop = "/usr/share/applications/" + desktopFile
+        systemAutostart = "/etc/xdg/autostart/" + desktopFile
+        if not os.path.isfile(systemAutostart) and os.path.isfile("/usr" + systemAutostart):
+            systemAutostart = "/usr" + systemAutostart
+        userAutostart = os.environ['HOME'] + "/.config/autostart/" + desktopFile
+
+        if not os.path.exists(os.path.dirname(userAutostart)):
+            os.system("mkdir -p " + os.path.dirname(userAutostart))
+
+        if self._menu_autostart.isChecked():
+            if os.path.isfile(systemAutostart) and os.path.isfile(userAutostart):
+                os.remove(userAutostart)
+            else:
+                os.system("cp " + systemDesktop + " " + userAutostart)
+        else:
+            if os.path.isfile(systemAutostart):
+                os.system("cp " + systemDesktop + " " + userAutostart)
+                os.system("echo 'Hidden=true' >> " + userAutostart)
+            elif os.path.isfile(userAutostart):
+                os.remove(userAutostart)
+
     def _show_gui_if_tray_not_available(self):
         """If the system tray is not available or ready, show the GUI after
         10s. This delay helps to skip showing up the GUI when DEs' autologin is on.

munix9 avatar May 25 '23 06:05 munix9

I am using Arch KDE and by default it autostarts in tray?

.config/autostart/opensnitch_ui.desktop:

[Desktop Entry] Categories=System;Security;Monitor;Network; Comment=Interactive application firewall Exec=/bin/sh -c "pkill -15 opensnitch-ui; opensnitch-ui" GenericName=OpenSnitch Firewall Icon=opensnitch-ui Keywords=system;firewall;policies;security;polkit;policykit; Name=OpenSnitch NoDisplay=false Terminal=false Type=Application X-GNOME-Autostart-Delay=3 X-GNOME-Autostart-enabled=true

swx-1 avatar May 25 '23 07:05 swx-1

I am using Arch KDE and by default it autostarts in tray?

.config/autostart/opensnitch_ui.desktop:

[Desktop Entry] Categories=System;Security;Monitor;Network; Comment=Interactive application firewall Exec=/bin/sh -c "pkill -15 opensnitch-ui; opensnitch-ui" GenericName=OpenSnitch Firewall Icon=opensnitch-ui Keywords=system;firewall;policies;security;polkit;policykit; Name=OpenSnitch NoDisplay=false Terminal=false Type=Application X-GNOME-Autostart-Delay=3 X-GNOME-Autostart-enabled=true

Yes, autostart already works. But the point here is to be able to enable/disable autostart as a user via the GUI.

munix9 avatar May 25 '23 07:05 munix9

hi @munix9 !

The only problem I see is the path /etc/xdg/autostart/opensnitch_ui.desktop which requires root privileges.

Anyway, this looks more to me like a Settings option, wouldn't it make more sense to add it to the Preferences->UI dialog?

gustavo-iniguez-goya avatar May 25 '23 11:05 gustavo-iniguez-goya

hi @munix9 !

The only problem I see is the path /etc/xdg/autostart/opensnitch_ui.desktop which requires root privileges.

Nope, that's not a problem:

/etc/xdg/autostart/opensnitch_ui.desktop should be provided by the package creator (and by make install?) and may belong to root without problems. /etc/xdg/autostart/opensnitch_ui.desktop is the central autostart file which says that every user gets opensnitch-ui started after login.

The only place that is write-accessed is ~/.config/autostart/opensnitch_ui.desktop and that belongs to the logged in user, so no problem there either.

The following applies: If there is a corresponding desktop file under /etc/xdg/autostart/, then the corresponding program is started after login. If there is also the file in the directory ~/.config/autostart/, but with the additional line Hidden=True, then the program is not started - the autostart is therefore disabled.

In the patch, the presence of the file /etc/xdg/autostart/opensnitch_ui.desktop is not strictly necessary, but it is still taken into account.

Anyway, this looks more to me like a Settings option, wouldn't it make more sense to add it to the Preferences->UI dialog?

Sure, or in both places? In the tray it is faster to find, but is certainly a matter of taste.

Background: It's been my experience that users don't necessarily like it when an option can't be disabled - at least simply via the GUI.

Via console it is feasible, sure, but not every user feels comfortable acting in a console.

munix9 avatar May 25 '23 12:05 munix9

In debian the file /etc/xdg/autostart/opensnitch_ui.desktop is a link to /usr/share/applications/opensnitch_ui.desktop - this is also possible.

https://salsa.debian.org/go-team/packages/opensnitch/-/blob/debian/sid/debian/python3-opensnitch-ui.postinst#L8

munix9 avatar May 25 '23 12:05 munix9

ok @munix9 , understood! I'll add it in a few days.

gustavo-iniguez-goya avatar May 27 '23 23:05 gustavo-iniguez-goya

ok @munix9 , understood! I'll add it in a few days.

Thank you, there is no hurry, it is a desirable feature.

I should mention that due to the "/etc and /usr/etc" topic there might be an additional system directory, namely /usr/etc/xdg/autostart/.

See also https://github.com/evilsocket/opensnitch/issues/671

munix9 avatar May 28 '23 06:05 munix9

This will be a nice feature, thanks! Perhaps we can resolve some of these path discrepancies using a module like xdg-base-dirs.

ghost avatar Jun 08 '23 03:06 ghost

This will be a nice feature, thanks! Perhaps we can resolve some of these path discrepancies using a module like xdg-base-dirs.

Or pyxdg - that would certainly make sense to use one of them.

munix9 avatar Jun 08 '23 07:06 munix9

added in 59621017e96a6e049f42db1dff609cc2a549cbf4

gustavo-iniguez-goya avatar Jun 24 '23 22:06 gustavo-iniguez-goya

+1 this would be a great feature. I only use opensnitch in certain working situations and would like it to easy to toggle the autostart feature. In most applications I see this offered as a setting or even an option in the tray itself.

jreus avatar Jul 03 '23 09:07 jreus

It works great. Thanks @munix9

ghost avatar Jul 05 '23 17:07 ghost