2023-07-11 03:28:09 +00:00
|
|
|
from django.conf import settings
|
2022-09-18 16:07:59 +00:00
|
|
|
from django.db import models
|
2023-06-30 07:54:06 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-12-14 09:19:35 +00:00
|
|
|
|
2022-09-18 16:07:59 +00:00
|
|
|
from common.db.models import ChoicesMixin
|
2023-07-04 02:29:27 +00:00
|
|
|
from common.decorators import cached_method
|
2023-06-30 07:54:06 +00:00
|
|
|
from .base import FillType
|
2022-09-18 16:07:59 +00:00
|
|
|
|
|
|
|
__all__ = ['Protocol']
|
|
|
|
|
|
|
|
|
|
|
|
class Protocol(ChoicesMixin, models.TextChoices):
|
|
|
|
ssh = 'ssh', 'SSH'
|
2023-07-25 09:12:06 +00:00
|
|
|
sftp = 'sftp', 'SFTP'
|
2022-09-18 16:07:59 +00:00
|
|
|
rdp = 'rdp', 'RDP'
|
|
|
|
telnet = 'telnet', 'Telnet'
|
|
|
|
vnc = 'vnc', 'VNC'
|
2023-03-29 09:10:58 +00:00
|
|
|
winrm = 'winrm', 'WinRM'
|
2022-09-18 16:07:59 +00:00
|
|
|
|
|
|
|
mysql = 'mysql', 'MySQL'
|
|
|
|
mariadb = 'mariadb', 'MariaDB'
|
|
|
|
oracle = 'oracle', 'Oracle'
|
|
|
|
postgresql = 'postgresql', 'PostgreSQL'
|
|
|
|
sqlserver = 'sqlserver', 'SQLServer'
|
2022-12-06 09:13:37 +00:00
|
|
|
clickhouse = 'clickhouse', 'ClickHouse'
|
2022-09-18 16:07:59 +00:00
|
|
|
redis = 'redis', 'Redis'
|
|
|
|
mongodb = 'mongodb', 'MongoDB'
|
|
|
|
|
|
|
|
k8s = 'k8s', 'K8S'
|
2023-06-30 07:54:06 +00:00
|
|
|
http = 'http', 'HTTP(s)'
|
2022-09-18 16:07:59 +00:00
|
|
|
|
2023-06-30 09:35:49 +00:00
|
|
|
chatgpt = 'chatgpt', 'ChatGPT'
|
|
|
|
|
2022-09-18 16:07:59 +00:00
|
|
|
@classmethod
|
2022-09-21 03:17:14 +00:00
|
|
|
def device_protocols(cls):
|
2022-09-20 13:19:05 +00:00
|
|
|
return {
|
|
|
|
cls.ssh: {
|
|
|
|
'port': 22,
|
2022-09-21 03:17:14 +00:00
|
|
|
'secret_types': ['password', 'ssh_key'],
|
2023-07-25 09:12:06 +00:00
|
|
|
},
|
|
|
|
cls.sftp: {
|
|
|
|
'port': 22,
|
|
|
|
'secret_types': ['password', 'ssh_key'],
|
2022-09-20 13:19:05 +00:00
|
|
|
'setting': {
|
2023-06-30 07:54:06 +00:00
|
|
|
'sftp_home': {
|
|
|
|
'type': 'str',
|
|
|
|
'default': '/tmp',
|
|
|
|
'label': _('SFTP home')
|
2023-07-25 09:12:06 +00:00
|
|
|
}
|
2022-09-20 13:19:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
cls.rdp: {
|
|
|
|
'port': 3389,
|
2022-09-21 03:17:14 +00:00
|
|
|
'secret_types': ['password'],
|
2022-09-20 13:19:05 +00:00
|
|
|
'setting': {
|
2023-06-30 07:54:06 +00:00
|
|
|
'console': {
|
|
|
|
'type': 'bool',
|
|
|
|
'default': False,
|
|
|
|
'label': _('Console'),
|
|
|
|
'help_text': _("Connect to console session")
|
|
|
|
},
|
|
|
|
'security': {
|
|
|
|
'type': 'choice',
|
|
|
|
'choices': [('any', _('Any')), ('rdp', 'RDP'), ('tls', 'TLS'), ('nla', 'NLA')],
|
|
|
|
'default': 'any',
|
|
|
|
'label': _('Security'),
|
|
|
|
'help_text': _("Security layer to use for the connection")
|
|
|
|
},
|
2023-07-07 08:15:32 +00:00
|
|
|
'ad_domain': {
|
|
|
|
'type': 'str',
|
|
|
|
'required': False,
|
|
|
|
'default': '',
|
|
|
|
'label': _('AD domain')
|
|
|
|
}
|
2022-09-20 13:19:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
cls.vnc: {
|
|
|
|
'port': 5900,
|
2022-09-21 03:17:14 +00:00
|
|
|
'secret_types': ['password'],
|
2022-09-20 13:19:05 +00:00
|
|
|
},
|
|
|
|
cls.telnet: {
|
|
|
|
'port': 23,
|
2022-09-21 03:17:14 +00:00
|
|
|
'secret_types': ['password'],
|
2023-08-02 08:53:47 +00:00
|
|
|
'setting': {
|
|
|
|
'username_prompt': {
|
|
|
|
'type': 'str',
|
|
|
|
'default': 'username:|login:',
|
|
|
|
'label': _('Username prompt'),
|
|
|
|
'help_text': _('We will send username when we see this prompt')
|
|
|
|
},
|
|
|
|
'password_prompt': {
|
|
|
|
'type': 'str',
|
|
|
|
'default': 'password:',
|
|
|
|
'label': _('Password prompt'),
|
|
|
|
'help_text': _('We will send password when we see this prompt')
|
|
|
|
},
|
|
|
|
'success_prompt': {
|
|
|
|
'type': 'str',
|
|
|
|
'default': 'success|成功|#|>|\$',
|
|
|
|
'label': _('Success prompt'),
|
|
|
|
'help_text': _('We will consider login success when we see this prompt')
|
|
|
|
}
|
|
|
|
}
|
2022-09-20 13:19:05 +00:00
|
|
|
},
|
2023-03-29 09:10:58 +00:00
|
|
|
cls.winrm: {
|
|
|
|
'port': 5985,
|
|
|
|
'secret_types': ['password'],
|
|
|
|
'setting': {
|
2023-06-30 07:54:06 +00:00
|
|
|
'use_ssl': {
|
|
|
|
'type': 'bool',
|
|
|
|
'default': False,
|
|
|
|
'label': _('Use SSL')
|
|
|
|
},
|
2023-03-29 09:10:58 +00:00
|
|
|
}
|
|
|
|
},
|
2022-09-20 13:19:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@classmethod
|
2022-09-21 03:17:14 +00:00
|
|
|
def database_protocols(cls):
|
2022-09-20 13:19:05 +00:00
|
|
|
return {
|
|
|
|
cls.mysql: {
|
|
|
|
'port': 3306,
|
2023-01-29 09:40:15 +00:00
|
|
|
'setting': {},
|
|
|
|
'required': True,
|
2022-09-21 03:17:14 +00:00
|
|
|
'secret_types': ['password'],
|
2022-09-20 13:19:05 +00:00
|
|
|
},
|
|
|
|
cls.mariadb: {
|
|
|
|
'port': 3306,
|
2023-01-29 09:40:15 +00:00
|
|
|
'required': True,
|
2022-09-21 03:17:14 +00:00
|
|
|
'secret_types': ['password'],
|
2022-09-20 13:19:05 +00:00
|
|
|
},
|
|
|
|
cls.postgresql: {
|
|
|
|
'port': 5432,
|
2023-01-29 09:40:15 +00:00
|
|
|
'required': True,
|
2022-09-21 03:17:14 +00:00
|
|
|
'secret_types': ['password'],
|
2023-07-11 11:29:56 +00:00
|
|
|
'xpack': True
|
2022-09-20 13:19:05 +00:00
|
|
|
},
|
|
|
|
cls.oracle: {
|
|
|
|
'port': 1521,
|
2023-01-29 09:40:15 +00:00
|
|
|
'required': True,
|
2022-09-21 03:17:14 +00:00
|
|
|
'secret_types': ['password'],
|
2023-07-25 06:35:42 +00:00
|
|
|
'xpack': True,
|
|
|
|
'setting': {
|
|
|
|
'sysdba': {
|
|
|
|
'type': 'bool',
|
|
|
|
'default': False,
|
|
|
|
'label': _('SYSDBA'),
|
|
|
|
'help_text': _('Connect as SYSDBA')
|
|
|
|
},
|
|
|
|
}
|
2022-09-20 13:19:05 +00:00
|
|
|
},
|
|
|
|
cls.sqlserver: {
|
|
|
|
'port': 1433,
|
2023-01-29 09:40:15 +00:00
|
|
|
'required': True,
|
2022-09-21 03:17:14 +00:00
|
|
|
'secret_types': ['password'],
|
2023-07-11 11:29:56 +00:00
|
|
|
'xpack': True,
|
2022-12-06 09:13:37 +00:00
|
|
|
},
|
|
|
|
cls.clickhouse: {
|
|
|
|
'port': 9000,
|
2023-01-29 09:40:15 +00:00
|
|
|
'required': True,
|
2022-12-06 09:13:37 +00:00
|
|
|
'secret_types': ['password'],
|
2023-07-11 11:29:56 +00:00
|
|
|
'xpack': True,
|
2022-09-20 13:19:05 +00:00
|
|
|
},
|
|
|
|
cls.mongodb: {
|
|
|
|
'port': 27017,
|
2023-01-29 09:40:15 +00:00
|
|
|
'required': True,
|
2022-09-21 03:17:14 +00:00
|
|
|
'secret_types': ['password'],
|
2022-09-20 13:19:05 +00:00
|
|
|
},
|
|
|
|
cls.redis: {
|
|
|
|
'port': 6379,
|
2023-01-29 09:40:15 +00:00
|
|
|
'required': True,
|
2022-09-21 03:17:14 +00:00
|
|
|
'secret_types': ['password'],
|
2023-02-13 11:42:42 +00:00
|
|
|
'setting': {
|
2023-06-30 07:54:06 +00:00
|
|
|
'auth_username': {
|
|
|
|
'type': 'bool',
|
|
|
|
'default': False,
|
|
|
|
'label': _('Auth username')
|
|
|
|
},
|
2023-02-13 11:42:42 +00:00
|
|
|
}
|
2022-09-20 13:19:05 +00:00
|
|
|
},
|
|
|
|
}
|
2022-09-18 16:07:59 +00:00
|
|
|
|
|
|
|
@classmethod
|
2022-09-21 03:17:14 +00:00
|
|
|
def cloud_protocols(cls):
|
2022-09-20 13:19:05 +00:00
|
|
|
return {
|
|
|
|
cls.k8s: {
|
|
|
|
'port': 443,
|
2023-07-04 02:29:27 +00:00
|
|
|
'port_from_addr': True,
|
2023-01-29 09:40:15 +00:00
|
|
|
'required': True,
|
2022-09-21 03:17:14 +00:00
|
|
|
'secret_types': ['token'],
|
2022-09-20 13:19:05 +00:00
|
|
|
},
|
|
|
|
cls.http: {
|
|
|
|
'port': 80,
|
2023-07-04 02:29:27 +00:00
|
|
|
'port_from_addr': True,
|
2022-09-21 03:17:14 +00:00
|
|
|
'secret_types': ['password'],
|
2022-09-20 13:19:05 +00:00
|
|
|
'setting': {
|
2023-08-03 08:09:54 +00:00
|
|
|
'safe_mode': {
|
|
|
|
'type': 'bool',
|
2023-08-03 08:52:15 +00:00
|
|
|
'default': False,
|
2023-08-03 08:09:54 +00:00
|
|
|
'label': _('Safe mode'),
|
|
|
|
'help_text': _(
|
|
|
|
'When safe mode is enabled, some operations will be disabled, such as: '
|
|
|
|
'New tab, right click, visit other website, etc.'
|
|
|
|
)
|
|
|
|
},
|
2023-06-30 07:54:06 +00:00
|
|
|
'autofill': {
|
2023-07-11 09:27:47 +00:00
|
|
|
'label': _('Autofill'),
|
2023-06-30 07:54:06 +00:00
|
|
|
'type': 'choice',
|
|
|
|
'choices': FillType.choices,
|
|
|
|
'default': 'basic',
|
|
|
|
},
|
|
|
|
'username_selector': {
|
|
|
|
'type': 'str',
|
|
|
|
'default': 'name=username',
|
|
|
|
'label': _('Username selector')
|
|
|
|
},
|
|
|
|
'password_selector': {
|
|
|
|
'type': 'str',
|
|
|
|
'default': 'name=password',
|
|
|
|
'label': _('Password selector')
|
|
|
|
},
|
|
|
|
'submit_selector': {
|
|
|
|
'type': 'str',
|
|
|
|
'default': 'type=submit',
|
|
|
|
'label': _('Submit selector')
|
2023-07-11 09:04:43 +00:00
|
|
|
},
|
|
|
|
'script': {
|
|
|
|
'type': 'text',
|
2023-07-11 09:59:18 +00:00
|
|
|
'default': [],
|
2023-07-11 09:04:43 +00:00
|
|
|
'label': _('Script'),
|
2023-06-30 07:54:06 +00:00
|
|
|
}
|
2022-09-20 13:19:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@classmethod
|
2023-06-30 09:35:49 +00:00
|
|
|
def gpt_protocols(cls):
|
2023-07-11 03:28:09 +00:00
|
|
|
protocols = {
|
2023-06-30 09:35:49 +00:00
|
|
|
cls.chatgpt: {
|
|
|
|
'port': 443,
|
|
|
|
'required': True,
|
2023-07-04 02:29:27 +00:00
|
|
|
'port_from_addr': True,
|
2023-06-30 10:33:18 +00:00
|
|
|
'secret_types': ['api_key'],
|
|
|
|
'setting': {
|
|
|
|
'api_mode': {
|
|
|
|
'type': 'choice',
|
|
|
|
'default': 'gpt-3.5-turbo',
|
|
|
|
'label': _('API mode'),
|
|
|
|
'choices': [
|
|
|
|
('gpt-3.5-turbo', 'GPT-3.5 Turbo'),
|
|
|
|
('gpt-3.5-turbo-16k', 'GPT-3.5 Turbo 16K'),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2023-06-30 09:35:49 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-11 03:28:09 +00:00
|
|
|
if settings.XPACK_ENABLED:
|
|
|
|
choices = protocols[cls.chatgpt]['setting']['api_mode']['choices']
|
|
|
|
choices.extend([
|
|
|
|
('gpt-4', 'GPT-4'),
|
|
|
|
('gpt-4-32k', 'GPT-4 32K'),
|
|
|
|
])
|
|
|
|
return protocols
|
2023-06-30 09:35:49 +00:00
|
|
|
|
2022-09-20 13:19:05 +00:00
|
|
|
@classmethod
|
2023-07-04 02:29:27 +00:00
|
|
|
@cached_method(ttl=600)
|
2022-09-20 13:19:05 +00:00
|
|
|
def settings(cls):
|
|
|
|
return {
|
2022-12-14 09:19:35 +00:00
|
|
|
**cls.device_protocols(),
|
|
|
|
**cls.database_protocols(),
|
2023-06-30 09:35:49 +00:00
|
|
|
**cls.cloud_protocols(),
|
|
|
|
**cls.gpt_protocols(),
|
2022-09-18 16:07:59 +00:00
|
|
|
}
|
2023-04-03 10:18:31 +00:00
|
|
|
|
2023-07-11 11:29:56 +00:00
|
|
|
@classmethod
|
|
|
|
@cached_method(ttl=600)
|
|
|
|
def xpack_protocols(cls):
|
|
|
|
return [
|
|
|
|
protocol
|
|
|
|
for protocol, config in cls.settings().items()
|
|
|
|
if config.get('xpack', False)
|
|
|
|
]
|
|
|
|
|
2023-04-03 10:18:31 +00:00
|
|
|
@classmethod
|
|
|
|
def protocol_secret_types(cls):
|
2023-07-11 11:29:56 +00:00
|
|
|
configs = cls.settings()
|
2023-04-03 10:18:31 +00:00
|
|
|
return {
|
2023-07-11 11:29:56 +00:00
|
|
|
protocol: configs[protocol]['secret_types'] or ['password']
|
|
|
|
for protocol in configs
|
2023-04-03 10:18:31 +00:00
|
|
|
}
|