Advertisement
DOGGYWOOF

Client

Sep 29th, 2024
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. -- Custom protocol (default)
  2. local chatProtocol = "doggyChat"
  3.  
  4. -- Function to check if a file exists
  5. local function fileExists(path)
  6. local file = fs.open(path, "r")
  7. if file then
  8. file.close()
  9. return true
  10. end
  11. return false
  12. end
  13.  
  14. -- Function to download or create the XOR.key file
  15. local function downloadOrCreateKey()
  16. local keyFilePath = "XOR.key"
  17.  
  18. -- Check if the key file exists
  19. if fileExists(keyFilePath) then
  20. print("Encryption key found.")
  21. else
  22. -- Simulate downloading the key by creating a new one
  23. print("Downloading encryption key...")
  24. local key = "defaultEncryptionKey123" -- You can customize this key
  25.  
  26. -- Save the key to the XOR.key file
  27. local file = fs.open(keyFilePath, "w")
  28. file.write(key)
  29. file.close()
  30. print("Encryption key downloaded and saved as XOR.key.")
  31. end
  32.  
  33. -- Read and return the key from the file
  34. local file = fs.open(keyFilePath, "r")
  35. local key = file.readAll()
  36. file.close()
  37.  
  38. return key
  39. end
  40.  
  41. -- XOR encryption function
  42. local function xorEncryptDecrypt(message, key)
  43. local encrypted = {}
  44. for i = 1, #message do
  45. local char = string.byte(message, i)
  46. local keyChar = string.byte(key, (i - 1) % #key + 1)
  47. table.insert(encrypted, string.char(bit.bxor(char, keyChar)))
  48. end
  49. return table.concat(encrypted)
  50. end
  51.  
  52. -- Function to list all connected modems
  53. local function listModems()
  54. local modems = {}
  55. for _, side in ipairs(peripheral.getNames()) do
  56. if peripheral.getType(side) == "modem" then
  57. table.insert(modems, side)
  58. end
  59. end
  60. return modems
  61. end
  62.  
  63. -- Function to setup the modem
  64. local function setupModem()
  65. local modems = listModems()
  66. if #modems == 0 then
  67. print("No modems found! Please connect a modem and restart.")
  68. return nil
  69. end
  70.  
  71. print("Connected modems:")
  72. for i, side in ipairs(modems) do
  73. print(i .. ": " .. side)
  74. end
  75.  
  76. -- Open the first modem found
  77. rednet.open(modems[1])
  78. print("Using modem on " .. modems[1])
  79. return modems[1]
  80. end
  81.  
  82. -- Function to display a message in the chat
  83. local function displayMessage(senderId, message)
  84. term.setTextColor(colors.yellow) -- Set text color
  85. print("[" .. senderId .. "]: " .. message)
  86. term.setTextColor(colors.white) -- Reset text color
  87. end
  88.  
  89. -- Function to handle incoming messages (with decryption and protocol)
  90. local function handleIncomingMessages(encryptionKey)
  91. while true do
  92. -- Receive message only with the custom protocol
  93. local senderId, encryptedMessage, protocol = rednet.receive(chatProtocol)
  94.  
  95. -- Decrypt the incoming message
  96. local decryptedMessage = xorEncryptDecrypt(encryptedMessage, encryptionKey)
  97.  
  98. -- Display the decrypted message
  99. displayMessage(senderId, decryptedMessage)
  100. end
  101. end
  102.  
  103. -- Function to send a message (with encryption and protocol)
  104. local function sendMessage(username, encryptionKey)
  105. while true do
  106. print("Type your message (or 'exit' to quit): ")
  107. local userInput = read() -- Get the user's input
  108. if userInput == "exit" then
  109. print("Exiting chat...")
  110. break
  111. end
  112.  
  113. -- Format the message to include the username
  114. local formattedMessage = username .. ": " .. userInput
  115.  
  116. -- Encrypt the message before broadcasting
  117. local encryptedMessage = xorEncryptDecrypt(formattedMessage, encryptionKey)
  118.  
  119. -- Broadcast the encrypted message to all users with the custom protocol
  120. rednet.broadcast(encryptedMessage, chatProtocol)
  121.  
  122. -- Display the unencrypted message locally
  123. displayMessage(username, userInput)
  124. end
  125. end
  126.  
  127. -- Main client program
  128. term.clear() -- Clear the terminal
  129. term.setCursorPos(1, 1) -- Set cursor position
  130.  
  131. print("Welcome to the Doggy OS Network Chatroom!")
  132.  
  133. -- Prompt for the username
  134. print("Enter your username:")
  135. local username = read()
  136.  
  137. -- Download or load the encryption key from XOR.key
  138. local encryptionKey = downloadOrCreateKey()
  139.  
  140. local modemSide = setupModem()
  141. if modemSide == nil then return end -- Exit if no modem was found
  142.  
  143. print("Type 'exit' to leave the chat.")
  144.  
  145. -- Start monitoring messages and sending in parallel
  146. parallel.waitForAny(function()
  147. handleIncomingMessages(encryptionKey)
  148. end, function()
  149. sendMessage(username, encryptionKey)
  150. end)
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement