Commit c2ad5098 authored by i317280's avatar i317280

feat: update and todo graceful close

parent 50b94b03
...@@ -8,11 +8,13 @@ import logging ...@@ -8,11 +8,13 @@ import logging
import socket import socket
from flask_socketio import SocketIO, Namespace, emit from flask_socketio import SocketIO, Namespace, emit
from flask import Flask, send_from_directory, render_template, request, jsonify from flask import Flask, send_from_directory, render_template, request, jsonify
from urllib.parse import urlparse
# Todo: Remove for future # Todo: Remove for future
from jms.models import User from jms.models import User
from .models import Request, Client, WSProxy from .models import Request, Client, WSProxy
from .forward import ProxyServer from .forward import ProxyServer
import http.client
__version__ = '0.4.0' __version__ = '0.4.0'
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) BASE_DIR = os.path.dirname(os.path.dirname(__file__))
...@@ -62,7 +64,6 @@ class SSHws(Namespace, BaseWebSocketHandler): ...@@ -62,7 +64,6 @@ class SSHws(Namespace, BaseWebSocketHandler):
self.ssh.connect("127.0.0.1", 22, "liuzheng", "liuzheng") self.ssh.connect("127.0.0.1", 22, "liuzheng", "liuzheng")
self.chan = self.ssh.invoke_shell(term='xterm', width=self.cols, height=self.rows) self.chan = self.ssh.invoke_shell(term='xterm', width=self.cols, height=self.rows)
self.socketio.start_background_task(self.send_data) self.socketio.start_background_task(self.send_data)
# self.chan.settimeout(0.1)
def send_data(self): def send_data(self):
while True: while True:
...@@ -75,32 +76,21 @@ class SSHws(Namespace, BaseWebSocketHandler): ...@@ -75,32 +76,21 @@ class SSHws(Namespace, BaseWebSocketHandler):
self.prepare(request) self.prepare(request)
self.forwarder = ProxyServer(self.app, self.client) self.forwarder = ProxyServer(self.app, self.client)
# InteractiveServer(self.app, self.client).interact_async()
def on_data(self, message): def on_data(self, message):
# self.chan.send(message)
# while not self.chan.recv_ready():
self.proxy.send({"data": message}) self.proxy.send({"data": message})
# emit('data', self.chan.recv(2048).decode('utf-8', 'replace'))
# def on_event(self, message):
# self.evt_handle(message)
def on_host(self, message): def on_host(self, message):
# 此处获取主机的信息 # 此处获取主机的信息
print(message) print(message)
asset = message.get('uuid', None) uuid = message.get('uuid', None)
username = message.get('username', None) userid = message.get('userid', None)
system_user = None if uuid and userid:
self.asset = self.app.service.get_asset(uuid)
if self.asset and username: system_user = self.app.service.get_system_user(userid)
# self.asset = self.app.service.get_asset(uuid) print(system_user)
# print(self.asset)
for i in self.asset['system_users_granted']:
if i.username == username:
system_user = username
if system_user: if system_user:
self.forwarder.proxy(self.asset, system_user) self.socketio.start_background_task(self.forwarder.proxy, self.asset, system_user)
# self.forwarder.proxy(self.asset, system_user)
else: else:
self.close() self.close()
else: else:
......
...@@ -73,7 +73,7 @@ class CommandRecorder(metaclass=abc.ABCMeta): ...@@ -73,7 +73,7 @@ class CommandRecorder(metaclass=abc.ABCMeta):
class ServerReplayRecorder(ReplayRecorder): class ServerReplayRecorder(ReplayRecorder):
filelist = {} filelist = dict()
def record_replay(self, data_set): def record_replay(self, data_set):
""" """
...@@ -89,16 +89,16 @@ class ServerReplayRecorder(ReplayRecorder): ...@@ -89,16 +89,16 @@ class ServerReplayRecorder(ReplayRecorder):
super().record_replay(data_set) super().record_replay(data_set)
for data in data_set: for data in data_set:
try: try:
self.filelist[data["session"]].write(data) ServerReplayRecorder.filelist[data["session"]].write(str(data))
except IndexError: except KeyError:
logger.error("session ({})file does not exist!".format(data["session"])) logger.error("session ({})file does not exist!".format(data["session"]))
def session_start(self, session_id): def session_start(self, session_id):
self.filelist[session_id] = open(session_id + '.log', 'a') ServerReplayRecorder.filelist[session_id] = open(session_id + '.log', 'a')
print("When session {} start exec".format(session_id)) print("When session {} start exec".format(session_id))
def session_end(self, session_id): def session_end(self, session_id):
self.filelist[session_id].close() ServerReplayRecorder.filelist[session_id].close()
# Todo: upload the file # Todo: upload the file
print("When session {} end start".format(session_id)) print("When session {} end start".format(session_id))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment