mirror of https://github.com/jumpserver/jumpserver
parent
9b6696bb6e
commit
71a3079221
@ -1,10 +1,52 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
import threading
|
||||
import time
|
||||
|
||||
from celery import shared_task
|
||||
from django.core.cache import cache
|
||||
|
||||
from .models import Session
|
||||
|
||||
|
||||
ASSETS_CACHE_KEY = "terminal__session__assets"
|
||||
USERS_CACHE_KEY = "terminal__session__users"
|
||||
SYSTEM_USER_CACHE_KEY = "terminal__session__system_users"
|
||||
CACHE_REFRESH_INTERVAL = 10
|
||||
|
||||
|
||||
# Todo: 定期清理上报history
|
||||
@shared_task
|
||||
def clean_terminal_history():
|
||||
pass
|
||||
|
||||
|
||||
def get_session_asset_list():
|
||||
return set(list(Session.objects.values_list('asset', flat=True)))
|
||||
|
||||
|
||||
def get_session_user_list():
|
||||
return set(list(Session.objects.values_list('user', flat=True)))
|
||||
|
||||
|
||||
def get_session_system_user_list():
|
||||
return set(list(Session.objects.values_list('system_user', flat=True)))
|
||||
|
||||
|
||||
def set_cache():
|
||||
while True:
|
||||
assets = get_session_asset_list()
|
||||
users = get_session_user_list()
|
||||
system_users = get_session_system_user_list()
|
||||
|
||||
cache.set(ASSETS_CACHE_KEY, assets)
|
||||
cache.set(USERS_CACHE_KEY, users)
|
||||
cache.set(SYSTEM_USER_CACHE_KEY, system_users)
|
||||
time.sleep(10)
|
||||
|
||||
|
||||
def main():
|
||||
thread = threading.Thread(target=set_cache)
|
||||
thread.start()
|
||||
|
||||
main()
|
||||
|
@ -1,159 +0,0 @@
|
||||
{% extends '_base_list.html' %}
|
||||
{% load i18n %}
|
||||
{% load static %}
|
||||
{% block content_left_head %}
|
||||
<link href="{% static 'css/plugins/datepicker/datepicker3.css' %}" rel="stylesheet">
|
||||
<style>
|
||||
#search_btn {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
|
||||
{% block table_search %}
|
||||
<form id="search_form" method="get" action="" class="pull-right form-inline">
|
||||
<div class="form-group" id="date">
|
||||
<div class="input-daterange input-group" id="datepicker">
|
||||
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
|
||||
<input type="text" class="input-sm form-control" style="width: 100px;" name="date_from" value="{{ date_from }}">
|
||||
<span class="input-group-addon">to</span>
|
||||
<input type="text" class="input-sm form-control" style="width: 100px;" name="date_to" value="{{ date_to }}">
|
||||
</div>
|
||||
</div>
|
||||
<div class="input-group">
|
||||
<select class="select2 form-control" name="username">
|
||||
<option value="">{% trans 'User' %}</option>
|
||||
{% for u in user_list %}
|
||||
<option value="{{ u }}" {% if u == username %} selected {% endif %}>{{ u }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="input-group">
|
||||
<select class="select2 form-control" name="ip">
|
||||
<option value="">{% trans 'Asset' %}</option>
|
||||
{% for a in asset_list %}
|
||||
<option value="{{ a }}" {% if a == ip %} selected {% endif %}>{{ a }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="input-group">
|
||||
<select class="select2 form-control" name="system_user">
|
||||
<option value="">{% trans 'System user' %}</option>
|
||||
{% for su in system_user_list %}
|
||||
<option value="{{ su }}" {% if su == system_user %} selected {% endif %}>{{ su }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="input-group">
|
||||
<input type="text" class="form-control input-sm" name="keyword" placeholder="Keyword" value="{{ keyword }}">
|
||||
</div>
|
||||
<div class="input-group">
|
||||
<div class="input-group-btn">
|
||||
<button id='search_btn' type="submit" class="btn btn-sm btn-primary">
|
||||
搜索
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
||||
{% block table_head %}
|
||||
<th class="text-center"></th>
|
||||
<th class="text-center">{% trans 'ID' %}</th>
|
||||
<th class="text-center">{% trans 'User' %}</th>
|
||||
<th class="text-center">{% trans 'Asset' %}</th>
|
||||
<th class="text-center">{% trans 'System user' %}</th>
|
||||
<th class="text-center">{% trans 'Terminal' %}</th>
|
||||
<th class="text-center">{% trans 'Command' %}</th>
|
||||
<th class="text-center">{% trans 'Success' %}</th>
|
||||
<th class="text-center">{% trans 'Finished' %}</th>
|
||||
<th class="text-center">{% trans 'Play' %}</th>
|
||||
<th class="text-center">{% trans 'Date start' %}</th>
|
||||
<th class="text-center">{% trans 'Time' %}</th>
|
||||
{% endblock %}
|
||||
|
||||
{% block table_body %}
|
||||
{% for proxy_log in proxy_log_list %}
|
||||
<tr class="gradeX">
|
||||
<td class="text-center"><input type="checkbox" class="cbx-term" value="{{ proxy_log.id }}"></td>
|
||||
<td class="text-center">
|
||||
<a href="{% url 'audits:proxy-log-detail' pk=proxy_log.id %}">{{ proxy_log.id }}</a>
|
||||
</td>
|
||||
<td class="text-center">{{ proxy_log.user }}</td>
|
||||
<td class="text-center">{{ proxy_log.asset }}</td>
|
||||
<td class="text-center">{{ proxy_log.system_user }}</td>
|
||||
<td class="text-center">{{ proxy_log.terminal }}</td>
|
||||
<td class="text-center">{{ proxy_log.commands.all|length}}</td>
|
||||
<td class="text-center">
|
||||
{% if proxy_log.is_failed %}
|
||||
<i class="fa fa-times text-danger"></i>
|
||||
{% else %}
|
||||
<i class="fa fa-check text-navy"></i>
|
||||
{% endif %}
|
||||
</td>
|
||||
{% if proxy_log.is_finished %}
|
||||
<td class="text-center">
|
||||
<i class="fa fa-check text-navy"></i>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<a><span class="text-navy"><i class="fa fa-play-circle"></i></span></a>
|
||||
</td>
|
||||
{% else %}
|
||||
<td class="text-center">
|
||||
<a class="btn-term" value="{{ proxy_log.id }}"><i class="fa fa-times text-danger"></i></a>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<a><span class="text-danger"><i class="fa fa-eye"></i></span></a>
|
||||
</td>
|
||||
{% endif %}
|
||||
<td class="text-center">{{ proxy_log.date_start }}</td>
|
||||
<td class="text-center">{{ proxy_log.date_finished|timeuntil:proxy_log.date_start }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{% endblock %}
|
||||
|
||||
|
||||
{% block custom_foot_js %}
|
||||
<script src="{% static 'js/plugins/datepicker/bootstrap-datepicker.js' %}"></script>
|
||||
<script>
|
||||
function terminateConnection(data) {
|
||||
function success() {
|
||||
window.setTimeout(function () {
|
||||
window.location.reload()
|
||||
}, 300)
|
||||
}
|
||||
var the_url = "";
|
||||
APIUpdateAttr({url: the_url, method: 'POST', body: JSON.stringify(data), success: success, success_message: 'Terminate success'});
|
||||
}
|
||||
$(document).ready(function() {
|
||||
$('table').DataTable({
|
||||
"searching": false,
|
||||
"paging": false,
|
||||
"bInfo" : false,
|
||||
"order": []
|
||||
});
|
||||
$('.select2').select2();
|
||||
$('#date .input-daterange').datepicker({
|
||||
dateFormat: 'mm/dd/yy',
|
||||
keyboardNavigation: false,
|
||||
forceParse: false,
|
||||
autoclose: true
|
||||
});
|
||||
}).on('click', '.btn-term', function () {
|
||||
var $this = $(this);
|
||||
var proxy_log_id = $this.attr('value');
|
||||
var data = {
|
||||
proxy_log_id: proxy_log_id
|
||||
};
|
||||
terminateConnection(data)
|
||||
}).on('click', '#btn_bulk_update', function () {
|
||||
var data = [];
|
||||
$('.cbx-term:checked').each(function () {
|
||||
data.push({proxy_log_id: $(this).attr('value')})
|
||||
});
|
||||
terminateConnection(data)
|
||||
})
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from django.core.cache import cache
|
||||
|
||||
from .tasks import USERS_CACHE_KEY, ASSETS_CACHE_KEY, SYSTEM_USER_CACHE_KEY
|
||||
|
||||
|
||||
def get_user_list_from_cache():
|
||||
return cache.get(USERS_CACHE_KEY)
|
||||
|
||||
|
||||
def get_asset_list_from_cache():
|
||||
return cache.get(ASSETS_CACHE_KEY)
|
||||
|
||||
|
||||
def get_system_user_list_from_cache():
|
||||
return cache.get(SYSTEM_USER_CACHE_KEY)
|
||||
|
||||
|
||||
|
@ -0,0 +1,84 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from datetime import datetime
|
||||
|
||||
from django.views.generic import ListView
|
||||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from ..models import Command
|
||||
from .. import utils
|
||||
from ..backends import get_command_store
|
||||
|
||||
__all__ = ['CommandListView']
|
||||
command_store = get_command_store()
|
||||
|
||||
|
||||
class CommandListView(ListView):
|
||||
model = Command
|
||||
template_name = "terminal/command_list.html"
|
||||
context_object_name = 'command_list'
|
||||
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
|
||||
command = user = asset = system_user = date_from_s = date_to_s = ''
|
||||
date_format = '%m/%d/%Y'
|
||||
|
||||
def get_queryset(self):
|
||||
date_to_default = timezone.now()
|
||||
date_from_default = timezone.now() - timezone.timedelta(7)
|
||||
date_to_default_s = date_to_default.strftime(self.date_format)
|
||||
date_from_default_s = date_from_default.strftime(self.date_format)
|
||||
|
||||
self.command = self.request.GET.get('command', '')
|
||||
self.user = self.request.GET.get('user')
|
||||
self.asset = self.request.GET.get('asset')
|
||||
self.system_user = self.request.GET.get('system_user')
|
||||
self.date_from_s = self.request.GET.get('date_from', date_from_default_s)
|
||||
self.date_to_s = self.request.GET.get('date_to', date_to_default_s)
|
||||
|
||||
filter_kwargs = {}
|
||||
if self.date_from_s:
|
||||
date_from = datetime.strptime(self.date_from_s, self.date_format)
|
||||
date_from = date_from.replace(
|
||||
tzinfo=timezone.get_current_timezone()
|
||||
)
|
||||
filter_kwargs['date_from'] = date_from
|
||||
if self.date_to_s:
|
||||
date_to = timezone.datetime.strptime(
|
||||
self.date_to_s + ' 23:59:59', '%m/%d/%Y %H:%M:%S')
|
||||
date_to = date_to.replace(tzinfo=timezone.get_current_timezone())
|
||||
filter_kwargs['date_to'] = date_to
|
||||
if self.user:
|
||||
filter_kwargs['user'] = self.user
|
||||
if self.asset:
|
||||
filter_kwargs['asset'] = self.asset
|
||||
if self.system_user:
|
||||
filter_kwargs['system_user'] = self.system_user
|
||||
if self.command:
|
||||
filter_kwargs['input'] = self.command
|
||||
if filter_kwargs:
|
||||
self.queryset = command_store.filter(**filter_kwargs)
|
||||
return self.queryset
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = {
|
||||
'app': _('Terminal'),
|
||||
'action': _('Command list'),
|
||||
'user_list': utils.get_user_list_from_cache(),
|
||||
'asset_list': utils.get_asset_list_from_cache(),
|
||||
'system_user_list': utils.get_system_user_list_from_cache(),
|
||||
'command': self.command,
|
||||
'date_from': self.date_from_s,
|
||||
'date_to': self.date_to_s,
|
||||
'user': self.user,
|
||||
'asset': self.asset,
|
||||
'system_user': self.system_user,
|
||||
}
|
||||
kwargs.update(context)
|
||||
return super().get_context_data(**kwargs)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in new issue