Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Custom protocol (default)
- local chatProtocol = "doggyChat"
- -- Function to check if a file exists
- local function fileExists(path)
- local file = fs.open(path, "r")
- if file then
- file.close()
- return true
- end
- return false
- end
- -- Function to download or create the XOR.key file
- local function downloadOrCreateKey()
- local keyFilePath = "XOR.key"
- -- Check if the key file exists
- if fileExists(keyFilePath) then
- print("Encryption key found.")
- else
- -- Simulate downloading the key by creating a new one
- print("Downloading encryption key...")
- local key = "defaultEncryptionKey123" -- You can customize this key
- -- Save the key to the XOR.key file
- local file = fs.open(keyFilePath, "w")
- file.write(key)
- file.close()
- print("Encryption key downloaded and saved as XOR.key.")
- end
- -- Read and return the key from the file
- local file = fs.open(keyFilePath, "r")
- local key = file.readAll()
- file.close()
- return key
- end
- -- XOR encryption function
- local function xorEncryptDecrypt(message, key)
- local encrypted = {}
- for i = 1, #message do
- local char = string.byte(message, i)
- local keyChar = string.byte(key, (i - 1) % #key + 1)
- table.insert(encrypted, string.char(bit.bxor(char, keyChar)))
- end
- return table.concat(encrypted)
- end
- -- Function to list all connected modems
- local function listModems()
- local modems = {}
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "modem" then
- table.insert(modems, side)
- end
- end
- return modems
- end
- -- Function to setup the modem
- local function setupModem()
- local modems = listModems()
- if #modems == 0 then
- print("No modems found! Please connect a modem and restart.")
- return nil
- end
- print("Connected modems:")
- for i, side in ipairs(modems) do
- print(i .. ": " .. side)
- end
- -- Open the first modem found
- rednet.open(modems[1])
- print("Using modem on " .. modems[1])
- return modems[1]
- end
- -- Function to display a message in the chat
- local function displayMessage(senderId, message)
- term.setTextColor(colors.yellow) -- Set text color
- print("[" .. senderId .. "]: " .. message)
- term.setTextColor(colors.white) -- Reset text color
- end
- -- Function to handle incoming messages (with decryption and protocol)
- local function handleIncomingMessages(encryptionKey)
- while true do
- -- Receive message only with the custom protocol
- local senderId, encryptedMessage, protocol = rednet.receive(chatProtocol)
- -- Decrypt the incoming message
- local decryptedMessage = xorEncryptDecrypt(encryptedMessage, encryptionKey)
- -- Display the decrypted message
- displayMessage(senderId, decryptedMessage)
- end
- end
- -- Main server program
- term.clear() -- Clear the terminal
- term.setCursorPos(1, 1) -- Set cursor position
- print("Doggy OS Network Chatroom Server!")
- -- Download or load the encryption key from XOR.key
- local encryptionKey = downloadOrCreateKey()
- local modemSide = setupModem()
- if modemSide == nil then return end -- Exit if no modem was found
- print("Server is running...")
- -- Start handling incoming messages
- handleIncomingMessages(encryptionKey)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement