From 9c38f39e5b18d4ec5ddafe8ff40634ea29b49076 Mon Sep 17 00:00:00 2001 From: liuzheng712 Date: Tue, 29 Mar 2016 17:20:05 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E7=B4=A7=E6=80=A5=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=9B=91=E6=8E=A7=E7=99=BD=E5=B1=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jlog/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jlog/views.py b/jlog/views.py index c3628d391..7c2627de8 100644 --- a/jlog/views.py +++ b/jlog/views.py @@ -317,7 +317,7 @@ class TermLogRecorder(object): if self._lists: self.file = self._lists.get(filename=filename) else: - self.file = TermLog.objects.get(user=user.id, filename=filename) + self.file = TermLog.objects.get(filename=filename) if self.file.logPath == 'locale': return self.file.log else: From cf1da2a4201fbe7b261fc5af1d6de945e00bbd96 Mon Sep 17 00:00:00 2001 From: liuzheng712 Date: Tue, 29 Mar 2016 20:56:19 +0800 Subject: [PATCH 2/8] bugfix --- jlog/log_api.py | 8 ++++++++ jlog/views.py | 11 +++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/jlog/log_api.py b/jlog/log_api.py index cce830c03..669960312 100644 --- a/jlog/log_api.py +++ b/jlog/log_api.py @@ -79,6 +79,14 @@ def renderTemplate(script_path, time_file_path, dimensions=(24, 80), templatenam return rendered +def renderJSON(script_path, time_file_path): + with copen(script_path, encoding='utf-8', errors='replace', newline='\r\n') as scriptf: + # with open(script_path) as scriptf: + with open(time_file_path) as timef: + timing = getTiming(timef) + json = scriptToJSON(scriptf, timing) + return json + def kill_invalid_connection(): unfinished_logs = Log.objects.filter(is_finished=False) now = datetime.datetime.now() diff --git a/jlog/views.py b/jlog/views.py index 7c2627de8..09c79a9d8 100644 --- a/jlog/views.py +++ b/jlog/views.py @@ -5,7 +5,7 @@ from django.shortcuts import render_to_response, render from jumpserver.api import * from jperm.perm_api import user_have_perm from django.http import HttpResponseNotFound -from jlog.log_api import renderTemplate +from jlog.log_api import renderJSON from jlog.models import Log, ExecLog, FileLog, TermLog from jumpserver.settings import LOG_DIR @@ -138,7 +138,14 @@ def log_record(request): if log_id: TermL = TermLogRecorder(request.user) log = Log.objects.get(id=int(log_id)) - return HttpResponse(TermL.load_full_log(filename=log.filename)) + if len(log.filename) == 0: + log_file = log.log_path + '.log' + log_time = log.log_path + '.time' + if os.path.isfile(log_file) and os.path.isfile(log_time): + content = renderJSON(log_file, log_time) + return HttpResponse(content) + else: + return HttpResponse(TermL.load_full_log(filename=log.filename)) else: return HttpResponse("ERROR") else: From 1c61ed6a8bcb80faca0fb6550299c8715684cc54 Mon Sep 17 00:00:00 2001 From: liuzheng712 Date: Tue, 29 Mar 2016 21:05:29 +0800 Subject: [PATCH 3/8] bugfix --- jlog/log_api.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/jlog/log_api.py b/jlog/log_api.py index 669960312..89b76d4be 100644 --- a/jlog/log_api.py +++ b/jlog/log_api.py @@ -84,7 +84,16 @@ def renderJSON(script_path, time_file_path): # with open(script_path) as scriptf: with open(time_file_path) as timef: timing = getTiming(timef) - json = scriptToJSON(scriptf, timing) + ret = {} + with closing(scriptf): + scriptf.readline() # ignore first header line from script file + offset = 0 + for t in timing: + dt = scriptf.read(t[1]) + data = escapeString(dt) + # print ('###### (%s, %s)' % (t[1], repr(data))) + offset += t[0] + ret[str(offset/float(1000))] = data return json def kill_invalid_connection(): From e07305ef46ed872f096de4f8a40f421d46923af9 Mon Sep 17 00:00:00 2001 From: liuzheng712 Date: Tue, 29 Mar 2016 21:06:46 +0800 Subject: [PATCH 4/8] bugfix --- jlog/log_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jlog/log_api.py b/jlog/log_api.py index 89b76d4be..c870869fa 100644 --- a/jlog/log_api.py +++ b/jlog/log_api.py @@ -94,7 +94,7 @@ def renderJSON(script_path, time_file_path): # print ('###### (%s, %s)' % (t[1], repr(data))) offset += t[0] ret[str(offset/float(1000))] = data - return json + return dumps(ret) def kill_invalid_connection(): unfinished_logs = Log.objects.filter(is_finished=False) From 2f2289a863c75118b93587053ceef084c4e5a38e Mon Sep 17 00:00:00 2001 From: liuzheng712 Date: Tue, 29 Mar 2016 21:16:30 +0800 Subject: [PATCH 5/8] bugfix --- jlog/log_api.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/jlog/log_api.py b/jlog/log_api.py index c870869fa..ab596c01b 100644 --- a/jlog/log_api.py +++ b/jlog/log_api.py @@ -90,7 +90,10 @@ def renderJSON(script_path, time_file_path): offset = 0 for t in timing: dt = scriptf.read(t[1]) - data = escapeString(dt) + try: + data = dt.encode('unicode_escape').decode('utf-8', 'ignore') + except (UnicodeEncodeError, UnicodeDecodeError): + data = dt.decode('utf-8', 'ignore') # print ('###### (%s, %s)' % (t[1], repr(data))) offset += t[0] ret[str(offset/float(1000))] = data From 263ff1ee08b994066f945fae770b5c87bd0c4464 Mon Sep 17 00:00:00 2001 From: liuzheng712 Date: Tue, 29 Mar 2016 21:18:04 +0800 Subject: [PATCH 6/8] bugfix --- jlog/log_api.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/jlog/log_api.py b/jlog/log_api.py index ab596c01b..356c9d97c 100644 --- a/jlog/log_api.py +++ b/jlog/log_api.py @@ -90,13 +90,13 @@ def renderJSON(script_path, time_file_path): offset = 0 for t in timing: dt = scriptf.read(t[1]) - try: - data = dt.encode('unicode_escape').decode('utf-8', 'ignore') - except (UnicodeEncodeError, UnicodeDecodeError): - data = dt.decode('utf-8', 'ignore') + # try: + # data = dt.encode('unicode_escape').decode('utf-8', 'ignore') + # except (UnicodeEncodeError, UnicodeDecodeError): + # data = dt.decode('utf-8', 'ignore') # print ('###### (%s, %s)' % (t[1], repr(data))) offset += t[0] - ret[str(offset/float(1000))] = data + ret[str(offset/float(1000))] = dt return dumps(ret) def kill_invalid_connection(): From a1187757bc13e737875a63982e6699c1091a7f6f Mon Sep 17 00:00:00 2001 From: liuzheng712 Date: Tue, 29 Mar 2016 21:22:18 +0800 Subject: [PATCH 7/8] bugfix --- jlog/log_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jlog/log_api.py b/jlog/log_api.py index 356c9d97c..3dccc0a45 100644 --- a/jlog/log_api.py +++ b/jlog/log_api.py @@ -96,7 +96,7 @@ def renderJSON(script_path, time_file_path): # data = dt.decode('utf-8', 'ignore') # print ('###### (%s, %s)' % (t[1], repr(data))) offset += t[0] - ret[str(offset/float(1000))] = dt + ret[str(offset/float(1000))] = dt.decode('utf-8', 'replace') return dumps(ret) def kill_invalid_connection(): From 1efede4de85794acc8d563c8b659a8b8ca0ec366 Mon Sep 17 00:00:00 2001 From: liuzheng712 Date: Tue, 29 Mar 2016 21:37:56 +0800 Subject: [PATCH 8/8] bugfix --- jlog/log_api.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/jlog/log_api.py b/jlog/log_api.py index 3dccc0a45..505a778c8 100644 --- a/jlog/log_api.py +++ b/jlog/log_api.py @@ -90,11 +90,6 @@ def renderJSON(script_path, time_file_path): offset = 0 for t in timing: dt = scriptf.read(t[1]) - # try: - # data = dt.encode('unicode_escape').decode('utf-8', 'ignore') - # except (UnicodeEncodeError, UnicodeDecodeError): - # data = dt.decode('utf-8', 'ignore') - # print ('###### (%s, %s)' % (t[1], repr(data))) offset += t[0] ret[str(offset/float(1000))] = dt.decode('utf-8', 'replace') return dumps(ret)