2022-11-16 13:05:15 +00:00
|
|
|
|
from django.shortcuts import get_object_or_404
|
2023-07-24 03:52:25 +00:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-04-12 09:45:10 +00:00
|
|
|
|
from rest_framework import status
|
2022-11-16 13:05:15 +00:00
|
|
|
|
from rest_framework.decorators import action
|
2022-07-13 08:29:05 +00:00
|
|
|
|
from rest_framework.request import Request
|
2022-11-16 13:05:15 +00:00
|
|
|
|
from rest_framework.response import Response
|
2022-10-25 04:57:34 +00:00
|
|
|
|
|
2022-04-12 09:45:10 +00:00
|
|
|
|
from assets.models import Asset
|
2023-10-12 08:17:32 +00:00
|
|
|
|
from authentication.permissions import IsValidUserOrConnectionToken
|
2023-01-16 11:02:09 +00:00
|
|
|
|
from common.api import JMSBulkModelViewSet
|
2022-04-12 09:45:10 +00:00
|
|
|
|
from orgs.utils import tmp_to_root_org
|
2022-10-25 04:57:34 +00:00
|
|
|
|
from terminal import serializers
|
2022-11-16 13:05:15 +00:00
|
|
|
|
from terminal.models import Session, Endpoint, EndpointRule
|
2022-04-12 09:45:10 +00:00
|
|
|
|
|
|
|
|
|
__all__ = ['EndpointViewSet', 'EndpointRuleViewSet']
|
|
|
|
|
|
|
|
|
|
|
2022-06-28 12:12:55 +00:00
|
|
|
|
class SmartEndpointViewMixin:
|
|
|
|
|
get_serializer: callable
|
2022-07-13 08:29:05 +00:00
|
|
|
|
request: Request
|
|
|
|
|
|
|
|
|
|
# View 处理过程中用的属性
|
|
|
|
|
target_instance: None
|
|
|
|
|
target_protocol: None
|
2022-06-28 12:12:55 +00:00
|
|
|
|
|
2022-11-18 11:29:19 +00:00
|
|
|
|
@action(methods=['get'], detail=False, permission_classes=[IsValidUserOrConnectionToken])
|
2023-02-08 06:27:37 +00:00
|
|
|
|
@tmp_to_root_org()
|
2022-06-28 12:12:55 +00:00
|
|
|
|
def smart(self, request, *args, **kwargs):
|
2022-07-13 08:29:05 +00:00
|
|
|
|
self.target_instance = self.get_target_instance()
|
|
|
|
|
self.target_protocol = self.get_target_protocol()
|
|
|
|
|
if not self.target_protocol:
|
2022-06-28 12:12:55 +00:00
|
|
|
|
error = _('Not found protocol query params')
|
|
|
|
|
return Response(data={'error': error}, status=status.HTTP_404_NOT_FOUND)
|
2022-07-13 08:29:05 +00:00
|
|
|
|
endpoint = self.match_endpoint()
|
2022-06-28 12:12:55 +00:00
|
|
|
|
serializer = self.get_serializer(endpoint)
|
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
2022-07-13 08:29:05 +00:00
|
|
|
|
def match_endpoint(self):
|
|
|
|
|
endpoint = self.match_endpoint_by_label()
|
2022-06-28 12:12:55 +00:00
|
|
|
|
if not endpoint:
|
2022-07-13 08:29:05 +00:00
|
|
|
|
endpoint = self.match_endpoint_by_target_ip()
|
2022-06-28 12:12:55 +00:00
|
|
|
|
return endpoint
|
2022-04-12 09:45:10 +00:00
|
|
|
|
|
2022-07-13 08:29:05 +00:00
|
|
|
|
def match_endpoint_by_label(self):
|
|
|
|
|
return Endpoint.match_by_instance_label(self.target_instance, self.target_protocol)
|
2022-06-28 12:12:55 +00:00
|
|
|
|
|
2022-07-13 08:29:05 +00:00
|
|
|
|
def match_endpoint_by_target_ip(self):
|
2022-09-22 07:52:47 +00:00
|
|
|
|
target_ip = self.request.GET.get('target_ip', '') # 支持target_ip参数,用来方便测试
|
2022-07-13 08:29:05 +00:00
|
|
|
|
if not target_ip and callable(getattr(self.target_instance, 'get_target_ip', None)):
|
|
|
|
|
target_ip = self.target_instance.get_target_ip()
|
2022-09-22 07:52:47 +00:00
|
|
|
|
endpoint = EndpointRule.match_endpoint(
|
|
|
|
|
self.target_instance, target_ip, self.target_protocol, self.request
|
|
|
|
|
)
|
2022-06-28 12:12:55 +00:00
|
|
|
|
return endpoint
|
2022-04-12 09:45:10 +00:00
|
|
|
|
|
2022-07-13 08:29:05 +00:00
|
|
|
|
def get_target_instance(self):
|
|
|
|
|
request = self.request
|
2022-04-12 09:45:10 +00:00
|
|
|
|
asset_id = request.GET.get('asset_id')
|
|
|
|
|
session_id = request.GET.get('session_id')
|
2022-07-11 10:09:06 +00:00
|
|
|
|
token_id = request.GET.get('token')
|
2022-11-18 11:29:19 +00:00
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
if token_id:
|
|
|
|
|
from authentication.models import ConnectionToken
|
|
|
|
|
token = ConnectionToken.objects.filter(id=token_id).first()
|
2022-08-16 08:34:16 +00:00
|
|
|
|
if token and token.asset:
|
|
|
|
|
asset_id = token.asset.id
|
2022-04-12 09:45:10 +00:00
|
|
|
|
if asset_id:
|
|
|
|
|
pk, model = asset_id, Asset
|
|
|
|
|
elif session_id:
|
|
|
|
|
pk, model = session_id, Session
|
|
|
|
|
else:
|
2022-06-28 12:12:55 +00:00
|
|
|
|
pk, model = None, None
|
|
|
|
|
if not pk or not model:
|
|
|
|
|
return None
|
2022-04-12 09:45:10 +00:00
|
|
|
|
with tmp_to_root_org():
|
|
|
|
|
instance = get_object_or_404(model, pk=pk)
|
2022-06-28 12:12:55 +00:00
|
|
|
|
return instance
|
2022-04-12 09:45:10 +00:00
|
|
|
|
|
2022-07-13 08:29:05 +00:00
|
|
|
|
def get_target_protocol(self):
|
|
|
|
|
protocol = None
|
|
|
|
|
if not protocol:
|
|
|
|
|
protocol = self.request.GET.get('protocol')
|
|
|
|
|
return protocol
|
|
|
|
|
|
2022-06-28 12:12:55 +00:00
|
|
|
|
|
|
|
|
|
class EndpointViewSet(SmartEndpointViewMixin, JMSBulkModelViewSet):
|
|
|
|
|
filterset_fields = ('name', 'host')
|
|
|
|
|
search_fields = filterset_fields
|
|
|
|
|
serializer_class = serializers.EndpointSerializer
|
|
|
|
|
queryset = Endpoint.objects.all()
|
2022-04-12 09:45:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EndpointRuleViewSet(JMSBulkModelViewSet):
|
|
|
|
|
filterset_fields = ('name',)
|
|
|
|
|
search_fields = filterset_fields
|
|
|
|
|
serializer_class = serializers.EndpointRuleSerializer
|
|
|
|
|
queryset = EndpointRule.objects.all()
|