Advertisement
onwardprogress

serverTest

Dec 30th, 2022 (edited)
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.86 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. -- Function to check if a file exists
  6. function fileExists(path)
  7.   local file = fs.open(path, "r")
  8.   if file then
  9.     file.close()
  10.     return true
  11.   end
  12.   return false
  13. end
  14.  
  15. -- Function to verify a user and their security level
  16. function verifyUser()
  17.     -- Wait for a message from the client
  18.     local door_id, message = rednet.receive()
  19.     -- Decrypt the message
  20.     local decryptedMessage = decrypt("aes-256-cbc", key, iv, message)  
  21.     -- Search the server for the user_id file
  22.     if fileExists(decryptedMessage .. ".txt") then
  23.         local file = fs.open(decryptedMessage .. ".txt", "r")
  24.         user_serverside_pass = file.readLine()
  25.         file.close()
  26.         -- Inform the client computer that the user has been recognized
  27.         local message = encrypt("aes-256-cbc", key, iv, "user recognized")
  28.         rednet.send(door_id, message)
  29.         -- Wait for a message from the client
  30.         local door_id, message = rednet.receive()
  31.         -- Decrypt the message
  32.         local user_clientside_pass = decrypt("aes-256-cbc", key, iv, message)
  33.         if (tostring(user_serverside_pass) == tostring(user_clientside_pass)) then
  34.             local message = encrypt("aes-256-cbc", key, iv, "user verified")
  35.             rednet.send(door_id, message)
  36.             -- Send the client computer the new user password to store
  37.             local file = fs.open(decryptedMessage .. ".txt", "w")
  38.             -- Create a new, random user password and store it in the user_id file
  39.             local newID = math.random(10000, 99999)
  40.             file.write(newID)
  41.             file.close()
  42.             local encryptedID = encrypt("aes-256-cbc", key, iv, tostring(newID))
  43.             rednet.send(door_id, encryptedID)
  44.             local user_level = fs.open(decryptedMessage .. "_level.txt", "r")
  45.             local door_level = fs.open("door_" .. door_id .. "_level", "r")
  46.             -- If the user has a sufficiently high access level, then return true
  47.             if (tonumber(user_level) >= tonumber(door_level)) then
  48.                 --inform the client computer that access is granted
  49.                 local message = encrypt("aes-256-cbc", key, iv, "access granted")
  50.                 rednet.send(door_id, message)
  51.                 return true
  52.             else
  53.                 -- inform client computer that the user has an insufficient access level for this door_
  54.                 local message = encrypt("aes-256-cbc", key, iv, "access denied")
  55.                 rednet.send(door_id, message)
  56.                 return false
  57.             end
  58.         else
  59.             -- Inform client computer that this is not a valid id card
  60.             local message = encrypt("aes-256-cbc", key, iv, "wrong password")
  61.             rednet.send(door_id, message)
  62.             return false
  63.         end
  64.     else
  65.         -- Inform client computer that this is not a valid user id
  66.         local message = encrypt("aes-256-cbc", key, iv, "invalid user id")
  67.         rednet.send(door_id, message)
  68.         return false
  69.     end
  70.  end
  71.  
  72. while true do
  73.     -- Wait for a message from a client
  74.     local door_id, message = rednet.receive()
  75.     -- Decrypt the message
  76.     local decryptedMessage = decrypt("aes-256-cbc", key, iv, message)
  77.     -- Check the message
  78.     if decryptedMessage == "add user" then
  79.         -- Verify if the user generating the card is an admin level account
  80.         if verifyUser() then
  81.             -- Wait for the client to send the new user_id
  82.             local door_id, message = rednet.receive()
  83.             -- Decrypt the message
  84.             local user_id = decrypt("aes-256-cbc", key, iv, message)
  85.             -- check to make sure user_id does not already exist
  86.             if not fileExists(user_id .. ".txt") then
  87.                 -- Inform the admin console that this was an acceptable user id
  88.                 -- Create a file with the new user_id
  89.                 local file = fs.open(user_id .. ".txt", "w")
  90.                 -- Create a new, random user password and store it in the user_id file
  91.                 local newID = math.random(10000, 99999)
  92.                 file.write(newID)
  93.                 file.close()
  94.                 -- Inform the admin console that the user has been added
  95.                 local message = encrypt("aes-256-cbc", key, iv, "user added")
  96.                 rednet.send(door_id, message)
  97.                 -- Send the admin console the new user password to store
  98.                 local encryptedID = encrypt("aes-256-cbc", key, iv, tostring(newID))
  99.                 rednet.send(door_id, encryptedID)
  100.                 -- Wait the admin console to send the access level for the user
  101.                 local door_id, message = rednet.receive()
  102.                 -- Decrypt the message
  103.                 local user_level = decrypt("aes-256-cbc", key, iv, message)
  104.                 local file = fs.open(user_id .. "_level.txt", "w")
  105.                 file.write(user_level)
  106.                 file.close()
  107.             else
  108.                 -- Tell admin console to pick a different user_id
  109.                 local message = encrypt("aes-256-cbc", key, iv, "not a unique user id")
  110.                 rednet.send(door_id, message)
  111.             end
  112.         end
  113.     else
  114.         if verifyUser() then
  115.             -- Client computer has been informed that access is granted
  116.         else
  117.             -- Client computer has been informed of reason for failure
  118.         end
  119.     end
  120. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement