summaryrefslogtreecommitdiffstats
path: root/src/windows/ogOperations.py
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-11-13 19:41:20 +0100
committerAlejandro Sirgo Rica <asirgo@soleta.eu>2024-11-21 17:19:27 +0100
commit0bc485e2c4ccdc5e117bbb9f12d85e926ba7deac (patch)
tree276f51d2c303f520a398aac8ddcbdfe560d6ddb7 /src/windows/ogOperations.py
parentbd921dcbd69526c22cd5f2acf60f6ab528e50c04 (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 check_interactive_session_change uses the WMI API to obtain session information. Obtain the list of sessions associated to interactive sessions and then filter the sessions with a valid user. Linux Check if any user has the DISPLAY or WAYLAND_DISPLAY environment variable defined as that means there is an interactive session in execution.
Diffstat (limited to 'src/windows/ogOperations.py')
-rw-r--r--src/windows/ogOperations.py32
1 files changed, 31 insertions, 1 deletions
diff --git a/src/windows/ogOperations.py b/src/windows/ogOperations.py
index 8c98da7..6396811 100644
--- a/src/windows/ogOperations.py
+++ b/src/windows/ogOperations.py
@@ -8,11 +8,13 @@
import os
import ctypes
+import pythoncom
import subprocess
from subprocess import CalledProcessError
import multiprocessing as mp
from multiprocessing import Process, freeze_support
from src.log import OgError
+import wmi
from PIL import Image, ImageDraw
from pystray import Icon, Menu, MenuItem
@@ -64,6 +66,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 +128,31 @@ 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
+ pythoncom.CoInitialize()
+ has_logged_user = False
+ try:
+ c = wmi.WMI()
+ sessions = c.Win32_LogonSession(LogonType=2)
+
+ for session in sessions:
+ if has_logged_user:
+ break
+ for user in session.associators("Win32_LoggedOnUser"):
+ if user and user.AccountType == 1:
+ has_logged_user = True
+ break
+ finally:
+ pythoncom.CoUninitialize()
+ self.session = has_logged_user
+
+ if self.session != old_status:
+ return self.session
+ return None