def check_cookie(cookies): if cookies is None: checker.mumble(error='no cookies =(') for cookie in cookies: if cookie.key == 'auth': return checker.mumble(error="auth cookie not found. '{}'".format(get_cookie_string(cookies)))
def process(self): if len(self.specs) == 0: checker.down(message='No one signal package') data = numpy.concatenate(self.specs) mx = numpy.max(data) data = numpy.log(data / mx) >= -1.5 skiped, comp = compress(data) if len(comp) < 2: checker.corrupt(message='No signal') bounds = get_bounds(numpy.unique(comp)) if len(bounds) == 0: checker.mumble(message="Can't understand dot size") silence = comp[1::2] signal = comp[0::2] if skiped == 2: signal, silence = silence, signal signal = list(numpy.where(signal <= bounds[0], '.', '-')) silence = apply_silence_bounds(silence, bounds) if skiped == 2: signal, silence = silence, signal morse = merge(signal, silence) text = convert_to_text(morse) if not text: checker.corrupt(message='No one symbol in signal') return text
async def check_status(response, log_info): if response.status >= 500: checker.down(error='{}\n\tstatus code is {}. Content: {}\n'.format( log_info, response.status, await response.text())) if response.status != 200: checker.mumble(error='{}\n\tstatus code is {}. Content: {}\n'.format( log_info, response.status, await response.text()))
async def start_internal(self): try: async with self.connection as ws: async for msg in ws: if msg.type == aiohttp.WSMsgType.TEXT: try: data = msg.json(loads=lambda s: checker.parse_json( s, [ 'id', 'x', 'y', 'message', 'public', 'user' ], ['id'])) except Exception as ex: checker.mumble( error='can\'t parse service responce', exception=ex) await self.queue.put(data) elif msg.type == aiohttp.WSMsgType.CLOSED: self.closed = True break else: checker.mumble( error= 'get message with unexpected type {}\nmessage: {}'. format(msg.type, msg.data)) except Exception as ex: checker.down(error='something down', exception=ex)
async def get_with_retries(self, url): status, text = await self.get(url, need_check_status=False) while status >= 500: status, text = await self.get(url, need_check_status=False) if status == 200: return text checker.mumble(message="unexpected status {}".format(status))
async def find(self, id): while True: if self.queue.empty() and self.closed: checker.mumble(error='point not found') top = await self.queue.get() if top['id'] == id: self.connection.close() return top
def compare(p1, p2, fields): error = [] for f in fields: if p1[f] != p2[f]: error.append(f) if len(error) > 0: checker.mumble( error='points are different in fields {}: {} vs {}'.format( error, p1, p2))
def check(self, hostname, username, data): h = self.getHash(hostname, username, data) if 'sign' not in data: checker.mumble(error='fail to find sign') try: sign = unhexlify(data['sign'].encode()) except Exception as ex: checker.mumble(error='fail to parse sign', exception=ex) return self.signer.verify(h, sign)
def convert_to_text(morse): chars = morse.split() if morse[0] != ' ': chars = chars[1:] if morse[-1] != ' ': chars = chars[:-1] res = '' for ch in chars: if ch not in morseMapping: checker.mumble(message='Unknown sequence {}'.format(ch)) res += morseMapping[ch] return res
def get_sections(self, section_name=None): if section_name is None: section_name = bytes([0] * 20) if type(section_name) == str: section_name = get_section_name(section_name) self.send_checked('all-section', section_name) res_length = self.recv(2).decode(encoding='ascii') if not res_length.isdigit(): checker.mumble(error="count is not digit '{}'".format(res_length)) res = [] for i in range(int(res_length)): res.append(self.recv(20)) return res
async def start_internal(self): async with self.connection as ws: async for msg in ws: if msg.type == aiohttp.WSMsgType.TEXT: try: data = msg.json(loads = lambda s : checker.parse_json(s, ['url', 'owner'])) except Exception as ex: checher.mumble(error='can\'t parse service responce', exception=ex) await self.queue.put((data['url'], data['owner'])) elif msg.type == aiohttp.WSMsgType.CLOSED: break else: checker.mumble(error='get message with unexpected type {}\nmessage: {}'.format(msg.type, msg.data))
async def check_path(username, sender, another, aname): responses = [] for i in range(random.randint(1, 3)): responses.append(await sender.put_point(user=username)) for i in range(random.randint(1, 3)): responses.append(await another.put_point(is_public=True, user=aname)) start = get_rand_point() finish = get_rand_point() ids = [point['id'] for point in responses] path = await sender.get_path(start, finish, ids) check_points_list(path, ['x', 'y']) if len(path) == 0: checker.mumble(error='path must contains at least one point') if not equal_points(path[0], start): checker.mumble( error='start point is bad: {} vs {}'.format(start, path[0])) if not equal_points(path[-1], finish): checker.mumble( error='finish point is bad: {} vs {}'.format(finish, path[-1])) for p in responses: for i in range(1, len(path)): if is_between(path[i - 1], path[i], p): break else: checker.mumble(error='point {} not in path {}'.format(p, path))
def get_section(self, section_name, apikey, start=None): if type(section_name) == str: section_name = get_section_name(section_name) if type(apikey) == str: apikey = get_api_key(apikey) if start is None: start = bytes([0] * 20) if type(start) == str: start = get_k(start) self.send_checked('get-section', section_name + apikey + start) res_length = self.recv(1).decode(encoding='ascii') if not res_length.isdigit(): checker.mumble(error="count is not digit '{}'".format(res_length)) res = [] for i in range(int(res_length)): el = self.recv_pair() res.append(el) return res
async def register(self, username=None, password=None): can_retry = username is None username = checker.get_value_or_rand_name(username) password = checker.get_value_or_rand_string(password, 16) self.create_session(username, password) status, text = await self.post('/register', need_check_status=False) if status == 200: return username, password if status >= 400 or can_retry: while status != 200: if can_retry: username = checker.get_rand_string(16) password = checker.get_rand_string(32) self.create_session(username, password) status, text = await self.post('/register', need_check_status=False) return username, password checker.mumble( error='error while register: status {}, response {}'.format( status, text))
async def start_internal(self): try: async for msg in self.connection: if msg.type == self.type: checker.log(self.log_info + 'get data, length: {}'.format(len(msg.data))) try: await self.process(msg) except Exception as ex: checker.mumble(error='can\'t process service responce', exception=ex) elif msg.type == aiohttp.WSMsgType.CLOSED: self.closed = True break else: checker.mumble( error='get message with unexpected type {}\nmessage: {}' .format(msg.type, msg.data)) except Exception as ex: checker.down(error='something down', exception=ex)
async def login(self, username=None, password=None, skills=None): can_retry = username is None if username is None: username = checker.get_rand_string(8) if password is None: password = checker.get_rand_string(16) request = {'user': username, 'password': password} if skills is not None: request['skills'] = skills status, text = await self.post('login', request, need_check_status = False) if status == 200: check_cookie(self.session.cookie_jar) return username, password if status == 400 and can_retry: while status == 400: request['user'] = checker.get_rand_string(16) request['password'] = checker.get_rand_string(32) status, text = await self.post('login', request, need_check_status = False) check_cookie(self.session.cookie_jar) return username, password checker.mumble(error='error while login: status {}, response {}'.format(status, text))
async def handler_put(hostname, id, flag): first = State(hostname, PORT, 'first') key = get_rnd_string(random.randint(10, 20)) msg = { "text": flag, "frequency": random.randint(1, 4000), "dpm": random.randint(1, 4000), "need_base32": False, "password": get_rnd_string(random.randint(15, 30)), "is_private": True, 'datetime': None, } status, _ = await first.post(key, msg, False) if status != 200: print('CANT PUT') checker.mumble() print(key) checker.ok()
async def register(self, username=None, password=None): can_retry = username is None request = { 'user': checker.get_value_or_rand_name(username), 'password': checker.get_value_or_rand_string(password, 16) } status, text = await self.post('/api/login', request, need_check_status=False) if status == 200: return request['user'], request['password'] if 500 > status >= 400 and can_retry: while 500 > status >= 400: request['user'] = checker.get_rand_string(16) request['password'] = checker.get_rand_string(32) status, text = await self.post('/api/login', request, need_check_status=False) return request['user'], request['password'] checker.mumble( error='error while login: status {}, response {}'.format( status, text))
def check_points_list(l, expected=[]): if not type(l) is list: checker.mumble(error='list of points not a list, {}'.format(type(l))) error = [] for i in range(len(l)): if not type(l[i]) is dict: checker.mumble( error='point #{} is not a dict, {}'.format(i, type(l[i]))) for field in expected: if field not in l[i]: error.append(field) if len(error) > 0: checker.mumble( error='not all expected fields have founded in point #{}. {}'. format(i, str(errors)))
async def get_post(self, url, signed=False, username=None): response = await self.get(url) data = checker.parse_json(response, ['title', 'body']) if signed and not self.signer.check(self.hostname, username, data): checker.mumble(error='fail check sign for url {}'.format(url)) return data
async def get_content(self, url, title, body): response = await self.get_post(url) if response['title'] != title: checker.mumble(error="wrong post title '{}': expexted '{}', found '{}'".format(url, title, response['title'])) if response['body'] != body: checker.mumble(error="wrong post body '{}': expexted '{}', found '{}'".format(url, body, response['body']))
def decode(s): try: return urllib.parse.unquote(s, encoding="cp1251") except Exception as ex: checker.mumble(error="bad data '{}'".format(s), exception=ex)