Advertisement
DOGGYWOOF

Untitled

Sep 29th, 2024
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 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. -- Main server program
  104. term.clear() -- Clear the terminal
  105. term.setCursorPos(1, 1) -- Set cursor position
  106.  
  107. print("Doggy OS Network Chatroom Server!")
  108.  
  109. -- Download or load the encryption key from XOR.key
  110. local encryptionKey = downloadOrCreateKey()
  111.  
  112. local modemSide = setupModem()
  113. if modemSide == nil then return end -- Exit if no modem was found
  114.  
  115. print("Server is running...")
  116.  
  117. -- Start handling incoming messages
  118. handleIncomingMessages(encryptionKey)
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement