def connect_host(self, message): asset_id = message.get('uuid', None) system_user_id = message.get('userid', None) secret = message.get('secret', None) cols, rows = message.get('size', (80, 24)) request.current_user = "******" connection = Connection.get_connection(request.sid) client_id = str(uuid.uuid4()) client = connection.new_client(client_id) client.request.kind = 'session' client.request.type = 'pty' client.request.meta.update({ 'pty': b'xterm', 'width': cols, 'height': rows, }) ws_proxy = WSProxy(self, client_id) client.chan = ws_proxy self.emit('room', {'room': client_id, 'secret': secret}) join_room(client_id) if not asset_id or not system_user_id: return forwarder = ProxyServer(client, None, None) def proxy(): forwarder.proxy() self.logout(client_id, connection) self.socketio.start_background_task(proxy)
def handle_connection(self, sock, addr): logger.debug("Handle new connection from: {}".format(addr)) transport = paramiko.Transport(sock, gss_kex=False) try: transport.load_server_moduli() except IOError: logger.warning("Failed load moduli -- gex will be unsupported") transport.add_server_key(self.host_key) transport.set_subsystem_handler( 'sftp', paramiko.SFTPServer, SFTPServer ) connection = Connection.new_connection(addr=addr, sock=sock) server = SSHInterface(connection) try: transport.start_server(server=server) transport.set_keepalive(60) while transport.is_active(): chan = transport.accept() server.event.wait(5) if chan is None: continue if not server.event.is_set(): logger.warning("Client not request invalid, exiting") sock.close() continue else: server.event.clear() client = connection.clients.get(chan.get_id()) client.chan = chan t = threading.Thread(target=self.dispatch, args=(client,)) t.daemon = True t.start() transport.close() except paramiko.SSHException as e: logger.warning("SSH negotiation failed: {}".format(e)) except EOFError as e: logger.warning("Handle connection EOF Error: {}".format(e)) except Exception as e: logger.error("Unexpect error occur on handle connection: {}".format(e)) logger.error(e, exc_info=True) finally: Connection.remove_connection(connection.id) sock.close()
def dispatch(client): supported = {'pty', 'x11', 'forward-agent'} chan_type = client.request.type kind = client.request.kind if kind == 'session' and chan_type in supported: logger.info("Request type `{}:{}`, dispatch to interactive mode".format(kind, chan_type)) try: InteractiveServer(client).interact() except Exception as e: logger.error("Unexpected error occur: {}".format(e)) connection = Connection.get_connection(client.connection_id) connection.remove_client(client.id) elif chan_type == 'subsystem': pass else: msg = "Request type `{}:{}` not support now".format(kind, chan_type) logger.info(msg) client.send(msg)
def dispatch(client): supported = {'pty', 'x11', 'forward-agent'} chan_type = client.request.type kind = client.request.kind try: if kind == 'session' and chan_type in supported: logger.info("Dispatch client to interactive mode") try: InteractiveServer(client).interact() except IndexError as e: logger.error("Unexpected error occur: {}".format(e)) elif chan_type == 'subsystem': while not client.closed: time.sleep(5) logger.debug("SFTP session finished") else: msg = "Request type `{}:{}` not support now".format(kind, chan_type) logger.error(msg) client.send_unicode(msg) finally: connection = Connection.get_connection(client.connection_id) if connection: connection.remove_client(client.id)
def new_connection(self, addr, sock): connection = Connection.new_connection(addr=addr, sock=sock) self.connections.append(connection) return connection