Advertisement
onwardprogress

serverTest

Jan 2nd, 2023 (edited)
1,203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.76 KB | None | 0 0
  1. rednet.open("top")
  2.  
  3. -- Function to check if a file exists
  4. function fileExists(path)
  5.   local file = fs.open(path, "r")
  6.   if file then
  7.     file.close()
  8.     return true
  9.   end
  10.   return false
  11. end
  12.  
  13. -- Function to verify a user and their security level
  14. function verifyUser(door_id, message)
  15.     local client_user_id = string.match(message, "<cui>(%w+)</cui>")
  16.     local client_user_pass = string.match(message, "<cup>(%w+)</cup>")
  17.     local client_user_pass = client_user_pass .. "0"
  18.     local server_user_id_result = "unmatched"
  19.     local server_user_pass_result = "incorrect"
  20.     local newID = 00000
  21.     local server_user_access_result = "denied"
  22.     local user_serverside_pass = 00000
  23.     local user_level = 0
  24.     local file = fs.open("door_" .. door_id .. "_level.txt", "r")
  25.     door_level = file.readLine()
  26.     file.close()
  27.     -- Search the server for the user_id file
  28.     if fileExists(client_user_id .. ".txt") then
  29.         local file = fs.open(client_user_id .. ".txt", "r")
  30.         user_serverside_pass = file.readLine()
  31.         file.close()
  32.         user_serverside_pass = user_serverside_pass:gsub("[^%w ]", "")
  33.         server_user_id_result = "recognized"
  34.         print("about to wait for client to send pass")
  35.         -- Wait for a message from the client
  36.         print("user pass is: " .. client_user_pass)
  37.         print("server pass is: " .. user_serverside_pass)
  38.         print("about to test passwords")
  39.     else
  40.         -- Inform client computer that this is not a valid user id
  41.         local server_user_id_result = "unmatched"
  42.     end
  43.     if (tostring(user_serverside_pass) == tostring(client_user_pass)) then
  44.         print("passwords matched")
  45.         server_user_pass_result = "verified"
  46.         -- Send the client computer the new user password to store
  47.         local file = fs.open(client_user_id .. ".txt", "w")
  48.         -- Create a new, random user password and store it in the user_id file
  49.         newID = math.random(10000, 99999)
  50.         file.write(newID)
  51.         file.close()
  52.         print(client_user_id .. "_level.txt")
  53.         local file = fs.open(client_user_id .. "_level.txt", "r")
  54.         user_level = file.readLine()
  55.         file.close()
  56.         print("door_id is: " .. door_id)
  57.         -- If the user has a sufficiently high access level, then return true
  58.         print("user level is: " .. tonumber(user_level))
  59.         print("door level is: " .. tonumber(door_level))
  60.     else
  61.         print("passwords did not match")
  62.         -- Inform client computer that this is not a valid id card
  63.         local server_user_pass_result = "incorrect"
  64.         end
  65.     if (tonumber(user_level) >= tonumber(door_level)) then
  66.     --inform the client computer that access is granted
  67.         server_user_access_result = "granted"
  68.         print(server_user_access_result)
  69.     else
  70.         -- inform client computer that the user has an insufficient access level for this door_
  71.         server_user_access_result = "denied"
  72.         print(server_user_access_result)
  73.     end
  74.     rednet.send(door_id, "<suir>" .. server_user_id_result .. "</suir>" .. "<supr>" .. server_user_pass_result .. "</supr>" .. "<sunp>" .. newID .. "</sunp>" .. "<suar>" .. server_user_access_result .. "</suar>")
  75.     if ((server_user_id_result == "recognized") and (server_user_pass_result == "verified") and (server_user_access_result == "granted")) then
  76.         return true
  77.     else
  78.         return false
  79.     end
  80.  end
  81.  
  82. while true do
  83.     -- Wait for a message from a client
  84.     local door_id, message = rednet.receive()
  85.     local access_reason = string.match(message, "<ar>(%w+)</ar>")
  86.     -- Check the message
  87.     print("enter loop")
  88.     print(message)
  89.     if access_reason == "door" then
  90.         print("received door access request")
  91.         verifyUser(door_id, message)
  92.     elseif access_reason == "add" then
  93.         print("received new user request")
  94.         -- Verify if the user generating the card is an admin level account
  95.         if verifyUser(door_id, message) then
  96.             -- Wait for the client to send the new user_id
  97.             local door_id, user_id = rednet.receive()
  98.             -- check to make sure user_id does not already exist
  99.             if not fileExists(user_id .. ".txt") then
  100.                 -- Inform the admin console that this was an acceptable user id
  101.                 local file = fs.open(user_id .. ".txt", "w")
  102.                 -- Create a new, random user password and store it in the user_id file
  103.                 local newID = math.random(10000, 99999)
  104.                 file.write(newID)
  105.                 file.close()
  106.                 -- Inform the admin console that the user has been added
  107.                 rednet.send(door_id, "user added")
  108.                 -- Send the admin console the new user password to store
  109.                 rednet.send(door_id, newID)
  110.                 -- Wait the admin console to send the access level for the user
  111.                 local door_id, user_level = rednet.receive()
  112.                 local file = fs.open(user_id .. "_level.txt", "w")
  113.                 file.write(user_level)
  114.                 file.close()
  115.             else
  116.                 -- Tell admin console to pick a different user_id
  117.                 rednet.send(door_id, "not a unique user id")
  118.             end
  119.         else
  120.             --Deny access
  121.             print("access denied")
  122.         end
  123.     else
  124.         --Do Nothing
  125.     end
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement