mirror of https://github.com/jumpserver/jumpserver
update
parent
17a7470a2a
commit
b3f83c3362
119
jlog/views.py
119
jlog/views.py
|
@ -162,17 +162,25 @@ class TermLogRecorder(object):
|
||||||
self.log is the all output with delta time log.
|
self.log is the all output with delta time log.
|
||||||
self.vim_pattern is the regexp for check vi/vim/fg model.
|
self.vim_pattern is the regexp for check vi/vim/fg model.
|
||||||
Usage:
|
Usage:
|
||||||
recorder = TermLogRecorder(User)
|
recorder = TermLogRecorder(user=UserObject) # or recorder = TermLogRecorder(uid=UserID)
|
||||||
recoder.write(messages)
|
recoder.write(messages)
|
||||||
recoder.save() # save all log into database
|
recoder.save() # save all log into database
|
||||||
|
# The following methods all have `user`,`uid`,args. Same as __init__
|
||||||
list = recoder.list() # will give a object about this user's all log info
|
list = recoder.list() # will give a object about this user's all log info
|
||||||
recoder.load_full_log(filemane) # will get full log
|
recoder.load_full_log(filemane) # will get full log
|
||||||
recoder.load_history(filename) # will only get the command history list
|
recoder.load_history(filename) # will only get the command history list
|
||||||
|
recoder.share_to(filename,user=UserObject) # or recoder.share_to(filename,uid=UserID). will share this commands to someone
|
||||||
|
recoder.unshare_to(filename,user=UserObject) # or recoder.unshare_to(filename,uid=UserID). will unshare this commands to someone
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, user):
|
def __init__(self, user=None, uid=None):
|
||||||
self.log = {}
|
self.log = {}
|
||||||
self.user = user
|
if isinstance(user, User):
|
||||||
|
self.user = user
|
||||||
|
elif uid:
|
||||||
|
self.user = User.objects.get(id=uid)
|
||||||
|
else:
|
||||||
|
self.user = None
|
||||||
self.recoderStartTime = time.time()
|
self.recoderStartTime = time.time()
|
||||||
self.__init_screen_stream()
|
self.__init_screen_stream()
|
||||||
self.recoder = True
|
self.recoder = True
|
||||||
|
@ -227,6 +235,8 @@ class TermLogRecorder(object):
|
||||||
date = datetime.datetime.now().strftime('%Y%m%d')
|
date = datetime.datetime.now().strftime('%Y%m%d')
|
||||||
filename = str(uuid.uuid4())
|
filename = str(uuid.uuid4())
|
||||||
filepath = os.path.join(path, 'tty', date, filename + '.zip')
|
filepath = os.path.join(path, 'tty', date, filename + '.zip')
|
||||||
|
if not os.path.isdir(os.path.join(path, 'tty', date)):
|
||||||
|
os.makedirs(os.path.join(path, 'tty', date), mode=0777)
|
||||||
while os.path.isfile(filepath):
|
while os.path.isfile(filepath):
|
||||||
filename = str(uuid.uuid4())
|
filename = str(uuid.uuid4())
|
||||||
filepath = os.path.join(path, 'tty', date, filename + '.zip')
|
filepath = os.path.join(path, 'tty', date, filename + '.zip')
|
||||||
|
@ -238,41 +248,90 @@ class TermLogRecorder(object):
|
||||||
zf.close()
|
zf.close()
|
||||||
record = TermLog.objects.create(logPath=filepath, logPWD=password, filename=filename,
|
record = TermLog.objects.create(logPath=filepath, logPWD=password, filename=filename,
|
||||||
history=json.dumps(self.CMD), timestamp=int(self.recoderStartTime))
|
history=json.dumps(self.CMD), timestamp=int(self.recoderStartTime))
|
||||||
record.user.add(self.user)
|
if self.user:
|
||||||
|
record.user.add(self.user)
|
||||||
except:
|
except:
|
||||||
record = TermLog.objects.create(logPath='locale', logPWD=password, log=json.dumps(self.log),
|
record = TermLog.objects.create(logPath='locale', logPWD=password, log=json.dumps(self.log),
|
||||||
filename=filename, history=json.dumps(self.CMD),
|
filename=filename, history=json.dumps(self.CMD),
|
||||||
timestamp=int(self.recoderStartTime))
|
timestamp=int(self.recoderStartTime))
|
||||||
record.user.add(self.user)
|
if self.user:
|
||||||
|
record.user.add(self.user)
|
||||||
|
|
||||||
def list(self):
|
def list(self, user=None, uid=None):
|
||||||
tmp = []
|
tmp = []
|
||||||
self._lists = TermLog.objects.filter(user=self.user.id)
|
if isinstance(user, User):
|
||||||
for i in self._lists.all():
|
user = user
|
||||||
tmp.append(
|
elif uid:
|
||||||
{'filename': i.filename, 'locale': i.logPath == 'locale', 'nick': i.nick, 'timestamp': i.timestamp,
|
user = User.objects.get(id=uid)
|
||||||
'date': i.datetimestamp})
|
else:
|
||||||
|
user = self.user
|
||||||
|
if user:
|
||||||
|
self._lists = TermLog.objects.filter(user=user.id)
|
||||||
|
for i in self._lists.all():
|
||||||
|
tmp.append(
|
||||||
|
{'filename': i.filename, 'locale': i.logPath == 'locale', 'nick': i.nick, 'timestamp': i.timestamp,
|
||||||
|
'date': i.datetimestamp})
|
||||||
return tmp
|
return tmp
|
||||||
|
|
||||||
def load_full_log(self, filename):
|
def load_full_log(self, filename, user=None, uid=None):
|
||||||
if self._lists:
|
if isinstance(user, User):
|
||||||
self.file = self._lists.get(filename=filename)
|
user = user
|
||||||
|
elif uid:
|
||||||
|
user = User.objects.get(id=uid)
|
||||||
else:
|
else:
|
||||||
self.file = TermLog.objects.get(user=self.user.id, filename=filename)
|
user = self.user
|
||||||
if self.file.logPath == 'locale':
|
if user:
|
||||||
return self.file.log
|
if self._lists:
|
||||||
else:
|
self.file = self._lists.get(filename=filename)
|
||||||
try:
|
else:
|
||||||
zf = zipfile.ZipFile(self.file.logPath, 'r', zipfile.ZIP_DEFLATED)
|
self.file = TermLog.objects.get(user=user.id, filename=filename)
|
||||||
zf.setpassword(self.file.logPWD)
|
if self.file.logPath == 'locale':
|
||||||
self._data = zf.read(zf.namelist()[0])
|
return self.file.log
|
||||||
return self._data
|
else:
|
||||||
except KeyError:
|
try:
|
||||||
return 'ERROR: Did not find %s file' % filename
|
zf = zipfile.ZipFile(self.file.logPath, 'r', zipfile.ZIP_DEFLATED)
|
||||||
|
zf.setpassword(self.file.logPWD)
|
||||||
|
self._data = zf.read(zf.namelist()[0])
|
||||||
|
return self._data
|
||||||
|
except KeyError:
|
||||||
|
return 'ERROR: Did not find %s file' % filename
|
||||||
|
return 'ERROR User(None)'
|
||||||
|
|
||||||
def load_history(self, filename):
|
def load_history(self, filename, user=None, uid=None):
|
||||||
if self._lists:
|
if isinstance(user, User):
|
||||||
self.file = self._lists.get(filename=filename)
|
user = user
|
||||||
|
elif uid:
|
||||||
|
user = User.objects.get(id=uid)
|
||||||
else:
|
else:
|
||||||
self.file = TermLog.objects.get(user=self.user.id, filename=filename)
|
user = self.user
|
||||||
return self.file.history
|
if user:
|
||||||
|
if self._lists:
|
||||||
|
self.file = self._lists.get(filename=filename)
|
||||||
|
else:
|
||||||
|
self.file = TermLog.objects.get(user=user.id, filename=filename)
|
||||||
|
return self.file.history
|
||||||
|
return 'ERROR User(None)'
|
||||||
|
|
||||||
|
def share_to(self, filename, user=None, uid=None):
|
||||||
|
if isinstance(user, User):
|
||||||
|
user = user
|
||||||
|
elif uid:
|
||||||
|
user = User.objects.get(id=uid)
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
if user:
|
||||||
|
TermLog.objects.get(filename=filename).user.add(user)
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def unshare_to(self, filename, user=None, uid=None):
|
||||||
|
if isinstance(user, User):
|
||||||
|
user = user
|
||||||
|
elif uid:
|
||||||
|
user = User.objects.get(id=uid)
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
if user:
|
||||||
|
TermLog.objects.get(filename=filename).user.remove(user)
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
|
@ -22,7 +22,7 @@ import tornado.httpclient
|
||||||
from tornado.websocket import WebSocketClosedError
|
from tornado.websocket import WebSocketClosedError
|
||||||
|
|
||||||
from tornado.options import define, options
|
from tornado.options import define, options
|
||||||
from pyinotify import WatchManager, ProcessEvent, IN_DELETE, IN_CREATE, IN_MODIFY, AsyncNotifier
|
# from pyinotify import WatchManager, ProcessEvent, IN_DELETE, IN_CREATE, IN_MODIFY, AsyncNotifier
|
||||||
import select
|
import select
|
||||||
|
|
||||||
from connect import Tty, User, Asset, PermRole, logger, get_object, gen_resource
|
from connect import Tty, User, Asset, PermRole, logger, get_object, gen_resource
|
||||||
|
@ -97,12 +97,12 @@ class MyThread(threading.Thread):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class EventHandler(ProcessEvent):
|
# class EventHandler(ProcessEvent):
|
||||||
def __init__(self, client=None):
|
# def __init__(self, client=None):
|
||||||
self.client = client
|
# self.client = client
|
||||||
|
#
|
||||||
def process_IN_MODIFY(self, event):
|
# def process_IN_MODIFY(self, event):
|
||||||
self.client.write_message(f.read())
|
# self.client.write_message(f.read())
|
||||||
|
|
||||||
|
|
||||||
def file_monitor(path='.', client=None):
|
def file_monitor(path='.', client=None):
|
||||||
|
|
Loading…
Reference in New Issue