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 new_connection(self, addr, sock): connection = Connection.new_connection(addr=addr, sock=sock) self.connections.append(connection) return connection