diff --git a/connect.py b/connect.py index f8dad935b..d0d1a5f3a 100644 --- a/connect.py +++ b/connect.py @@ -269,12 +269,7 @@ class Tty(object): today_connect_log_dir = os.path.join(tty_log_dir, date_start) log_file_path = os.path.join(today_connect_log_dir, '%s_%s_%s' % (self.username, self.asset_name, time_start)) - if self.login_type == 'ssh': - pid = os.getpid() - remote_ip = os.popen("who -m | awk '{ print $5 }'").read().strip('()\n') - else: - pid = 0 - remote_ip = 'Web' + try: is_dir(today_connect_log_dir, mode=0777) @@ -287,8 +282,19 @@ class Tty(object): except IOError: raise ServerError('Create logfile failed, Please modify %s permission.' % today_connect_log_dir) - log = Log(user=self.username, host=self.asset_name, remote_ip=remote_ip, - log_path=log_file_path, start_time=datetime.datetime.now(), pid=pid) + if self.login_type == 'ssh': + pid = os.getpid() + remote_ip = os.popen("who -m | awk '{ print $5 }'").read().strip('()\n') + log = Log(user=self.username, host=self.asset_name, remote_ip=remote_ip, + log_path=log_file_path, start_time=datetime.datetime.now(), pid=pid) + else: + remote_ip = 'Web' + log = Log(user=self.username, host=self.asset_name, remote_ip=remote_ip, + log_path=log_file_path, start_time=datetime.datetime.now(), pid=0) + log.save() + log.pid = log.id + log.save() + log_file_f.write('Start at %s\n' % datetime.datetime.now()) log.save() return log_file_f, log_time_f, log diff --git a/jlog/views.py b/jlog/views.py index 02c3678ff..fbd953820 100644 --- a/jlog/views.py +++ b/jlog/views.py @@ -48,7 +48,8 @@ def log_list(request, offset): contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) - web_monitor_uri = '%s/monitor' % web_socket_host + web_monitor_uri = 'ws://%s/monitor' % web_socket_host + web_kill_uri = 'http://%s/kill' % web_socket_host return render_to_response('jlog/log_%s.html' % offset, locals(), context_instance=RequestContext(request)) @@ -103,6 +104,6 @@ def log_record(request): def web_terminal(request): - web_terminal_uri = '%s/terminal' % web_socket_host + web_terminal_uri = 'ws://%s/terminal' % web_socket_host return render_to_response('jlog/web_terminal.html', locals()) diff --git a/jumpserver.conf b/jumpserver.conf index cb0eba98d..7ab7e7f31 100644 --- a/jumpserver.conf +++ b/jumpserver.conf @@ -23,7 +23,7 @@ root_pw = secret234 [websocket] -web_socket_host = ws://192.168.244.129:3000 +web_socket_host = 192.168.244.129:3000 [mail] diff --git a/run_websocket.py b/run_websocket.py index 62d62998b..2daf002d4 100644 --- a/run_websocket.py +++ b/run_websocket.py @@ -7,6 +7,7 @@ import os import sys import os.path import threading +import uuid import tornado.ioloop import tornado.options @@ -96,6 +97,7 @@ class Application(tornado.web.Application): handlers = [ (r'/monitor', MonitorHandler), (r'/terminal', WebTerminalHandler), + (r'/kill', WebTerminalKillHandler), ] setting = { @@ -165,8 +167,20 @@ class WebTty(Tty): self.input_mode = False +class WebTerminalKillHandler(tornado.web.RequestHandler): + def get(self): + ws_id = self.get_argument('id') + for ws in WebTerminalHandler.clients: + print ws.id + if ws.id == int(ws_id): + print "killed" + ws.close() + print len(WebTerminalHandler.clients) + + class WebTerminalHandler(tornado.websocket.WebSocketHandler): tasks = [] + clients = [] def __init__(self, *args, **kwargs): self.term = None @@ -174,6 +188,7 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler): self.log_file_f = None self.log_time_f = None self.log = None + self.id = 0 super(WebTerminalHandler, self).__init__(*args, **kwargs) def check_origin(self, origin): @@ -184,6 +199,7 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler): self.term.get_connection() self.channel = self.term.ssh.invoke_shell(term='xterm') WebTerminalHandler.tasks.append(MyThread(target=self.forward_outbound)) + WebTerminalHandler.clients.append(self) for t in WebTerminalHandler.tasks: if t.is_alive(): @@ -205,6 +221,8 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler): def on_close(self): print 'On_close' + if self in WebTerminalHandler.clients: + WebTerminalHandler.clients.remove(self) self.log_file_f.write('End time is %s' % datetime.datetime.now()) self.log.is_finished = True self.log.end_time = datetime.datetime.now() @@ -213,6 +231,7 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler): def forward_outbound(self): self.log_file_f, self.log_time_f, self.log = self.term.get_log_file() + self.id = self.log.id try: data = '' pre_timestamp = time.time() diff --git a/templates/jlog/log_online.html b/templates/jlog/log_online.html index 2f855b084..3bc55bc5c 100644 --- a/templates/jlog/log_online.html +++ b/templates/jlog/log_online.html @@ -97,7 +97,7 @@ {% ifnotequal session_role_id 0 %} 命令统计 监控 - + {% endifnotequal %} {{ post.start_time|date:"Y-m-d H:i:s" }} @@ -202,8 +202,14 @@ {# }#} - function cut(num){ - var g_url = "/jlog/log_kill/?id="+num; + function cut(num, host){ + console.log(host); + if (host=='Web'){ + var g_url = '{{ web_kill_uri }}' + '?id=' + num; + } else { + g_url = "/jlog/log_kill/?id=" + num; + } + $.ajax({ type: "GET", url: g_url,