Advertisement
DOGGYWOOF

Untitled

Sep 29th, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 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. local file = fs.open(keyFilePath, "r")
  22. local keyData = file.readAll()
  23. file.close()
  24. return keyData
  25. else
  26. -- Simulate downloading the key by creating a new one
  27. print("Downloading encryption key...")
  28. local clientId = "Doggy.net/client_v2.0" -- You can customize this ID
  29. local key = "x3A7rB8zD2L5qW4nH1" -- You can customize this key
  30.  
  31. -- Save the ID and key to the XOR.key file
  32. local file = fs.open(keyFilePath, "w")
  33. file.write(clientId .. "," .. key) -- Store ID and key separated by a comma
  34. file.close()
  35. print("Encryption key downloaded and saved as XOR.key.")
  36.  
  37. return clientId .. "," .. key
  38. end
  39. end
  40.  
  41. -- Function to get client ID and key
  42. local function getClientIdAndKey(encryptionKey)
  43. local clientId, key = encryptionKey:match("^(.*),(.*)$")
  44. return clientId, key
  45. end
  46.  
  47. -- XOR encryption function
  48. local function xorEncryptDecrypt(message, key)
  49. local encrypted = {}
  50. for i = 1, #message do
  51. local char = string.byte(message, i)
  52. local keyChar = string.byte(key, (i - 1) % #key + 1)
  53. table.insert(encrypted, string.char(bit.bxor(char, keyChar)))
  54. end
  55. return table.concat(encrypted)
  56. end
  57.  
  58. -- Function to list all connected modems
  59. local function listModems()
  60. local modems = {}
  61. for _, side in ipairs(peripheral.getNames()) do
  62. if peripheral.getType(side) == "modem" then
  63. table.insert(modems, side)
  64. end
  65. end
  66. return modems
  67. end
  68.  
  69. -- Function to setup the modem
  70. local function setupModem()
  71. local modems = listModems()
  72. if #modems == 0 then
  73. print("No modems found! Please connect a modem and restart.")
  74. return nil
  75. end
  76.  
  77. print("Connected modems:")
  78. for i, side in ipairs(modems) do
  79. print(i .. ": " .. side)
  80. end
  81.  
  82. -- Open the first modem found
  83. rednet.open(modems[1])
  84. print("Using modem on " .. modems[1])
  85. return modems[1]
  86. end
  87.  
  88. -- Function to display a message in the chat
  89. local function displayMessage(senderId, message, isOlderClient)
  90. term.setTextColor(colors.yellow) -- Set text color
  91. if isOlderClient then
  92. print("[Unknown User (" .. senderId .. ")]: " .. message) -- Show ID for older clients
  93. else
  94. print("[" .. senderId .. "]: " .. message)
  95. end
  96. term.setTextColor(colors.white) -- Reset text color
  97. end
  98.  
  99. -- Function to handle incoming messages (with decryption and protocol)
  100. local function handleIncomingMessages(encryptionKey)
  101. local clientId, key = getClientIdAndKey(encryptionKey)
  102.  
  103. while true do
  104. -- Receive message only with the custom protocol
  105. local senderId, encryptedMessage, protocol = rednet.receive(chatProtocol)
  106.  
  107. -- Decrypt the incoming message
  108. local decryptedMessage = xorEncryptDecrypt(encryptedMessage, key)
  109.  
  110. -- Split the username and chat message
  111. local username, chatMessage = decryptedMessage:match("^(.-): (.+)$")
  112. if username and chatMessage then
  113. displayMessage(username, chatMessage, false)
  114. else
  115. displayMessage(senderId, decryptedMessage, true) -- Handle messages from older clients
  116. end
  117. end
  118. end
  119.  
  120. -- Function to send a message (with encryption and protocol)
  121. local function sendMessage(username, encryptionKey)
  122. local clientId, key = getClientIdAndKey(encryptionKey)
  123.  
  124. while true do
  125. print("Type your message (or 'exit' to quit): ")
  126. local userInput = read() -- Get the user's input
  127. if userInput == "exit" then
  128. print("Exiting chat...")
  129. shell.run("/disk/os/gui") -- Run the GUI program on exit
  130. break
  131. end
  132.  
  133. -- Format the message to include the username
  134. local formattedMessage = username .. ": " .. userInput
  135.  
  136. -- Encrypt the message before broadcasting
  137. local encryptedMessage = xorEncryptDecrypt(formattedMessage, key)
  138.  
  139. -- Broadcast the encrypted message to all users with the custom protocol
  140. rednet.broadcast(encryptedMessage, chatProtocol)
  141.  
  142. -- Display the unencrypted message locally
  143. displayMessage(username, userInput, false)
  144. end
  145. end
  146.  
  147. -- Function to verify the encryption key
  148. local function verifyEncryptionKey(encryptionKey)
  149. local clientId, key = getClientIdAndKey(encryptionKey)
  150.  
  151. -- Check if the client ID matches the expected value (you can customize this)
  152. if clientId ~= "Client123" then
  153. rednet.broadcast("Error: Invalid client ID or encryption key! Generating a new key...", chatProtocol)
  154.  
  155. -- Regenerate the encryption key
  156. local newEncryptionKey = downloadOrCreateKey()
  157. return newEncryptionKey, false -- Return the new key and verification status
  158. end
  159.  
  160. return encryptionKey, true -- Return the original key and verification status
  161. end
  162.  
  163. -- Main program
  164. term.clear() -- Clear the terminal
  165. term.setCursorPos(1, 1) -- Set cursor position
  166.  
  167. print("Welcome to the Doggy OS Network Chatroom!")
  168.  
  169. -- Prompt for the username
  170. print("Enter your username:")
  171. local username = read()
  172.  
  173. -- Ask the user to choose between public and private chatroom
  174. print("Would you like to join a public chatroom or a private chatroom? (p for public, c for private)")
  175. local chatroomChoice = read()
  176.  
  177. if chatroomChoice == "p" or chatroomChoice == "P" then
  178. print("Joining public chatroom with default protocol: " .. chatProtocol)
  179. else
  180. print("Enter a custom protocol name for the private chatroom:")
  181. chatProtocol = read()
  182. print("Using custom protocol for the private chatroom: " .. chatProtocol)
  183. end
  184.  
  185. -- Download or load the encryption key from XOR.key
  186. local encryptionKey = downloadOrCreateKey()
  187.  
  188. -- Verify the encryption key and regenerate if necessary
  189. encryptionKey, isVerified = verifyEncryptionKey(encryptionKey)
  190.  
  191. -- Setup the modem
  192. local modemSide = setupModem()
  193. if modemSide == nil then return end -- Exit if no modem was found
  194.  
  195. print("Type 'exit' to leave the chat.")
  196.  
  197. -- Start monitoring messages and detecting jamming in parallel
  198. parallel.waitForAny(function()
  199. handleIncomingMessages(encryptionKey)
  200. end, function()
  201. sendMessage(username, encryptionKey)
  202. end)
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement