diff --git a/msp_remoteadmin/hooks.py b/msp_remoteadmin/hooks.py index f4d5fa9..401ceed 100644 --- a/msp_remoteadmin/hooks.py +++ b/msp_remoteadmin/hooks.py @@ -215,3 +215,6 @@ app_license = "MIT" # auth_hooks = [ # "msp_remoteadmin.auth.validate" # ] +app_include_js = [ + "/assets/msp_remoteadmin/js/msp_remote.js" +] \ No newline at end of file diff --git a/msp_remoteadmin/public/js/msp_remote.js b/msp_remoteadmin/public/js/msp_remote.js new file mode 100644 index 0000000..cb27896 --- /dev/null +++ b/msp_remoteadmin/public/js/msp_remote.js @@ -0,0 +1,32 @@ +frappe.ui.form.on('IT Object', { + refresh: function (frm) { + // Verifica se todos os campos necessários estão preenchidos + if (frm.doc.link) { + // Adiciona o botão "Connect" + frm.add_custom_button(__('Connect'), null, 'btn-default', null, 'btn-connect'); + frm.page.add_menu_item(__('RDP'), function () { + connect_remote(frm, 'RDP'); + }, 'btn-connect'); + + frm.page.add_menu_item(__('SSH'), function () { + connect_remote(frm, 'SSH'); + }, 'btn-connect'); + } + } +}); + +function connect_remote(frm, type) { + frappe.call({ + method: "msp_removeadmin.tools.create_session", + args: { + doc: frm.doc, + protocol: type + }, + callback: function (r) { + if (r.message) { + var url = r.message; + window.open(url, 'GuacamoleConsole', 'width=1024,height=768'); + } + } + }) +} diff --git a/msp_remoteadmin/tools.py b/msp_remoteadmin/tools.py new file mode 100644 index 0000000..6c2df20 --- /dev/null +++ b/msp_remoteadmin/tools.py @@ -0,0 +1,41 @@ +# Copyright (c) 2024, Luiz Costa and contributors +# For license information, please see license.txt + +import frappe +import requests +import urllib.parse + +PROTOCOL_PORT = { + "SSH": 22, + "RDP": 3389 +} + +@frappe.whitelist() +def create_session(doc, protocol): + # DEBUG + print(doc) + + guaca_config = frappe.get_single('Remote Connections Settings') + guacamole_url = f'{guaca_config.guacamole_server}/api/tokens' + auth = { + 'username': guaca_config.guacamole_user, + 'password': guaca_config.get_password('guacamole_pass') + } + response = requests.post(guacamole_url, data=auth) + if response.status_code == 200: + try: + token = response.json()['authToken'] + except: + print("Error: Could not get token") + token = None + if token: + # Get credentials from IT User Account + acc_doc = frappe.get_doc('IT User Account', doc.link) + username = acc_doc.username + password = acc_doc.get_password('password') + + uri = urllib.parse.quote_plus(f"{protocol}://{username if username else ''}{':' + password if password else ''}@{doc.main_ip}{':' + PROTOCOL_PORT[protocol]}").lower() + if protocol == 'RDP': + uri = f"{uri}/?ignore-cert=true&disable-audio=true" + url = f'{guacamole_url}/?#/?token={token}&quickconnect={uri}' + return url