DOGGYWOOF

Untitled

Sep 13th, 2024 (edited)
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.41 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. -- Function to get password from user
  48. local function getPassword()
  49. term.clear()
  50. drawTitleBar()
  51. print("Enter DNS Password:")
  52. return read("*") -- Mask the input for security
  53. end
  54.  
  55. -- Automatically find and open the first available modem
  56. local function openModem()
  57. local sides = {"top", "bottom", "left", "right", "front", "back"}
  58. for _, side in ipairs(sides) do
  59. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  60. rednet.open(side)
  61. return side
  62. end
  63. end
  64. displayError("No modem found.")
  65. return nil
  66. end
  67.  
  68. -- Load DNS Provider ID from file
  69. local function loadDNSProviderID()
  70. local filePath = "DNS-Provider.txt"
  71. if not fs.exists(filePath) then
  72. displayError("DNS-Provider.txt not found.")
  73. return nil
  74. end
  75. local file = fs.open(filePath, "r")
  76. local id = file.readAll()
  77. file.close()
  78. return tonumber(id)
  79. end
  80.  
  81. -- Save DNS Provider ID to file
  82. local function saveDNSProviderID(id)
  83. local filePath = "DNS-Provider.txt"
  84. local file = fs.open(filePath, "w")
  85. file.write(id)
  86. file.close()
  87. print("DNS Provider ID updated.")
  88. end
  89.  
  90. -- Connect to DNS and get IP address
  91. local function connectToDNS(url, dnsID, password)
  92. local modemSide = openModem()
  93. if not modemSide then return nil end
  94.  
  95. term.clear()
  96. drawTitleBar()
  97. print("Connecting to DNS...")
  98.  
  99. rednet.send(dnsID, {url, password})
  100. local timer = os.startTimer(5)
  101. while true do
  102. local event, p1, p2 = os.pullEvent()
  103. if event == "rednet_message" and p1 == dnsID then
  104. rednet.close(modemSide)
  105. return p2
  106. elseif event == "timer" and p1 == timer then
  107. rednet.close(modemSide)
  108. displayError("DNS Timeout.")
  109. return nil
  110. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  111. rednet.close(modemSide)
  112. return nil
  113. end
  114. end
  115. end
  116.  
  117. -- Connect to webserver and get website code
  118. local function connectToWebserver(ip)
  119. local modemSide = openModem()
  120. if not modemSide then return nil end
  121.  
  122. term.clear()
  123. drawTitleBar()
  124. print("Connecting to Webserver...")
  125.  
  126. rednet.send(tonumber(ip), "GET")
  127. local timer = os.startTimer(5)
  128. while true do
  129. local event, p1, p2 = os.pullEvent()
  130. if event == "rednet_message" and p1 == tonumber(ip) then
  131. rednet.close(modemSide)
  132. return p2
  133. elseif event == "timer" and p1 == timer then
  134. rednet.close(modemSide)
  135. displayError("Connection timed out.")
  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. -- Execute website code and render it
  145. local function runWebsite(code)
  146. local func, err = load(code, "website", "t", _ENV)
  147. if func then
  148. term.clear()
  149. drawTitleBar()
  150. func() -- Run the website code
  151. -- Continuously check for user interaction (mouse clicks) to close the browser
  152. while not waitForClick() do
  153. -- Keep checking for click events to close the browser
  154. end
  155. term.clear()
  156. else
  157. displayError("Error loading website: " .. err)
  158. end
  159. end
  160.  
  161. -- Main menu function
  162. local function mainMenu()
  163. term.clear()
  164. drawTitleBar()
  165. print("Select an option:")
  166. print("1. Enter URL")
  167. print("2. Edit DNS Provider ID")
  168. print("3. Exit")
  169. local choice = tonumber(read())
  170.  
  171. if choice == 1 then
  172. -- Enter URL
  173. while true do
  174. local url = getURL()
  175. if url == nil then
  176. break
  177. end
  178.  
  179. local dnsID = loadDNSProviderID()
  180. if not dnsID then
  181. displayError("Failed to load DNS Provider ID.")
  182. return
  183. end
  184.  
  185. local password = getPassword() -- Request password
  186. local ip = connectToDNS(url, dnsID, password)
  187. if ip == "404_NOT_FOUND" then
  188. displayError("Error 404: Webpage cannot be found!")
  189. return
  190. elseif not ip then
  191. displayError("Failed to connect to DNS.")
  192. return
  193. end
  194.  
  195. local code = connectToWebserver(ip)
  196. if not code then
  197. displayError("Failed to connect to Webserver.")
  198. return
  199. end
  200.  
  201. -- Write code to file and then delete it
  202. local tempFile = "received_code.lua"
  203. local tempFileHandle = fs.open(tempFile, "w")
  204. tempFileHandle.write(code)
  205. tempFileHandle.close()
  206.  
  207. -- Immediately delete the file after writing
  208. local success, err = pcall(function() fs.delete(tempFile) end)
  209. if success then
  210. print("Temporary file deleted successfully.")
  211. else
  212. displayError("Error deleting temporary file: " .. err)
  213. end
  214.  
  215. runWebsite(code)
  216. -- After running the website, return to the main menu
  217. term.clear()
  218. drawTitleBar()
  219. print("Returning to main menu...")
  220. os.sleep(2) -- Pause to allow the user to see the message
  221. break
  222. end
  223. elseif choice == 2 then
  224. -- Edit DNS Provider ID
  225. term.clear()
  226. drawTitleBar()
  227. print("Current DNS Provider ID: " .. (loadDNSProviderID() or "Not set"))
  228. print("Enter new DNS Provider ID:")
  229. local newID = tonumber(read())
  230. if newID then
  231. saveDNSProviderID(newID)
  232. else
  233. displayError("Invalid DNS Provider ID.")
  234. end
  235. elseif choice == 3 then
  236. print("Exiting...")
  237. return true -- Indicate exit
  238. else
  239. displayError("Invalid option. Please select 1, 2, or 3.")
  240. end
  241.  
  242. return false -- Continue showing menu
  243. end
  244.  
  245. -- Main execution loop
  246. while true do
  247. local exit = mainMenu()
  248. if exit then
  249. break -- Exit the loop if the user chose to exit
  250. end
  251. end
  252.  
Add Comment
Please, Sign In to add comment