2016-10-15 09:14:56 +00:00
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
2018-12-18 03:29:21 +00:00
|
|
|
|
import os
|
2017-11-23 06:08:01 +00:00
|
|
|
|
import uuid
|
|
|
|
|
|
2016-10-15 09:14:56 +00:00
|
|
|
|
from django.db import models
|
2016-10-15 10:28:49 +00:00
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-02-27 11:39:27 +00:00
|
|
|
|
from django.utils import timezone
|
2018-01-20 14:22:09 +00:00
|
|
|
|
from django.conf import settings
|
2018-12-18 03:29:21 +00:00
|
|
|
|
from django.core.files.storage import default_storage
|
2019-01-15 02:23:30 +00:00
|
|
|
|
from django.core.cache import cache
|
2016-10-15 09:14:56 +00:00
|
|
|
|
|
2016-10-15 10:28:49 +00:00
|
|
|
|
from users.models import User
|
2018-07-25 07:13:53 +00:00
|
|
|
|
from orgs.mixins import OrgModelMixin
|
2018-11-19 04:45:33 +00:00
|
|
|
|
from common.utils import get_command_storage_setting, get_replay_storage_setting
|
2019-01-21 09:05:31 +00:00
|
|
|
|
from .backends import get_multi_command_storage
|
2017-11-29 11:27:04 +00:00
|
|
|
|
from .backends.command.models import AbstractSessionCommand
|
2016-10-15 10:28:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Terminal(models.Model):
|
2017-11-23 06:08:01 +00:00
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2017-12-25 04:22:49 +00:00
|
|
|
|
name = models.CharField(max_length=32, verbose_name=_('Name'))
|
2018-11-23 02:25:35 +00:00
|
|
|
|
remote_addr = models.CharField(max_length=128, blank=True, verbose_name=_('Remote Address'))
|
2017-10-31 03:34:20 +00:00
|
|
|
|
ssh_port = models.IntegerField(verbose_name=_('SSH Port'), default=2222)
|
|
|
|
|
http_port = models.IntegerField(verbose_name=_('HTTP Port'), default=5000)
|
2018-01-23 02:36:20 +00:00
|
|
|
|
command_storage = models.CharField(max_length=128, verbose_name=_("Command storage"), default='default')
|
2018-01-20 14:22:09 +00:00
|
|
|
|
replay_storage = models.CharField(max_length=128, verbose_name=_("Replay storage"), default='default')
|
2017-10-31 03:34:20 +00:00
|
|
|
|
user = models.OneToOneField(User, related_name='terminal', verbose_name='Application User', null=True, on_delete=models.CASCADE)
|
2016-12-25 15:10:53 +00:00
|
|
|
|
is_accepted = models.BooleanField(default=False, verbose_name='Is Accepted')
|
2017-11-14 01:44:16 +00:00
|
|
|
|
is_deleted = models.BooleanField(default=False)
|
2016-10-15 15:34:02 +00:00
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True)
|
2016-10-19 10:33:14 +00:00
|
|
|
|
comment = models.TextField(blank=True, verbose_name=_('Comment'))
|
2019-01-21 09:05:31 +00:00
|
|
|
|
STATUS_KEY_PREFIX = 'terminal_status_'
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def is_alive(self):
|
|
|
|
|
key = self.STATUS_KEY_PREFIX + str(self.id)
|
|
|
|
|
return bool(cache.get(key))
|
|
|
|
|
|
|
|
|
|
@is_alive.setter
|
|
|
|
|
def is_alive(self, value):
|
|
|
|
|
key = self.STATUS_KEY_PREFIX + str(self.id)
|
|
|
|
|
cache.set(key, value, 60)
|
2016-10-15 10:28:49 +00:00
|
|
|
|
|
2016-12-21 16:36:31 +00:00
|
|
|
|
@property
|
|
|
|
|
def is_active(self):
|
|
|
|
|
if self.user and self.user.is_active:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
@is_active.setter
|
|
|
|
|
def is_active(self, active):
|
|
|
|
|
if self.user:
|
|
|
|
|
self.user.is_active = active
|
|
|
|
|
self.user.save()
|
|
|
|
|
|
2019-01-21 09:05:31 +00:00
|
|
|
|
def get_command_storage_setting(self):
|
2018-11-19 04:45:33 +00:00
|
|
|
|
storage_all = get_command_storage_setting()
|
2018-01-21 07:12:59 +00:00
|
|
|
|
if self.command_storage in storage_all:
|
|
|
|
|
storage = storage_all.get(self.command_storage)
|
|
|
|
|
else:
|
|
|
|
|
storage = storage_all.get('default')
|
|
|
|
|
return {"TERMINAL_COMMAND_STORAGE": storage}
|
2018-01-20 14:22:09 +00:00
|
|
|
|
|
2019-01-21 09:05:31 +00:00
|
|
|
|
def get_replay_storage_setting(self):
|
2018-11-19 04:45:33 +00:00
|
|
|
|
storage_all = get_replay_storage_setting()
|
2018-02-26 10:11:46 +00:00
|
|
|
|
if self.replay_storage in storage_all:
|
|
|
|
|
storage = storage_all.get(self.replay_storage)
|
|
|
|
|
else:
|
|
|
|
|
storage = storage_all.get('default')
|
|
|
|
|
return {"TERMINAL_REPLAY_STORAGE": storage}
|
2018-01-20 14:22:09 +00:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def config(self):
|
2018-01-21 07:12:59 +00:00
|
|
|
|
configs = {}
|
|
|
|
|
for k in dir(settings):
|
2019-01-10 03:50:08 +00:00
|
|
|
|
if not k.startswith('TERMINAL'):
|
|
|
|
|
continue
|
|
|
|
|
configs[k] = getattr(settings, k)
|
2019-01-21 09:05:31 +00:00
|
|
|
|
configs.update(self.get_command_storage_setting())
|
|
|
|
|
configs.update(self.get_replay_storage_setting())
|
2018-09-03 03:24:25 +00:00
|
|
|
|
configs.update({
|
2018-11-22 04:27:27 +00:00
|
|
|
|
'SECURITY_MAX_IDLE_TIME': settings.SECURITY_MAX_IDLE_TIME
|
2018-09-03 03:24:25 +00:00
|
|
|
|
})
|
2018-01-21 07:12:59 +00:00
|
|
|
|
return configs
|
2018-01-20 14:22:09 +00:00
|
|
|
|
|
2018-11-23 02:25:35 +00:00
|
|
|
|
@property
|
|
|
|
|
def service_account(self):
|
|
|
|
|
return self.user
|
|
|
|
|
|
2017-11-14 01:44:16 +00:00
|
|
|
|
def create_app_user(self):
|
2017-12-25 04:22:49 +00:00
|
|
|
|
random = uuid.uuid4().hex[:6]
|
2018-11-19 04:45:33 +00:00
|
|
|
|
user, access_key = User.create_app_user(
|
|
|
|
|
name="{}-{}".format(self.name, random), comment=self.comment
|
|
|
|
|
)
|
2016-12-25 09:44:39 +00:00
|
|
|
|
self.user = user
|
|
|
|
|
self.save()
|
|
|
|
|
return user, access_key
|
2016-10-17 07:24:41 +00:00
|
|
|
|
|
2017-01-02 16:11:44 +00:00
|
|
|
|
def delete(self, using=None, keep_parents=False):
|
|
|
|
|
if self.user:
|
|
|
|
|
self.user.delete()
|
2017-12-25 04:22:49 +00:00
|
|
|
|
self.user = None
|
2017-11-14 01:44:16 +00:00
|
|
|
|
self.is_deleted = True
|
|
|
|
|
self.save()
|
|
|
|
|
return
|
2017-01-02 16:11:44 +00:00
|
|
|
|
|
2017-10-31 03:34:20 +00:00
|
|
|
|
def __str__(self):
|
2017-11-14 01:44:16 +00:00
|
|
|
|
status = "Active"
|
|
|
|
|
if not self.is_accepted:
|
|
|
|
|
status = "NotAccept"
|
|
|
|
|
elif self.is_deleted:
|
|
|
|
|
status = "Deleted"
|
|
|
|
|
elif not self.is_active:
|
|
|
|
|
status = "Disable"
|
|
|
|
|
return '%s: %s' % (self.name, status)
|
2016-12-21 16:36:31 +00:00
|
|
|
|
|
2016-10-15 10:28:49 +00:00
|
|
|
|
class Meta:
|
2016-12-26 16:59:52 +00:00
|
|
|
|
ordering = ('is_accepted',)
|
2017-12-01 09:28:47 +00:00
|
|
|
|
db_table = "terminal"
|
2016-10-15 10:28:49 +00:00
|
|
|
|
|
|
|
|
|
|
2017-12-01 09:28:47 +00:00
|
|
|
|
class Status(models.Model):
|
2017-11-23 06:08:01 +00:00
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2017-11-14 01:44:16 +00:00
|
|
|
|
session_online = models.IntegerField(verbose_name=_("Session Online"), default=0)
|
|
|
|
|
cpu_used = models.FloatField(verbose_name=_("CPU Usage"))
|
|
|
|
|
memory_used = models.FloatField(verbose_name=_("Memory Used"))
|
|
|
|
|
connections = models.IntegerField(verbose_name=_("Connections"))
|
|
|
|
|
threads = models.IntegerField(verbose_name=_("Threads"))
|
|
|
|
|
boot_time = models.FloatField(verbose_name=_("Boot Time"))
|
|
|
|
|
terminal = models.ForeignKey(Terminal, null=True, on_delete=models.CASCADE)
|
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
db_table = 'terminal_status'
|
2017-12-01 13:22:32 +00:00
|
|
|
|
get_latest_by = 'date_created'
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return self.date_created.strftime("%Y-%m-%d %H:%M:%S")
|
2017-11-14 01:44:16 +00:00
|
|
|
|
|
|
|
|
|
|
2018-07-25 07:13:53 +00:00
|
|
|
|
class Session(OrgModelMixin):
|
2017-11-14 01:44:16 +00:00
|
|
|
|
LOGIN_FROM_CHOICES = (
|
|
|
|
|
('ST', 'SSH Terminal'),
|
|
|
|
|
('WT', 'Web Terminal'),
|
|
|
|
|
)
|
2018-04-23 03:32:46 +00:00
|
|
|
|
PROTOCOL_CHOICES = (
|
|
|
|
|
('ssh', 'ssh'),
|
2018-12-26 06:43:43 +00:00
|
|
|
|
('rdp', 'rdp'),
|
|
|
|
|
('vnc', 'vnc')
|
2018-04-23 03:32:46 +00:00
|
|
|
|
)
|
2017-11-14 01:44:16 +00:00
|
|
|
|
|
2017-12-03 16:21:26 +00:00
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2017-11-14 01:44:16 +00:00
|
|
|
|
user = models.CharField(max_length=128, verbose_name=_("User"))
|
|
|
|
|
asset = models.CharField(max_length=1024, verbose_name=_("Asset"))
|
2017-12-15 09:38:46 +00:00
|
|
|
|
system_user = models.CharField(max_length=128, verbose_name=_("System user"))
|
2017-11-14 01:44:16 +00:00
|
|
|
|
login_from = models.CharField(max_length=2, choices=LOGIN_FROM_CHOICES, default="ST")
|
2018-01-05 10:37:36 +00:00
|
|
|
|
remote_addr = models.CharField(max_length=15, verbose_name=_("Remote addr"), blank=True, null=True)
|
2017-11-14 01:44:16 +00:00
|
|
|
|
is_finished = models.BooleanField(default=False)
|
2017-11-22 02:54:59 +00:00
|
|
|
|
has_replay = models.BooleanField(default=False, verbose_name=_("Replay"))
|
|
|
|
|
has_command = models.BooleanField(default=False, verbose_name=_("Command"))
|
2018-03-07 11:05:43 +00:00
|
|
|
|
terminal = models.ForeignKey(Terminal, null=True, on_delete=models.SET_NULL)
|
2018-04-23 03:40:30 +00:00
|
|
|
|
protocol = models.CharField(choices=PROTOCOL_CHOICES, default='ssh', max_length=8)
|
2018-02-27 11:39:27 +00:00
|
|
|
|
date_last_active = models.DateTimeField(verbose_name=_("Date last active"), default=timezone.now)
|
2018-04-23 03:40:30 +00:00
|
|
|
|
date_start = models.DateTimeField(verbose_name=_("Date start"), db_index=True, default=timezone.now)
|
2017-12-15 09:38:46 +00:00
|
|
|
|
date_end = models.DateTimeField(verbose_name=_("Date end"), null=True)
|
2017-11-14 01:44:16 +00:00
|
|
|
|
|
2018-12-18 03:29:21 +00:00
|
|
|
|
upload_to = 'replay'
|
2019-01-15 02:23:30 +00:00
|
|
|
|
ACTIVE_CACHE_KEY_PREFIX = 'SESSION_ACTIVE_{}'
|
2018-12-18 03:29:21 +00:00
|
|
|
|
|
2019-02-15 03:14:37 +00:00
|
|
|
|
def get_rel_replay_path(self, version=2):
|
2018-12-18 03:29:21 +00:00
|
|
|
|
"""
|
|
|
|
|
获取session日志的文件路径
|
|
|
|
|
:param version: 原来后缀是 .gz,为了统一新版本改为 .replay.gz
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
suffix = '.replay.gz'
|
|
|
|
|
if version == 1:
|
|
|
|
|
suffix = '.gz'
|
2019-02-15 03:14:37 +00:00
|
|
|
|
date = self.date_start.strftime('%Y-%m-%d')
|
2018-12-18 03:29:21 +00:00
|
|
|
|
return os.path.join(date, str(self.id) + suffix)
|
|
|
|
|
|
2019-02-15 03:14:37 +00:00
|
|
|
|
def get_local_path(self, version=2):
|
2018-12-18 03:29:21 +00:00
|
|
|
|
rel_path = self.get_rel_replay_path(version=version)
|
|
|
|
|
if version == 2:
|
|
|
|
|
local_path = os.path.join(self.upload_to, rel_path)
|
|
|
|
|
else:
|
|
|
|
|
local_path = rel_path
|
|
|
|
|
return local_path
|
|
|
|
|
|
2019-03-19 11:09:09 +00:00
|
|
|
|
def can_replay(self):
|
|
|
|
|
if self.has_replay:
|
|
|
|
|
return True
|
|
|
|
|
version = settings.VERSION.split('.')
|
|
|
|
|
if [int(i) for i in version] > [1, 4, 8]:
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
2018-12-18 03:29:21 +00:00
|
|
|
|
def save_to_storage(self, f):
|
|
|
|
|
local_path = self.get_local_path()
|
|
|
|
|
try:
|
|
|
|
|
name = default_storage.save(local_path, f)
|
|
|
|
|
return name, None
|
|
|
|
|
except OSError as e:
|
|
|
|
|
return None, e
|
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
|
@classmethod
|
2019-01-21 09:05:31 +00:00
|
|
|
|
def set_sessions_active(cls, sessions_id):
|
2019-01-15 02:23:30 +00:00
|
|
|
|
data = {cls.ACTIVE_CACHE_KEY_PREFIX.format(i): i for i in sessions_id}
|
|
|
|
|
cache.set_many(data, timeout=5*60)
|
|
|
|
|
|
2019-01-21 09:05:31 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def get_active_sessions(cls):
|
|
|
|
|
return cls.objects.filter(is_finished=False)
|
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
|
def is_active(self):
|
|
|
|
|
if self.protocol in ['ssh', 'telnet']:
|
|
|
|
|
key = self.ACTIVE_CACHE_KEY_PREFIX.format(self.id)
|
|
|
|
|
return bool(cache.get(key))
|
|
|
|
|
return True
|
|
|
|
|
|
2019-01-21 09:05:31 +00:00
|
|
|
|
@property
|
|
|
|
|
def command_amount(self):
|
|
|
|
|
command_store = get_multi_command_storage()
|
|
|
|
|
return command_store.count(session=str(self.id))
|
|
|
|
|
|
2017-11-14 01:44:16 +00:00
|
|
|
|
class Meta:
|
|
|
|
|
db_table = "terminal_session"
|
2017-12-04 08:41:00 +00:00
|
|
|
|
ordering = ["-date_start"]
|
2017-11-14 01:44:16 +00:00
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return "{0.id} of {0.user} to {0.asset}".format(self)
|
|
|
|
|
|
|
|
|
|
|
2017-12-01 09:28:47 +00:00
|
|
|
|
class Task(models.Model):
|
2017-12-03 12:42:36 +00:00
|
|
|
|
NAME_CHOICES = (
|
|
|
|
|
("kill_session", "Kill Session"),
|
|
|
|
|
)
|
|
|
|
|
|
2017-11-23 06:08:01 +00:00
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2017-12-03 12:42:36 +00:00
|
|
|
|
name = models.CharField(max_length=128, choices=NAME_CHOICES, verbose_name=_("Name"))
|
2017-12-15 09:38:46 +00:00
|
|
|
|
args = models.CharField(max_length=1024, verbose_name=_("Args"))
|
2018-03-07 11:05:43 +00:00
|
|
|
|
terminal = models.ForeignKey(Terminal, null=True, on_delete=models.SET_NULL)
|
2017-11-14 01:44:16 +00:00
|
|
|
|
is_finished = models.BooleanField(default=False)
|
2016-10-18 15:49:04 +00:00
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True)
|
2017-11-14 01:44:16 +00:00
|
|
|
|
date_finished = models.DateTimeField(null=True)
|
2016-10-15 10:28:49 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
2017-11-14 01:44:16 +00:00
|
|
|
|
db_table = "terminal_task"
|
2017-11-29 11:27:04 +00:00
|
|
|
|
|
|
|
|
|
|
2017-12-01 09:28:47 +00:00
|
|
|
|
class Command(AbstractSessionCommand):
|
2017-11-29 11:27:04 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
2017-12-01 09:28:47 +00:00
|
|
|
|
db_table = "terminal_command"
|
2017-12-04 08:41:00 +00:00
|
|
|
|
ordering = ('-timestamp',)
|