def handle(self): """Hands control off to pyftpd to process the client connection. """ # server attributes/methods expected by pyftp handler self.server.backlog = 50 self.server.ip_map = [] self.server._accept_new_cons = lambda: True self.server._af = socket.AF_INET tmpdir = None try: # set up a temp dir as the ftp root for the user tmpdir = tempfile.mkdtemp(prefix='tmpftp') ftproot = os.path.join(tmpdir, self.dirseed).decode('utf-8') shutil.copytree(get_data_file(self.dirseed), ftproot) # hand off control to their handler with its own async ioloop handler = PyFTPHandler(self.request, self.server, ioloop=IOLoop()) handler.authorizer = PermissiveAuthorizer(ftproot) handler.handle() handler.ioloop.loop(1) finally: if handler.ioloop: handler.ioloop.close() if tmpdir: shutil.rmtree(tmpdir)
self.shared_ip_map[self.remote_ip] = count self.logline("Connected, shared ip map: %s" % self.shared_ip_map) except RemoteError, e: self.logerror("Connection tracking failed: %s" % e) finally: self.shared_lock.release() self.logline("Connection track: %s -> %s" % (self.remote_ip, count)) if count > self.max_cons_per_ip: self.handle_max_cons_per_ip() return FTPHandler.handle(self) def handle_error(self): """Catch some 'expected' exceptions not processed by FTPHandler/AsyncChat.""" # this is aesthetic only t, v, _ = sys.exc_info() if t == socket.error: self.log("Connection error: %s" % v) self.handle_close() return FTPHandler.handle_error(self) def close(self): """Remove the ip from the shared map before calling close.""" if not self._closed and self.max_cons_per_ip and self.shared_ip_map != None:
self.shared_lock.acquire() count = self.shared_ip_map.get(self.remote_ip, 0) + 1 self.shared_ip_map[self.remote_ip] = count self.logline("Connected, shared ip map: %s" % self.shared_ip_map) except RemoteError, e: self.logerror("Connection tracking failed: %s" % e) finally: self.shared_lock.release() self.logline("Connection track: %s -> %s" % (self.remote_ip, count)) if count > self.max_cons_per_ip: self.handle_max_cons_per_ip() return FTPHandler.handle(self) def handle_error(self): """Catch some 'expected' exceptions not processed by FTPHandler/AsyncChat.""" # this is aesthetic only t, v, _ = sys.exc_info() if t == socket.error: self.log("Connection error: %s" % v) self.handle_close() return FTPHandler.handle_error(self) def close(self): """Remove the ip from the shared map before calling close.""" if not self._closed and self.max_cons_per_ip and self.shared_ip_map != None: