Advertisement
onwardprogress

clientTest

Dec 30th, 2022 (edited)
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.08 KB | Software | 0 0
  1. -- The key and IV used for encryption and decryption
  2. local key = "a secret key"
  3. local iv = "an initialization vector"
  4.  
  5. -- The server computer id
  6. local server_id = "2"
  7.  
  8. os.pullEvent = os.pullEventRaw
  9.  
  10. -- Function to check if a disk is inserted into any of the computer drives
  11. function checkDisk()
  12.     local result = false
  13.     if disk.isPresent("left") or disk.isPresent("right") then
  14.         result = true
  15.     end
  16.     return result
  17. end
  18.  
  19. -- Main program loop
  20. while true do
  21.     -- Prompt the user to insert a disk into a drive
  22.     print("Insert an id disk into the drive.")
  23.     -- Wait for a disk to be inserted into a drive
  24.     local event, diskSide = os.pullEvent("disk")
  25.     while not checkDisk() do
  26.         sleep(1)
  27.     end
  28.     local mountPath = disk.getMountPath(diskSide)
  29.     if fs.exists(mountPath .. "/user_id.txt") and fs.exists(mountPath .. "user_pass.txt") then
  30.         -- Open the "id" file on the left disk
  31.         local file = fs.open(mountPath .. "/user_id.txt", "r")
  32.         -- Read the contents of the file
  33.         local admin_user_id = file.readAll()
  34.         -- Close the file
  35.         file.close()
  36.         local encryptedMessage = encrypt("aes-256-cbc", key, iv, "add user")
  37.         rednet.send(server_id, encryptedMessage)
  38.         local encrypted_admin_user_id = encrypt("aes-256-cbc", key, iv, admin_user_id)
  39.         rednet.send(server_id, encrypted_admin_user_id)
  40.         local _, response = rednet.receive()
  41.         local decryptedResponse = decrypt("aes-256-cbc", key, iv, response)
  42.         if (decryptedResponse == "user recognized") then
  43.             -- Send the user pass
  44.             local file = fs.open(mountPath .. "/user_pass.txt", "r")
  45.             -- Read the contents of the file
  46.             local admin_user_pass = file.readAll()
  47.             -- Close the file
  48.             file.close()
  49.             local encrypted_admin_user_pass = encrypt("aes-256-cbc", key, iv, admin_user_pass)
  50.             rednet.send(server_id, encrypted_admin_user_pass)
  51.             local _, response = rednet.receive()
  52.             local decryptedResponse = decrypt("aes-256-cbc", key, iv, response)
  53.             if (decryptedResponse == "user verified") then
  54.                 -- Receive the new user pass and write it to the file
  55.                 local _, encrypted_new_admin_user_pass = rednet.receive()
  56.                 local new_admin_user_pass = decrypt("aes-256-cbc", key, iv, encrypted_new_admin_user_pass)
  57.                 local file = fs.open(mountPath .. "/user_pass.txt", "w")
  58.                 file.write(new_admin_user_pass)
  59.                 file.close()
  60.                 local _, response = rednet.receive()
  61.                 local decryptedResponse = decrypt("aes-256-cbc", key, iv, response)
  62.                 if (decryptedResponse == "access granted") then
  63.                     redstone.setOutput("back", true)
  64.                     sleep(4)
  65.                     redstone.setOutput("back", false)
  66.                 else
  67.                     -- Inform the user that the card in the right slot is not an admin account
  68.                     print("The inserted disk does not have sufficient permissions for this door")
  69.                     sleep(3)
  70.                 end
  71.             else
  72.                 print("The inserted disk has a mismatched user password. Please contact a site administrator.")
  73.                 sleep(3)
  74.             end
  75.         else
  76.             print("The inserted disk is not a recognized user.")
  77.             sleep(3)
  78.         end
  79.     else
  80.         print("The inserted disk is not a valid id card.")
  81.         sleep(3)
  82.     end
  83.     -- Eject both disks
  84.     disk.eject("left")
  85.     disk.eject("right")
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement