2022-12-07 07:09:01 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2023-06-07 09:39:56 +00:00
|
|
|
import itertools
|
2022-12-07 07:09:01 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2022-12-16 10:37:27 +00:00
|
|
|
from django.conf import settings
|
2022-12-07 07:09:01 +00:00
|
|
|
from django.db.models import TextChoices
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-12-07 07:09:01 +00:00
|
|
|
|
|
|
|
from assets.const import Protocol
|
|
|
|
from .const import TerminalType
|
|
|
|
|
|
|
|
|
|
|
|
class WebMethod(TextChoices):
|
|
|
|
web_gui = 'web_gui', 'Web GUI'
|
|
|
|
web_cli = 'web_cli', 'Web CLI'
|
|
|
|
web_sftp = 'web_sftp', 'Web SFTP'
|
|
|
|
|
|
|
|
@classmethod
|
2023-07-11 11:29:56 +00:00
|
|
|
def get_spec_methods(cls):
|
2023-02-24 07:09:17 +00:00
|
|
|
methods = {
|
2023-07-25 09:12:06 +00:00
|
|
|
Protocol.sftp: [cls.web_sftp]
|
2022-12-07 07:09:01 +00:00
|
|
|
}
|
2023-02-24 07:09:17 +00:00
|
|
|
return methods
|
2022-12-07 07:09:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NativeClient(TextChoices):
|
|
|
|
# Koko
|
2023-08-09 14:59:53 +00:00
|
|
|
ssh_client = 'ssh_client', _('SSH Client')
|
|
|
|
ssh_guide = 'ssh_guide', _('SSH Guide')
|
|
|
|
sftp_client = 'sftp_client', _('SFTP Client')
|
2022-12-07 07:09:01 +00:00
|
|
|
# Magnus
|
2023-08-09 14:59:53 +00:00
|
|
|
db_guide = 'db_guide', _('DB Guide')
|
2023-06-07 09:39:56 +00:00
|
|
|
db_client = 'db_client', _('DB Client')
|
2022-12-07 07:09:01 +00:00
|
|
|
# Razor
|
2023-08-09 14:59:53 +00:00
|
|
|
mstsc = 'mstsc', _('Remote Desktop')
|
2024-04-07 04:11:44 +00:00
|
|
|
rdp_guide = 'rdp_guide', _('RDP Guide')
|
2022-12-07 07:09:01 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_native_clients(cls):
|
|
|
|
# native client 关注的是 endpoint 的 protocol,
|
|
|
|
# 比如 telnet mysql, koko 都支持,到那时暴露的是 ssh 协议
|
|
|
|
clients = {
|
2023-08-09 14:59:53 +00:00
|
|
|
Protocol.ssh: [cls.ssh_client, cls.ssh_guide],
|
|
|
|
Protocol.sftp: [cls.sftp_client],
|
2022-12-07 07:09:01 +00:00
|
|
|
Protocol.rdp: [cls.mstsc],
|
2023-08-09 14:59:53 +00:00
|
|
|
Protocol.mysql: [cls.db_client, cls.db_guide],
|
|
|
|
Protocol.mariadb: [cls.db_client, cls.db_guide],
|
|
|
|
Protocol.redis: [cls.db_client, cls.db_guide],
|
|
|
|
Protocol.mongodb: [cls.db_client, cls.db_guide],
|
|
|
|
Protocol.oracle: [cls.db_client, cls.db_guide],
|
|
|
|
Protocol.postgresql: [cls.db_client, cls.db_guide],
|
2023-12-11 05:57:35 +00:00
|
|
|
Protocol.sqlserver: [cls.db_client, cls.db_guide],
|
2022-12-07 07:09:01 +00:00
|
|
|
}
|
|
|
|
return clients
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_target_protocol(cls, name, os):
|
|
|
|
for protocol, clients in cls.get_native_clients().items():
|
|
|
|
if isinstance(clients, dict):
|
2023-06-07 09:39:56 +00:00
|
|
|
if os == 'all':
|
|
|
|
clients = list(itertools.chain(*clients.values()))
|
|
|
|
else:
|
|
|
|
clients = clients.get(os) or clients.get('default')
|
2022-12-07 07:09:01 +00:00
|
|
|
if name in clients:
|
|
|
|
return protocol
|
|
|
|
return None
|
|
|
|
|
2022-12-16 10:37:27 +00:00
|
|
|
@classmethod
|
|
|
|
def xpack_methods(cls):
|
2024-06-04 07:54:16 +00:00
|
|
|
return [cls.mstsc, cls.db_client]
|
2023-06-14 06:48:50 +00:00
|
|
|
|
2022-12-07 07:09:01 +00:00
|
|
|
@classmethod
|
|
|
|
def get_methods(cls, os='windows'):
|
|
|
|
clients_map = cls.get_native_clients()
|
|
|
|
methods = defaultdict(list)
|
2023-07-11 11:29:56 +00:00
|
|
|
xpack_protocols = Protocol.xpack_protocols()
|
2022-12-07 07:09:01 +00:00
|
|
|
|
|
|
|
for protocol, _clients in clients_map.items():
|
2023-10-18 06:10:24 +00:00
|
|
|
if not settings.XPACK_LICENSE_IS_VALID and protocol in xpack_protocols:
|
2023-06-15 01:37:00 +00:00
|
|
|
continue
|
2022-12-07 07:09:01 +00:00
|
|
|
if isinstance(_clients, dict):
|
2023-06-07 09:39:56 +00:00
|
|
|
if os == 'all':
|
|
|
|
_clients = list(itertools.chain(*_clients.values()))
|
|
|
|
else:
|
|
|
|
_clients = _clients.get(os, _clients['default'])
|
2022-12-07 07:09:01 +00:00
|
|
|
for client in _clients:
|
2023-10-18 06:10:24 +00:00
|
|
|
if not settings.XPACK_LICENSE_IS_VALID and client in cls.xpack_methods():
|
2022-12-16 10:37:27 +00:00
|
|
|
continue
|
2022-12-07 07:09:01 +00:00
|
|
|
methods[protocol].append({
|
|
|
|
'value': client.value,
|
|
|
|
'label': client.label,
|
|
|
|
'type': 'native',
|
|
|
|
})
|
|
|
|
return methods
|
|
|
|
|
|
|
|
|
|
|
|
class AppletMethod:
|
|
|
|
@classmethod
|
|
|
|
def get_methods(cls):
|
|
|
|
from .models import Applet, AppletHost
|
|
|
|
|
|
|
|
methods = defaultdict(list)
|
2024-05-28 02:57:43 +00:00
|
|
|
has_applet_hosts = AppletHost.objects.filter(is_active=True).exists()
|
2022-12-07 07:09:01 +00:00
|
|
|
applets = Applet.objects.filter(is_active=True)
|
|
|
|
for applet in applets:
|
|
|
|
for protocol in applet.protocols:
|
|
|
|
methods[protocol].append({
|
|
|
|
'value': applet.name,
|
|
|
|
'label': applet.display_name,
|
|
|
|
'type': 'applet',
|
|
|
|
'icon': applet.icon,
|
|
|
|
'disabled': not applet.is_active or not has_applet_hosts,
|
|
|
|
})
|
|
|
|
return methods
|
|
|
|
|
|
|
|
|
2023-12-05 08:52:11 +00:00
|
|
|
class VirtualAppMethod:
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_methods(cls):
|
|
|
|
from .models import VirtualApp
|
|
|
|
methods = defaultdict(list)
|
|
|
|
if not getattr(settings, 'VIRTUAL_APP_ENABLED'):
|
|
|
|
return methods
|
|
|
|
virtual_apps = VirtualApp.objects.filter(is_active=True)
|
|
|
|
for virtual_app in virtual_apps:
|
|
|
|
for protocol in virtual_app.protocols:
|
|
|
|
methods[protocol].append({
|
|
|
|
'value': virtual_app.name,
|
|
|
|
'label': virtual_app.name,
|
|
|
|
'type': 'virtual_app',
|
|
|
|
'disabled': not virtual_app.is_active,
|
|
|
|
})
|
|
|
|
return methods
|
|
|
|
|
|
|
|
|
2022-12-07 07:09:01 +00:00
|
|
|
class ConnectMethodUtil:
|
2023-02-23 07:58:55 +00:00
|
|
|
_all_methods = {}
|
2022-12-07 07:09:01 +00:00
|
|
|
|
|
|
|
@classmethod
|
2023-07-11 11:29:56 +00:00
|
|
|
def components(cls):
|
2022-12-07 07:09:01 +00:00
|
|
|
protocols = {
|
|
|
|
TerminalType.koko: {
|
2023-07-11 11:29:56 +00:00
|
|
|
'web_methods': [WebMethod.web_cli],
|
2023-07-26 07:31:02 +00:00
|
|
|
'listen': [Protocol.http, Protocol.ssh, Protocol.sftp],
|
2022-12-07 07:09:01 +00:00
|
|
|
'support': [
|
2023-07-26 07:31:02 +00:00
|
|
|
Protocol.ssh, Protocol.telnet, Protocol.sftp,
|
2023-02-16 08:55:20 +00:00
|
|
|
Protocol.redis, Protocol.mongodb,
|
|
|
|
Protocol.k8s, Protocol.clickhouse,
|
2023-08-31 10:23:30 +00:00
|
|
|
|
|
|
|
Protocol.mysql, Protocol.mariadb,
|
|
|
|
Protocol.sqlserver, Protocol.postgresql,
|
2024-06-04 06:51:47 +00:00
|
|
|
Protocol.oracle
|
2022-12-07 07:09:01 +00:00
|
|
|
],
|
2023-07-26 07:31:02 +00:00
|
|
|
# 限制客户端的协议,比如 koko 虽然也支持 数据库的 ssh 连接,但是不再这里拉起
|
|
|
|
# Listen协议: [Asset协议]
|
|
|
|
'client_limits': {
|
|
|
|
Protocol.sftp: [Protocol.sftp],
|
|
|
|
Protocol.ssh: [Protocol.ssh, Protocol.telnet],
|
|
|
|
},
|
2022-12-07 07:09:01 +00:00
|
|
|
'match': 'm2m'
|
|
|
|
},
|
2023-06-19 10:17:46 +00:00
|
|
|
TerminalType.chen: {
|
2022-12-07 07:09:01 +00:00
|
|
|
'web_methods': [WebMethod.web_gui],
|
|
|
|
'listen': [Protocol.http],
|
|
|
|
'support': [
|
2023-02-09 06:35:57 +00:00
|
|
|
Protocol.mysql, Protocol.postgresql,
|
2023-06-29 03:37:34 +00:00
|
|
|
Protocol.oracle, Protocol.sqlserver,
|
2024-05-14 13:32:02 +00:00
|
|
|
Protocol.mariadb, Protocol.db2,
|
|
|
|
Protocol.dameng
|
2022-12-07 07:09:01 +00:00
|
|
|
],
|
|
|
|
'match': 'm2m'
|
|
|
|
},
|
|
|
|
TerminalType.lion: {
|
|
|
|
'web_methods': [WebMethod.web_gui],
|
|
|
|
'listen': [Protocol.http],
|
2023-07-04 09:58:04 +00:00
|
|
|
'support': [Protocol.rdp, Protocol.vnc],
|
2022-12-07 07:09:01 +00:00
|
|
|
'match': 'm2m'
|
|
|
|
},
|
|
|
|
TerminalType.magnus: {
|
2023-02-17 09:14:53 +00:00
|
|
|
'web_methods': [],
|
2022-12-07 07:09:01 +00:00
|
|
|
'listen': [],
|
|
|
|
'support': [
|
|
|
|
Protocol.mysql, Protocol.postgresql,
|
2023-01-30 07:09:13 +00:00
|
|
|
Protocol.oracle, Protocol.mariadb,
|
2023-12-11 05:57:35 +00:00
|
|
|
Protocol.redis, Protocol.sqlserver
|
2022-12-07 07:09:01 +00:00
|
|
|
],
|
|
|
|
'match': 'map'
|
|
|
|
},
|
|
|
|
TerminalType.razor: {
|
2023-02-17 09:14:53 +00:00
|
|
|
'web_methods': [],
|
2022-12-07 07:09:01 +00:00
|
|
|
'listen': [Protocol.rdp],
|
|
|
|
'support': [Protocol.rdp],
|
|
|
|
'match': 'map'
|
|
|
|
},
|
2023-07-11 09:04:43 +00:00
|
|
|
TerminalType.kael: {
|
|
|
|
'web_methods': [WebMethod.web_gui],
|
|
|
|
'listen': [Protocol.http],
|
|
|
|
'support': [Protocol.chatgpt],
|
|
|
|
'match': 'm2m'
|
|
|
|
}
|
2022-12-07 07:09:01 +00:00
|
|
|
}
|
|
|
|
return protocols
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_connect_method(cls, name, protocol, os='linux'):
|
|
|
|
methods = cls.get_protocols_connect_methods(os)
|
|
|
|
protocol_methods = methods.get(protocol, [])
|
|
|
|
for method in protocol_methods:
|
|
|
|
if method['value'] == name:
|
|
|
|
return method
|
|
|
|
return None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def refresh_methods(cls):
|
2023-02-23 07:58:55 +00:00
|
|
|
cls._all_methods = {}
|
2022-12-07 07:09:01 +00:00
|
|
|
|
2023-02-15 09:46:17 +00:00
|
|
|
@classmethod
|
|
|
|
def get_filtered_protocols_connect_methods(cls, os):
|
|
|
|
methods = dict(cls.get_protocols_connect_methods(os))
|
2023-02-15 10:16:56 +00:00
|
|
|
methods = cls._filter_disable_components_connect_methods(methods)
|
|
|
|
methods = cls._filter_disable_protocols_connect_methods(methods)
|
|
|
|
return methods
|
|
|
|
|
2023-06-09 09:16:23 +00:00
|
|
|
@classmethod
|
|
|
|
def get_user_allowed_connect_methods(cls, os, user):
|
|
|
|
from acls.models import ConnectMethodACL
|
|
|
|
methods = cls.get_filtered_protocols_connect_methods(os)
|
|
|
|
acls = ConnectMethodACL.get_user_acls(user)
|
|
|
|
disabled_connect_methods = acls.values_list('connect_methods', flat=True)
|
|
|
|
disabled_connect_methods = set(itertools.chain.from_iterable(disabled_connect_methods))
|
|
|
|
|
|
|
|
new_queryset = {}
|
|
|
|
for protocol, methods in methods.items():
|
|
|
|
new_queryset[protocol] = [x for x in methods if x['value'] not in disabled_connect_methods]
|
|
|
|
return new_queryset
|
|
|
|
|
2023-02-15 10:16:56 +00:00
|
|
|
@classmethod
|
|
|
|
def _filter_disable_components_connect_methods(cls, methods):
|
2023-02-15 09:46:17 +00:00
|
|
|
component_setting = {
|
|
|
|
'razor': 'TERMINAL_RAZOR_ENABLED',
|
|
|
|
'magnus': 'TERMINAL_MAGNUS_ENABLED',
|
|
|
|
}
|
|
|
|
disabled_component = [comp for comp, attr in component_setting.items() if not getattr(settings, attr)]
|
|
|
|
if not disabled_component:
|
|
|
|
return methods
|
|
|
|
|
|
|
|
for protocol, ms in methods.items():
|
|
|
|
filtered_methods = [m for m in ms if m['component'] not in disabled_component]
|
|
|
|
methods[protocol] = filtered_methods
|
|
|
|
return methods
|
|
|
|
|
2023-02-15 10:16:56 +00:00
|
|
|
@classmethod
|
|
|
|
def _filter_disable_protocols_connect_methods(cls, methods):
|
|
|
|
# 过滤一些特殊的协议方式
|
|
|
|
if not getattr(settings, 'TERMINAL_KOKO_SSH_ENABLED'):
|
2024-05-16 09:31:12 +00:00
|
|
|
disable_ssh_client_protocols = [Protocol.ssh, Protocol.sftp, Protocol.telnet]
|
|
|
|
for protocol in disable_ssh_client_protocols:
|
|
|
|
methods[protocol] = [m for m in methods[protocol] if m['type'] != 'native']
|
2023-02-15 10:16:56 +00:00
|
|
|
return methods
|
|
|
|
|
2022-12-07 07:09:01 +00:00
|
|
|
@classmethod
|
2023-06-07 09:39:56 +00:00
|
|
|
def get_protocols_connect_methods(cls, os='windows'):
|
2023-02-23 07:58:55 +00:00
|
|
|
if cls._all_methods.get('os'):
|
|
|
|
return cls._all_methods['os']
|
2022-12-07 07:09:01 +00:00
|
|
|
|
|
|
|
methods = defaultdict(list)
|
2023-07-11 11:29:56 +00:00
|
|
|
spec_web_methods = WebMethod.get_spec_methods()
|
2022-12-07 07:09:01 +00:00
|
|
|
applet_methods = AppletMethod.get_methods()
|
2023-12-05 08:52:11 +00:00
|
|
|
virtual_app_methods = VirtualAppMethod.get_methods()
|
2023-07-26 07:31:02 +00:00
|
|
|
native_methods = NativeClient.get_methods(os=os)
|
2022-12-07 07:09:01 +00:00
|
|
|
|
2023-07-11 11:29:56 +00:00
|
|
|
for component, component_protocol in cls.components().items():
|
2022-12-07 07:09:01 +00:00
|
|
|
support = component_protocol['support']
|
2023-07-26 07:31:02 +00:00
|
|
|
default_web_methods = component_protocol.get('web_methods', [])
|
|
|
|
client_limits = component_protocol.get('client_limits', {})
|
2022-12-07 07:09:01 +00:00
|
|
|
|
2023-07-26 07:31:02 +00:00
|
|
|
for asset_protocol in support:
|
2022-12-07 07:09:01 +00:00
|
|
|
# Web 方式
|
2023-07-26 07:31:02 +00:00
|
|
|
web_methods = spec_web_methods.get(asset_protocol, [])
|
|
|
|
if not web_methods:
|
|
|
|
web_methods = default_web_methods
|
|
|
|
methods[str(asset_protocol)].extend([
|
2022-12-07 07:09:01 +00:00
|
|
|
{
|
|
|
|
'component': component.value,
|
|
|
|
'type': 'web',
|
|
|
|
'endpoint_protocol': 'http',
|
|
|
|
'value': method.value,
|
|
|
|
'label': method.label,
|
|
|
|
}
|
2023-07-11 11:29:56 +00:00
|
|
|
for method in web_methods
|
2022-12-07 07:09:01 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
# 客户端方式
|
|
|
|
if component_protocol['match'] == 'map':
|
2023-07-26 07:31:02 +00:00
|
|
|
listen = [asset_protocol]
|
2022-12-07 07:09:01 +00:00
|
|
|
else:
|
|
|
|
listen = component_protocol['listen']
|
2023-07-11 11:29:56 +00:00
|
|
|
|
2022-12-07 07:09:01 +00:00
|
|
|
for listen_protocol in listen:
|
2023-07-26 07:31:02 +00:00
|
|
|
limits = client_limits.get(listen_protocol, [])
|
|
|
|
if limits and asset_protocol not in limits:
|
2023-02-15 09:55:29 +00:00
|
|
|
continue
|
2023-07-26 07:31:02 +00:00
|
|
|
# Native method
|
|
|
|
client_methods = native_methods.get(listen_protocol, [])
|
|
|
|
methods[str(asset_protocol)].extend([
|
2022-12-07 07:09:01 +00:00
|
|
|
{
|
|
|
|
'component': component.value,
|
|
|
|
'type': 'native',
|
|
|
|
'endpoint_protocol': listen_protocol,
|
|
|
|
**method
|
|
|
|
}
|
2023-07-26 07:31:02 +00:00
|
|
|
for method in client_methods
|
2022-12-07 07:09:01 +00:00
|
|
|
])
|
|
|
|
|
2023-07-11 11:29:56 +00:00
|
|
|
# 远程应用方式,这个只有 tinker 提供,并且协议可能是自定义的
|
2023-07-26 07:31:02 +00:00
|
|
|
for asset_protocol, applet_methods in applet_methods.items():
|
2022-12-07 07:09:01 +00:00
|
|
|
for method in applet_methods:
|
|
|
|
method['listen'] = 'rdp'
|
|
|
|
method['component'] = TerminalType.tinker.value
|
2023-07-26 07:31:02 +00:00
|
|
|
methods[asset_protocol].extend(applet_methods)
|
2022-12-07 07:09:01 +00:00
|
|
|
|
2023-12-05 08:52:11 +00:00
|
|
|
# 虚拟应用方式,这个只有 panda 提供,并且协议可能是自定义的
|
|
|
|
for protocol, virtual_app_methods in virtual_app_methods.items():
|
|
|
|
for method in virtual_app_methods:
|
|
|
|
method['listen'] = Protocol.http
|
|
|
|
method['component'] = TerminalType.panda.value
|
|
|
|
methods[protocol].extend(virtual_app_methods)
|
|
|
|
|
2023-02-23 07:58:55 +00:00
|
|
|
cls._all_methods[os] = methods
|
2022-12-07 07:09:01 +00:00
|
|
|
return methods
|