async def main(self, host, port): app = web.Application() app.add_routes([web.get('/{tail:.*}', self.handler)]) runner = web.AppRunner(app) await runner.setup() site = web.TCPSite(runner) await site.start() log(LogLevel.INFO, 'HTTP CDN server is listening for connections...') await asyncio.Event().wait()
async def handler(self, request: web.Request): log(LogLevel.INFO, 'HTTP CDN ' + str(request)) # log(LogLevel.DEBUG, 'headers =>', dict(request.headers)) try: val = open('amazingcdn\\' + str(request.rel_url), 'rb').read() return web.Response(body=val) except: log(LogLevel.WARN, 'HTTP CDN Not Found: ' + str(request.rel_url)) return web.HTTPNotFound()
async def process_message(self, peer_name: str, data: bytearray) -> BitStream: request_bs = BitStream(data) message_header = MessageHeader() message_header.deserialize(request_bs) message = self.message_factory.build_message(message_header) if not message: log( LogLevel.WARN, '{} [bold yellow]<{}>[/] is not supported yet: {}'.format( peer_name, message_header.message_type, bytes(request_bs.data))) return if message_header.is_response: log( LogLevel.WARN, '{} [bold yellow]<{}>[/] responses from client are not supported yet: {}' .format(peer_name, message_header.message_type, bytes(request_bs.data))) return message.request.deserialize(request_bs) await message.process(message_header) message_header.is_response = True log( LogLevel.INFO, '{} Processed [bold blue]<{}>[/] with [bold blue]<{}> <{}>[/]'. format(peer_name, message_header.message_type, message_header.result_code, message_header.app_code)) response_bs = BitStream() message_header.serialize(response_bs) message.response.serialize(response_bs) log( LogLevel.DEBUG, '=>', { 'request_bs': bytes(request_bs.data), 'response_bs': bytes(response_bs.data), 'request': message.request.to_dict(), 'response': message.response.to_dict() }) return response_bs
async def client_connected(self, reader: StreamReader, writer: StreamWriter): peer_name = writer.transport.get_extra_info('peername') log(LogLevel.INFO, f'{peer_name} connected') while True: try: data = await self.bit_protocol.read_data(reader) response = await self.process_message(peer_name, data) if response: await self.bit_protocol.write_message( writer, response.data) except ConnectionError as err: log(LogLevel.INFO, f'{peer_name} disconnected: {err}') break except NotImplementedError as err: log(LogLevel.ERROR, f'{peer_name}: {data}') except Exception as err: log(LogLevel.FATAL, f'{peer_name} disconnected: {data}') break writer.close()
async def process_message(self, peer_name: str, data: bytearray) -> BitStream: request_bs = BitStream(data) message_header = MessageHeader() message_header.deserialize(request_bs) message = self.message_factory.build_message(message_header) if message: message.request.deserialize(request_bs) log(f'{peer_name} Request <{message_header}> {message.request}', LogLevel.TRACE) await message.process(message_header) if message.response: message_header.is_response = True log(f'{peer_name} Response <{message_header}> {message.response}', LogLevel.TRACE) response_bs = BitStream() message_header.serialize(response_bs) message.response.serialize(response_bs) return response_bs else: log(f'{peer_name} No Response <{message_header.message_type}>', LogLevel.TRACE) else: log(f'{peer_name} Request <{message_header.message_type}> is not supported yet: {bytes(request_bs.data)}', LogLevel.WARN)
async def client_connected(self, reader: StreamReader, writer: StreamWriter): peer_name = writer.transport.get_extra_info('peername') log(f'{peer_name} connected', LogLevel.INFO) session = Session() try: while True: data = await self.bit_protocol.read_data(reader) try: response = await session.process_message(peer_name, data) except NotImplementedError as err: log(f'{peer_name} {err}', LogLevel.ERROR) except Exception as err: log(f'{peer_name} {err}', LogLevel.FATAL) raise err # print traceback somewhere if response: await self.bit_protocol.write_message( writer, response.data) except ConnectionError as err: log(f'{peer_name} disconnected: {err}', LogLevel.INFO) finally: writer.close()
async def main(self, host, port): tcp_server = await asyncio.start_server(self.client_connected, host, port) log('server is listening for connections...', LogLevel.INFO) await tcp_server.serve_forever()