Advertisement
DOGGYWOOF

Untitled

Sep 12th, 2024
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 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. while true do
  93. local event, p1, p2 = os.pullEvent()
  94. if event == "rednet_message" and p1 == dnsID then
  95. if p2 == "PASSWORD_REQUIRED" then
  96. -- Prompt for password
  97. term.clear()
  98. drawTitleBar()
  99. print("Enter DNS Password:")
  100. local password = read("*")
  101. rednet.send(dnsID, password)
  102. elseif p2 == "AUTH_SUCCESS" then
  103. print("Authenticated successfully!")
  104. elseif p2 == "AUTH_FAILED" then
  105. displayError("Authentication failed. Incorrect password.")
  106. return nil
  107. else
  108. rednet.close(modemSide)
  109. return p2
  110. end
  111. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  112. rednet.close(modemSide)
  113. return nil
  114. end
  115. end
  116. end
  117.  
  118. -- Connect to webserver and get website code
  119. local function connectToWebserver(ip)
  120. local modemSide = openModem()
  121. if not modemSide then return nil end
  122.  
  123. term.clear()
  124. drawTitleBar()
  125. print("Connecting to Webserver...")
  126.  
  127. rednet.send(tonumber(ip), "GET")
  128. local timer = os.startTimer(5)
  129. while true do
  130. local event, p1, p2 = os.pullEvent()
  131. if event == "rednet_message" and p1 == tonumber(ip) then
  132. rednet.close(modemSide)
  133. return p2
  134. elseif event == "timer" and p1 == timer then
  135. rednet.close(modemSide)
  136. displayError("Connection timed out.")
  137. return nil
  138. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  139. rednet.close(modemSide)
  140. return nil
  141. end
  142. end
  143. end
  144.  
  145. -- Execute website code and render it
  146. local function runWebsite(code)
  147. local func, err = load(code, "website", "t", _ENV)
  148. if func then
  149. term.clear()
  150. drawTitleBar()
  151. func() -- Run the website code
  152. while not waitForClick() do end -- Keep checking for click events to close the browser
  153. term.clear()
  154. else
  155. displayError("Error loading website: " .. err)
  156. end
  157. end
  158.  
  159. -- Main menu function
  160. local function mainMenu()
  161. term.clear()
  162. drawTitleBar()
  163. print("Select an option:")
  164. print("1. Enter URL")
  165. print("2. Edit DNS Provider ID")
  166. print("3. Exit")
  167. local choice = tonumber(read())
  168.  
  169. if choice == 1 then
  170. while true do
  171. local url = getURL()
  172. if url == nil then break end
  173.  
  174. local dnsID = loadDNSProviderID()
  175. if not dnsID then
  176. displayError("Failed to load DNS Provider ID.")
  177. return
  178. end
  179.  
  180. local ip = connectToDNS(url, dnsID)
  181. if ip == "404_NOT_FOUND" then
  182. displayError("Error 404: Webpage cannot be found!")
  183. return
  184. elseif not ip then
  185. displayError("No response from DNS.")
  186. return
  187. end
  188.  
  189. local site = connectToWebserver(ip)
  190. if site then
  191. runWebsite(site)
  192. end
  193. end
  194. elseif choice == 2 then
  195. print("Enter new DNS Provider ID:")
  196. local newID = tonumber(read())
  197. if newID then
  198. saveDNSProviderID(newID)
  199. else
  200. displayError("Invalid input. Please enter a number.")
  201. end
  202. elseif choice == 3 then
  203. return
  204. else
  205. displayError("Invalid choice.")
  206. end
  207. end
  208.  
  209. -- Run the main menu loop
  210. while true do
  211. mainMenu()
  212. end
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement