2017-12-12 04:19:45 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2022-11-17 12:48:50 +00:00
|
|
|
from collections import defaultdict
|
2017-12-12 04:19:45 +00:00
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
from django.db.models import TextChoices
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2019-12-05 07:09:25 +00:00
|
|
|
|
2022-11-16 13:05:15 +00:00
|
|
|
from assets.const import Protocol
|
|
|
|
|
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
# Replay & Command Storage Choices
|
|
|
|
# --------------------------------
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
|
2022-11-04 03:40:16 +00:00
|
|
|
class ReplayStorageType(TextChoices):
|
2021-01-12 10:06:42 +00:00
|
|
|
null = 'null', 'Null',
|
|
|
|
server = 'server', 'Server'
|
|
|
|
s3 = 's3', 'S3'
|
|
|
|
ceph = 'ceph', 'Ceph'
|
|
|
|
swift = 'swift', 'Swift'
|
|
|
|
oss = 'oss', 'OSS'
|
|
|
|
azure = 'azure', 'Azure'
|
2021-04-12 11:37:04 +00:00
|
|
|
obs = 'obs', 'OBS'
|
2022-02-28 11:28:58 +00:00
|
|
|
cos = 'cos', 'COS'
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
|
2022-11-04 03:40:16 +00:00
|
|
|
class CommandStorageType(TextChoices):
|
2021-01-12 10:06:42 +00:00
|
|
|
null = 'null', 'Null',
|
|
|
|
server = 'server', 'Server'
|
|
|
|
es = 'es', 'Elasticsearch'
|
2020-12-10 12:50:22 +00:00
|
|
|
|
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
# Component Status Choices
|
|
|
|
# ------------------------
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2022-11-04 03:40:16 +00:00
|
|
|
class ComponentLoad(TextChoices):
|
2020-12-10 12:50:22 +00:00
|
|
|
critical = 'critical', _('Critical')
|
|
|
|
high = 'high', _('High')
|
|
|
|
normal = 'normal', _('Normal')
|
2021-03-26 11:09:34 +00:00
|
|
|
offline = 'offline', _('Offline')
|
2020-12-10 12:50:22 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def status(cls):
|
|
|
|
return set(dict(cls.choices).keys())
|
|
|
|
|
|
|
|
|
2022-11-17 12:48:50 +00:00
|
|
|
class HttpMethod(TextChoices):
|
|
|
|
web_gui = 'web_gui', 'Web GUI'
|
|
|
|
web_cli = 'web_cli', 'Web CLI'
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2022-11-16 13:05:15 +00:00
|
|
|
|
2022-11-17 12:48:50 +00:00
|
|
|
class NativeClient(TextChoices):
|
2022-11-16 13:05:15 +00:00
|
|
|
# Koko
|
|
|
|
ssh = 'ssh', 'ssh'
|
|
|
|
putty = 'putty', 'PuTTY'
|
|
|
|
xshell = 'xshell', 'Xshell'
|
|
|
|
|
|
|
|
# Magnus
|
|
|
|
mysql = 'mysql', 'MySQL Client'
|
|
|
|
psql = 'psql', 'psql'
|
|
|
|
sqlplus = 'sqlplus', 'sqlplus'
|
|
|
|
redis = 'redis-cli', 'redis-cli'
|
2022-11-17 12:48:50 +00:00
|
|
|
mongodb = 'mongo', 'mongo'
|
2022-11-16 13:05:15 +00:00
|
|
|
|
|
|
|
# Razor
|
|
|
|
mstsc = 'mstsc', 'Remote Desktop'
|
|
|
|
|
|
|
|
@classmethod
|
2022-11-17 12:48:50 +00:00
|
|
|
def get_native_clients(cls):
|
2022-11-17 03:46:35 +00:00
|
|
|
clients = {
|
2022-11-17 12:48:50 +00:00
|
|
|
Protocol.ssh: {
|
|
|
|
'default': [cls.ssh],
|
|
|
|
'windows': [cls.putty],
|
|
|
|
},
|
|
|
|
Protocol.rdp: [cls.mstsc],
|
|
|
|
Protocol.mysql: [cls.mysql],
|
|
|
|
Protocol.oracle: [cls.sqlplus],
|
|
|
|
Protocol.postgresql: [cls.psql],
|
|
|
|
Protocol.redis: [cls.redis],
|
|
|
|
Protocol.mongodb: [cls.mongodb],
|
2022-11-17 03:46:35 +00:00
|
|
|
}
|
2022-11-17 12:48:50 +00:00
|
|
|
return clients
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_native_methods(cls, os='windows'):
|
|
|
|
clients_map = cls.get_native_clients()
|
|
|
|
methods = defaultdict(list)
|
|
|
|
|
|
|
|
for protocol, _clients in clients_map.items():
|
|
|
|
if isinstance(_clients, dict):
|
|
|
|
_clients = _clients.get(os, _clients['default'])
|
|
|
|
for client in _clients:
|
|
|
|
methods[protocol].append({
|
|
|
|
'value': client.value,
|
|
|
|
'label': client.label,
|
|
|
|
'type': 'native',
|
|
|
|
})
|
|
|
|
return methods
|
2022-11-17 03:46:35 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_launch_command(cls, name, os='windows'):
|
|
|
|
commands = {
|
2022-11-16 13:05:15 +00:00
|
|
|
'ssh': 'ssh {username}@{hostname} -p {port}',
|
|
|
|
'putty': 'putty -ssh {username}@{hostname} -P {port}',
|
|
|
|
'xshell': '-url ssh://root:passwd@192.168.10.100',
|
|
|
|
'mysql': 'mysql -h {hostname} -P {port} -u {username} -p',
|
|
|
|
'psql': {
|
|
|
|
'default': 'psql -h {hostname} -p {port} -U {username} -W',
|
|
|
|
'windows': 'psql /h {hostname} /p {port} /U {username} -W',
|
|
|
|
},
|
|
|
|
'sqlplus': 'sqlplus {username}/{password}@{hostname}:{port}',
|
|
|
|
'redis': 'redis-cli -h {hostname} -p {port} -a {password}',
|
|
|
|
'mstsc': 'mstsc /v:{hostname}:{port}',
|
|
|
|
}
|
2022-11-17 03:46:35 +00:00
|
|
|
command = commands.get(name)
|
|
|
|
if isinstance(command, dict):
|
|
|
|
command = command.get(os, command.get('default'))
|
|
|
|
return command
|
|
|
|
|
|
|
|
|
|
|
|
class RemoteAppMethod:
|
|
|
|
@classmethod
|
2022-11-17 12:48:50 +00:00
|
|
|
def get_remote_app_methods(cls):
|
2022-11-17 03:46:35 +00:00
|
|
|
from .models import Applet
|
2022-11-17 12:48:50 +00:00
|
|
|
applets = Applet.objects.all()
|
|
|
|
methods = defaultdict(list)
|
|
|
|
for applet in applets:
|
|
|
|
for protocol in applet.protocols:
|
|
|
|
methods[protocol].append({
|
|
|
|
'value': applet.name,
|
|
|
|
'label': applet.display_name,
|
|
|
|
'icon': applet.icon,
|
|
|
|
'type': 'remote_app',
|
|
|
|
})
|
|
|
|
return methods
|
2022-11-16 13:05:15 +00:00
|
|
|
|
|
|
|
|
2022-11-17 12:48:50 +00:00
|
|
|
class TerminalType(TextChoices):
|
|
|
|
koko = 'koko', 'KoKo'
|
|
|
|
guacamole = 'guacamole', 'Guacamole'
|
|
|
|
omnidb = 'omnidb', 'OmniDB'
|
|
|
|
xrdp = 'xrdp', 'Xrdp'
|
|
|
|
lion = 'lion', 'Lion'
|
|
|
|
core = 'core', 'Core'
|
|
|
|
celery = 'celery', 'Celery'
|
|
|
|
magnus = 'magnus', 'Magnus'
|
|
|
|
razor = 'razor', 'Razor'
|
|
|
|
tinker = 'tinker', 'Tinker'
|
2022-11-16 13:05:15 +00:00
|
|
|
|
|
|
|
@classmethod
|
2022-11-17 12:48:50 +00:00
|
|
|
def types(cls):
|
|
|
|
return set(dict(cls.choices).keys())
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def protocols(cls):
|
2022-11-16 13:05:15 +00:00
|
|
|
return {
|
2022-11-17 12:48:50 +00:00
|
|
|
cls.koko: {
|
|
|
|
'http_method': HttpMethod.web_cli,
|
|
|
|
'listen': [Protocol.ssh, Protocol.http],
|
|
|
|
'support': [
|
|
|
|
Protocol.ssh, Protocol.telnet,
|
|
|
|
Protocol.mysql, Protocol.postgresql,
|
|
|
|
Protocol.oracle, Protocol.sqlserver,
|
|
|
|
Protocol.mariadb, Protocol.redis,
|
|
|
|
Protocol.mongodb,
|
|
|
|
],
|
|
|
|
'match': 'm2m'
|
|
|
|
},
|
|
|
|
cls.omnidb: {
|
|
|
|
'http_method': HttpMethod.web_gui,
|
|
|
|
'listen': [Protocol.http],
|
|
|
|
'support': [
|
|
|
|
Protocol.mysql, Protocol.postgresql, Protocol.oracle,
|
|
|
|
Protocol.sqlserver, Protocol.mariadb
|
|
|
|
],
|
|
|
|
'match': 'm2m'
|
|
|
|
},
|
|
|
|
cls.lion: {
|
|
|
|
'http_method': HttpMethod.web_gui,
|
|
|
|
'listen': [Protocol.http],
|
|
|
|
'support': [Protocol.rdp, Protocol.vnc],
|
|
|
|
'match': 'm2m'
|
|
|
|
},
|
|
|
|
cls.magnus: {
|
|
|
|
'listen': [],
|
|
|
|
'support': [
|
|
|
|
Protocol.mysql, Protocol.postgresql, Protocol.oracle,
|
|
|
|
Protocol.mariadb
|
|
|
|
],
|
|
|
|
'match': 'map'
|
|
|
|
},
|
|
|
|
cls.razor: {
|
|
|
|
'listen': [Protocol.rdp],
|
|
|
|
'support': [Protocol.rdp],
|
|
|
|
'match': 'map'
|
|
|
|
}
|
2022-11-16 13:05:15 +00:00
|
|
|
}
|
2022-11-17 12:48:50 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_protocols_connect_methods(cls, os):
|
|
|
|
methods = defaultdict(list)
|
|
|
|
native_methods = NativeClient.get_native_methods(os)
|
|
|
|
remote_app_methods = RemoteAppMethod.get_remote_app_methods()
|
|
|
|
|
|
|
|
for component, component_protocol in cls.protocols().items():
|
|
|
|
component_methods = defaultdict(list)
|
|
|
|
support = component_protocol['support']
|
|
|
|
|
|
|
|
for protocol in support:
|
|
|
|
if component_protocol['match'] == 'map':
|
|
|
|
listen = [protocol]
|
|
|
|
else:
|
|
|
|
listen = component_protocol['listen']
|
|
|
|
|
|
|
|
for listen_protocol in listen:
|
|
|
|
if listen_protocol == Protocol.http:
|
|
|
|
web_protocol = component_protocol['http_method']
|
|
|
|
component_methods[protocol.value].append({
|
|
|
|
'value': web_protocol.value,
|
|
|
|
'label': web_protocol.label,
|
|
|
|
'type': 'web',
|
|
|
|
})
|
|
|
|
|
|
|
|
# Native method
|
|
|
|
component_methods[protocol.value].extend(native_methods[listen_protocol])
|
|
|
|
component_methods[protocol.value].extend(remote_app_methods[listen_protocol])
|
|
|
|
|
|
|
|
for protocol, _methods in component_methods.items():
|
|
|
|
for method in _methods:
|
|
|
|
method['component'] = component.value
|
|
|
|
methods[protocol].extend(_methods)
|
|
|
|
return methods
|