diff --git a/centralLoggingSystem/readme.md b/centralLoggingSystem/readme.md new file mode 100644 index 0000000..357262b --- /dev/null +++ b/centralLoggingSystem/readme.md @@ -0,0 +1,17 @@ +# central logging system +Logs data sent to it by various computers and prints it on printed pieces of paper. + +# Server Setup +1. Download `server.lua` and put it on your computer (for example with wget) and put it in the startup folder (you might have to make this). +2. Attach a printer and a modem to the computer and note on what side of the computer they are. +3. Edit the file and change the variables `printerSide` and `modemSide` if needed. +4. Download the dependency and "install" it by putting `AES.lua` in the same directory as the program. +5. Decide on a shared key, this has to be a number (for example use something like `openssl rand -hex 16` and prefix it with `0x` to get a random 16 byte number) +6. Run `set AES.key ` +7. Choose a channel number and set the channel by changing the `channel` variable. + +# Client Setup + +# Dependencies +- [Lua_AES](https://github.com/idiomic/Lua_AES) + diff --git a/centralLoggingSystem/server.lua b/centralLoggingSystem/server.lua new file mode 100644 index 0000000..747f9c3 --- /dev/null +++ b/centralLoggingSystem/server.lua @@ -0,0 +1,72 @@ +settings.define("AES.key", { + description = "AES key used for encryption", + type = number +}) + +local printerSide = "right" +local modemSide = "top" + +local channel = 59598 + +local AES = require("/AES") +local key = settings.get("AES.key") or error("No AES Key set") + +local modem = peripheral.wrap(modemSide) or error("No modem found") +modem.closeAll() +modem.open(channel) + +local printer = peripheral.wrap(printerSide) or error("No printer found") + +function startPage() + if not printer.newPage() then + error("cant start new page") + end + printer.setPageTitle(os.date()) +end + +local pageOpened = false +function printNewLog(line) + print(line) + if not pageOpened then + startPage() + pageOpened = true + end + local width, height = printer.getPageSize() + local currX, currY = printer.getCursorPos() + + local neededLines = math.ceil(#line / width) + if neededLines > height+1 - currY then -- +1 because being on the last line still leaves one free + startPage() + end + local i = 1 + while i <= #line do + printer.write(line:sub(i, i + width-1)) + i = i + width - 1 + printer.setCursorPos(1, currY+1) + currX, currY = printer.getCursorPos() + end + if currY == height then + printer.endPage() + pageOpened = false + end +end + + +local modemMessage = {} +local decrypted = nil +local obj = nil +while true do + modemMessage = {os.pullEvent("modem_message")} + decrypted = AES.ECB_256(AES.decrypt, key, modemMessage[5]) + obj = textutils.unserialise(decrypted) + if obj then + if obj.sender and obj.message then + printNewLog(string.format("%s %s: %s", os.date(), obj.sender, obj.message)) + else + print("invalid object received") + print(decrypted) + end + else + print("invalid object received") + end +end