fixes for transports

This commit is contained in:
Adolfo Gómez García 2022-10-16 18:46:56 +02:00
parent 65d38d8722
commit 4f4f1f24fd
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
12 changed files with 43 additions and 57 deletions

View File

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

View File

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

View File

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

View File

@ -194,4 +194,4 @@ class RDPTransport(BaseRDPTransport):
}
)
return self.getScript('scripts/{}/direct.py', osName, sp)
return self.getScript(osName, 'direct', sp)

View File

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

View File

@ -243,4 +243,4 @@ class TRDPTransport(BaseRDPTransport):
}
)
return self.getScript('scripts/{}/tunnel.py', osName, sp)
return self.getScript(osName, 'tunnel', sp)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -200,4 +200,4 @@ class TX2GOTransport(BaseX2GOTransport):
'xf': xf,
}
return self.getScript('scripts/{}/tunnel.py', osName, sp)
return self.getScript(osName, 'tunnel', sp)