Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- GUI-Based Login System for Doggy OS
- local MAX_ATTEMPTS = 3 -- Maximum number of incorrect password attempts allowed
- local LOCKOUT_TIME = 30 -- Lockout time in seconds after reaching maximum attempts
- local USERS_FOLDER = "/disk/users/"
- local ERROR_FOLDER = "/disk/error/"
- local BSOD_PROGRAM = "BSOD.lua"
- local CURRENT_USER_FILE = ".currentusr"
- local SHOW_ALL_USERS_FILE = "/disk/config/security/login/ShowAllUsers.cfg"
- -- Utility function to draw a centered window
- local function drawWindow(x, y, width, height, title)
- paintutils.drawFilledBox(x, y, x + width - 1, y + height - 1, colors.gray) -- Window background
- paintutils.drawBox(x, y, x + width - 1, y + height - 1, colors.lightGray) -- Window border
- term.setCursorPos(x + 2, y)
- term.setTextColor(colors.white)
- term.write(title)
- end
- -- Function to list all users
- local function listUsers()
- local users = fs.list(USERS_FOLDER)
- local usernames = {}
- for _, user in ipairs(users) do
- local userDir = fs.combine(USERS_FOLDER, user)
- if fs.isDir(userDir) then
- table.insert(usernames, user)
- end
- end
- return usernames
- end
- -- Function to display a selection menu
- local function selectUserFromList(usernames)
- local w, h = term.getSize()
- local windowWidth, windowHeight = 30, #usernames + 4
- local x, y = math.floor(w / 2 - windowWidth / 2), math.floor(h / 2 - windowHeight / 2)
- local selectedIndex = 1
- while true do
- drawWindow(x, y, windowWidth, windowHeight, "Select User")
- for i, username in ipairs(usernames) do
- term.setCursorPos(x + 2, y + 1 + i)
- if i == selectedIndex then
- term.setTextColor(colors.yellow)
- term.write("> " .. username)
- else
- term.setTextColor(colors.white)
- term.write(" " .. username)
- end
- end
- local event, key = os.pullEvent("key")
- if key == keys.up then
- selectedIndex = math.max(1, selectedIndex - 1)
- elseif key == keys.down then
- selectedIndex = math.min(#usernames, selectedIndex + 1)
- elseif key == keys.enter then
- return usernames[selectedIndex]
- end
- end
- end
- -- Function to get user credentials
- local function getUserCredentials(username)
- local passwordFile = fs.combine(USERS_FOLDER .. username, "password.txt")
- if fs.exists(passwordFile) then
- local file = fs.open(passwordFile, "r")
- local storedPassword = file.readLine()
- file.close()
- return storedPassword
- else
- return nil
- end
- end
- -- Function to display login screen
- local function drawLoginScreen(username, attemptsLeft)
- local w, h = term.getSize()
- local windowWidth, windowHeight = 40, 8
- local x, y = math.floor(w / 2 - windowWidth / 2), math.floor(h / 2 - windowHeight / 2)
- drawWindow(x, y, windowWidth, windowHeight, "Login to Doggy OS")
- term.setCursorPos(x + 2, y + 2)
- term.write("Username: " .. username)
- term.setCursorPos(x + 2, y + 3)
- term.write("Attempts left: " .. attemptsLeft)
- term.setCursorPos(x + 2, y + 5)
- term.write("Enter password: ")
- term.setCursorPos(x + 18, y + 5)
- end
- -- Function to lockout user
- local function lockoutUser(username)
- local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
- local file = fs.open(disabledFile, "w")
- file.close()
- end
- -- Function to check if user is disabled
- local function checkDisabled(username)
- local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
- return fs.exists(disabledFile)
- end
- -- Function to save the current user
- local function saveCurrentUser(username)
- if fs.exists(CURRENT_USER_FILE) then
- fs.delete(CURRENT_USER_FILE)
- end
- local file = fs.open(CURRENT_USER_FILE, "w")
- file.write(username)
- file.close()
- end
- -- Function to handle user login
- local function checkCredentials(username)
- if checkDisabled(username) then
- drawWindow(10, 10, 40, 6, "Access Denied")
- term.setCursorPos(12, 12)
- term.write("Account is disabled.")
- os.sleep(2)
- return false
- end
- local storedPassword = getUserCredentials(username)
- if not storedPassword then
- return false
- end
- local attempts = 0
- while attempts < MAX_ATTEMPTS do
- drawLoginScreen(username, MAX_ATTEMPTS - attempts)
- local enteredPassword = read("*")
- if enteredPassword == storedPassword then
- saveCurrentUser(username)
- drawWindow(10, 10, 40, 6, "Access Granted")
- term.setCursorPos(12, 12)
- term.write("Welcome, " .. username .. "!")
- os.sleep(2)
- return true
- else
- attempts = attempts + 1
- drawWindow(10, 10, 40, 6, "Access Denied")
- term.setCursorPos(12, 12)
- term.write("Incorrect password.")
- os.sleep(2)
- end
- end
- drawWindow(10, 10, 40, 6, "Account Disabled")
- term.setCursorPos(12, 12)
- term.write("Too many failed attempts.")
- lockoutUser(username)
- os.sleep(2)
- return false
- end
- -- Main function
- local function main()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- if fs.exists(SHOW_ALL_USERS_FILE) then
- local usernames = listUsers()
- local username = selectUserFromList(usernames)
- if username and checkCredentials(username) then
- shell.run("/disk/os/gui")
- else
- shell.run("/disk/os/lock.lua")
- end
- else
- drawWindow(10, 10, 40, 6, "Doggy OS")
- term.setCursorPos(12, 12)
- term.write("Enter username:")
- term.setCursorPos(28, 12)
- local username = read()
- if checkCredentials(username) then
- shell.run("/disk/os/gui")
- else
- shell.run("/disk/os/lock.lua")
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement