Advertisement
DOGGYWOOF

Untitled

Sep 12th, 2024
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 KB | None | 0 0
  1. -- Function to draw the title bar with the red "X"
  2. local function drawTitleBar()
  3. local width, _ = term.getSize()
  4. term.setBackgroundColor(colors.blue)
  5. term.setTextColor(colors.white)
  6. term.setCursorPos(1, 1)
  7. term.write("Doggy OS Browser")
  8. term.setCursorPos(width, 1)
  9. term.write("X")
  10. term.setBackgroundColor(colors.black)
  11. term.setTextColor(colors.white)
  12. end
  13.  
  14. -- Function to check if a mouse click is on the red "X" in the title bar
  15. local function isCloseButtonClicked(x, y)
  16. local width, _ = term.getSize()
  17. return x == width and y == 1
  18. end
  19.  
  20. -- Function to wait for a mouse click event
  21. local function waitForClick()
  22. while true do
  23. local event, button, x, y = os.pullEvent("mouse_click")
  24. if isCloseButtonClicked(x, y) then
  25. return true
  26. end
  27. end
  28. end
  29.  
  30. -- Function to display an error message and wait
  31. local function displayError(message)
  32. term.clear()
  33. drawTitleBar()
  34. print("Error: " .. message)
  35. print("Press any key to return to the main menu.")
  36. os.pullEvent("key") -- Wait for any key press
  37. end
  38.  
  39. -- Function to get URL from user
  40. local function getURL()
  41. term.clear()
  42. drawTitleBar()
  43. print("Enter URL:")
  44. return read()
  45. end
  46.  
  47. -- Automatically find and open the first available modem
  48. local function openModem()
  49. local sides = {"top", "bottom", "left", "right", "front", "back"}
  50. for _, side in ipairs(sides) do
  51. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  52. rednet.open(side)
  53. return side
  54. end
  55. end
  56. displayError("No modem found.")
  57. return nil
  58. end
  59.  
  60. -- Load DNS Provider ID from file
  61. local function loadDNSProviderID()
  62. local filePath = "DNS-Provider.txt"
  63. if not fs.exists(filePath) then
  64. displayError("DNS-Provider.txt not found.")
  65. return nil
  66. end
  67. local file = fs.open(filePath, "r")
  68. local id = file.readAll()
  69. file.close()
  70. return tonumber(id)
  71. end
  72.  
  73. -- Save DNS Provider ID to file
  74. local function saveDNSProviderID(id)
  75. local filePath = "DNS-Provider.txt"
  76. local file = fs.open(filePath, "w")
  77. file.write(id)
  78. file.close()
  79. print("DNS Provider ID updated.")
  80. end
  81.  
  82. -- Function to check if DNS is locked by checking for /DNS/locked.cfg file
  83. local function isDNSLocked()
  84. return fs.exists("/DNS/locked.cfg")
  85. end
  86.  
  87. -- Function to prompt for the DNS password
  88. local function promptForPassword()
  89. term.clear()
  90. drawTitleBar()
  91. print("Enter DNS Password:")
  92. return read("*") -- Mask the input for security
  93. end
  94.  
  95. -- Function to verify the DNS password
  96. local function verifyPassword()
  97. local filePath = "/DNS/locked.cfg"
  98. if not fs.exists(filePath) then
  99. displayError("Locked configuration file not found.")
  100. return false
  101. end
  102.  
  103. local file = fs.open(filePath, "r")
  104. local storedPassword = file.readAll()
  105. file.close()
  106.  
  107. local enteredPassword = promptForPassword()
  108.  
  109. if enteredPassword == storedPassword then
  110. return true
  111. else
  112. displayError("Incorrect password.")
  113. return false
  114. end
  115. end
  116.  
  117. -- Connect to DNS and get IP address
  118. local function connectToDNS(url, dnsID)
  119. -- Check if DNS is locked
  120. if isDNSLocked() then
  121. if not verifyPassword() then
  122. return nil -- Exit if password verification fails
  123. end
  124. end
  125.  
  126. local modemSide = openModem()
  127. if not modemSide then return nil end
  128.  
  129. term.clear()
  130. drawTitleBar()
  131. print("Connecting to DNS...")
  132.  
  133. rednet.send(dnsID, url)
  134. local timer = os.startTimer(5)
  135. while true do
  136. local event, p1, p2 = os.pullEvent()
  137. if event == "rednet_message" and p1 == dnsID then
  138. rednet.close(modemSide)
  139. return p2
  140. elseif event == "timer" and p1 == timer then
  141. rednet.close(modemSide)
  142. displayError("DNS Timeout.")
  143. return nil
  144. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  145. rednet.close(modemSide)
  146. return nil
  147. end
  148. end
  149. end
  150.  
  151. -- Connect to webserver and get website code
  152. local function connectToWebserver(ip)
  153. local modemSide = openModem()
  154. if not modemSide then return nil end
  155.  
  156. term.clear()
  157. drawTitleBar()
  158. print("Connecting to Webserver...")
  159.  
  160. rednet.send(tonumber(ip), "GET")
  161. local timer = os.startTimer(5)
  162. while true do
  163. local event, p1, p2 = os.pullEvent()
  164. if event == "rednet_message" and p1 == tonumber(ip) then
  165. rednet.close(modemSide)
  166. return p2
  167. elseif event == "timer" and p1 == timer then
  168. rednet.close(modemSide)
  169. displayError("Connection timed out.")
  170. return nil
  171. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  172. rednet.close(modemSide)
  173. return nil
  174. end
  175. end
  176. end
  177.  
  178. -- Execute website code and render it
  179. local function runWebsite(code)
  180. local func, err = load(code, "website", "t", _ENV)
  181. if func then
  182. term.clear()
  183. drawTitleBar()
  184. func() -- Run the website code
  185. -- Continuously check for user interaction (mouse clicks) to close the browser
  186. while not waitForClick() do
  187. -- Keep checking for click events to close the browser
  188. end
  189. term.clear()
  190. else
  191. displayError("Error loading website: " .. err)
  192. end
  193. end
  194.  
  195. -- Main menu function
  196. local function mainMenu()
  197. term.clear()
  198. drawTitleBar()
  199. print("Select an option:")
  200. print("1. Enter URL")
  201. print("2. Edit DNS Provider ID")
  202. print("3. Exit")
  203. local choice = tonumber(read())
  204.  
  205. if choice == 1 then
  206. -- Enter URL
  207. while true do
  208. local url = getURL()
  209. if url == nil then
  210. break
  211. end
  212.  
  213. local dnsID = loadDNSProviderID()
  214. if not dnsID then
  215. displayError("Failed to load DNS Provider ID.")
  216. return
  217. end
  218.  
  219. local ip = connectToDNS(url, dnsID)
  220. if ip == "404_NOT_FOUND" then
  221. displayError("Error 404: Webpage cannot be found!")
  222. return
  223. elseif not ip then
  224. displayError("Failed to connect to DNS.")
  225. return
  226. end
  227.  
  228. local code = connectToWebserver(ip)
  229. if not code then
  230. displayError("Failed to connect to Webserver.")
  231. return
  232. end
  233.  
  234. -- Write code to file and then delete it
  235. local tempFile = "received_code.lua"
  236. local tempFileHandle = fs.open(tempFile, "w")
  237. tempFileHandle.write(code)
  238. tempFileHandle.close()
  239.  
  240. -- Immediately delete the file after writing
  241. local success, err = pcall(function() fs.delete(tempFile) end)
  242. if success then
  243. print("Temporary file deleted successfully.")
  244. else
  245. displayError("Error deleting temporary file: " .. err)
  246. end
  247.  
  248. runWebsite(code)
  249. -- After running the website, return to the main menu
  250. term.clear()
  251. drawTitleBar()
  252. print("Returning to main menu...")
  253. os.sleep(2) -- Pause to allow the user to see the message
  254. break
  255. end
  256. elseif choice == 2 then
  257. -- Edit DNS Provider ID
  258. term.clear()
  259. drawTitleBar()
  260. print("Current DNS Provider ID: " .. (loadDNSProviderID() or "Not set"))
  261. print("Enter new DNS Provider ID:")
  262. local newID = tonumber(read())
  263. if newID then
  264. saveDNSProviderID(newID)
  265. else
  266. displayError("Invalid DNS Provider ID.")
  267. end
  268. elseif choice == 3 then
  269. print("Exiting...")
  270. return true -- Indicate exit
  271. else
  272. displayError("Invalid option. Please select 1, 2, or 3.")
  273. end
  274.  
  275. return false -- Continue showing menu
  276. end
  277.  
  278. -- Main execution loop
  279. while true do
  280. local exit = mainMenu()
  281. if exit then
  282. break -- Exit the loop if the user chose to exit
  283. end
  284. end
  285.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement