Advertisement
DOGGYWOOF

Untitled

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