implement manage of remote

This commit is contained in:
Luiz Costa 2024-06-03 11:27:44 +01:00
parent 7186d61d11
commit d5e47e1a91
3 changed files with 76 additions and 0 deletions

View File

@ -215,3 +215,6 @@ app_license = "MIT"
# auth_hooks = [
# "msp_remoteadmin.auth.validate"
# ]
app_include_js = [
"/assets/msp_remoteadmin/js/msp_remote.js"
]

View File

@ -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');
}
}
})
}

41
msp_remoteadmin/tools.py Normal file
View File

@ -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