From 4f4f1f24fd44b4d310010cc4db42af00ec65bf3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Sun, 16 Oct 2022 18:46:56 +0200 Subject: [PATCH] fixes for transports --- server/src/uds/core/transports/transport.py | 29 +++++++++++++++++++ server/src/uds/core/util/tools.py | 2 +- server/src/uds/models/service_pool.py | 10 +++---- server/src/uds/transports/RDP/rdp.py | 2 +- server/src/uds/transports/RDP/rdp_base.py | 17 ----------- server/src/uds/transports/RDP/rdptunnel.py | 2 +- server/src/uds/transports/SPICE/spice.py | 4 ++- server/src/uds/transports/SPICE/spice_base.py | 14 --------- .../src/uds/transports/SPICE/spice_tunnel.py | 2 +- server/src/uds/transports/X2GO/x2go.py | 2 +- server/src/uds/transports/X2GO/x2go_base.py | 14 --------- server/src/uds/transports/X2GO/x2go_tunnel.py | 2 +- 12 files changed, 43 insertions(+), 57 deletions(-) diff --git a/server/src/uds/core/transports/transport.py b/server/src/uds/core/transports/transport.py index 74758237..cccf961c 100644 --- a/server/src/uds/core/transports/transport.py +++ b/server/src/uds/core/transports/transport.py @@ -30,6 +30,8 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ +import os +import sys import codecs import logging import typing @@ -267,6 +269,33 @@ class Transport(Module): params, ) + def getRelativeScript( + self, scriptName: str, params: typing.Mapping[str, typing.Any] + ) -> typing.Tuple[str, str, typing.Mapping[str, typing.Any]]: + """Returns a script that will be executed on client, but will be downloaded from server + + Args: + scriptName: Name of the script to be downloaded, relative path (i.e. 'scripts/direct/transport.py') + params: Parameters for the return tuple + """ + # Reads script and signature + basePath = os.path.dirname(sys.modules[self.__module__].__file__ or 'not_found') # Will raise if not found + + script = open(os.path.join(basePath, scriptName), 'r').read() + signature = open(os.path.join(basePath, scriptName + '.signature'), 'r').read() + + return script, signature, params + + def getScript( + self, osName: str, type: typing.Literal['tunnel', 'direct'], params: typing.Mapping[str, typing.Any] + ) -> typing.Tuple[str, str, typing.Mapping[str, typing.Any]]: + """ + Returns a script for the given os and type + """ + return self.getRelativeScript( + 'scripts/{os}/{type}.py'.format(os=osName, type=type), params + ) + def getLink( self, userService: 'models.UserService', diff --git a/server/src/uds/core/util/tools.py b/server/src/uds/core/util/tools.py index 144091f9..c815434e 100644 --- a/server/src/uds/core/util/tools.py +++ b/server/src/uds/core/util/tools.py @@ -130,7 +130,7 @@ def packageRelativeFile(moduleName: str, fileName: str) -> str: This allows to keep images alongside report """ mod = sys.modules[moduleName] - if mod and mod.__file__: + if mod and hasattr(mod, '__file__') and mod.__file__: pkgpath = os.path.dirname(mod.__file__) return os.path.join(pkgpath, fileName) # Not found, return fileName diff --git a/server/src/uds/models/service_pool.py b/server/src/uds/models/service_pool.py index 7ba6d33d..ed3a0009 100644 --- a/server/src/uds/models/service_pool.py +++ b/server/src/uds/models/service_pool.py @@ -83,14 +83,14 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore name = models.CharField(max_length=128, default='') short_name = models.CharField(max_length=32, default='') comments = models.CharField(max_length=256, default='') - service: 'models.ForeignKey[ServicePool, Service]' = models.ForeignKey( + service: 'models.ForeignKey[Service|None]' = models.ForeignKey( Service, null=True, blank=True, related_name='deployedServices', on_delete=models.CASCADE, ) - osmanager: 'models.ForeignKey[ServicePool, OSManager]' = models.ForeignKey( + osmanager: 'models.ForeignKey[OSManager|None]' = models.ForeignKey( OSManager, null=True, blank=True, @@ -114,7 +114,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore ignores_unused = models.BooleanField(default=False) - image: 'models.ForeignKey[ServicePool, Image]' = models.ForeignKey( + image: 'models.ForeignKey[Image|None]' = models.ForeignKey( Image, null=True, blank=True, @@ -122,7 +122,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore on_delete=models.SET_NULL, ) - servicesPoolGroup: 'models.ForeignKey[ServicePool, ServicePoolGroup]' = ( + servicesPoolGroup: 'models.ForeignKey[ServicePoolGroup|None]' = ( models.ForeignKey( ServicePoolGroup, null=True, @@ -138,7 +138,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore fallbackAccess = models.CharField(default=states.action.ALLOW, max_length=8) # Usage accounting - account: 'models.ForeignKey[ServicePool, Account]' = models.ForeignKey( + account: 'models.ForeignKey[Account|None]' = models.ForeignKey( Account, null=True, blank=True, diff --git a/server/src/uds/transports/RDP/rdp.py b/server/src/uds/transports/RDP/rdp.py index 7cde5ba8..6528464c 100644 --- a/server/src/uds/transports/RDP/rdp.py +++ b/server/src/uds/transports/RDP/rdp.py @@ -194,4 +194,4 @@ class RDPTransport(BaseRDPTransport): } ) - return self.getScript('scripts/{}/direct.py', osName, sp) + return self.getScript(osName, 'direct', sp) diff --git a/server/src/uds/transports/RDP/rdp_base.py b/server/src/uds/transports/RDP/rdp_base.py index b8cbabb5..dec8e74f 100644 --- a/server/src/uds/transports/RDP/rdp_base.py +++ b/server/src/uds/transports/RDP/rdp_base.py @@ -433,20 +433,3 @@ class BaseRDPTransport(transports.Transport): password, altUsername=username, ) - - def getScript( - self, - scriptNameTemplate: str, - osName: str, - params: typing.Mapping[str, typing.Any], - ) -> typing.Tuple[str, str, typing.Mapping[str, typing.Any]]: - # Reads script - scriptNameTemplate = scriptNameTemplate.format(osName) - with open(os.path.join(os.path.dirname(__file__), scriptNameTemplate)) as f: - script = f.read() - # Reads signature - with open( - os.path.join(os.path.dirname(__file__), scriptNameTemplate + '.signature') - ) as f: - signature = f.read() - return script, signature, params diff --git a/server/src/uds/transports/RDP/rdptunnel.py b/server/src/uds/transports/RDP/rdptunnel.py index c9ceee2a..15cb23b4 100644 --- a/server/src/uds/transports/RDP/rdptunnel.py +++ b/server/src/uds/transports/RDP/rdptunnel.py @@ -243,4 +243,4 @@ class TRDPTransport(BaseRDPTransport): } ) - return self.getScript('scripts/{}/tunnel.py', osName, sp) + return self.getScript(osName, 'tunnel', sp) diff --git a/server/src/uds/transports/SPICE/spice.py b/server/src/uds/transports/SPICE/spice.py index dcf2f86f..28d01dcc 100644 --- a/server/src/uds/transports/SPICE/spice.py +++ b/server/src/uds/transports/SPICE/spice.py @@ -95,6 +95,8 @@ class SPICETransport(BaseSpiceTransport): con['cert_subject'], fullscreen=self.fullScreen.isTrue(), ) + r.proxy = con.get('proxy', None) + r.usb_auto_share = self.usbShare.isTrue() r.new_usb_auto_share = self.autoNewUsbShare.isTrue() r.smartcard = self.smartCardRedirect.isTrue() @@ -117,4 +119,4 @@ class SPICETransport(BaseSpiceTransport): 'as_file': r.as_file, } - return self.getScript('scripts/{}/direct.py', osName, sp) + return self.getScript(osName, 'direct', sp) diff --git a/server/src/uds/transports/SPICE/spice_base.py b/server/src/uds/transports/SPICE/spice_base.py index 005ff65c..a271b49e 100644 --- a/server/src/uds/transports/SPICE/spice_base.py +++ b/server/src/uds/transports/SPICE/spice_base.py @@ -197,17 +197,3 @@ class BaseSpiceTransport(transports.Transport): password: str, ) -> typing.Dict[str, str]: return self.processUserPassword(userService, user, password) - - def getScript( - self, scriptNameTemplate: str, osName: str, params: typing.Dict[str, typing.Any] - ) -> typing.Tuple[str, str, typing.Dict[str, typing.Any]]: - # Reads script - scriptNameTemplate = scriptNameTemplate.format(osName) - with open(os.path.join(os.path.dirname(__file__), scriptNameTemplate)) as f: - script = f.read() - # Reads signature - with open( - os.path.join(os.path.dirname(__file__), scriptNameTemplate + '.signature') - ) as f: - signature = f.read() - return script, signature, params diff --git a/server/src/uds/transports/SPICE/spice_tunnel.py b/server/src/uds/transports/SPICE/spice_tunnel.py index 3b3554a0..89dfdc3b 100644 --- a/server/src/uds/transports/SPICE/spice_tunnel.py +++ b/server/src/uds/transports/SPICE/spice_tunnel.py @@ -202,4 +202,4 @@ class TSPICETransport(BaseSpiceTransport): 'ticket_secure': ticket_secure, } - return self.getScript('scripts/{}/tunnel.py', osName, sp) + return self.getScript(osName, 'tunnel', sp) diff --git a/server/src/uds/transports/X2GO/x2go.py b/server/src/uds/transports/X2GO/x2go.py index b73867af..c1312090 100644 --- a/server/src/uds/transports/X2GO/x2go.py +++ b/server/src/uds/transports/X2GO/x2go.py @@ -118,4 +118,4 @@ class X2GOTransport(BaseX2GOTransport): sp = {'ip': ip, 'port': '22', 'key': priv, 'xf': xf} - return self.getScript('scripts/{}/direct.py', osName, sp) + return self.getScript(osName, 'direct', sp) diff --git a/server/src/uds/transports/X2GO/x2go_base.py b/server/src/uds/transports/X2GO/x2go_base.py index 70d73c58..e5af082c 100644 --- a/server/src/uds/transports/X2GO/x2go_base.py +++ b/server/src/uds/transports/X2GO/x2go_base.py @@ -283,17 +283,3 @@ class BaseX2GOTransport(transports.Transport): authScript = self.getAuthorizeScript(userName, pub) userServiceManager().sendScript(userService, authScript) return priv, pub - - def getScript( - self, scriptNameTemplate: str, osName: str, params: typing.Dict[str, typing.Any] - ) -> typing.Tuple[str, str, typing.Dict[str, typing.Any]]: - # Reads script - scriptNameTemplate = scriptNameTemplate.format(osName) - with open(os.path.join(os.path.dirname(__file__), scriptNameTemplate)) as f: - script = f.read() - # Reads signature - with open( - os.path.join(os.path.dirname(__file__), scriptNameTemplate + '.signature') - ) as f: - signature = f.read() - return script, signature, params diff --git a/server/src/uds/transports/X2GO/x2go_tunnel.py b/server/src/uds/transports/X2GO/x2go_tunnel.py index 6f1d9262..ca7f1307 100644 --- a/server/src/uds/transports/X2GO/x2go_tunnel.py +++ b/server/src/uds/transports/X2GO/x2go_tunnel.py @@ -200,4 +200,4 @@ class TX2GOTransport(BaseX2GOTransport): 'xf': xf, } - return self.getScript('scripts/{}/tunnel.py', osName, sp) + return self.getScript(osName, 'tunnel', sp)