Advertisement
Il_Bambino

AuthServer

Mar 29th, 2024
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.75 KB | Gaming | 0 0
  1. local users = {}
  2.  
  3. local function loadUsers()
  4.   local file = fs.open("users.txt", "r")
  5.   if file then
  6.     for line in file.readLine() do
  7.       local username, password = line:match("([^:]+):([^:]+)")
  8.       users[username] = password
  9.     end
  10.     file.close()
  11.   else
  12.     error("Benutzerdatei nicht gefunden.")
  13.   end
  14. end
  15.  
  16. local function authenticate(username, password)
  17.   return users[username] and users[username] == password
  18. end
  19.  
  20. local function getModemSide()
  21.   local file = fs.open("modemSide.txt", "r")
  22.   if file then
  23.     local side = file.readLine()
  24.     file.close()
  25.     return side
  26.   else
  27.     return nil
  28.   end
  29. end
  30.  
  31. local function setModemSide(side)
  32.   local file = fs.open("modemSide.txt", "w")
  33.   file.writeLine(side)
  34.   file.close()
  35. end
  36.  
  37. local function askForModemSide()
  38.   print("Bitte wähle die Seite des Modems: ")
  39.   local sides = {"left", "right", "top", "bottom", "front", "back"}
  40.   for i, side in ipairs(sides) do
  41.     print(i .. ". " .. side)
  42.   end
  43.   local choice = read()
  44.   return sides[tonumber(choice)]
  45. end
  46.  
  47. local function startAuthServer(modemSide)
  48.   loadUsers()
  49.  
  50.   if not rednet.isOpen(modemSide) then
  51.     rednet.open(modemSide)
  52.   end
  53.  
  54.   print("Auth-Server läuft auf der Seite: " .. modemSide)
  55.   while true do
  56.     local senderId, message, protocol = rednet.receive()
  57.     local username, password = string.match(message, "([^:]+):([^:]+)")
  58.     if authenticate(username, password) then
  59.       rednet.send(senderId, "SUCCESS", protocol)
  60.     else
  61.       rednet.send(senderId, "FAILURE", protocol)
  62.     end
  63.   end
  64. end
  65.  
  66. textutils.slowPrint("Daedalus OS")
  67. sleep(2)
  68.  
  69. local modemSide = getModemSide()
  70.  
  71. if not modemSide then
  72.   modemSide = askForModemSide()
  73.   setModemSide(modemSide)
  74. end
  75.  
  76. startAuthServer(modemSide)
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement