diff --git a/msp_remoteadmin/hooks.py b/msp_remoteadmin/hooks.py index 401ceed..36eee50 100644 --- a/msp_remoteadmin/hooks.py +++ b/msp_remoteadmin/hooks.py @@ -217,4 +217,9 @@ app_license = "MIT" # ] app_include_js = [ "/assets/msp_remoteadmin/js/msp_remote.js" -] \ No newline at end of file +] +scheduler_events = { + "all": [ + "msp_remoteadmin.tools.check_session_status" + ] +} \ No newline at end of file diff --git a/msp_remoteadmin/tools.py b/msp_remoteadmin/tools.py index f72b432..6ee537f 100644 --- a/msp_remoteadmin/tools.py +++ b/msp_remoteadmin/tools.py @@ -1,7 +1,9 @@ # Copyright (c) 2024, Luiz Costa and contributors # For license information, please see license.txt +import time import frappe +from frappe.utils import now import requests import urllib.parse @@ -10,6 +12,58 @@ PROTOCOL_PORT = { "RDP": 3389 } +def log_guacamole_session(url, protocol, host, user): + # Wait for guacamole to create the session + time.sleep(1.2) + # Debug + print(f"DEBUG: New session -> Protocol: {protocol} :: Host: {host} :: User: {user}") + # Set guacamole url + guacamole_url = url.replace('https:', 'http:').replace('/guacamole', '') + # Get last id created on guacamole + last_id = requests.get(f'{guacamole_url}:8085/last_id') + if last_id.status_code == 200: + last_id = last_id.json()['last_id'] + # add log session + doc = frappe.new_doc('Remote Connection Sessions') + doc.id = last_id + doc.protocol = protocol + doc.host = host + doc.user = user + doc.start_datetime = time.strftime('%Y-%m-%d %H:%M:%S') + doc.insert() + doc.save() + frappe.db.commit() + frappe.logger().info(f"Session {last_id} created and updated in Frappe") + else: + last_id = 0 + +@frappe.whitelist(guest=True) +def check_session_status(): + # URL from guacamole server + guaca_config = frappe.get_single('Remote Connections Settings') + guacamole_url = guaca_config.guacamole_server.replace('https:', 'http:').replace('/guacamole', '') + + # Get all active sessions in "Remote Connection Sessions" Doctype + active_sessions = frappe.get_all("Remote Connection Sessions", filters={"end_datetime": ["is", ""]}, fields=["name", "id"]) + + for session in active_sessions: + session_id = session["id"] + try: + response = requests.get(f"{guacamole_url}:8085/session/{session_id}") + response_data = response.json() + + # Verify if the session was ended + if "end" in response_data.keys(): + # Update the "end_datetime" field of the corresponding document + doc = frappe.get_doc("Remote Connection Sessions", session["name"]) + doc.end_datetime = now() + doc.save() + frappe.db.commit() + frappe.logger().info(f"Session {session_id} ended and updated in Frappe") + except Exception as e: + frappe.logger().error(f"Error checking session {session_id}: {str(e)}") + + @frappe.whitelist() def create_session(name, protocol): guaca_config = frappe.get_single('Remote Connections Settings') @@ -62,4 +116,5 @@ def create_session(name, protocol): if params: uri = f"{uri}/?{'&'.join(params)}" url = f'{guaca_config.guacamole_server}/?#/?token={token}&quickconnect={urllib.parse.quote(uri)}' + frappe.enqueue(log_guacamole_session, queue='short', url=guaca_config.guacamole_server, protocol=protocol, ip_address=ip_address, user=frappe.session.user) return { 'url': url, 'resolution': guaca_config.resolution if guaca_config.get('resolution') else '800x600'}