async def getflag(self, logger, task, collection): try: logger.debug("Getting flag {}".format(task.flag)) data = await collection.find_one({"flag": task.flag}) if data is None: raise BrokenServiceException( "Couldn't retrieve the db for {}".format(task.flag)) user = data["user"] passw = data["password"] logger.debug("Logging is as User: {}".format(user)) reader, writer = await self.login_user(user, passw, task.address) logger.debug("Logged is as User: {}".format(user)) logger.debug("Querying the songs") song_list = await self.get_song_list(reader, writer) if task.flag not in song_list: raise BrokenServiceException( "Flag {} not in songlist {}".format(task.flag, song_list)) writer.write(b"q\n") except OfflineException as e: raise e except Exception as e: raise BrokenServiceException("Failed to get flag: {}, {}".format( task.flag, e))
async def login_user(self, user, password, addr): try: try: reader, writer = await asyncio.open_connection(addr, self.port) except: raise OfflineException("Couldn't connect to {}:{}".format( addr, self.port)) ret = await reader.readuntil(b": ") writer.write(b"l\n") await reader.readuntil(b": ") writer.write(user.encode() + b"\n") await reader.readuntil(b": ") writer.write(password.encode() + b"\n") ret = await reader.readline() if b"Sorry" in ret: raise Exception( "Unable to log in as the user {} doesn't exist!".format( user)) await reader.readuntil(b"? ") return reader, writer except OfflineException as e: raise e except Exception as e: raise BrokenServiceException( "Couldn't log in as the user {}, {}".format(user, e))
async def test(self) -> None: try: response:aiohttp.ClientResponse = await self.http_session.get(self.scheme + "://" + self.address + ":" + str(self.port) + "/api/debug/test") except: raise OfflineException() if response.status!=200: raise BrokenServiceException(f"test Failed: {response}") return await response.text()
async def add_to_session(self, sessionid:int, username:str) -> None: try: params = {'sessionid': sessionid, 'username': username} response:aiohttp.ClientResponse = await self.http_session.post(self.scheme + "://" + self.address + ":" + str(self.port) + "/api/gamesession/adduser", data=params) except: raise OfflineException() if response.status!=200: raise BrokenServiceException(f"add_to_session Failed: {response.status}")
async def login(self, username: str, password: str) ->aiohttp.ClientResponse: try: params = {'username': username, 'password': password} response:aiohttp.ClientResponse = await self.http_session.post(self.scheme + "://" + self.address + ":" + str(self.port) + "/api/account/login", data=params) except: raise OfflineException() if response.status!=200: raise BrokenServiceException(f"Login Failed: {response}")
async def create_session(self, name:str, notes:str, password:str) -> aiohttp.ClientResponse: try: params = {'name': name, 'notes': notes, 'password': password} response:aiohttp.ClientResponse = await self.http_session.post(self.scheme + "://" + self.address + ":" + str(self.port) + "/api/gamesession/create", data=params) except: raise OfflineException() if response.status!=200: raise BrokenServiceException(f"create_session Failed: {response}") return response
async def submit_song(self, reader, writer, song): try: writer.write(b"a\n") await reader.readuntil(b"> ") writer.write(song.encode() + b"\n") await reader.readuntil(b"? ") except Exception as e: raise BrokenServiceException("Couldn't submit Song {}, {}".format( song, e))
async def register(self, username: str, email: str, password: str) ->str: try: params = {'username': username, 'email':email, 'password': password} response:aiohttp.ClientResponse = await self.http_session.post(self.scheme + "://" + self.address + ":" + str(self.port) + "/api/account/register", data=params) text = await response.text() self.logger.debug(text) response.close() except: raise OfflineException() if response.status!=200: raise BrokenServiceException(f"Register Failed: {response}")
async def get_song_list(self, reader, writer): try: writer.write(b"l\n") l = await reader.readuntil(b"\n\n") await reader.readuntil(b"? ") l = l.strip() l = [x.decode().split(") ")[1] for x in l.split(b"\n")] return l except Exception as e: raise BrokenServiceException( "Couldn't get the full songlist, {}. {}".format(e, l))
async def putflag(self, logger, task, collection): try: logger.debug("Putting flag {}".format(task.flag)) self.address = task.address tag = ''.join( random.choice(ascii_letters + digits) for _ in range(20)) user = ''.join( random.choice(ascii_letters + digits) for _ in range(20)) passw = ''.join( random.choice(ascii_letters + digits) for _ in range(20)) await collection.insert_one({ 'flag': task.flag, 'tag': tag, 'user': user, 'password': passw }) logger.debug("Registering User: {} with Password: {}".format( user, passw)) await self.create_user(user, passw, task.address) logger.debug("Registered User: {}".format(user)) logger.debug("Putting Flag: {}".format(task.flag)) logger.debug("Logging in User: {}".format(user)) reader, writer = await self.login_user(user, passw, task.address) logger.debug("Logged in as User: {}".format(user)) logger.debug("Submitting all the songs") selected_songs = [ random.choice(songs) for x in range(random.randrange(3, 5)) ] rand_idx = random.randint(0, len(selected_songs) - 1) selected_songs = selected_songs[:rand_idx] + [ task.flag ] + selected_songs[rand_idx:] for song in selected_songs: logger.debug("Submitting song: {} for user {}".format( song, user)) await self.submit_song(reader, writer, song) logger.debug("Done submitting all the songs. Quitting!") writer.write(b"q\n") except OfflineException as e: raise e except Exception as e: raise BrokenServiceException("Failed to put flag: {}, {}".format( task.flag, e))
async def create_user(self, user, password, addr): try: try: reader, writer = await asyncio.open_connection(addr, self.port) except: raise OfflineException("Couldn't connect to {}:{}".format( addr, self.port)) ret = await reader.readuntil(b": ") writer.write(b"r\n") await reader.readuntil(b": ") writer.write(user.encode() + b"\n") await reader.readuntil(b": ") writer.write(password.encode() + b"\n") writer.close() except OfflineException as e: raise e except Exception as e: raise BrokenServiceException( "Couldn't create the user {}, {}".format(user, e))