Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Table to hold active private chatrooms
- local privateChatrooms = {}
- -- 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 for encryption
- local function downloadOrCreateKey(protocol)
- local keyFilePath = protocol .. "_XOR.key"
- -- Check if the key file exists
- if fileExists(keyFilePath) then
- print("Encryption key for protocol " .. protocol .. " found.")
- else
- -- Simulate downloading or generating the key
- print("Downloading encryption key for protocol " .. protocol .. "...")
- local key = "defaultEncryptionKey123" -- Can customize this key
- -- Save the key to a file
- local file = fs.open(keyFilePath, "w")
- file.write(key)
- file.close()
- print("Encryption key saved as " .. keyFilePath)
- end
- -- Read the key from the file
- local file = fs.open(keyFilePath, "r")
- local key = file.readAll()
- file.close()
- return key
- end
- -- XOR encryption function for message security
- 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 display a message within a chatroom
- local function displayMessage(protocol, senderId, message)
- term.setTextColor(colors.yellow)
- print("[" .. protocol .. "] [" .. senderId .. "]: " .. message)
- term.setTextColor(colors.white) -- Reset text color
- end
- -- Function to handle messages for a private chatroom
- local function handlePrivateMessages(protocol, encryptionKey)
- while true do
- local senderId, encryptedMessage, receivedProtocol = rednet.receive(protocol)
- if receivedProtocol == protocol then
- local decryptedMessage = xorEncryptDecrypt(encryptedMessage, encryptionKey)
- -- Split username and message
- local username, chatMessage = decryptedMessage:match("^(.-): (.+)$")
- if username and chatMessage then
- displayMessage(protocol, username, chatMessage)
- else
- displayMessage(protocol, senderId, decryptedMessage)
- end
- end
- end
- end
- -- Function to send messages within a private chatroom
- local function sendPrivateMessage(protocol, username, encryptionKey)
- while true do
- print("[" .. protocol .. "] Type your message (or 'exit' to leave): ")
- local message = read()
- if message == "exit" then
- print("Exiting chatroom " .. protocol)
- break
- end
- local formattedMessage = username .. ": " .. message
- local encryptedMessage = xorEncryptDecrypt(formattedMessage, encryptionKey)
- rednet.broadcast(encryptedMessage, protocol)
- displayMessage(protocol, username, message)
- end
- end
- -- Function to create or join a private chatroom with password protection
- local function createOrJoinPrivateChatroom(protocol, username)
- -- Check if the chatroom already exists
- if privateChatrooms[protocol] then
- -- If the chatroom exists, ask for the password to join
- print("Chatroom for protocol " .. protocol .. " exists.")
- print("Enter the password to join:")
- local passwordAttempt = read("*") -- Read password securely (hidden input)
- -- Verify password
- if privateChatrooms[protocol].password == passwordAttempt then
- print("Password accepted! Joining chatroom...")
- else
- print("Incorrect password. Access denied.")
- return
- end
- else
- -- If the chatroom doesn't exist, create it with a password
- print("Creating new chatroom for protocol: " .. protocol)
- print("Enter a password for this chatroom:")
- local password = read("*") -- Read password securely (hidden input)
- -- Save the password for this chatroom
- local encryptionKey = downloadOrCreateKey(protocol)
- privateChatrooms[protocol] = { encryptionKey = encryptionKey, password = password }
- print("Chatroom created with password protection.")
- end
- -- Start handling messages and sending messages for this protocol
- parallel.waitForAny(
- function() handlePrivateMessages(protocol, privateChatrooms[protocol].encryptionKey) end,
- function() sendPrivateMessage(protocol, username, privateChatrooms[protocol].encryptionKey) end
- )
- end
- -- Function to list available 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 set up the modem for communication
- local function setupModem()
- local modems = listModems()
- if #modems == 0 then
- print("No modems found! Please attach a modem and restart.")
- return nil
- end
- print("Available modems:")
- for i, side in ipairs(modems) do
- print(i .. ": " .. side)
- end
- rednet.open(modems[1]) -- Open the first modem found
- print("Modem on " .. modems[1] .. " initialized.")
- return modems[1]
- end
- -- Main server function to manage connections and private chatrooms
- term.clear()
- term.setCursorPos(1, 1)
- print("Welcome to the Protocol Private Chatroom Server with Password Protection")
- -- Setup modem for server communication
- local modemSide = setupModem()
- if modemSide == nil then return end -- Exit if no modem found
- while true do
- print("Enter a protocol for a private chatroom (or 'exit' to quit): ")
- local protocol = read()
- if protocol == "exit" then
- print("Server shutting down...")
- break
- end
- if protocol ~= "" then
- print("Enter your username: ")
- local username = read()
- createOrJoinPrivateChatroom(protocol, username)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement