2018-11-23 02:25:35 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from collections import OrderedDict
|
|
|
|
import logging
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
from django.core.cache import cache
|
2020-07-29 06:57:44 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2018-11-23 02:25:35 +00:00
|
|
|
from rest_framework import viewsets
|
|
|
|
from rest_framework.views import APIView, Response
|
|
|
|
from rest_framework.permissions import AllowAny
|
|
|
|
|
2020-07-29 06:57:44 +00:00
|
|
|
from common.drf.api import JMSBulkModelViewSet
|
2018-11-23 02:25:35 +00:00
|
|
|
from common.utils import get_object_or_none
|
|
|
|
from common.permissions import IsAppUser, IsOrgAdminOrAppUser, IsSuperUser
|
2019-03-18 03:34:13 +00:00
|
|
|
from ..models import Terminal, Status, Session
|
|
|
|
from .. import serializers
|
2020-07-29 06:57:44 +00:00
|
|
|
from .. import exceptions
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'TerminalViewSet', 'TerminalTokenApi', 'StatusViewSet', 'TerminalConfig',
|
|
|
|
]
|
|
|
|
logger = logging.getLogger(__file__)
|
|
|
|
|
|
|
|
|
2020-07-29 06:57:44 +00:00
|
|
|
class TerminalViewSet(JMSBulkModelViewSet):
|
2018-11-23 02:25:35 +00:00
|
|
|
queryset = Terminal.objects.filter(is_deleted=False)
|
|
|
|
serializer_class = serializers.TerminalSerializer
|
|
|
|
permission_classes = (IsSuperUser,)
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ['name', 'remote_addr', 'type']
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
2020-07-29 06:57:44 +00:00
|
|
|
if isinstance(request.data, list):
|
|
|
|
raise exceptions.BulkCreateNotSupport()
|
|
|
|
|
2018-11-23 02:25:35 +00:00
|
|
|
name = request.data.get('name')
|
|
|
|
remote_ip = request.META.get('REMOTE_ADDR')
|
|
|
|
x_real_ip = request.META.get('X-Real-IP')
|
|
|
|
remote_addr = x_real_ip or remote_ip
|
|
|
|
|
|
|
|
terminal = get_object_or_none(Terminal, name=name, is_deleted=False)
|
|
|
|
if terminal:
|
|
|
|
msg = 'Terminal name %s already used' % name
|
|
|
|
return Response({'msg': msg}, status=409)
|
|
|
|
|
|
|
|
serializer = self.serializer_class(data={
|
|
|
|
'name': name, 'remote_addr': remote_addr
|
|
|
|
})
|
|
|
|
|
|
|
|
if serializer.is_valid():
|
|
|
|
terminal = serializer.save()
|
|
|
|
|
|
|
|
# App should use id, token get access key, if accepted
|
|
|
|
token = uuid.uuid4().hex
|
|
|
|
cache.set(token, str(terminal.id), 3600)
|
|
|
|
data = {"id": str(terminal.id), "token": token, "msg": "Need accept"}
|
|
|
|
return Response(data, status=201)
|
|
|
|
else:
|
|
|
|
data = serializer.errors
|
|
|
|
logger.error("Register terminal error: {}".format(data))
|
|
|
|
return Response(data, status=400)
|
|
|
|
|
2020-12-10 12:50:22 +00:00
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
queryset = super().filter_queryset(queryset)
|
|
|
|
status = self.request.query_params.get('status')
|
|
|
|
if not status:
|
|
|
|
return queryset
|
|
|
|
filtered_queryset_id = [str(q.id) for q in queryset if q.status == status]
|
|
|
|
queryset = queryset.filter(id__in=filtered_queryset_id)
|
|
|
|
return queryset
|
|
|
|
|
2018-11-23 02:25:35 +00:00
|
|
|
def get_permissions(self):
|
|
|
|
if self.action == "create":
|
|
|
|
self.permission_classes = (AllowAny,)
|
|
|
|
return super().get_permissions()
|
|
|
|
|
|
|
|
|
|
|
|
class TerminalTokenApi(APIView):
|
|
|
|
permission_classes = (AllowAny,)
|
|
|
|
queryset = Terminal.objects.filter(is_deleted=False)
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
terminal = self.queryset.get(id=kwargs.get('terminal'))
|
|
|
|
except Terminal.DoesNotExist:
|
|
|
|
terminal = None
|
|
|
|
|
|
|
|
token = request.query_params.get("token")
|
|
|
|
|
|
|
|
if terminal is None:
|
|
|
|
return Response('May be reject by administrator', status=401)
|
|
|
|
|
|
|
|
if token is None or cache.get(token, "") != str(terminal.id):
|
|
|
|
return Response('Token is not valid', status=401)
|
|
|
|
|
|
|
|
if not terminal.is_accepted:
|
|
|
|
return Response("Terminal was not accepted yet", status=400)
|
|
|
|
|
2018-12-13 02:06:51 +00:00
|
|
|
if not terminal.user or not terminal.user.access_key:
|
2018-11-23 02:25:35 +00:00
|
|
|
return Response("No access key generate", status=401)
|
|
|
|
|
2018-12-25 05:33:37 +00:00
|
|
|
access_key = terminal.user.access_key()
|
2018-11-23 02:25:35 +00:00
|
|
|
data = OrderedDict()
|
|
|
|
data['access_key'] = {'id': access_key.id, 'secret': access_key.secret}
|
|
|
|
return Response(data, status=200)
|
|
|
|
|
|
|
|
|
|
|
|
class StatusViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = Status.objects.all()
|
|
|
|
serializer_class = serializers.StatusSerializer
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
session_serializer_class = serializers.SessionSerializer
|
|
|
|
task_serializer_class = serializers.TaskSerializer
|
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
2019-01-15 02:23:30 +00:00
|
|
|
self.handle_sessions()
|
2018-11-23 02:25:35 +00:00
|
|
|
tasks = self.request.user.terminal.task_set.filter(is_finished=False)
|
|
|
|
serializer = self.task_serializer_class(tasks, many=True)
|
|
|
|
return Response(serializer.data, status=201)
|
|
|
|
|
|
|
|
def handle_sessions(self):
|
2019-01-15 02:23:30 +00:00
|
|
|
sessions_id = self.request.data.get('sessions', [])
|
2020-02-03 05:54:59 +00:00
|
|
|
# guacamole 上报的 session 是字符串
|
|
|
|
# "[53cd3e47-210f-41d8-b3c6-a184f3, 53cd3e47-210f-41d8-b3c6-a184f4]"
|
|
|
|
if isinstance(sessions_id, str):
|
|
|
|
sessions_id = sessions_id[1:-1].split(',')
|
|
|
|
sessions_id = [sid.strip() for sid in sessions_id if sid.strip()]
|
2019-01-21 09:05:31 +00:00
|
|
|
Session.set_sessions_active(sessions_id)
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
terminal_id = self.kwargs.get("terminal", None)
|
|
|
|
if terminal_id:
|
|
|
|
terminal = get_object_or_404(Terminal, id=terminal_id)
|
|
|
|
self.queryset = terminal.status_set.all()
|
|
|
|
return self.queryset
|
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
serializer.validated_data["terminal"] = self.request.user.terminal
|
|
|
|
return super().perform_create(serializer)
|
|
|
|
|
|
|
|
def get_permissions(self):
|
|
|
|
if self.action == "create":
|
|
|
|
self.permission_classes = (IsAppUser,)
|
|
|
|
return super().get_permissions()
|
|
|
|
|
|
|
|
|
|
|
|
class TerminalConfig(APIView):
|
|
|
|
permission_classes = (IsAppUser,)
|
|
|
|
|
|
|
|
def get(self, request):
|
2021-01-12 10:06:42 +00:00
|
|
|
config = request.user.terminal.config
|
|
|
|
return Response(config, status=200)
|