Guest User

Untitled

a guest
Dec 12th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.48 KB | None | 0 0
  1. local sides = {"top", "bottom", "left", "right", "back", "front"}
  2. local serverID = os.computerID()
  3. local users = {}
  4. local passwords = {}
  5.  
  6. function clear()
  7.    term.clear()
  8.    term.setCursorPos(1,1)
  9. end
  10.  
  11. function updateAdminText()
  12.       print("Welcome, admin.")
  13.       print("What would you like to do?")
  14.       print("[1] Make a new account.")
  15.       print("[2] Quit the server.")
  16.       print("[3] Return to normal server mode.")
  17.       print("[4] Change the admin password.")
  18.       print("[5] Print out the current users database.")
  19.       write("> ")
  20. end
  21.  
  22. function saveUsersVar()
  23.    local usersFile = fs.open("users", "w")
  24.    usersFile.write(textutils.serialize(users))
  25.    usersFile.close()
  26. end
  27.  
  28. function getUsersVar()
  29.    local usersFile = fs.open("users", "r")
  30.    users = textutils.unserialize(usersFile.readAll())
  31.    usersFile.close()
  32. end
  33.  
  34. function savePasswordsVar()
  35.    local passwordsFile = fs.open("passwords", "w")
  36.    passwordsFile.write(textutils.serialize(passwords))
  37.    passwordsFile.close()
  38. end
  39.  
  40. function getPasswordsVar()
  41.    local passwordsFile = fs.open("passwords", "r")
  42.    passwords = textutils.unserialize(passwordsFile.readAll())
  43.    passwordsFile.close()
  44. end
  45.  
  46. function bootUp()
  47.    local rnetSuccess = false
  48.    for i = 1, #sides do
  49.       rednet.open(sides[i])
  50.       if peripheral.getType(sides[i]) == "modem" then
  51.          rnetSuccess = true
  52.       end
  53.    end
  54.    if not rnetSuccess then
  55.       print("Error: No wireless modem attached.")
  56.       error()
  57.    end
  58.    if not fs.exists("adminPassword") then
  59.       local adminPass = ""
  60.       repeat
  61.          clear()
  62.          textutils.slowPrint("No admin password detected.")
  63.          textutils.slowWrite("Enter a new password: ")
  64.          adminPass = read()
  65.       until adminPass ~= ""
  66.       adminPassFile = fs.open("adminPassword", "w")
  67.       adminPassFile.write(adminPass)
  68.       adminPassFile.close()
  69.    end
  70.    if not fs.exists("users") then
  71.       saveUsersVar()
  72.    else
  73.       getUsersVar()
  74.    end
  75.    if not fs.exists("passwords") then
  76.       savePasswordsVar()
  77.    else
  78.       getPasswordsVar()
  79.    end
  80. end
  81.  
  82. function newAccount(newUsername, newPassword)
  83.    local usernameKey = usernameToKey(newUsername)
  84.    if usernameKey == nil then
  85.       table.insert(users, 1, newUsername)
  86.       table.insert(passwords, 1, newPassword)
  87.       print("New account created:")
  88.       print("New username: "..username)
  89.       print("New password: "..password)
  90.       sleep(2)
  91.    else
  92.       print("Username "..newUsername.." already in use:")
  93.       print("Setting "..newUsername.."\'s password as "..newPassword..".")
  94.       passwords[usernameKey] = newPassword
  95.       sleep(3)
  96.    end
  97.    saveUsersVar()
  98.    savePasswordsVar()
  99. end
  100.  
  101. function usernameToKey(username)
  102.    for i = 1, #users do
  103.       if users[i] == username then
  104.          return i
  105.       end
  106.    end
  107.    return nil
  108. end
  109.  
  110. function adminMode()
  111.    while true do
  112.       clear()
  113.       updateAdminText()
  114.       local option = read()
  115.       if option == "1" then
  116.          repeat
  117.             write("Enter the new account's username: ")
  118.             username = read()
  119.             write("Enter the new account's password: ")
  120.             password = read()
  121.          until username ~= "" and password ~= ""
  122.          newAccount(username, password)
  123.       elseif option == "2" then
  124.          clear()
  125.          write("Entering normal command-line mode")
  126.          os.sleep(0.2)
  127.          textutils.slowPrint("...", 3)
  128.          os.sleep(0.2)
  129.          error()
  130.       elseif option == "3" then
  131.          clear()
  132.          write("Returning to normal server mode")
  133.          os.sleep(0.2)
  134.          textutils.slowPrint("...", 3)
  135.          os.sleep(0.2)
  136.          return
  137.       elseif option == "4" then
  138.          local newPassword = ""
  139.          repeat
  140.             write("Enter the new password: ")
  141.             newPassword = read()
  142.          until newPassword ~= ""
  143.          adminPassFile = fs.open("adminPassword", "w")
  144.          adminPassFile.write(newPassword)
  145.          adminPassFile.close()
  146.          print("Successfully changed the password to "..newPassword..".")
  147.          sleep(1.5)
  148.       elseif option == "5" then
  149.          clear()
  150.          print("Username(Password)")
  151.          for key, user in ipairs(users) do
  152.             print(users[key].."("..passwords[key]..")")
  153.             sleep(0.5)
  154.          end
  155.          print("Press any key to continue...")
  156.          os.pullEvent("key")
  157.          sleep(0.2)
  158.       else
  159.          print("Not a valid option.")
  160.          os.sleep(1.5)
  161.       end
  162.    end
  163. end
  164.  
  165. function updateText()
  166.    print("This is the login server with ID "..serverID..".")
  167.    print("Only the administrator can login here.")
  168.    print("Enter the password to be granted access.")
  169.    write("> ")
  170. end
  171.  
  172. function normalMode()
  173.    while true do
  174.       clear()
  175.       updateText()
  176.       local password = read("*")
  177.       local adminPassFile = fs.open("adminPassword", "r")
  178.       adminPass = adminPassFile.readLine()
  179.       adminPassFile.close()
  180.       if password == adminPass then
  181.          adminMode()
  182.       else
  183.          print("Password invalid!")
  184.          sleep(2)
  185.       end
  186.    end
  187. end
  188.  
  189. --======Rednet Message Interpreterâ„¢======--
  190. ----To Server----
  191. --LOGIN = Signalling server to receive username and password in next 2 messages
  192. ----To Terminal----
  193. --ERROR100 = Error recieving message
  194. --ERROR101 = Username doesn't exist - ask admin to make one
  195. --ERROR102 = Invalid password!
  196. --VALIDATED = No errors! Yay!
  197. --NOMOREFILES = No more files to send
  198.  
  199. function serverSub()
  200.    while true do
  201.       local senderID, message = rednet.receive()
  202.       rednet.send(senderID, message)
  203.       if message == "LOGIN" then
  204.          local senderID2, username = rednet.receive(10)
  205.          local senderID3, password = rednet.receive(10)
  206.          if senderID ~= senderID2 or senderID ~= senderID3 then
  207.             rednet.send(senderID, "ERROR100")
  208.          else
  209.             local accountKey = usernameToKey(username)
  210.             if accountKey == nil then
  211.                rednet.send(senderID, "ERROR101")
  212.             else
  213.                if password ~= passwords[accountKey] then
  214.                   rednet.send(senderID, "ERROR102")
  215.                else
  216.                   rednet.send("VALIDATED")
  217.                   if fs.exists("/"..username.."/") then
  218.                      print(":D file sending :P")
  219.                   end
  220.                   rednet.send("NOMOREFILES")
  221.                end
  222.             end
  223.          end
  224.       end
  225.    end
  226. end
  227.  
  228. bootUp()
  229. parallel.waitForAny(normalMode(), serverSub())
Add Comment
Please, Sign In to add comment