def upload_monitoring(self, connection, upload): sz = 0 log("Monitoring started").write() start = time.time() while connection.connected: if self.bytesSent != sz: end = time.time() percentage = int(self.position + self.bytesSent) / int(self.size) * 100 completed = int(self.position + self.bytesSent) elapsed = end-start speed = int((self.bytesSent-sz)/elapsed) if speed != 0: tm = (int(self.size)-int(self.bytesSent)-self.position) / speed else: tm = 99999 eta = utcnow() + datetime.timedelta(seconds=tm) speed = int(speed/1024) upload.status, upload.progress, upload.completed, upload.eta, upload.timeleft = 'Uploading...', percentage, completed, eta, tm #workaround for occasionnal empty packages if speed: upload.speed = speed upload.save() sz = self.bytesSent start = time.time() if percentage >=100: self.reactor.disconnect_all() return "" time.sleep(1) log("Monitoring stoped").write()
def on_welcome(self, connection, event): c = self.connection c.whois(event.target) PORT = None t=threading.Timer(0, self.signal_listener, [connection, event]) t.daemon=True t.start() with open(os.path.join(settings.BASE_DIR, "config.ini"), "r") as cfg: content = cfg.readlines() PORT = content[2][1+content[2].index("="):content[2].index("#")].strip(" ") cfg.close() if PORT: #manual port forwarding used PORT = int(PORT) self.dcc = self.dcc_listen(dcctype="raw", port=PORT) else: self.dcc = self.dcc_listen(dcctype="raw") try: self.prepupnp() except Exception as e: log("Couldn't create port mapping. Is upnp supported and activated ?").write() c.quit() return #we need to wait to get the externalip address, therefore spawn a deamon thread s=threading.Timer(0, self.dccsend, [connection]) s.daemon=True s.start()
def monitoring(self, connection, bot, position=0): sz = 0 log("Monitoring started").write() while connection.connected: percentage = int(position + self.dict[bot]["received_bytes"]) / int(self.dict[bot]["size"]) * 100 completed = int(position + self.dict[bot]["received_bytes"]) speed = (self.dict[bot]["received_bytes"]-sz) if speed != 0: tm = (int(self.dict[bot]["size"])-int(self.dict[bot]["received_bytes"])-position) / speed else: tm = 99999 eta = utcnow() + datetime.timedelta(seconds=tm) speed = int(speed/1024) (self.dict[bot]["down"].status, self.dict[bot]["down"].progress, self.dict[bot]["down"].completed, self.dict[bot]["down"].eta, self.dict[bot]["down"].timeleft)= 'Downloading...', percentage, completed, eta, tm #workaround for occasionnal empty packages if speed: self.dict[bot]["down"].speed = speed self.dict[bot]["down"].save() sz = self.dict[bot]["received_bytes"] if percentage >=100: self.DCCconnections_dict[bot].disconnect() return "" time.sleep(1) log("Monitoring stoped").write()
def DCC_deamonthread(c, server, nickname, hist, down, size=0): """ If a server connection fails, there is no point in checking the queue for further downloads; There, we try to reconnect as soon as the connection timed out. To be able to interrupt the connection attempt loop, we use the signal listener and pass the server connection """ botqueue = main.queuedict[server+c.bot] = Queue() i=1 while True: try: c.connect(server, 6667, nickname) c.start() break except irc.client.ServerConnectionError as x: log("error (%s)" % i + str(x)).write() hist.status, hist.time, hist.sizeraw = "Error during connection (%d)" % i, utcnow(), size hist.save() down.status = "Connecting to server... (%d)" % (i+1) down.save() i+=1 if not botqueue.empty(): mystring = botqueue.get() if "cancel" in mystring: views.manage_queue(down) break
def on_whoisactually(self, c, e): #this event contains our public IP address for x in e.arguments: self.externalip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', x ) if self.externalip: self.externalip = self.externalip[0] log("External IP obtained from IRC server: %s" % self.externalip).write() break
def on_dcc_disconnect(self, connection, event): self.file.close() self.upnp.deleteportmapping(self.eport, 'TCP') log("Portmapping for port %d deleted" % self.eport).write() log("DCC connection closed").write() try: self.connection.quit() except: pass
def interactive(channel, command): # 获取原tty属性 oldtty = termios.tcgetattr(sys.stdin) try: # 设置 tty 的属性 tty.setraw(sys.stdin.fileno()) tty.setcbreak(sys.stdin.fileno()) channel.settimeout(0.0) # ssh连接进入主机后 执行命令 比如: 要进入docker 容器 if command != "": channel.send(command + "\r") tm = 2 input_cmd = "" while True: rlist, wlist, errlist = select.select([channel, sys.stdin], [], []) # 从标准输入和socket 获取数据然后写入标准输出 if channel in rlist: try: if tm > 0: time.sleep(0.2) tm = tm - 1 r = channel.recv(256) output_string = u(r) # 如果返回的长度为 0 (就是没有返回), 或者 返回的是exit 就退出程序(使用与进入第二层:比如在远程主机,进入了docker) if len(output_string) == 0 or output_string == '\r\nexit\r\n': sys.stdout.write('\r\nWelcome back again, Bye!\r\n') break (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_debug'], log_fmode="a") logger.debug("{out_string: %s}" % [output_string]) logger.removeHandler(fh) ishow = filter.filter_channel(output_string) if ishow == True: sys.stdout.write(output_string) sys.stdout.flush() except socket.timeout: pass # 从标准输入获取输入的字符:然后,处理输入,对输入进行过滤 if sys.stdin in rlist: input_string = funkey(stdinput=sys.stdin) # 命令过滤 input_string, input_cmd = filter.filter_stdin(input_string=input_string, input_cmd=input_cmd) # 把输入的字符发送给 远程ssh (准确的说是:经过过滤过的输入) channel.send(input_string) except Exception as e: fe = traceback.format_exc() (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_error'], log_fmode="a") logger.error("{ error }:%s \n %s" %(e, fe)) logger.removeHandler(fh) finally: termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
def on_dccchat(self, c, e): log("dccchat").write() if len(e.arguments) != 2: return args = e.arguments[1].split() if len(args) == 4: try: address = ip_numstr_to_quad(args[2]) port = int(args[3]) except ValueError: return self.dcc_connect(address, port)
def on_privnotice(self, c, e): bot = e.source.nick a = e.arguments[0].split(":", 1) log(a).write() if "invalid pack number" in str(a).lower(): self.DCCconnections_dict[bot].disconnect() self.dict[bot]["hist"].status, self.dict[bot]["hist"].time = "Dead link from the search engine", utcnow() self.dict[bot]["down"].status, self.dict[bot]["down"].active = "Invalid pack number", False self.dict[bot]["down"].save() self.dict[bot]["hist"].save() log("Discarding download due to invalid pack number, proceeding to next item in queue if existing.").write() print ("3") views.manage_queue(self.dict[bot]["down"])
def xdcc(resume=False, **kwargs): """ when this function is called, every check has been made and the download has to be launched; a new bot has to be created as well as a new Download_Ongoing item; One bot corresponds to one server connection instance; One bot can have multiple DCC connections, thus handling multiple downloads the History item is created for easy access, but saved only when download is finished, e.g completed/interrupted/canceled """ down = hist = None #these are the two database objects irc.client.ServerConnection.buffer_class.encoding = 'latin-1' if resume: #resume = True log("Resume = True").write() down = kwargs["down_obj"] down.status, down.active ="Connecting to server...", True try: hist = Download_History.objects.filter(down.server).latest("id") if hist.attempts: hist.attempts+=1 except: hist = Download_History(filename=kwargs["filename"]) if not "filename" in kwargs: #direct file transfer between two IRCapp users server = "irc.rizon.net"# I know, pretty arbitrary, we'll make this configurable in a future version (config.ini probably) nickname = kwargs["nick"] down = Download_Ongoing(filename="Direct file transfer enabled", server=server, username=nickname, status="Connecting to server...", active=False) hist = Download_History() log("Direct = True").write() c = DCCReceive(direct=True, nickname=nickname, password=kwargs["pw"], down=down, hist=hist) t = threading.Thread(target=DCC_deamonthread, args=(c, server, nickname, hist, down)) else: if not resume: #regular download, create new database objects down = Download_Ongoing(filename=kwargs["filename"], status="Connecting to server...", active=True) hist = Download_History(filename=kwargs["filename"]) size, server, channel, xdccbot, xdccrequest = (kwargs["size"], kwargs["server"], kwargs["channel"], kwargs["username"], kwargs["package_number"]) msg = "xdcc send #%s" % xdccrequest down.server, down.channel, down.username, down.package_number = server, channel, xdccbot, xdccrequest c = DCCReceive(bot=xdccbot, msg=msg, channel=channel, size=size, filename=kwargs["filename"], down=down, hist=hist) #anonymous nickname nickname = ''.join(random.choice(string.ascii_lowercase) for i in range(8)) t = threading.Thread(target=DCC_deamonthread, args=(c, server, nickname, hist, down, size)) down.save() t.daemon=True t.start()
def login(): try: host, port, username, password, cmd = auth.auth() tran = paramiko.Transport((host, port)) tran.start_client() tran.auth_password(username, password) # 打开一个通道 chan = tran.open_session() # 终端配置: 类型,大小 t = configs.CONFIG['term'] ptype = configs.CONFIG['type'][t] term_size = os.get_terminal_size() w = term_size.columns h = term_size.lines # 获取一个终端 chan.get_pty(term=ptype, width=w, height=h) # 激活器 chan.invoke_shell() interactive.interactive(chan, cmd) chan.close() tran.close() except Exception as e: ef = traceback.format_exc() (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_error'], log_fmode="a") logger.error("{ error }:%s \n %s" % (e, ef)) logger.removeHandler(fh)
def DCC_deamonthread(c, server, nickname, upload): botqueue = main.queuedict[server+c.bot] = Queue() i=1 while True: try: c.connect(server, 6667, nickname) c.start() break except irc.client.ServerConnectionError as x: log("error (%s)" % i + str(x)).write() upload.status, upload.active = "Error during connection", False upload.save() i+=1 if not botqueue.empty(): mystring = botqueue.get() if "cancel" in mystring: upload.delete() break
def startapp(): '''this "main" is application entry''' try: trigger.triggerman(protect=True) login.login() except Exception as e: ef = traceback.format_exc() (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_error'], log_fmode="a") logger.error("{ error }:%s \n %s" % (e, ef)) logger.removeHandler(fh)
def on_welcome(self, connection, event, first=True): if self.direct: self.dict[self.bot]["down"].status = "Waiting for direct file transfer" self.dict[self.bot]["down"].save() print("bot launched for direct file transfer") else: print("bot launched for %s" % self.bot) self.dict[self.bot]["down"].status, self.dict[self.bot]["down"].sizeraw = ( "Waiting for package (in queue)...", self.dict[self.bot]["size"]) self.dict[self.bot]["down"].save() s=threading.Timer(0, self.signal_listener, [connection, event]) s.daemon=True s.start() if first and not self.direct: connection.join(self.dict[self.bot]["channel"]) #must join mg-chat channel to download on moviegods if "moviegods" in self.dict[self.bot]["channel"]: connection.join("#MG-CHAT") log("Channel #mg-chat joined to be able to download").write() connection.privmsg(self.bot, self.dict[self.bot]["msg"]) log("Channel joined (%s) and package requested" % self.dict[self.bot]["channel"]).write()
def select(): # http://www.cnblogs.com/starof/p/4703820.html # curses.wrapper(screen_init) 这个函数会初始化 stdscr = initscr() 返回一个window对象:stdscr # 把 stdscr 传递给 函数 screen_init(stdscr) 的第一个参数:stdscr # screen_init(stdscr) 中的 stdscr 只是一个用于接受 wrapper的 window 对象的形式参数 position = curses.wrapper(screen_init) (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_debug'], log_fmode='a') host_id = position['y']*2 + position['x'] #logger.debug("{position: %s host id: %s}" %(position, host_id)) logger.removeHandler(fh) return host_id
def process(self, c, e, resume=False): #"When receiving a file with DCC, accept it" downloads_dir = directory() bot = e.source.nick try: connection = self.dcc_connect(self.ip, self.port, "raw") except irc.client.DCCConnectionError as e: log('Couldn\'t connect to DCC peer: %s' % e).write() self.dict[bot]["hist"].status, self.dict[bot]["hist"].time = "Peer connection error", utcnow() self.dict[bot]["hist"].save() views.manage_queue(self.dict[bot]["down"]) return "" if resume: self.dict[bot]["file"] = open(os.path.join(downloads_dir, self.dict[bot]["filename"]), 'ab') self.dict[bot]["down"].filename, self.dict[bot]["down"].status = ( self.dict[bot]["filename"], "Resuming download...") log('Resuming download of %s' % self.dict[bot]["filename"]).write() else: self.dict[bot]["file"] = open(os.path.join(downloads_dir, self.dict[bot]["filename"]), 'wb') self.dict[bot]["down"].filename, self.dict[bot]["down"].status, self.dict[bot]["down"].sizeraw = ( self.dict[bot]["filename"], "Starting download...", self.dict[bot]["size"]) log('Starting download of %s' % self.dict[bot]["filename"]).write() self.DCCconnections_dict[bot] = connection self.dict[bot]["hist"].time = utcnow() self.dict[bot]["down"].save() t = threading.Timer(0, monitoring, [self, connection, bot, int(self.dict[bot]["position"])]) t.start() s=threading.Timer(0, self.retry_pack, [connection, bot]) s.daemon=True s.start()
def screen_init(stdscr): (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_debug'], log_fmode='a') stdscr.clear() stdscr.refresh() stdscr.scrollok(1) #height, width = stdscr.getmaxyx() #logger.debug("{height: %s width: %s screen }" %(height, width)) curses.start_color() # 设置颜色对 下面设置了 3 个颜色对 黑底白字 黑底红字 黑底青蓝字 curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK) curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK) curses.init_pair(3, curses.COLOR_CYAN, curses.COLOR_BLACK) # 这个部分不需要 每次都改变,只需要显示一次,不刷新就不会消失 stdscr.addstr(3, 33, "日志查看器", curses.color_pair(3)) stdscr.addstr(6, 10, "请选择要查看日志的项目:", curses.color_pair(1)) lines = int(len(configs.HOSTS)/2) position = {"x":0, "y":0} change_color(stdscr, position, lines) while True: # 获取键盘输入 key = stdscr.getch() #logger.debug("{key: %s key type: %s color: %s}" % ([key], type(key), position)) if key == 259 and position['y'] > 0: position['y'] = position['y'] - 1 elif key == 258 and position['y'] < lines - 1: position['y'] = position['y'] + 1 elif key == 260 and position['x'] > 0: position['x'] = position['x'] - 1 elif key == 261 and position['x'] < 1: position['x'] = position['x'] + 1 # 如果是方向键 就改变 颜色 if key in [258, 259, 260, 261]: change_color(stdscr, position, lines) # 如果是确定键 就跳出循环 返回位置信息 elif key == 10: #logger.debug("{key: %s type: %s}" % ([key], type(key))) break # 如果输入: q/Q 表示退出 elif key == ord('q') or key == ord('Q'): stdscr.refresh() curses.endwin() logger.removeHandler(fh) sys.exit(0) stdscr.refresh() curses.endwin() logger.removeHandler(fh) return position
def getinfo(querystring, pn=0): """ internal purpose only, not a view """ #for normal search try: #normal search par = {"q" : querystring, "pn" : pn} data = requests.get("http://ixirc.com/api/", params=par) except: log("Couldn't access ixirc search engine, exiting").write() return "Couldn't access ixirc search engine, is there a problem with your internet connexion?" data = json.loads(data.text) if data["c"] == 0: log("No match for %s on the search engine" % querystring).write() return "No match for “%s” on the search engine" % querystring try: num = len(data["results"]) for result in data["results"][:]: #print (result["cname"], result["cid"]) if testresult(result) is False: data["results"].remove(result) #quick download impossible except: #quick download impossible return "No match for “%s” on the search engine" % querystring if len(data["results"]) == 0: return "No match for “%s” on the search engine" % querystring results = data["results"] response = {"results" : results, "num" : num, "pn" : pn} return response
def prepupnp(self): """ prep upnp """ self.upnp = miniupnpc.UPnP() self.upnp.discoverdelay = 200 try: ndevices = self.upnp.discover() print (ndevices, 'device(s) detected') # select an igd self.upnp.selectigd() """ check database for previous port mapping ; check if port mapping still exist ; if so, delete it if check database for port mapping: try: u.deleteportmapping(port, 'TCP') except NoSuchEntryInArray: print("port mapping already deleted") """ # display information about the IGD and the internet connection externalipaddress = self.upnp.externalipaddress() print (self.upnp.statusinfo(), self.upnp.connectiontype()) self.eport = self.dcc.localport # find a free port for the redirection r = self.upnp.getspecificportmapping(self.eport, 'TCP') while r != None and self.eport < 65536: self.eport = self.eport + 1 r = self.upnp.getspecificportmapping(self.eport, 'TCP') log('trying to redirect %s port %u TCP => %s port %u TCP' % (externalipaddress, self.eport, self.upnp.lanaddr, self.dcc.localport)).write() b = self.upnp.addportmapping(self.eport, 'TCP', self.upnp.lanaddr, self.dcc.localport, 'UPnP IGD Tester port %u' % self.eport, '') if b: log('Success').write() else: log('Failed').write() except Exception as e: log('UPnP exception :', e).write()
def auth(): try: host_id = select_host.select() # (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_debug'], log_fmode="a") # logger.debug("{host id: %s}" % host_id) # logger.removeHandler(fh) host, port, username, password, cmd = confirm_host(host_id) return (host, port, username, password, cmd) except Exception as e: ef = traceback.format_exc() (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_error'], log_fmode="a") logger.error("{ error }:%s \n %s" % (e, ef)) logger.removeHandler(fh)
def deny_cmd(input_cmd): is_allow = True cmd_deny = configs.CONFIG['cmd_deny'] for deny in cmd_deny: r = re.findall(deny, input_cmd) (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_debug'], log_fmode="a") #logger.debug("{from: %s re.findall: %s result: %s}" % ([input_cmd], [deny], [r])) logger.removeHandler(fh) if r != []: is_allow = False if is_allow == False: print("\r\n %s :是 - 禁止使用的命令 \r\n" % [input_cmd]) return_string = configs.KEYS['BACKSPACE'] * len( input_cmd) + configs.KEYS['ENTER'] return_cmd = "" return return_string, return_cmd else: return None, None
def allow_cmd(input_cmd): is_allow = False # 过滤命令: 只允许通过的 cmd_allow = configs.CONFIG['cmd_allow'] for allow in cmd_allow: r = re.findall(allow, input_cmd) (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_debug'], log_fmode="a") #logger.debug("{from: %s re.findall: %s result: %s}" % ([input_cmd], [allow], [r])) logger.removeHandler(fh) if r != []: is_allow = True if is_allow == False: print("\r\n %s : 不是 - 允许使用的命令 ( 这玩意儿有 bug , 找作者!!! ) \r\n" % [input_cmd]) return_string = configs.KEYS['BACKSPACE'] * len( input_cmd) + configs.KEYS['ENTER'] return_cmd = "" return return_string, return_cmd else: return None, None
def on_dcc_disconnect(self, connection, event, bot=""): for key, value in self.DCCconnections_dict.items(): if value == connection: bot = key break try: self.dict[bot]["file"].close() except: bot = bot if self.dict[bot]["stop"]: return "" if self.dict[bot]["cancel"] == False: (self.dict[bot]["down"].status, self.dict[bot]["down"].progress, self.dict[bot]["down"].speed, self.dict[bot]["down"].progress, self.dict[bot]["down"].eta) = "Extracting...", None, None, None, None self.dict[bot]["down"].save() tot = self.dict[bot]["received_bytes"]+self.dict[bot]["position"] log("Received file (%d bytes)." % tot ).write() log("Size (%d bytes)." % int(self.dict[bot]["size"]) ).write() percdone = tot/int(self.dict[bot]["size"]) duration = (utcnow() - self.dict[bot]["hist"].time) average = int(self.dict[bot]["size"])/1024/duration.total_seconds() (self.dict[bot]["hist"].filename, self.dict[bot]["hist"].status, self.dict[bot]["hist"].average, self.dict[bot]["hist"].duration, self.dict[bot]["hist"].time, self.dict[bot]["hist"].sizeraw) = (self.dict[bot]["filename"], "Downloaded", round(average, 0), duration, utcnow(), self.dict[bot]["size"]) self.dict[bot]["hist"].save() if os.path.exists(os.path.join(directory(), self.dict[bot]["filename"])): #added to prevent extracting incomplete files (internet connection interrupted) if percdone > 99/100: untar(os.path.join(directory(), self.dict[bot]["filename"]), self.dict[bot]["filename"], self.dict[bot]["down"], self.dict[bot]["hist"]) return "" self.dict[bot]["down"].status = "Error during file transfer" self.dict[bot]["down"].save() self.dict[bot]["hist"].status, self.dict[bot]["hist"].time = "Error during file transfer", utcnow() self.dict[bot]["hist"].save() log("Error during file transfer. Completed percentage: %d" % int(percdone*100) ).write() print ("2") views.manage_queue(self.dict[bot]["down"]) return "" else: self.dict[bot]["cancel"] = False views.manage_queue(self.dict[bot]["down"]) return ""
def funkey(stdinput): input_string = stdinput.read(1) (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_debug'], log_fmode="a") logger.debug("{ stdin.read(1): %s}" % [input_string]) logger.removeHandler(fh) if len(input_string) == 0: return "break" # 如果是功能键 if input_string == "\x1b": input_string = input_string + stdinput.read(2) (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_debug'], log_fmode="a") logger.debug("{ stdin.read(2): %s}" % [input_string]) logger.removeHandler(fh) # 只允许上下键 通过 其他的全当回车用 if input_string in (configs.KEYS['UP_ARROW'], configs.KEYS['DOWN_ARROW']): #input_string = "\t" return input_string # elif input_string in (configs.KEYS['LEFT_ARROW']): # #print("\r\n 不支持:左键,你输入的是:%s \r\n" %[input_string]) # input_string = 'left_arrow' + input_string # return input_string # elif input_string in (configs.KEYS['RIGHT_ARROW']): # #print("\r\n 不支持:右键,你输入的是:%s \r\n" %[input_string]) # input_string = 'right_arrow' + input_string # return input_string elif input_string in configs.KEYS['KEY_DENY1']: print( "\r\n 不支持:左右键,功能键 = (F1~F12、Home、Insert、Delete、End、PageUp、PageDown),你输入的是:%s \r\n" % [input_string]) input_string = "\r" return input_string else: input_string = input_string + stdinput.read(1) if input_string in configs.KEYS['KEY_DENY2']: print( "\r\n 不支持:左右键,功能键 = (F1~F12、Home、Insert、Delete、End、PageUp、PageDown),你输入的是:%s \r\n" % [input_string]) input_string = "\r" return input_string else: input_string = input_string + stdinput.read(1) if input_string in configs.KEYS['KEY_DENY3']: print( "\r\n 不支持:左右键,功能键 = (F1~F12、Home、Insert、Delete、End、PageUp、PageDown),你输入的是:%s \r\n" % [input_string]) input_string = "\r" return input_string else: print( "\r\n 不支持:左右键,功能键 = (F1~F12、Home、Insert、Delete、End、PageUp、PageDown),你输入的是:%s \r\n" % [input_string]) input_string = "\r" return input_string else: return input_string
def on_kill(self, connection, event): log("booted").write
def on_disconnect(self, connection, event): print ("disconnected") log("disconnected").write()
def on_nicknameinuse(self, c, e): c.nick(c.get_nickname() + "_") log("Nickname already used. Now using %s" % (c.get_nickname() + "_")).write()
def on_disconnect(self, connection, event): log("disconnected").write() self.upload.delete()
def untar(completefile, filename, down, hist): log("Extracting the file...").write() log(completefile + ' ' + filename).write() # tar file to extract theTarFile = completefile # tar file path to extract extractTarPath = os.path.join(directory(), '_UNPACK_' + splitext(filename)[0]) if tarfile.is_tarfile(theTarFile): # extract all contents try: tfile = tarfile.open(theTarFile) tfile.extractall(extractTarPath) tfile.close() os.remove(theTarFile) #extract rar files x=0 completefile = False #this test is added in case some txt is extracted with the folder while not os.path.isdir(os.path.join(extractTarPath, os.listdir(extractTarPath)[x])): #sometimes the file extracted is the final file, no futher rar extraction needed; in this case, test for it's size and finish if os.path.getsize(os.path.join(extractTarPath, os.listdir(extractTarPath)[x])) > 90/100*hist.sizeraw: completefile = True break x+=1 if not completefile: origin = os.path.join(extractTarPath, os.listdir(extractTarPath)[x]) log(origin).write() for fl in os.listdir(origin): shutil.move(os.path.join(origin, fl), extractTarPath) shutil.rmtree(origin) for archivefile in os.listdir(extractTarPath): if ".rar" in archivefile: arch_ref = rarfile.RarFile(os.path.join(extractTarPath, archivefile),'r') arch_ref.extractall(extractTarPath) #remove rest for fl in os.listdir(extractTarPath): ext = splitext(fl)[1].lower() pathToDelete = os.path.join(extractTarPath, fl) if os.path.isdir(pathToDelete): # Do not delete non-empty dirs, could contain useful files. try: os.rmdir(pathToDelete); except OSError as ex: if ex.errno == errno.ENOTEMPTY: log("Did not delete non-empty directory : %s" % pathToDelete).write() else: log("An error occurred deleting directory : %s" % pathToDelete).write() else: if ext[:2] == '.r' or ext in ['.sfv', '.nfo', '.png', '.jpg'] or 'sample' in fl: os.remove(os.path.join(extractTarPath, fl)) #remove UNPACK from name when done os.rename(extractTarPath, os.path.join(directory(), splitext(filename)[0])) except Exception as e: log("An error occured during file extraction : %s" % e).write() hist.status, hist.time = "Error during file extraction", utcnow() down.status, down.sizeraw, down.active = "Error during file extraction", None, False down.save() hist.save() print ("4") views.manage_queue(down) return "" else: log(theTarFile + " is not a tarfile.").write() down.status, down.sizeraw, down.active = "Downloaded and extracted properly", None, False hist.status, hist.time = "Downloaded and extracted properly", utcnow() hist.save() down.save() print ("5") views.manage_queue(down) return ""
def on_dcc_connect(self, c, e): t = threading.Timer(0, upload_monitoring, [self, c, self.upload]) t.start() log("connection made with %s" % self.nickname).write() self.file = open(self.filename, 'rb') self.sendBlock()
def read_log(request): log_file = open(log().my_log) response = HttpResponse(content=log_file) response['Content-Type'] = 'text/plain' log_file.close() return response
def quickinfo(querystring): """ internal purpose only, not a view """ #for quickdownload i = 0 total = [] while True: try: par = { 'q' : querystring, 'pn' : i } data = requests.get("http://ixirc.com/api/", params=par) except: log("Couldn't access ixirc search engine, exiting.").write() return "Couldn't access ixirc search engine, is there a problem with your internet connexion?" i += 1 data = json.loads(data.text) try: if len(data["results"]) == 0: #quick download impossible return "" except: #quick download impossible return "" query = querystring.split(" ") quality_list = ['MAXI', 'EXCE', 'GOOD', 'NORM'] quality_words = ['bluray', '1080', '720', 'hd'] quality_index = quality_list.index(Quick_Download.objects.latest('id').priority) for x in data["results"][:]: test = True for element in query: if not element in x["name"].lower(): test = False print (element, x["name"].lower()) if Quick_Download_Contains.objects.all().count() > 0: for obj in Quick_Download_Contains.objects.all(): if not str(obj.contains).lower() in x["name"].lower(): test = False if Quick_Download_Excludes.objects.all().count() > 0: for obj in Quick_Download_Excludes.objects.all(): if str(obj.excludes).lower() in x["name"].lower(): test = False #first loop through all the keywords for word in quality_words[quality_index:]: #for each key word, loop through all the elements to find a match before doing the same for the next keyword if word in x["name"].lower(): if word == "hd": #also, if word is "hd", exclude other words since x["name"] might be like "720p.HDTV" for otherword in quality_words[:quality_index]: if otherword in x["name"].lower(): test = False else: test = False if test and testresult(x): return x if not (len(data["results"]) % 30 == 1 and len(data["results"]) != 1): break
def filter_stdin(input_string, input_cmd, yesno): (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_debug'], log_fmode="a") return_cmd = input_cmd + input_string return_string = input_string logger.debug("{input_string: %s input_cmd: %s}" % ([input_string], [input_cmd])) # yes or no note = yesno # logger.debug("{3 note.yn: %s}" % note.yn) # if note.yn == True and input_string != 'q': # return_cmd = input_cmd # return_string = input_string # logger.debug("{4 note.yn: %s}" % note.yn) # logger.removeHandler(fh) # return return_string, return_cmd if note.yn == True: return_cmd = input_cmd return_string = input_string if input_string == "q" or input_string == "n": note.yn = False # logger.debug("{5 note.yn: %s}" % note.yn) logger.removeHandler(fh) return return_string, return_cmd # 匹配 退格键 if input_string == configs.KEYS['BACKSPACE']: return_cmd = input_cmd[:-1] #logger.debug("{return_string: %s return_cmd: %s}" % ([return_string], [return_cmd])) logger.removeHandler(fh) return return_string, return_cmd # 匹配 tab键, 向上键, 向下键 if input_string == configs.KEYS['TAB']: return_cmd = input_cmd logger.removeHandler(fh) return return_string, return_cmd # 匹配 向上键, 向下键 if input_string in (configs.KEYS['UP_ARROW'], configs.KEYS['DOWN_ARROW']): return_cmd = input_cmd logger.removeHandler(fh) return return_string, return_cmd if input_string == configs.KEYS['ENTER']: # 退出处理 special_cmd.exit(input_cmd) if input_cmd == "": return_cmd = input_cmd #logger.debug("{return_string: %s return_cmd: %s}" % ([return_string], [return_cmd])) logger.removeHandler(fh) return return_string, return_cmd # s, c = special_cmd.help_cmd(input_cmd) # # 不等None 就是匹配到 help 命令 # if s != None: # return_string = s # return_cmd = c # #logger.debug("{return_string: %s return_cmd: %s}" % ([return_string], [return_cmd])) # logger.removeHandler(fh) # return return_string, return_cmd s, c = allow_cmd(input_cmd) # 不等None 就是没有匹配到 允许的命令 (清除命令输入,然后回车) if s != None: return_string = s return_cmd = c #logger.debug("{return_string: %s return_cmd: %s}" % ([return_string], [return_cmd])) logger.removeHandler(fh) return return_string, return_cmd s, c = deny_cmd(input_cmd) # 不等None 就是匹配到了 不允许的命令 (清除命令输入,然后回车) if s != None: return_string = s return_cmd = c #logger.debug("{return_string: %s return_cmd: %s}" % ([return_string], [return_cmd])) logger.removeHandler(fh) return return_string, return_cmd return_cmd = "" #logger.debug("{return_string: %s return_cmd: %s}" % ([return_string], [return_cmd])) logger.removeHandler(fh) return return_string, return_cmd #logger.debug("{return_string: %s return_cmd: %s}" % ([return_string], [return_cmd])) logger.removeHandler(fh) return return_string, return_cmd
def interactive(channel, command): # 获取原tty属性 oldtty = termios.tcgetattr(sys.stdin) try: # 设置 tty 的属性 tty.setraw(sys.stdin.fileno()) tty.setcbreak(sys.stdin.fileno()) channel.settimeout(1.5) # ssh连接进入主机后 执行命令 比如: 要进入docker 容器 if command != "": channel.send(command + "\r") # 获取终端大小 term_size = os.get_terminal_size() w_old = term_size.columns h_old = term_size.lines tm = 2 input_cmd = "" input_string = "\r" note = input_handle.InputHandle() left_arrow_count = 0 right_arrow_count = 0 while True: # 获取终端大小 term_size = os.get_terminal_size() w = term_size.columns h = term_size.lines # 如果窗口大小变了,更更改 if w != w_old or h != h_old: w_old = w h_old = h # 重置 虚拟终端的大小 channel.resize_pty(w, h) continue # 更改 channel 会发生 InterruptedError 异常。 try: rlist, wlist, errlist = select.select([channel, sys.stdin], [], []) except InterruptedError: continue # (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_debug'], log_fmode="a") # logger.debug("{channel len: %s}" % dir(channel.in_buffer._buffer)) # logger.debug("{dir(channel.recv): %s}" % help(channel.recv)) # logger.debug("{channel : %s}" % channel.in_buffer._buffer) # logger.debug("{channel len: %s}" % len(channel.in_buffer._buffer)) # logger.removeHandler(fh) # 从标准输入和socket 获取数据然后写入标准输出 if channel in rlist: try: # 启动交互 头两次等待一小会儿,然数据接受完整(为了登录时不显示 docker exec -it 进入容器这一步) if tm > 0: time.sleep(0.2 * tm) tm = tm - 1 # 接收多少个字节 r = channel.recv(1024) # tab 补全 if input_string == '\t' and r[:2] != b"\r\n" and r[:1] != b"\x07" and r[ -23:] != b"possibilities? (y or n)": input_cmd = input_cmd + output.u_reconsitution(r) # (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_debug'], log_fmode="a") # logger.debug("{input_string: %s r[:1]: %s }" % ([input_string], r[:1])) # logger.debug("{input_cmd: %s r[:3]: %s }" % ([input_cmd], r[:3])) # logger.removeHandler(fh) # (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_debug'], log_fmode="a") # logger.debug("{1 note.yn: %s}" % note.yn) recv_list = r.split(b'\r\n') # logger.debug("{rlist : %s}" % recv_list) # logger.debug("{r length : %s}" % len(r)) # logger.debug("{rlist[-1] : %s}" % rlist[-1]) # 更改 补全提示状态 if recv_list[-1] != b"--More--" and len(r) < 1024: note.yn = False elif recv_list[-1] == b"--More--": note.yn = True # logger.debug("{6 note.yn : %s}" % note.yn) # logger.removeHandler(fh) if input_string == "\t" and r[ -23:] == b"possibilities? (y or n)": note.yn = True # (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_debug'], log_fmode="a") # logger.debug("{2 note.yn: %s}" % note.yn) # logger.removeHandler(fh) status = output.output(r) if status == "exit": break elif status == None: pass except socket.timeout: pass # 从标准输入获取输入的字符:然后,处理输入,对输入进行过滤 if sys.stdin in rlist: input_string = funkey(stdinput=sys.stdin) if input_string == "break": break # elif input_string == "left_arrow\x1b[D": # left_arrow_count = left_arrow_count + 1 # input_string = '\x1b[D' # elif input_string == "right_arrow\x1b[C": # right_arrow_count = right_arrow_count + 1 # input_string = '\x1b[C' # # if len(input_cmd) == 0: # pass # else: # cursor_position = left_arrow_count - right_arrow_count # if cursor_position <= 0: # cursor_position = 0 # elif cursor_position >= len(input_cmd): # cursor_position = len(input_cmd) # # if input_string == "\x08" and cursor_position < len(input_cmd): # input_cmd = input_cmd[:-(cursor_position + 1)] + input_cmd[-cursor_position:] # 命令过滤 input_string, input_cmd = filter.filter_stdin( input_string=input_string, input_cmd=input_cmd, yesno=note) # 把输入的字符发送给 远程ssh (准确的说是:经过过滤过的输入) channel.send(input_string) except Exception as e: fe = traceback.format_exc() (logger, fh) = logs.log(log_file=configs.CONFIG['logfile_error'], log_fmode="a") logger.error("{ error }:%s \n %s" % (e, fe)) logger.removeHandler(fh) finally: termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
def signal_listener(self, connection, event): botqueue = main.queuedict[connection.server+self.bot] while True: while botqueue.empty(): time.sleep(0.2) mystring = botqueue.get() bot = self.bot print ("Signal received on %s: %s" % (connection.server+':'+bot, mystring)) if "stop" in mystring: #this is the case when there are no more DCCconnections or when IRCapp shutdown is pressed self.dict[bot]["stop"] = True self.reactor.disconnect_all() break elif "cancel" in mystring: if self.direct: self.reactor.disconnect_all() del botqueue self.dict[bot]["down"].delete() return #connection.privmsg(bot, "xdcc cancel") self.dict[bot]["cancel"] = True try: self.DCCconnections_dict[bot].disconnect() except KeyError: #DCC connection not yet established self.on_dcc_disconnect(connection, event, bot=bot) else: mylist = [x for x in mystring.split(",")] if mylist[0] == "queue_item": connection.privmsg(bot, "xdcc send #%s" % mylist[3]) log("New package requested").write() hist = Download_History(filename=mylist[1], sizeraw=mylist[2]) self.dict[bot]["filename"], self.dict[bot]["size"], self.dict[bot]["received_bytes"] = mylist[1], mylist[2], 0 (self.dict[bot]["down"].status, self.dict[bot]["down"].progress, self.dict[bot]["down"].completed, self.dict[bot]["down"].eta, self.dict[bot]["down"].timeleft, self.dict[bot]["down"].active)= 'New package requested', 0, 0, None, None, True self.dict[bot]["down"].filename, self.dict[bot]["down"].sizeraw, self.dict[bot]["down"].package_number = ( mylist[1], mylist[2], mylist[3]) self.dict[bot]["down"].save() self.dict[bot]["hist"] = hist else: main.queuedict[connection.server+mylist[1]] = Queue() #remaining case : the bot has to join another channel of this server #since we're about to open a new DCCconnection, create new download object down = Download_Ongoing(filename=mylist[3], status="Requesting package...", active=True, server=self.dict[bot]["down"].server, channel=mylist[0], username=mylist[1], package_number=mylist[2], sizeraw=mylist[4]) hist = Download_History(filename=mylist[3], sizeraw=mylist[4]) down.save() bot = self.bot = mylist[1] data = { "received_bytes" : 0, "cancel" : False, "stop" : False, "position" : 0, "down" : down, "hist" : hist, "filename" : mylist[3], "size" : mylist[4] } self.dict[bot] = data connection.join(mylist[0]) connection.privmsg(mylist[1], "xdcc send #%s" % mylist[2]) if "moviegods" in mylist[0]: connection.join("#MG-CHAT") log("Channel joined (%s) and package requested" % mylist[0]).write() self.on_welcome(connection, event, first=False)