Advertisement
Il_Bambino

AuthServer

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