diff options
author | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-11-13 19:41:20 +0100 |
---|---|---|
committer | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-11-25 10:26:52 +0100 |
commit | 37bf71c8bfc5c09f842fb1aa859ebf84de8c8d66 (patch) | |
tree | d06b28dcc343d9c920b3352f18a591c4afa8a71d | |
parent | bd921dcbd69526c22cd5f2acf60f6ab528e50c04 (diff) |
src: add user session detection implementationwinlinux
Detect user login and logout for Linux and Windows.
Report an active interactive session through the /refresh response
so a new ogserver instance can update the session status.
Poll the session change in 5 second intervals in a thread. Use the
same event socket previously used by the old session detection
mechanism to notify a session change.
Use the method check_interactive_session_change in each
ogOperations.py to report the session status.
Return values:
None: no session changes are found
True: login
False: logout
Windows
Verify if psutil.users() has any value.
Linux
Verify all the psutil.users() asociated to a terminal.
-rw-r--r-- | src/linux/ogOperations.py | 23 | ||||
-rw-r--r-- | src/live/ogOperations.py | 3 | ||||
-rw-r--r-- | src/ogClient.py | 24 | ||||
-rw-r--r-- | src/ogRest.py | 3 | ||||
-rw-r--r-- | src/virtual/ogOperations.py | 3 | ||||
-rw-r--r-- | src/windows/ogOperations.py | 20 |
6 files changed, 73 insertions, 3 deletions
diff --git a/src/linux/ogOperations.py b/src/linux/ogOperations.py index 4728201..bbce41a 100644 --- a/src/linux/ogOperations.py +++ b/src/linux/ogOperations.py @@ -7,6 +7,7 @@ # (at your option) any later version. import os +import psutil import subprocess from subprocess import CalledProcessError from src.log import OgError @@ -15,6 +16,9 @@ from src.ogRest import ThreadState class OgLinuxOperations: + def __init__(self): + self.session = False + def _restartBrowser(self, url): raise OgError('Function not implemented') @@ -66,4 +70,21 @@ class OgLinuxOperations: raise OgError('Function not implemented') def refresh(self, ogRest): - return {"status": "LINUX"} + if self.session: + session_value = 'LINUXS' + else: + session_value = 'LINUX' + return {"status": session_value} + + def check_interactive_session_change(self): + old_status = self.session + has_logged_user = False + for user in psutil.users(): + if user.terminal: + has_logged_user = True + break + self.session = has_logged_user + + if self.session != old_status: + return self.session + return None diff --git a/src/live/ogOperations.py b/src/live/ogOperations.py index 2666606..ef18e33 100644 --- a/src/live/ogOperations.py +++ b/src/live/ogOperations.py @@ -837,3 +837,6 @@ class OgLiveOperations: logging.info('Sending response to refresh request') return json_body + + def check_interactive_session_change(self): + return False diff --git a/src/ogClient.py b/src/ogClient.py index 51aa33b..2635a3f 100644 --- a/src/ogClient.py +++ b/src/ogClient.py @@ -6,6 +6,7 @@ # Free Software Foundation; either version 3 of the License, or # (at your option) any later version. +import threading import errno import select import socket @@ -26,6 +27,8 @@ class State(Enum): class ogClient: OG_PATH = '/opt/opengnsys/' + SESSION_POLL_INTERVAL = 5 + EVENT_SOCKET_PORT = 55885 def __init__(self, config): self.CONFIG = config @@ -36,7 +39,7 @@ class ogClient: if self.mode in {'linux', 'windows'}: self.event_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.event_sock.setblocking(0) - self.event_sock.bind(('127.0.0.1', 55885)) + self.event_sock.bind(('127.0.0.1', ogClient.EVENT_SOCKET_PORT)) else: self.event_sock = None @@ -49,6 +52,25 @@ class ogClient: self.ogrest = ogRest(self.CONFIG) self.seq = None + self.session_check_thread = threading.Thread(target=self._session_check_loop, daemon=True) + self.session_check_thread.start() + + def _session_check_loop(self): + while True: + session_status = self.ogrest.check_interactive_session_change() + if session_status is True: + message = "session start user" + elif session_status is False: + message = "session stop user" + else: + message = None + + if message: + self.event_sock.sendto(message.encode('utf-8'), + ('127.0.0.1', ogClient.EVENT_SOCKET_PORT)) + + time.sleep(ogClient.SESSION_POLL_INTERVAL) + def get_socket(self): return self.sock diff --git a/src/ogRest.py b/src/ogRest.py index 7511c76..5731350 100644 --- a/src/ogRest.py +++ b/src/ogRest.py @@ -443,3 +443,6 @@ class ogRest(): def process_refresh(self, client): threading.Thread(target=ogThread.refresh, args=(client, self,)).start() + + def check_interactive_session_change(self): + return self.operations.check_interactive_session_change() diff --git a/src/virtual/ogOperations.py b/src/virtual/ogOperations.py index c51f59d..86ae0ed 100644 --- a/src/virtual/ogOperations.py +++ b/src/virtual/ogOperations.py @@ -629,3 +629,6 @@ class OgVirtualOperations: for k, v in device_names.items(): f.write(f'{k}={v}\n') f.truncate() + + def check_interactive_session_change(self): + return None diff --git a/src/windows/ogOperations.py b/src/windows/ogOperations.py index 8c98da7..135b031 100644 --- a/src/windows/ogOperations.py +++ b/src/windows/ogOperations.py @@ -8,6 +8,7 @@ import os import ctypes +import psutil import subprocess from subprocess import CalledProcessError import multiprocessing as mp @@ -64,6 +65,7 @@ def create_systray(): class OgWindowsOperations: def __init__(self): + self.session = False freeze_support() mp.set_start_method('spawn') self.systray_p = Process(target=create_systray, daemon=True) @@ -125,4 +127,20 @@ class OgWindowsOperations: raise OgError('Function not implemented') def refresh(self, ogRest): - return {"status": "WIN"} + if self.session: + session_value = 'WINS' + else: + session_value = 'WIN' + return {"status": session_value} + + def check_interactive_session_change(self): + old_status = self.session + has_logged_user = False + for user in psutil.users(): + has_logged_user = True + break + self.session = has_logged_user + + if self.session != old_status: + return self.session + return None |