#!/usr/bin/env python3 import argparse import json import logging import os import signal import tempfile import time from threading import Thread from typing import Union import dbus import dbus.mainloop.glib import dbus.service from gi.repository import GLib from session import Session from trigger import TriggerServerThread ALLOW_ONLY_ONE_SESSION = True class RWAService(dbus.service.Object): """D-Bus Session Service for RWA. D-Bus namespace: ``org.ArcticaProject.RWA`` D-Bus object name: ``/RWA`` :param loop: GLib main loop running the service :param mockup_mode: Starts the service in mock up mode """ def __init__( self, loop: GLib.MainLoop, mockup_mode: bool = False, one_time: bool = False ): self.loop = loop self.mockup_mode = mockup_mode self.one_time = one_time self.bus = dbus.SessionBus() name = dbus.service.BusName("org.ArcticaProject.RWA", bus=self.bus) self.trigger_service = TriggerServerThread(self._trigger) self.trigger_service.start() self.update_service_running = False self.sessions = {} super().__init__(name, "/RWA") logging.info("D-Bus service has been started.") @dbus.service.method("org.ArcticaProject.RWA", out_signature="s") def start(self) -> str: """Start a new remote session and register it in RWA. :return: Result as JSON (D-Bus string) **Structure of returned JSON (success):** :: {"status": "success", "id": , "url": "", "pin": } **Structure of returned JSON (error):** :: {"status": "error"} """ if ALLOW_ONLY_ONE_SESSION and len(self.sessions.values()) > 0: logging.warning( "There is already one session running and the service is configured to allow only one " "session, so this session won't be started." ) return json.dumps({"status": "error"}) # Start session session = Session(self.trigger_service.port, mockup_mode) # Add session to sessions list self.sessions[session.pid] = session # Start session update service self._ensure_update_service() return_json = session.client_meta return_json["status"] = "success" logging.info(f"New session #{session.pid} was started with meta {return_json}.") return json.dumps(return_json) @dbus.service.method("org.ArcticaProject.RWA", in_signature="i", out_signature="s") def status(self, pid: int) -> str: """Return the status of a session. .. note:: This uses the last status version got by the update service in the background. :param pid: (Process) ID of session (D-Bus integer) :return: Session status as JSON (D-Bus string) **Structure of returned JSON:** :: {"id": , "status": } **Possible status options:** ============ ====================== ``running`` The session is running and ready for connecting. ``active`` The session is running and a the remote connected to the session. ``stopped`` The session was stopped. ``dead`` There was a problem, so that the session is dead. ============ ====================== """ return self._get_status(pid) @dbus.service.method("org.ArcticaProject.RWA", in_signature="i", out_signature="s") def refresh_status(self, pid: int) -> str: """Same as :meth:`status`, but updates status from RWA before returning it here. """ self._update_session(pid) return self._get_status(pid) @dbus.service.method("org.ArcticaProject.RWA", in_signature="i", out_signature="s") def stop(self, pid: int) -> str: """Stop a remote session. :param pid: (Process) ID of session (D-Bus integer) :return: Session status as JSON (D-Bus string) **Structure of returned JSON:** :: {"id": , "status": "stopped"} """ try: session = self.sessions[pid] except KeyError: return json.dumps({"pid": pid, "status": "stopped"}, sort_keys=True) session.stop() return json.dumps({"id": pid, "status": "stopped"}, sort_keys=True) def _get_status(self, pid: int) -> str: try: session = self.sessions[pid] except KeyError: return json.dumps({"id": pid, "status": "dead"}, sort_keys=True) return json.dumps(session.status) def _ensure_update_service(self): """Start session update thread if it isn't already running.""" if not self.update_service_running: self.update_thread = Thread(target=self._update_sessions) self.update_thread.start() def _update_session(self, pid: int): """Update the status of a session.""" logging.info(f"Update status for session #{pid} …") try: session = self.sessions[pid] except KeyError: logging.warning(" Session is dead.") return # Check if VNC process is still running running = session.vnc_process_running if running: logging.info(" Session is running.") elif session.status_text == "stopped" and session.pid in self.sessions: logging.warning(" Session is dead.") del self.sessions[session.pid] else: logging.warning(" VNC was stopped, so session is dead.") session.stop() del self.sessions[session.pid] def _update_sessions(self): """Go through all running sessions and update their status using ``_update_session``.""" logging.info("Started update service for sessions.") while len(self.sessions.values()) > 0: for session in list(self.sessions.values()): self._update_session(session.pid) time.sleep(2) self.update_service_running = False logging.info("Stopped update service for sessions.") if self.one_time: self._stop_all() def _trigger(self, token: str) -> bool: """Trigger a specific session via trigger token.""" logging.info(f"Triggered with token {token}") for session in self.sessions.values(): if token == session.trigger_token: logging.info(f"Session #{session.pid} matches the token.") session.trigger() return True logging.warning(" No matching session found for this token.") return False def _stop_all(self): """Stop all sessions and this daemon.""" logging.info("Stop all sessions and exit service.") for session in list(self.sessions.values()): session.stop() del self.sessions[session.pid] self.trigger_service.shutdown() self.loop.quit() def str2bool(v: Union[str, bool, int]) -> bool: """Return true or false if the given string can be interpreted as a boolean otherwise raise an exception.""" if isinstance(v, bool): return v if v.lower() in ("yes", "true", "t", "y", "1", 1): return True elif v.lower() in ("no", "false", "f", "n", "0", 0): return False else: raise argparse.ArgumentTypeError("Boolean value expected.") if __name__ == "__main__": lock_file_path = os.path.join(tempfile.gettempdir(), "rwa-session-service.lock") # Check for lock file if os.path.exists(lock_file_path): logging.error("The service is already running.") exit(1) # Create lock file with open(lock_file_path, "w") as f: f.write("lock") parser = argparse.ArgumentParser(description="D-Bus Session Service for RWA") parser.add_argument( "-m", "--mockup-mode", type=str2bool, nargs="?", const=True, default=False, help="Activates mock up mode. Acts like the real session service but don't do changes or call RWA.", ) parser.add_argument( "-o", "--one-time", type=str2bool, nargs="?", const=True, default=False, help="Runs as one-time-service. Stops after one session.", ) args = parser.parse_args() mockup_mode = args.mockup_mode one_time = args.one_time if mockup_mode: logging.warning( "All API responses are faked and should NOT BE USED IN PRODUCTION!" ) dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) loop = GLib.MainLoop() object = RWAService(loop, mockup_mode, one_time) def signal_handler(sig, frame): logging.info("Service was terminated.") object._stop_all() signal.signal(signal.SIGINT, signal_handler) loop.run() logging.info("Remove lock file ...") os.remove(lock_file_path)