2018-11-23 02:25:35 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import os
|
|
|
|
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from django.core.files.storage import default_storage
|
|
|
|
from django.http import HttpResponseNotFound
|
|
|
|
from django.conf import settings
|
|
|
|
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
|
|
|
import jms_storage
|
|
|
|
|
2019-07-03 10:03:01 +00:00
|
|
|
from common.utils import is_uuid, get_logger
|
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
|
2019-03-18 03:34:13 +00:00
|
|
|
from ..hands import SystemUser
|
2019-12-06 03:36:36 +00:00
|
|
|
from ..models import Session, ReplayStorage
|
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
|
2018-11-23 02:25:35 +00:00
|
|
|
serializer_class = serializers.SessionSerializer
|
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
|
|
|
|
|
|
|
class SessionReplayViewSet(viewsets.ViewSet):
|
|
|
|
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
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
def retrieve(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
|
|
|
|
2018-12-19 11:55:41 +00:00
|
|
|
tp = 'json'
|
|
|
|
if session.protocol in ('rdp', 'vnc'):
|
|
|
|
tp = 'guacamole'
|
|
|
|
|
|
|
|
data = {'type': tp, 'src': ''}
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
# 新版本和老版本的文件后缀不同
|
2018-12-18 03:29:21 +00:00
|
|
|
session_path = session.get_rel_replay_path() # 存在外部存储上的路径
|
|
|
|
local_path = session.get_local_path()
|
|
|
|
local_path_v1 = session.get_local_path(version=1)
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
# 去default storage中查找
|
|
|
|
for _local_path in (local_path, local_path_v1, session_path):
|
|
|
|
if default_storage.exists(_local_path):
|
|
|
|
url = default_storage.url(_local_path)
|
|
|
|
data['src'] = url
|
|
|
|
return Response(data)
|
|
|
|
|
2019-12-06 03:36:36 +00:00
|
|
|
replay_storages = ReplayStorage.objects.all()
|
|
|
|
configs = {
|
|
|
|
storage.name: storage.config
|
|
|
|
for storage in replay_storages
|
|
|
|
if not storage.in_defaults()
|
|
|
|
}
|
2018-11-23 02:25:35 +00:00
|
|
|
if not configs:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
target_path = os.path.join(default_storage.base_location, local_path) # 保存到storage的路径
|
|
|
|
target_dir = os.path.dirname(target_path)
|
|
|
|
if not os.path.isdir(target_dir):
|
|
|
|
os.makedirs(target_dir, exist_ok=True)
|
|
|
|
storage = jms_storage.get_multi_object_storage(configs)
|
|
|
|
ok, err = storage.download(session_path, target_path)
|
|
|
|
if not ok:
|
|
|
|
logger.error("Failed download replay file: {}".format(err))
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
data['src'] = default_storage.url(local_path)
|
|
|
|
return Response(data)
|
|
|
|
|