Advertisement
DOGGYWOOF

Untitled

Jan 6th, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 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. -- Check for blocked scripts in the website code
  83. local function checkForBlockedScripts(code)
  84. local blockedPatterns = {
  85. {pattern = "fs%.", exception = "fs%.exists"},
  86. {pattern = "shell%.run"}
  87. }
  88.  
  89. local warnings = {}
  90. local lines = {}
  91. for lineNumber, line in ipairs(string.split(code, "\n")) do
  92. for _, block in ipairs(blockedPatterns) do
  93. if line:match(block.pattern) and not line:match(block.exception or "") then
  94. table.insert(warnings, block.pattern)
  95. table.insert(lines, {lineNumber = lineNumber, content = line})
  96. end
  97. end
  98. end
  99.  
  100. if #warnings > 0 then
  101. term.clear()
  102. drawTitleBar()
  103. print("Warning: The following blocked scripts were found:")
  104. for i, warning in ipairs(warnings) do
  105. print(string.format("- Line %d: %s", lines[i].lineNumber, lines[i].content))
  106. end
  107. print("Do you want to continue? (yes/no)")
  108. local choice = read()
  109. if choice:lower() ~= "yes" then
  110. return false
  111. end
  112. end
  113.  
  114. return true
  115. end
  116.  
  117. -- Connect to DNS and get IP address
  118. local function connectToDNS(url, dnsID)
  119. local modemSide = openModem()
  120. if not modemSide then return nil end
  121.  
  122. term.clear()
  123. drawTitleBar()
  124. print("Connecting to DNS...")
  125.  
  126. rednet.send(dnsID, url)
  127. local timer = os.startTimer(5)
  128. while true do
  129. local event, p1, p2 = os.pullEvent()
  130. if event == "rednet_message" and p1 == dnsID then
  131. rednet.close(modemSide)
  132. return p2
  133. elseif event == "timer" and p1 == timer then
  134. rednet.close(modemSide)
  135. displayError("DNS Timeout.")
  136. return nil
  137. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  138. rednet.close(modemSide)
  139. return nil
  140. end
  141. end
  142. end
  143.  
  144. -- Connect to webserver and get website code
  145. local function connectToWebserver(ip)
  146. local modemSide = openModem()
  147. if not modemSide then return nil end
  148.  
  149. term.clear()
  150. drawTitleBar()
  151. print("Connecting to Webserver...")
  152.  
  153. rednet.send(tonumber(ip), "GET")
  154. local timer = os.startTimer(5)
  155. while true do
  156. local event, p1, p2 = os.pullEvent()
  157. if event == "rednet_message" and p1 == tonumber(ip) then
  158. rednet.close(modemSide)
  159. return p2
  160. elseif event == "timer" and p1 == timer then
  161. rednet.close(modemSide)
  162. displayError("Connection timed out.")
  163. return nil
  164. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  165. rednet.close(modemSide)
  166. return nil
  167. end
  168. end
  169. end
  170.  
  171. -- Execute website code and render it
  172. local function runWebsite(code)
  173. if not checkForBlockedScripts(code) then
  174. return
  175. end
  176.  
  177. local func, err = load(code, "website", "t", _ENV)
  178. if func then
  179. term.clear()
  180. drawTitleBar()
  181. func() -- Run the website code
  182. -- Continuously check for user interaction (mouse clicks) to close the browser
  183. while not waitForClick() do
  184. -- Keep checking for click events to close the browser
  185. end
  186. term.clear()
  187. else
  188. displayError("Error loading website: " .. err)
  189. end
  190. end
  191.  
  192. -- Main menu function
  193. local function mainMenu()
  194. term.clear()
  195. drawTitleBar()
  196. print("Select an option:")
  197. print("1. Enter URL")
  198. print("2. Edit DNS Provider ID")
  199. print("3. Exit")
  200. local choice = tonumber(read())
  201.  
  202. if choice == 1 then
  203. -- Enter URL
  204. while true do
  205. local url = getURL()
  206. if url == nil then
  207. break
  208. end
  209.  
  210. local dnsID = loadDNSProviderID()
  211. if not dnsID then
  212. displayError("Failed to load DNS Provider ID.")
  213. return
  214. end
  215.  
  216. local ip = connectToDNS(url, dnsID)
  217. if ip == "404_NOT_FOUND" then
  218. displayError("Error 404: Webpage cannot be found!")
  219. return
  220. elseif not ip then
  221. displayError("Failed to connect to DNS.")
  222. return
  223. end
  224.  
  225. local code = connectToWebserver(ip)
  226. if not code then
  227. displayError("Failed to connect to Webserver.")
  228. return
  229. end
  230.  
  231. -- Write code to file and then delete it
  232. local tempFile = "received_code.lua"
  233. local tempFileHandle = fs.open(tempFile, "w")
  234. tempFileHandle.write(code)
  235. tempFileHandle.close()
  236.  
  237. -- Immediately delete the file after writing
  238. local success, err = pcall(function() fs.delete(tempFile) end)
  239. if success then
  240. print("Temporary file deleted successfully.")
  241. else
  242. displayError("Error deleting temporary file: " .. err)
  243. end
  244.  
  245. runWebsite(code)
  246. -- After running the website, return to the main menu
  247. term.clear()
  248. drawTitleBar()
  249. print("Returning to main menu...")
  250. os.sleep(2) -- Pause to allow the user to see the message
  251. break
  252. end
  253. elseif choice == 2 then
  254. -- Edit DNS Provider ID
  255. term.clear()
  256. drawTitleBar()
  257. print("Current DNS Provider ID: " .. (loadDNSProviderID() or "Not set"))
  258. print("Enter new DNS Provider ID:")
  259. local newID = tonumber(read())
  260. if newID then
  261. saveDNSProviderID(newID)
  262. else
  263. displayError("Invalid DNS Provider ID.")
  264. end
  265. elseif choice == 3 then
  266. print("Exiting...")
  267. shell.run("/disk/os/gui")
  268. return true -- Indicate exit
  269. else
  270. displayError("Invalid option. Please select 1, 2, or 3.")
  271. end
  272.  
  273. return false -- Continue showing menu
  274. end
  275.  
  276. -- Main execution loop
  277. while true do
  278. local exit = mainMenu()
  279. if exit then
  280. break -- Exit the loop if the user chose to exit
  281. end
  282. end
  283.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement