2018-11-23 02:25:35 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-03-12 08:24:38 +00:00
|
|
|
from django.shortcuts import get_object_or_404, reverse
|
2018-11-23 02:25:35 +00:00
|
|
|
from django.core.files.storage import default_storage
|
|
|
|
from rest_framework import viewsets
|
2019-03-18 03:34:13 +00:00
|
|
|
from rest_framework.response import Response
|
2018-11-23 02:25:35 +00:00
|
|
|
|
2019-07-03 10:03:01 +00:00
|
|
|
from common.utils import is_uuid, get_logger
|
2020-03-12 08:24:38 +00:00
|
|
|
from common.mixins.api import AsyncApiMixin
|
2019-09-12 10:56:26 +00:00
|
|
|
from common.permissions import IsOrgAdminOrAppUser, IsOrgAuditor
|
2019-12-05 07:09:25 +00:00
|
|
|
from common.drf.filters import DatetimeRangeFilter
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
2020-03-12 08:24:38 +00:00
|
|
|
from ..utils import find_session_replay_local, download_session_replay
|
2019-03-18 03:34:13 +00:00
|
|
|
from ..hands import SystemUser
|
2020-03-12 08:24:38 +00:00
|
|
|
from ..models import Session
|
2019-03-18 03:34:13 +00:00
|
|
|
from .. import serializers
|
2018-11-23 02:25:35 +00:00
|
|
|
|
2019-07-03 10:03:01 +00:00
|
|
|
|
|
|
|
__all__ = ['SessionViewSet', 'SessionReplayViewSet',]
|
|
|
|
logger = get_logger(__name__)
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
|
2019-07-03 14:28:20 +00:00
|
|
|
class SessionViewSet(OrgBulkModelViewSet):
|
2019-10-18 07:05:45 +00:00
|
|
|
model = Session
|
2020-03-12 08:24:38 +00:00
|
|
|
serializer_classes = {
|
|
|
|
'default': serializers.SessionSerializer,
|
|
|
|
'display': serializers.SessionDisplaySerializer,
|
|
|
|
}
|
2019-09-25 08:58:41 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser, )
|
2019-10-18 07:05:45 +00:00
|
|
|
filterset_fields = [
|
2019-07-04 07:36:57 +00:00
|
|
|
"user", "asset", "system_user", "remote_addr",
|
|
|
|
"protocol", "terminal", "is_finished",
|
2019-07-03 14:28:20 +00:00
|
|
|
]
|
|
|
|
date_range_filter_fields = [
|
|
|
|
('date_start', ('date_from', 'date_to'))
|
|
|
|
]
|
2019-09-23 06:41:34 +00:00
|
|
|
extra_filter_backends = [DatetimeRangeFilter]
|
2019-07-03 08:29:39 +00:00
|
|
|
|
2019-07-04 09:09:36 +00:00
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
queryset = super().filter_queryset(queryset)
|
2019-06-26 09:29:52 +00:00
|
|
|
# 解决guacamole更新session时并发导致幽灵会话的问题
|
2019-07-04 09:09:36 +00:00
|
|
|
if self.request.method in ('PATCH',):
|
|
|
|
queryset = queryset.select_for_update()
|
2019-01-15 02:23:30 +00:00
|
|
|
return queryset
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
if hasattr(self.request.user, 'terminal'):
|
|
|
|
serializer.validated_data["terminal"] = self.request.user.terminal
|
|
|
|
sid = serializer.validated_data["system_user"]
|
2019-01-15 02:23:30 +00:00
|
|
|
# guacamole提交的是id
|
2018-11-23 02:25:35 +00:00
|
|
|
if is_uuid(sid):
|
2019-06-21 12:57:51 +00:00
|
|
|
_system_user = get_object_or_404(SystemUser, id=sid)
|
|
|
|
serializer.validated_data["system_user"] = _system_user.name
|
2018-11-23 02:25:35 +00:00
|
|
|
return super().perform_create(serializer)
|
|
|
|
|
2019-09-25 08:58:41 +00:00
|
|
|
def get_permissions(self):
|
|
|
|
if self.request.method.lower() in ['get']:
|
|
|
|
self.permission_classes = (IsOrgAdminOrAppUser | IsOrgAuditor, )
|
|
|
|
return super().get_permissions()
|
|
|
|
|
2018-11-23 02:25:35 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class SessionReplayViewSet(AsyncApiMixin, viewsets.ViewSet):
|
2018-11-23 02:25:35 +00:00
|
|
|
serializer_class = serializers.ReplaySerializer
|
2019-09-12 10:56:26 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser | IsOrgAuditor,)
|
2018-11-23 02:25:35 +00:00
|
|
|
session = None
|
2020-03-12 08:24:38 +00:00
|
|
|
download_cache_key = "SESSION_REPLAY_DOWNLOAD_{}"
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
session_id = kwargs.get('pk')
|
2018-12-18 03:29:21 +00:00
|
|
|
session = get_object_or_404(Session, id=session_id)
|
2018-11-23 02:25:35 +00:00
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
|
|
|
|
if serializer.is_valid():
|
|
|
|
file = serializer.validated_data['file']
|
2018-12-18 03:29:21 +00:00
|
|
|
name, err = session.save_to_storage(file)
|
2018-11-23 02:25:35 +00:00
|
|
|
if not name:
|
|
|
|
msg = "Failed save replay `{}`: {}".format(session_id, err)
|
|
|
|
logger.error(msg)
|
|
|
|
return Response({'msg': str(err)}, status=400)
|
|
|
|
url = default_storage.url(name)
|
|
|
|
return Response({'url': url}, status=201)
|
|
|
|
else:
|
|
|
|
msg = 'Upload data invalid: {}'.format(serializer.errors)
|
|
|
|
logger.error(msg)
|
|
|
|
return Response({'msg': serializer.errors}, status=401)
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_replay_data(session, url):
|
2018-12-19 11:55:41 +00:00
|
|
|
tp = 'json'
|
|
|
|
if session.protocol in ('rdp', 'vnc'):
|
|
|
|
tp = 'guacamole'
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
download_url = reverse('terminal:session-replay-download', kwargs={'pk': session.id})
|
|
|
|
data = {
|
|
|
|
'type': tp, 'src': url,
|
|
|
|
'user': session.user, 'asset': session.asset,
|
|
|
|
'system_user': session.system_user,
|
|
|
|
'date_start': session.date_start,
|
|
|
|
'date_end': session.date_end,
|
|
|
|
'download_url': download_url,
|
2019-12-06 03:36:36 +00:00
|
|
|
}
|
2020-03-12 08:24:38 +00:00
|
|
|
return data
|
2018-11-23 02:25:35 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def is_need_async(self):
|
|
|
|
if self.action != 'retrieve':
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
session_id = kwargs.get('pk')
|
|
|
|
session = get_object_or_404(Session, id=session_id)
|
|
|
|
local_path, url = find_session_replay_local(session)
|
|
|
|
|
|
|
|
if not local_path:
|
|
|
|
local_path, url = download_session_replay(session)
|
|
|
|
if not local_path:
|
|
|
|
return Response({"error": url})
|
|
|
|
data = self.get_replay_data(session, url)
|
|
|
|
return Response(data)
|