Add magic check

This commit is contained in:
Eli Ribble 2023-06-19 18:10:47 -07:00
parent e227d349b2
commit 461fc7ce0b
1 changed files with 16 additions and 3 deletions

View File

@ -4,19 +4,32 @@ import logging
LOGGER = logging.getLogger("server") LOGGER = logging.getLogger("server")
MAGIC = "e8437140-4347-48cc-a31d-dcdc944ffc16"
def main(): def main():
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
asyncio.run(run()) asyncio.run(run())
async def on_connect(reader, writer): async def on_connect(reader, writer):
LOGGER.info("connected") LOGGER.info("connected")
data = await reader.read() data = await reader.read(36)
print(data.decode("UTF-8")) magic = data.decode("UTF-8")
LOGGER.debug("Received magic '%s'", magic)
if magic != MAGIC:
writer.write("ERR1: Magic not recognized".encode("UTF-8"))
writer.close()
LOGGER.info("Bad magic, closing connection.")
return
LOGGER.debug("Magic looks good.")
async def run(): async def run():
server = await asyncio.start_server(on_connect, host="localhost", port=9988) server = await asyncio.start_server(on_connect, host="localhost", port=9988)
async with server: async with server:
await server.serve_forever() try:
await server.serve_forever()
except KeyboardInterrupt:
LOGGER.info("Exiting at user request.")
if __name__ == "__main__": if __name__ == "__main__":
main() main()