Advertisement
DOGGYWOOF

BETA BROWSER

Jul 25th, 2024 (edited)
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 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. -- Connect to DNS and get IP address
  83. local function connectToDNS(url, dnsID)
  84. local modemSide = openModem()
  85. if not modemSide then return nil end
  86.  
  87. term.clear()
  88. drawTitleBar()
  89. print("Connecting to DNS...")
  90.  
  91. rednet.send(dnsID, url)
  92. local timer = os.startTimer(5)
  93. while true do
  94. local event, p1, p2 = os.pullEvent()
  95. if event == "rednet_message" and p1 == dnsID then
  96. rednet.close(modemSide)
  97. return p2
  98. elseif event == "timer" and p1 == timer then
  99. rednet.close(modemSide)
  100. displayError("DNS Timeout.")
  101. return nil
  102. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  103. rednet.close(modemSide)
  104. return nil
  105. end
  106. end
  107. end
  108.  
  109. -- Connect to webserver and get website code
  110. local function connectToWebserver(ip)
  111. local modemSide = openModem()
  112. if not modemSide then return nil end
  113.  
  114. term.clear()
  115. drawTitleBar()
  116. print("Connecting to Webserver...")
  117.  
  118. rednet.send(tonumber(ip), "GET")
  119. local timer = os.startTimer(5)
  120. while true do
  121. local event, p1, p2 = os.pullEvent()
  122. if event == "rednet_message" and p1 == tonumber(ip) then
  123. rednet.close(modemSide)
  124. return p2
  125. elseif event == "timer" and p1 == timer then
  126. rednet.close(modemSide)
  127. displayError("Connection timed out.")
  128. return nil
  129. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  130. rednet.close(modemSide)
  131. return nil
  132. end
  133. end
  134. end
  135.  
  136. -- Execute website code and render it
  137. local function runWebsite(code)
  138. local func, err = load(code, "website", "t", _ENV)
  139. if func then
  140. term.clear()
  141. drawTitleBar()
  142. func() -- Run the website code
  143. -- Continuously check for user interaction (mouse clicks) to close the browser
  144. while not waitForClick() do
  145. -- Keep checking for click events to close the browser
  146. end
  147. term.clear()
  148. else
  149. displayError("Error loading website: " .. err)
  150. end
  151. end
  152.  
  153. -- Main menu function
  154. local function mainMenu()
  155. term.clear()
  156. drawTitleBar()
  157. print("Select an option:")
  158. print("1. Enter URL")
  159. print("2. Edit DNS Provider ID")
  160. print("3. Exit")
  161. local choice = tonumber(read())
  162.  
  163. if choice == 1 then
  164. -- Enter URL
  165. while true do
  166. local url = getURL()
  167. if url == nil then
  168. break
  169. end
  170.  
  171. local dnsID = loadDNSProviderID()
  172. if not dnsID then
  173. displayError("Failed to load DNS Provider ID.")
  174. return
  175. end
  176.  
  177. local ip = connectToDNS(url, dnsID)
  178. if ip == "404_NOT_FOUND" then
  179. displayError("Error 404: Webpage cannot be found!")
  180. return
  181. elseif ip == "ERROR 403: Your IP address has been blocked from this DNS Network" then
  182. displayError("ERROR 403: Your IP address has been blocked from this DNS Network")
  183. return
  184. elseif not ip then
  185. displayError("Failed to connect to DNS.")
  186. return
  187. end
  188.  
  189. local code = connectToWebserver(ip)
  190. if not code then
  191. displayError("Failed to connect to Webserver.")
  192. return
  193. end
  194.  
  195. -- Write code to file and then delete it
  196. local tempFile = "received_code.lua"
  197. local tempFileHandle = fs.open(tempFile, "w")
  198. tempFileHandle.write(code)
  199. tempFileHandle.close()
  200.  
  201. -- Immediately delete the file after writing
  202. local success, err = pcall(function() fs.delete(tempFile) end)
  203. if success then
  204. print("Temporary file deleted successfully.")
  205. else
  206. displayError("Error deleting temporary file: " .. err)
  207. end
  208.  
  209. runWebsite(code)
  210. -- After running the website, return to the main menu
  211. term.clear()
  212. drawTitleBar()
  213. print("Returning to main menu...")
  214. os.sleep(2) -- Pause to allow the user to see the message
  215. break
  216. end
  217. elseif choice == 2 then
  218. -- Edit DNS Provider ID
  219. term.clear()
  220. drawTitleBar()
  221. print("Current DNS Provider ID: " .. (loadDNSProviderID() or "Not set"))
  222. print("Enter new DNS Provider ID:")
  223. local newID = tonumber(read())
  224. if newID then
  225. saveDNSProviderID(newID)
  226. else
  227. displayError("Invalid DNS Provider ID.")
  228. end
  229. elseif choice == 3 then
  230. print("Exiting...")
  231. return true -- Indicate exit
  232. else
  233. displayError("Invalid option. Please select 1, 2, or 3.")
  234. end
  235.  
  236. return false -- Continue showing menu
  237. end
  238.  
  239. -- Main execution loop
  240. while true do
  241. local exit = mainMenu()
  242. if exit then
  243. break -- Exit the loop if the user chose to exit
  244. end
  245. end
  246.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement