Advertisement
DOGGYWOOF

Untitled

Jan 6th, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. -- GUI-Based Login System for Doggy OS
  2. local MAX_ATTEMPTS = 3 -- Maximum number of incorrect password attempts allowed
  3. local LOCKOUT_TIME = 30 -- Lockout time in seconds after reaching maximum attempts
  4.  
  5. local USERS_FOLDER = "/disk/users/"
  6. local ERROR_FOLDER = "/disk/error/"
  7. local BSOD_PROGRAM = "BSOD.lua"
  8. local CURRENT_USER_FILE = ".currentusr"
  9. local SHOW_ALL_USERS_FILE = "/disk/config/security/login/ShowAllUsers.cfg"
  10.  
  11. -- Utility function to draw a centered window
  12. local function drawWindow(x, y, width, height, title)
  13. paintutils.drawFilledBox(x, y, x + width - 1, y + height - 1, colors.gray) -- Window background
  14. paintutils.drawBox(x, y, x + width - 1, y + height - 1, colors.lightGray) -- Window border
  15. term.setCursorPos(x + 2, y)
  16. term.setTextColor(colors.white)
  17. term.write(title)
  18. end
  19.  
  20. -- Function to list all users
  21. local function listUsers()
  22. local users = fs.list(USERS_FOLDER)
  23. local usernames = {}
  24. for _, user in ipairs(users) do
  25. local userDir = fs.combine(USERS_FOLDER, user)
  26. if fs.isDir(userDir) then
  27. table.insert(usernames, user)
  28. end
  29. end
  30. return usernames
  31. end
  32.  
  33. -- Function to display a selection menu
  34. local function selectUserFromList(usernames)
  35. local w, h = term.getSize()
  36. local windowWidth, windowHeight = 30, #usernames + 4
  37. local x, y = math.floor(w / 2 - windowWidth / 2), math.floor(h / 2 - windowHeight / 2)
  38.  
  39. local selectedIndex = 1
  40. while true do
  41. drawWindow(x, y, windowWidth, windowHeight, "Select User")
  42. for i, username in ipairs(usernames) do
  43. term.setCursorPos(x + 2, y + 1 + i)
  44. if i == selectedIndex then
  45. term.setTextColor(colors.yellow)
  46. term.write("> " .. username)
  47. else
  48. term.setTextColor(colors.white)
  49. term.write(" " .. username)
  50. end
  51. end
  52.  
  53. local event, key = os.pullEvent("key")
  54. if key == keys.up then
  55. selectedIndex = math.max(1, selectedIndex - 1)
  56. elseif key == keys.down then
  57. selectedIndex = math.min(#usernames, selectedIndex + 1)
  58. elseif key == keys.enter then
  59. return usernames[selectedIndex]
  60. end
  61. end
  62. end
  63.  
  64. -- Function to get user credentials
  65. local function getUserCredentials(username)
  66. local passwordFile = fs.combine(USERS_FOLDER .. username, "password.txt")
  67. if fs.exists(passwordFile) then
  68. local file = fs.open(passwordFile, "r")
  69. local storedPassword = file.readLine()
  70. file.close()
  71. return storedPassword
  72. else
  73. return nil
  74. end
  75. end
  76.  
  77. -- Function to display login screen
  78. local function drawLoginScreen(username, attemptsLeft)
  79. local w, h = term.getSize()
  80. local windowWidth, windowHeight = 40, 8
  81. local x, y = math.floor(w / 2 - windowWidth / 2), math.floor(h / 2 - windowHeight / 2)
  82.  
  83. drawWindow(x, y, windowWidth, windowHeight, "Login to Doggy OS")
  84. term.setCursorPos(x + 2, y + 2)
  85. term.write("Username: " .. username)
  86. term.setCursorPos(x + 2, y + 3)
  87. term.write("Attempts left: " .. attemptsLeft)
  88. term.setCursorPos(x + 2, y + 5)
  89. term.write("Enter password: ")
  90. term.setCursorPos(x + 18, y + 5)
  91. end
  92.  
  93. -- Function to lockout user
  94. local function lockoutUser(username)
  95. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  96. local file = fs.open(disabledFile, "w")
  97. file.close()
  98. end
  99.  
  100. -- Function to check if user is disabled
  101. local function checkDisabled(username)
  102. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  103. return fs.exists(disabledFile)
  104. end
  105.  
  106. -- Function to save the current user
  107. local function saveCurrentUser(username)
  108. if fs.exists(CURRENT_USER_FILE) then
  109. fs.delete(CURRENT_USER_FILE)
  110. end
  111. local file = fs.open(CURRENT_USER_FILE, "w")
  112. file.write(username)
  113. file.close()
  114. end
  115.  
  116. -- Function to handle user login
  117. local function checkCredentials(username)
  118. if checkDisabled(username) then
  119. drawWindow(10, 10, 40, 6, "Access Denied")
  120. term.setCursorPos(12, 12)
  121. term.write("Account is disabled.")
  122. os.sleep(2)
  123. return false
  124. end
  125.  
  126. local storedPassword = getUserCredentials(username)
  127. if not storedPassword then
  128. return false
  129. end
  130.  
  131. local attempts = 0
  132. while attempts < MAX_ATTEMPTS do
  133. drawLoginScreen(username, MAX_ATTEMPTS - attempts)
  134. local enteredPassword = read("*")
  135. if enteredPassword == storedPassword then
  136. saveCurrentUser(username)
  137. drawWindow(10, 10, 40, 6, "Access Granted")
  138. term.setCursorPos(12, 12)
  139. term.write("Welcome, " .. username .. "!")
  140. os.sleep(2)
  141. return true
  142. else
  143. attempts = attempts + 1
  144. drawWindow(10, 10, 40, 6, "Access Denied")
  145. term.setCursorPos(12, 12)
  146. term.write("Incorrect password.")
  147. os.sleep(2)
  148. end
  149. end
  150.  
  151. drawWindow(10, 10, 40, 6, "Account Disabled")
  152. term.setCursorPos(12, 12)
  153. term.write("Too many failed attempts.")
  154. lockoutUser(username)
  155. os.sleep(2)
  156. return false
  157. end
  158.  
  159. -- Main function
  160. local function main()
  161. term.setBackgroundColor(colors.black)
  162. term.setTextColor(colors.white)
  163. term.clear()
  164.  
  165. if fs.exists(SHOW_ALL_USERS_FILE) then
  166. local usernames = listUsers()
  167. local username = selectUserFromList(usernames)
  168. if username and checkCredentials(username) then
  169. shell.run("/disk/os/gui")
  170. else
  171. shell.run("/disk/os/lock.lua")
  172. end
  173. else
  174. drawWindow(10, 10, 40, 6, "Doggy OS")
  175. term.setCursorPos(12, 12)
  176. term.write("Enter username:")
  177. term.setCursorPos(28, 12)
  178. local username = read()
  179.  
  180. if checkCredentials(username) then
  181. shell.run("/disk/os/gui")
  182. else
  183. shell.run("/disk/os/lock.lua")
  184. end
  185. end
  186. end
  187.  
  188. main()
  189.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement