Advertisement
DOGGYWOOF

Untitled

Sep 17th, 2024 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.52 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. -- Function to get Webserver ID from user
  49. local function getWebserverID()
  50. term.clear()
  51. drawTitleBar()
  52. print("Enter Webserver ID:")
  53. return tonumber(read())
  54. end
  55.  
  56. -- Automatically find and open the first available modem
  57. local function openModem()
  58. local sides = {"top", "bottom", "left", "right", "front", "back"}
  59. for _, side in ipairs(sides) do
  60. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  61. rednet.open(side)
  62. return side
  63. end
  64. end
  65. displayError("No modem found.")
  66. return nil
  67. end
  68.  
  69. -- Load DNS Provider ID from file
  70. local function loadDNSProviderID()
  71. local filePath = "DNS-Provider.txt"
  72. if not fs.exists(filePath) then
  73. displayError("DNS-Provider.txt not found.")
  74. return nil
  75. end
  76. local file = fs.open(filePath, "r")
  77. local id = file.readAll()
  78. file.close()
  79. return tonumber(id)
  80. end
  81.  
  82. -- Save DNS Provider ID to file
  83. local function saveDNSProviderID(id)
  84. local filePath = "DNS-Provider.txt"
  85. local file = fs.open(filePath, "w")
  86. file.write(id)
  87. file.close()
  88. print("DNS Provider ID updated.")
  89. end
  90.  
  91. -- Connect to DNS and get IP address
  92. local function connectToDNS(url, dnsID)
  93. local modemSide = openModem()
  94. if not modemSide then return nil end
  95.  
  96. term.clear()
  97. drawTitleBar()
  98. print("Connecting to DNS...")
  99.  
  100. rednet.send(dnsID, url)
  101. local timer = os.startTimer(5)
  102. while true do
  103. local event, p1, p2 = os.pullEvent()
  104. if event == "rednet_message" and p1 == dnsID then
  105. rednet.close(modemSide)
  106. return p2
  107. elseif event == "timer" and p1 == timer then
  108. rednet.close(modemSide)
  109. displayError("DNS Timeout.")
  110. return nil
  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. -- Continuously check for user interaction (mouse clicks) to close the browser
  153. while not waitForClick() do
  154. -- Keep checking for click events to close the browser
  155. end
  156. term.clear()
  157. else
  158. displayError("Error loading website: " .. err)
  159. end
  160. end
  161.  
  162. -- Main menu function
  163. local function mainMenu()
  164. term.clear()
  165. drawTitleBar()
  166. print("Select an option:")
  167. print("1. Enter URL")
  168. print("2. Enter Webserver ID directly")
  169. print("3. Edit DNS Provider ID")
  170. print("4. Exit")
  171. local choice = tonumber(read())
  172.  
  173. if choice == 1 then
  174. -- Enter URL
  175. while true do
  176. local url = getURL()
  177. if url == nil then
  178. break
  179. end
  180.  
  181. local dnsID = loadDNSProviderID()
  182. if not dnsID then
  183. displayError("Failed to load DNS Provider ID.")
  184. return
  185. end
  186.  
  187. local ip = connectToDNS(url, dnsID)
  188. if ip == "404_NOT_FOUND" then
  189. displayError("Error 404: Webpage cannot be found!")
  190. return
  191. elseif ip == "ERROR 403: Your IP address has been blocked from this DNS Network" then
  192. displayError("ERROR 403: Your IP address has been blocked from this DNS Network")
  193. return
  194. elseif not ip then
  195. displayError("Failed to connect to DNS.")
  196. return
  197. end
  198.  
  199. local code = connectToWebserver(ip)
  200. if not code then
  201. displayError("Failed to connect to Webserver.")
  202. return
  203. end
  204.  
  205. -- Write code to file and then delete it
  206. local tempFile = "received_code.lua"
  207. local tempFileHandle = fs.open(tempFile, "w")
  208. tempFileHandle.write(code)
  209. tempFileHandle.close()
  210.  
  211. -- Immediately delete the file after writing
  212. local success, err = pcall(function() fs.delete(tempFile) end)
  213. if success then
  214. print("Temporary file deleted successfully.")
  215. else
  216. displayError("Error deleting temporary file: " .. err)
  217. end
  218.  
  219. runWebsite(code)
  220. -- After running the website, return to the main menu
  221. term.clear()
  222. drawTitleBar()
  223. print("Returning to main menu...")
  224. os.sleep(2) -- Pause to allow the user to see the message
  225. break
  226. end
  227. elseif choice == 2 then
  228. -- Enter Webserver ID directly
  229. term.clear()
  230. drawTitleBar()
  231. print("WARNING: Connecting directly with Webserver ID bypasses DNS malware scans and may be dangerous.")
  232. print("Proceed with caution. Enter Webserver ID directly:")
  233.  
  234. while true do
  235. local id = getWebserverID()
  236. if not id then
  237. displayError("Invalid Webserver ID.")
  238. return
  239. end
  240.  
  241. local code = connectToWebserver(id)
  242. if not code then
  243. displayError("Failed to connect to Webserver.")
  244. return
  245. end
  246.  
  247. runWebsite(code)
  248. -- After running the website, return to the main menu
  249. term.clear()
  250. drawTitleBar()
  251. print("Returning to main menu...")
  252. os.sleep(2) -- Pause to allow the user to see the message
  253. break
  254. end
  255. elseif choice == 3 then
  256. -- Edit DNS Provider ID
  257. term.clear()
  258. drawTitleBar()
  259. print("Current DNS Provider ID: " .. (loadDNSProviderID() or "Not set"))
  260. print("Enter new DNS Provider ID:")
  261. local newID = tonumber(read())
  262. if newID then
  263. saveDNSProviderID(newID)
  264. else
  265. displayError("Invalid DNS Provider ID.")
  266. end
  267. elseif choice == 4 then
  268. print("Exiting...")
  269. return true -- Indicate exit
  270. else
  271. displayError("Invalid option. Please select 1, 2, 3, or 4.")
  272. end
  273.  
  274. return false -- Continue showing menu
  275. end
  276.  
  277. -- Main execution loop
  278. while true do
  279. local exit = mainMenu()
  280. if exit then
  281. break -- Exit the loop if the user chose to exit
  282. end
  283. end
  284.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement