Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add background status support #152

Merged
merged 1 commit into from
Apr 5, 2024
Merged
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
20 changes: 20 additions & 0 deletions gui/src/linux/flatpak/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
import subprocess

is_flatpak = os.path.exists("/.flatpak-info")

def register_autostart(autostart: bool):
if is_flatpak:
print("Running in flatpak, registering with background portal for autostart.")
try:
subprocess.Popen(["python3", "/app/bin/src/linux/flatpak/autostart.py"], start_new_session=True)
except:
pass


def set_status(status: str):
if is_flatpak:
try:
subprocess.Popen(["python3", "/app/bin/src/linux/flatpak/status.py", status], start_new_session=True)
except:
pass
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import gi
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
import gc
from gi.repository import Gtk, Adw, GLib, Gio
from gi.repository import GLib, Gio
from random import randint
import time
import os
import sys
from threading import Timer


def receive_autostart(self, *args):
print("autostart enabled..!?")
print(args)
os._exit(0)
sys.exit(0)

def request_autostart():
bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
Expand Down
37 changes: 37 additions & 0 deletions gui/src/linux/flatpak/status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
Script to set the status of the background process.
Run separately so that gtk dependencies don't stick around in memory.
"""
import gi
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import GLib, Gio
import sys

def set_status(message):
bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
proxy = Gio.DBusProxy.new_sync(
bus,
Gio.DBusProxyFlags.NONE,
None,
'org.freedesktop.portal.Desktop',
'/org/freedesktop/portal/desktop',
'org.freedesktop.portal.Background',
None,
)

options = {
'message': GLib.Variant('s', message),
}

try:
request = proxy.SetStatus('(a{sv})', options)
sys.exit(0)
except Exception as e:
print(e)

if len(sys.argv) > 1:
set_status(sys.argv[1])

loop = GLib.MainLoop()
loop.run()
11 changes: 4 additions & 7 deletions gui/src/linux/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from tendo import singleton
from .monitors import dbus_autofill_monitor
from .monitors import dbus_monitor
from .monitors import locked_monitor
import sys
from src.services import goldwarden
from src.services import pinentry
Expand All @@ -12,6 +13,7 @@
import secrets
import time
import os
import src.linux.flatpak.api as flatpak_api

root_path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir, os.pardir))
is_flatpak = os.path.exists("/.flatpak-info")
Expand All @@ -37,20 +39,15 @@ def main():
# start daemons
dbus_autofill_monitor.run_daemon(token) # todo: remove after migration
dbus_monitor.run_daemon(token)
locked_monitor.run_daemon(token)
pinentry.daemonize()

if not "--hidden" in sys.argv:
p = subprocess.Popen(["python3", "-m", "src.gui.settings"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=root_path, start_new_session=True)
p.stdin.write(f"{token}\n".encode())
p.stdin.flush()

if is_flatpak:
# to autostart the appes
try:
print("Enabling autostart...")
subprocess.Popen(["python3", "-m", "src.linux.background"], cwd=root_path, start_new_session=True)
except Exception as e:
pass
flatpak_api.register_autostart(True)

while True:
time.sleep(60)
30 changes: 30 additions & 0 deletions gui/src/linux/monitors/locked_monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from gi.repository import Gtk
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
from threading import Thread
import subprocess
import os
from src.services import goldwarden
import time
import src.linux.flatpak.api as flatpak_api

daemon_token = None

def daemon():
time.sleep(5)
goldwarden.create_authenticated_connection(daemon_token)
while True:
status = goldwarden.get_vault_status()
if status["locked"]:
flatpak_api.set_status("Locked")
else:
flatpak_api.set_status("Unlocked")
time.sleep(1)

def run_daemon(token):
print("running locked status daemon")
global daemon_token
daemon_token = token
thread = Thread(target=daemon)
thread.start()
1 change: 1 addition & 0 deletions gui/src/services/goldwarden.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
authenticated_connection = None

def create_authenticated_connection(token):
print("create authenticated connection")
global authenticated_connection
authenticated_connection = subprocess.Popen([f"{BINARY_PATH}", "session"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if not token == None:
Expand Down
Loading