2024-06-03 11:40:58 +01:00

43 lines
1.4 KiB
Python

# 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.name)
print(acc_doc)
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