Advertisement
DOGGYWOOF

New lockscreen test

Jul 1st, 2024 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.77 KB | None | 0 0
  1. local MAX_ATTEMPTS = 3 -- Maximum number of incorrect password attempts allowed
  2. local LOCKOUT_TIME = 30 -- Lockout time in seconds after reaching maximum attempts
  3.  
  4. local USERS_FOLDER = "/disk/users/"
  5. local ERROR_FOLDER = "/disk/error/"
  6. local BSOD_PROGRAM = "BSOD.lua"
  7.  
  8. local function getUserCredentials(username)
  9. local passwordFile = fs.combine(USERS_FOLDER .. username, "password.txt")
  10.  
  11. if fs.exists(passwordFile) then
  12. local file = fs.open(passwordFile, "r")
  13. local storedPassword = file.readLine()
  14. file.close()
  15. return storedPassword
  16. else
  17. return nil -- User does not exist
  18. end
  19. end
  20.  
  21. local function drawCenteredText(y, text, color)
  22. local screenWidth, _ = term.getSize()
  23. local x = math.floor((screenWidth - #text) / 2)
  24. term.setCursorPos(x, y)
  25. term.setTextColor(color or colors.white)
  26. term.write(text)
  27. end
  28.  
  29. local function drawBackground()
  30. term.setBackgroundColor(colors.gray)
  31. term.clear()
  32. end
  33.  
  34. local function drawLoginScreen(username, attemptsLeft)
  35. drawBackground()
  36. local screenWidth, screenHeight = term.getSize()
  37.  
  38. -- Draw icons and colors
  39. term.setBackgroundColor(colors.blue)
  40. term.setTextColor(colors.white)
  41. drawCenteredText(2, "🔒 Doggy OS", colors.lightBlue)
  42.  
  43. drawCenteredText(4, "Login Screen", colors.white)
  44.  
  45. drawCenteredText(6, "Username: " .. username, colors.yellow)
  46. drawCenteredText(7, "Attempts left: " .. attemptsLeft, colors.red)
  47.  
  48. -- Draw profile picture box
  49. term.setBackgroundColor(colors.lightGray)
  50. term.setCursorPos(screenWidth // 2 - 1, screenHeight // 2 - 2)
  51. term.write(" ")
  52. term.setCursorPos(screenWidth // 2 - 1, screenHeight // 2 - 1)
  53. term.write(" ")
  54. term.setCursorPos(screenWidth // 2 - 1, screenHeight // 2)
  55. term.write(" ")
  56.  
  57. -- Password prompt
  58. drawCenteredText(screenHeight // 2 + 2, "Enter password:", colors.white)
  59. term.setCursorPos(screenWidth // 2 - 10, screenHeight // 2 + 3)
  60. term.setBackgroundColor(colors.white)
  61. term.setTextColor(colors.black)
  62. term.write(string.rep(" ", 20))
  63. term.setCursorPos(screenWidth // 2 - 10, screenHeight // 2 + 3)
  64. end
  65.  
  66. local function lockoutUser(username)
  67. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  68. local file = fs.open(disabledFile, "w")
  69. file.close()
  70. end
  71.  
  72. local function checkDisabled(username)
  73. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  74. return fs.exists(disabledFile)
  75. end
  76.  
  77. local function checkCredentials(username)
  78. if checkDisabled(username) then
  79. term.clear()
  80. term.setCursorPos(1, 1)
  81. term.setTextColor(colors.red)
  82. print("This user has been disabled due to security reasons.")
  83. os.sleep(3) -- Display the disabled message for 3 seconds
  84. print("Contact your administrator for help.")
  85. os.sleep(2) -- Display the contact administrator message for 2 seconds
  86. shell.run("/disk/os/lock.lua") -- Run the lock.lua program
  87. return false
  88. end
  89.  
  90. local storedPassword = getUserCredentials(username)
  91.  
  92. if not storedPassword then
  93. return false -- User does not exist
  94. end
  95.  
  96. local attempts = 0
  97.  
  98. repeat
  99. drawLoginScreen(username, MAX_ATTEMPTS - attempts)
  100.  
  101. local enteredPassword = read("*")
  102. attempts = attempts + 1
  103.  
  104. if enteredPassword == storedPassword then
  105. return true
  106. else
  107. term.setCursorPos(1, 10)
  108. term.setTextColor(colors.red)
  109. print("Incorrect password. Please try again.")
  110. os.sleep(2) -- Display the error message for 2 seconds
  111. end
  112. until attempts > MAX_ATTEMPTS
  113.  
  114. print("Too many incorrect attempts. User has been disabled.")
  115. lockoutUser(username)
  116. os.sleep(2) -- Display the lockout message for 2 seconds
  117.  
  118. return false
  119. end
  120.  
  121. local function checkDiskIDs()
  122. -- Get a list of all connected peripherals
  123. local peripherals = peripheral.getNames()
  124.  
  125. -- Array to store disk IDs
  126. local diskIDs = {}
  127.  
  128. -- Loop through all peripherals
  129. for _, name in ipairs(peripherals) do
  130. -- Check if the peripheral is a disk drive
  131. if peripheral.getType(name) == "drive" then
  132. -- Get the disk ID from the disk drive
  133. local diskID = disk.getID(name)
  134.  
  135. -- If a disk is inserted, add its ID to the array
  136. if diskID then
  137. table.insert(diskIDs, {id = diskID, name = name})
  138. end
  139. end
  140. end
  141.  
  142. -- Check if any disks were found
  143. if #diskIDs > 0 then
  144. return diskIDs
  145. else
  146. return nil
  147. end
  148. end
  149.  
  150. local function drawSecurityCardPrompt()
  151. term.clear()
  152. term.setCursorPos(1, 1)
  153.  
  154. term.setTextColor(colors.blue)
  155. term.write("+--------------------------+")
  156. term.setCursorPos(1, 2)
  157. term.write("| Insert Security Card |")
  158. term.setCursorPos(1, 3)
  159. term.write("+--------------------------+")
  160.  
  161. term.setCursorPos(1, 5)
  162. term.setTextColor(colors.yellow)
  163. term.write("Please insert your security card.")
  164. term.setCursorPos(1, 6)
  165. term.write("Press ENTER to use password instead.")
  166. end
  167.  
  168. local function drawErrorMessage(message)
  169. term.clear()
  170. term.setCursorPos(1, 1)
  171.  
  172. term.setTextColor(colors.red)
  173. print(message)
  174.  
  175. os.sleep(2)
  176. end
  177.  
  178. local function ejectDisk(diskName)
  179. peripheral.call(diskName, "ejectDisk")
  180. end
  181.  
  182. local function checkSecurityCard(username)
  183. local idFolder = fs.combine(USERS_FOLDER .. username, "ID")
  184. if not fs.exists(idFolder) then
  185. return false
  186. end
  187.  
  188. while true do
  189. drawSecurityCardPrompt()
  190.  
  191. local event, key = os.pullEvent()
  192.  
  193. if event == "key" and key == keys.enter then
  194. return false
  195. elseif event == "disk" or event == "disk_insert" then
  196. local diskIDs = checkDiskIDs()
  197. if diskIDs then
  198. for _, disk in ipairs(diskIDs) do
  199. ejectDisk(disk.name) -- Eject the disk
  200. local idFile = fs.combine(idFolder, tostring(disk.id) .. ".file")
  201. if fs.exists(idFile) then
  202. return true -- Allow access if a valid ID is found
  203. end
  204. end
  205. drawErrorMessage("Error: Unregistered security key.")
  206. end
  207. end
  208. end
  209. end
  210.  
  211. local function main()
  212. term.clear()
  213. term.setCursorPos(1, 1)
  214.  
  215. term.setTextColor(colors.green)
  216. term.write("+-------------------------------------+")
  217. term.setCursorPos(1, 2)
  218. term.write("| Protected by Doggy OS Security |")
  219. term.setCursorPos(1, 3)
  220. term.write("+-------------------------------------+")
  221.  
  222. term.setCursorPos(1, 5)
  223. term.setTextColor(colors.white)
  224. print("Enter username:")
  225.  
  226. local enteredUsername = read()
  227.  
  228. if checkDisabled(enteredUsername) then
  229. term.clear()
  230. term.setCursorPos(1, 1)
  231. term.setTextColor(colors.red)
  232. print("This user has been disabled due to security reasons.")
  233. os.sleep(3) -- Display the disabled message for 3 seconds
  234. print("Contact your administrator for help.")
  235. os.sleep(2) -- Display the contact administrator message for 2 seconds
  236. shell.run("/disk/os/lock.lua") -- Run the lock.lua program
  237. return
  238. end
  239.  
  240. local users = fs.list(USERS_FOLDER)
  241.  
  242. if not users or #users == 0 then
  243. -- No users detected, run the BSOD program
  244. shell.run(ERROR_FOLDER .. BSOD_PROGRAM)
  245. elseif checkSecurityCard(enteredUsername) then
  246. term.clear()
  247. term.setCursorPos(1, 1)
  248.  
  249. term.setTextColor(colors.lime)
  250. print("Access granted. Welcome, " .. enteredUsername .. "!")
  251. os.sleep(2) -- Display the success message for 2 seconds
  252. term.setTextColor(colors.white)
  253. shell.run("/disk/os/gui")
  254. -- Your main OS code goes here
  255. elseif checkCredentials(enteredUsername) then
  256. term.clear()
  257. term.setCursorPos(1, 1)
  258.  
  259. term.setTextColor(colors.lime)
  260. print("Access granted. Welcome, " .. enteredUsername .. "!")
  261. os.sleep(2) -- Display the success message for 2 seconds
  262. term.setTextColor(colors.white)
  263. shell.run("/disk/os/gui")
  264. -- Your main OS code goes here
  265. else
  266. term.clear()
  267. term.setCursorPos(1, 1)
  268.  
  269. term.setTextColor(colors.red)
  270. print("Access denied.")
  271. os.sleep(2) -- Display the access denied message for 2 seconds
  272. shell.run("/disk/os/lock.lua")
  273. end
  274. end
  275.  
  276. main()
  277.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement