Advertisement
DOGGYWOOF

Untitled

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