Changed OS Detection system

This commit is contained in:
Adolfo Gómez García 2022-02-15 14:45:11 +01:00
parent ccd429454e
commit 18d4147d59
16 changed files with 89 additions and 83 deletions

View File

@ -126,7 +126,7 @@ class Transports(ModelHandler):
'value': [],
'values': sorted(
[
{'id': x, 'text': x.replace('CrOS', 'Chrome OS')}
{'id': x.value[0], 'text': x.value[0].replace('CrOS', 'Chrome OS')}
for x in OsDetector.knownOss
],
key=lambda x: x['text'].lower(),

View File

@ -229,7 +229,7 @@ def __registerUser(
events.addEvent(
authenticator,
events.ET_PLATFORM,
platform=request.os['OS'],
platform=request.os['OS'].value[0],
browser=request.os['Browser'],
version=request.os['Version'],
)
@ -383,7 +383,7 @@ def webLogin(
user.name,
password,
get_language() or '',
request.os['OS'],
request.os['OS'].value[0],
user.is_admin,
user.staff_member,
cookie,
@ -460,7 +460,7 @@ def authLogLogin(
authenticator.name,
userName,
request.ip,
request.os['OS'],
request.os['OS'].value[0],
logStr,
request.META.get('HTTP_USER_AGENT', 'Undefined'),
]
@ -471,7 +471,7 @@ def authLogLogin(
authenticator,
level,
'user {} has {} from {} where os is {}'.format(
userName, logStr, request.ip, request.os['OS']
userName, logStr, request.ip, request.os['OS'].value[0]
),
log.WEB,
)
@ -481,7 +481,7 @@ def authLogLogin(
log.doLog(
user,
level,
'{} from {} where OS is {}'.format(logStr, request.ip, request.os['OS']),
'{} from {} where OS is {}'.format(logStr, request.ip, request.os['OS'].value[0]),
log.WEB,
)
except Exception:

View File

@ -152,12 +152,12 @@ class Transport(Module):
return False
@classmethod
def supportsOs(cls, osName: str) -> bool:
def supportsOs(cls, osType: OsDetector.KnownOS) -> bool:
"""
Helper method to check if transport supports requested operating system.
Class method
"""
return cls.supportedOss.count(osName) > 0
return cls.supportedOss.count(osType) > 0
@classmethod
def providesConnetionInfo(cls) -> bool:

View File

@ -30,44 +30,47 @@
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import enum
import re
import logging
import typing
from sympy import Li
from .tools import DictAsObj
logger = logging.getLogger(__name__)
# Knowns OSs
Linux = 'Linux'
ChromeOS = 'CrOS'
WindowsPhone = 'Windows Phone'
Windows = 'Windows'
Macintosh = 'Mac'
Android = 'Android'
iPad = 'iPad' #
iPhone = 'iPhone' # In fact, these are IOS both, but we can diferentiate it...
WYSE = 'WYSE'
Unknown = 'Unknown'
class KnownOS(enum.Enum):
Linux = ('Linux', 'armv7l')
ChromeOS = ('CrOS',)
WindowsPhone = ('Windows Phone',)
Windows = ('Windows',)
Macintosh = ('Mac',)
Android = ('Android',)
iPad = ('iPad',) #
iPhone = ('iPhone',) # In fact, these are IOS both, but we can diferentiate it...
WYSE = ('WYSE',)
Unknown = ('Unknown',)
knownOss = (
WindowsPhone,
Android,
Linux,
Windows,
iPad,
iPhone,
Macintosh,
ChromeOS,
WYSE,
KnownOS.WindowsPhone,
KnownOS.Android,
KnownOS.Linux,
KnownOS.Windows,
KnownOS.iPad,
KnownOS.iPhone,
KnownOS.Macintosh,
KnownOS.ChromeOS,
KnownOS.WYSE,
) # Android is linux also, so it is cheched on first place
allOss = knownOss + (Unknown,)
desktopOss = (Linux, Windows, Macintosh)
allOss = knownOss + (KnownOS.Unknown,)
desktopOss = (KnownOS.Linux, KnownOS.Windows, KnownOS.Macintosh)
mobilesODD = list(set(allOss) - set(desktopOss))
DEFAULT_OS = 'Windows'
DEFAULT_OS = KnownOS.Windows
# Known browsers
Firefox = 'Firefox'
@ -115,18 +118,18 @@ def getOsFromUA(
Basic OS Client detector (very basic indeed :-))
"""
if ua is None:
ua = Unknown
ua = KnownOS.Unknown.value[0]
os = Unknown
res = DictAsObj({'OS': os, 'Version': '0.0', 'Browser': 'unknown'})
res = DictAsObj({'OS': KnownOS.Unknown, 'Version': '0.0', 'Browser': 'unknown'})
found: bool = False
for os in knownOss:
try:
ua.index(os)
res.OS = os # type: ignore
if found:
break
for osName in os.value:
if osName in ua:
res.OS = os # type: ignore
found = True
break
except Exception:
pass
match = None

View File

@ -129,9 +129,12 @@ def packageRelativeFile(moduleName: str, fileName: str) -> str:
Helper to get image path from relative to a module.
This allows to keep images alongside report
"""
pkgpath = os.path.dirname(sys.modules[moduleName].__file__)
mod = sys.modules[moduleName]
if mod and mod.__file__:
pkgpath = os.path.dirname(mod.__file__)
return os.path.join(pkgpath, fileName)
# Not found, return fileName
return fileName
def timestampAsStr(stamp, format_='SHORT_DATETIME_FORMAT'):
"""

View File

@ -96,7 +96,7 @@ class RDPTransport(BaseRDPTransport):
userService: 'models.UserService',
transport: 'models.Transport',
ip: str,
os: typing.Dict[str, str],
os: typing.Dict[str, typing.Any],
user: 'models.User',
password: str,
request: 'HttpRequest',
@ -144,9 +144,9 @@ class RDPTransport(BaseRDPTransport):
r.enforcedShares = self.enforceDrives.value
osName = {
OsDetector.Windows: 'windows',
OsDetector.Linux: 'linux',
OsDetector.Macintosh: 'macosx',
OsDetector.KnownOS.Windows: 'windows',
OsDetector.KnownOS.Linux: 'linux',
OsDetector.KnownOS.Macintosh: 'macosx',
}.get(os['OS'])
if osName is None:

View File

@ -77,7 +77,7 @@ class RDPFile:
width: typing.Union[str, int],
height: typing.Union[str, int],
bpp: str,
target: str = OsDetector.Windows,
target: OsDetector.KnownOS = OsDetector.KnownOS.Windows,
):
self.width = str(width)
self.height = str(height)
@ -86,7 +86,7 @@ class RDPFile:
self.target = target
def get(self):
if self.target in (OsDetector.Windows, OsDetector.Linux, OsDetector.Macintosh):
if self.target in (OsDetector.KnownOS.Windows, OsDetector.KnownOS.Linux, OsDetector.KnownOS.Macintosh):
return self.getGeneric()
# Unknown target
return ''
@ -115,7 +115,7 @@ class RDPFile:
params.append('/smartcard')
if self.redirectAudio:
if self.alsa and self.target != OsDetector.Macintosh:
if self.alsa and self.target != OsDetector.KnownOS.Macintosh:
params.append('/sound:sys:alsa,format:1,quality:high')
params.append('/microphone:sys:alsa')
else:
@ -126,7 +126,7 @@ class RDPFile:
params.append('/video')
if self.redirectDrives != 'false':
if self.target in (OsDetector.Linux, OsDetector.Macintosh):
if self.target in (OsDetector.KnownOS.Linux, OsDetector.KnownOS.Macintosh):
params.append('/drive:home,$HOME')
else:
params.append('/drive:Users,/Users')
@ -152,7 +152,7 @@ class RDPFile:
params.append('/multimon')
if self.fullScreen:
if self.target != OsDetector.Macintosh:
if self.target != OsDetector.KnownOS.Macintosh:
params.append('/f')
else: # On mac, will fix this later...
params.append('/w:#WIDTH#')
@ -221,7 +221,7 @@ class RDPFile:
if self.username:
res += 'username:s:' + self.username + '\n'
res += 'domain:s:' + self.domain + '\n'
if self.target == OsDetector.Windows:
if self.target == OsDetector.KnownOS.Windows:
res += 'password 51:b:' + password + '\n'
res += 'alternate shell:s:' + '\n'

View File

@ -140,7 +140,7 @@ class TRDPTransport(BaseRDPTransport):
userService: 'models.UserService',
transport: 'models.Transport',
ip: str,
os: typing.Dict[str, str],
os: typing.Dict[str, typing.Any],
user: 'models.User',
password: str,
request: 'HttpRequest',
@ -197,9 +197,9 @@ class TRDPTransport(BaseRDPTransport):
r.enforcedShares = self.enforceDrives.value
osName = {
OsDetector.Windows: 'windows',
OsDetector.Linux: 'linux',
OsDetector.Macintosh: 'macosx',
OsDetector.KnownOS.Windows: 'windows',
OsDetector.KnownOS.Linux: 'linux',
OsDetector.KnownOS.Macintosh: 'macosx',
}.get(os['OS'])
if osName is None:

View File

@ -65,12 +65,12 @@ class SPICETransport(BaseSpiceTransport):
autoNewUsbShare = BaseSpiceTransport.autoNewUsbShare
smartCardRedirect = BaseSpiceTransport.smartCardRedirect
def getUDSTransportScript( # pylint: disable=too-many-locals
def getUDSTransportScript(
self,
userService: 'models.UserService',
transport: 'models.Transport',
ip: str,
os: typing.Dict[str, str],
os: typing.Dict[str, typing.Any],
user: 'models.User',
password: str,
request: 'HttpRequest',
@ -100,9 +100,9 @@ class SPICETransport(BaseSpiceTransport):
r.smartcard = self.smartCardRedirect.isTrue()
osName = {
OsDetector.Windows: 'windows',
OsDetector.Linux: 'linux',
OsDetector.Macintosh: 'macosx',
OsDetector.KnownOS.Windows: 'windows',
OsDetector.KnownOS.Linux: 'linux',
OsDetector.KnownOS.Macintosh: 'macosx',
}.get(os['OS'])
if osName is None:

View File

@ -112,7 +112,7 @@ class TSPICETransport(BaseSpiceTransport):
userService: 'models.UserService',
transport: 'models.Transport',
ip: str,
os: typing.Dict[str, str],
os: typing.Dict[str, typing.Any],
user: 'models.User',
password: str,
request: 'HttpRequest',
@ -155,9 +155,9 @@ class TSPICETransport(BaseSpiceTransport):
r.smartcard = self.smartCardRedirect.isTrue()
osName = {
OsDetector.Windows: 'windows',
OsDetector.Linux: 'linux',
OsDetector.Macintosh: 'macosx',
OsDetector.KnownOS.Windows: 'windows',
OsDetector.KnownOS.Linux: 'linux',
OsDetector.KnownOS.Macintosh: 'macosx',
}.get(os['OS'])
if osName is None:

View File

@ -74,7 +74,7 @@ class X2GOTransport(BaseX2GOTransport):
userService: 'models.UserService',
transport: 'models.Transport',
ip: str,
os: typing.Dict[str, str],
os: typing.Dict[str, typing.Any],
user: 'models.User',
password: str,
request: 'HttpRequest',
@ -106,8 +106,8 @@ class X2GOTransport(BaseX2GOTransport):
)
osName = {
OsDetector.Windows: 'windows',
OsDetector.Linux: 'linux',
OsDetector.KnownOS.Windows: 'windows',
OsDetector.KnownOS.Linux: 'linux',
# OsDetector.Macintosh: 'macosx'
}.get(os['OS'])

View File

@ -63,7 +63,7 @@ class BaseX2GOTransport(transports.Transport):
iconFile = 'x2go.png'
protocol = transports.protocols.X2GO
supportedOss = (OsDetector.Linux, OsDetector.Windows)
supportedOss = (OsDetector.KnownOS.Linux, OsDetector.KnownOS.Windows)
fixedName = gui.TextField(
order=2,

View File

@ -121,7 +121,7 @@ class TX2GOTransport(BaseX2GOTransport):
userService: 'models.UserService',
transport: 'models.Transport',
ip: str,
os: typing.Dict[str, str],
os: typing.Dict[str, typing.Any],
user: 'models.User',
password: str,
request: 'HttpRequest',
@ -180,8 +180,8 @@ class TX2GOTransport(BaseX2GOTransport):
m = tools.DictAsObj(data)
osName = {
OsDetector.Windows: 'windows',
OsDetector.Linux: 'linux',
OsDetector.KnownOS.Windows: 'windows',
OsDetector.KnownOS.Linux: 'linux',
# OsDetector.Macintosh: 'macosx'
}.get(os['OS'])

View File

@ -151,7 +151,7 @@ def udsJs(request: 'ExtendedHttpRequest') -> str:
getAuthInfo(auth) for auth in authenticators if auth.getType()
],
'tag': tag,
'os': request.os['OS'],
'os': request.os['OS'].value[0],
'csrf_field': CSRF_FIELD,
'csrf': csrf_token,
'image_size': Image.MAX_IMAGE_SIZE,

View File

@ -97,8 +97,8 @@ def getServicesData(
nets = ''
validTrans = ''
osName = request.os['OS']
logger.debug('OS: %s', osName)
osType = request.os['OS']
logger.debug('OS: %s', osType)
if request.user.isStaff():
nets = ','.join([n.name for n in Network.networksFor(request.ip)])
@ -120,8 +120,8 @@ def getServicesData(
if (
typeTrans
and t.validForIp(request.ip)
and typeTrans.supportsOs(osName)
and t.validForOs(osName)
and typeTrans.supportsOs(osType)
and t.validForOs(osType)
):
yield t
except Exception as e:
@ -200,8 +200,8 @@ def getServicesData(
if (
typeTrans
and t.validForIp(request.ip)
and typeTrans.supportsOs(osName)
and t.validForOs(osName)
and typeTrans.supportsOs(osType)
and t.validForOs(osType)
):
metaTransports = [
{
@ -272,8 +272,8 @@ def getServicesData(
if (
typeTrans
and t.validForIp(request.ip)
and typeTrans.supportsOs(osName)
and t.validForOs(osName)
and typeTrans.supportsOs(osType)
and t.validForOs(osType)
):
if typeTrans.ownLink:
link = reverse('TransportOwnLink', args=('F' + sPool.uuid, t.uuid))

View File

@ -283,7 +283,7 @@ def ticketAuth(
except Authenticator.DoesNotExist:
logger.error('Ticket has an non existing authenticator')
return errors.errorView(request, errors.ACCESS_DENIED)
except ServicePool.DoesNotExist:
except ServicePool.DoesNotExist: # type: ignore # DoesNotExist is different for each model
logger.error('Ticket has an invalid Service Pool')
return errors.errorView(request, errors.SERVICE_NOT_FOUND)
except Exception as e: