Advertisement
DOGGYWOOF

Untitled

Jan 6th, 2025 (edited)
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.72 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 display a warning and get user confirmation
  40. local function displayWarning(message)
  41. term.clear()
  42. drawTitleBar()
  43. print("Warning: " .. message)
  44. print("Do you want to continue? (yes/no)")
  45. while true do
  46. local input = read():lower()
  47. if input == "yes" then
  48. return true
  49. elseif input == "no" then
  50. return false
  51. else
  52. print("Please type 'yes' or 'no'.")
  53. end
  54. end
  55. end
  56.  
  57. -- Function to get URL from user
  58. local function getURL()
  59. term.clear()
  60. drawTitleBar()
  61. print("Enter URL:")
  62. return read()
  63. end
  64.  
  65. -- Automatically find and open the first available modem
  66. local function openModem()
  67. local sides = {"top", "bottom", "left", "right", "front", "back"}
  68. for _, side in ipairs(sides) do
  69. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  70. rednet.open(side)
  71. return side
  72. end
  73. end
  74. displayError("No modem found.")
  75. return nil
  76. end
  77.  
  78. -- Load DNS Provider ID from file
  79. local function loadDNSProviderID()
  80. local filePath = "DNS-Provider.txt"
  81. if not fs.exists(filePath) then
  82. displayError("DNS-Provider.txt not found.")
  83. return nil
  84. end
  85. local file = fs.open(filePath, "r")
  86. local id = file.readAll()
  87. file.close()
  88. return tonumber(id)
  89. end
  90.  
  91. -- Save DNS Provider ID to file
  92. local function saveDNSProviderID(id)
  93. local filePath = "DNS-Provider.txt"
  94. local file = fs.open(filePath, "w")
  95. file.write(id)
  96. file.close()
  97. print("DNS Provider ID updated.")
  98. end
  99.  
  100. -- Connect to DNS and get IP address
  101. local function connectToDNS(url, dnsID)
  102. local modemSide = openModem()
  103. if not modemSide then return nil end
  104.  
  105. term.clear()
  106. drawTitleBar()
  107. print("Connecting to DNS...")
  108.  
  109. rednet.send(dnsID, url)
  110. local timer = os.startTimer(5)
  111. while true do
  112. local event, p1, p2 = os.pullEvent()
  113. if event == "rednet_message" and p1 == dnsID then
  114. rednet.close(modemSide)
  115. return p2
  116. elseif event == "timer" and p1 == timer then
  117. rednet.close(modemSide)
  118. displayError("DNS Timeout.")
  119. return nil
  120. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  121. rednet.close(modemSide)
  122. return nil
  123. end
  124. end
  125. end
  126.  
  127. -- Connect to webserver and get website code
  128. local function connectToWebserver(ip)
  129. local modemSide = openModem()
  130. if not modemSide then return nil end
  131.  
  132. term.clear()
  133. drawTitleBar()
  134. print("Connecting to Webserver...")
  135.  
  136. rednet.send(tonumber(ip), "GET")
  137. local timer = os.startTimer(5)
  138. while true do
  139. local event, p1, p2 = os.pullEvent()
  140. if event == "rednet_message" and p1 == tonumber(ip) then
  141. rednet.close(modemSide)
  142. return p2
  143. elseif event == "timer" and p1 == timer then
  144. rednet.close(modemSide)
  145. displayError("Connection timed out.")
  146. return nil
  147. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  148. rednet.close(modemSide)
  149. return nil
  150. end
  151. end
  152. end
  153.  
  154. -- Check if the code contains banned scripts
  155. local function containsBannedScripts(code)
  156. local bannedKeywords = {"fs.", "shell.run"}
  157. for _, keyword in ipairs(bannedKeywords) do
  158. if code:find(keyword, 1, true) then
  159. return true, keyword
  160. end
  161. end
  162. return false, nil
  163. end
  164.  
  165. -- Execute website code and render it
  166. local function runWebsite(code)
  167. local isBanned, keyword = containsBannedScripts(code)
  168. if isBanned then
  169. local proceed = displayWarning("This website has been blocked due to some code that may compromise the security of your system, The following script has been flagged: '" .. keyword .. "'.")
  170. if not proceed then
  171. return
  172. end
  173. end
  174.  
  175. local func, err = load(code, "website", "t", _ENV)
  176. if func then
  177. term.clear()
  178. drawTitleBar()
  179. func() -- Run the website code
  180. -- Continuously check for user interaction (mouse clicks) to close the browser
  181. while not waitForClick() do
  182. -- Keep checking for click events to close the browser
  183. end
  184. term.clear()
  185. else
  186. displayError("Error loading website: " .. err)
  187. end
  188. end
  189.  
  190. -- Main menu function
  191. local function mainMenu()
  192. term.clear()
  193. drawTitleBar()
  194. print("Select an option:")
  195. print("1. Enter URL")
  196. print("2. Edit DNS Provider ID")
  197. print("3. Exit")
  198. local choice = tonumber(read())
  199.  
  200. if choice == 1 then
  201. -- Enter URL
  202. while true do
  203. local url = getURL()
  204. if url == nil then
  205. break
  206. end
  207.  
  208. local dnsID = loadDNSProviderID()
  209. if not dnsID then
  210. displayError("Failed to load DNS Provider ID.")
  211. return
  212. end
  213.  
  214. local ip = connectToDNS(url, dnsID)
  215. if ip == "404_NOT_FOUND" then
  216. displayError("Error 404: Webpage cannot be found!")
  217. return
  218. elseif not ip then
  219. displayError("Failed to connect to DNS.")
  220. return
  221. end
  222.  
  223. local code = connectToWebserver(ip)
  224. if not code then
  225. displayError("Failed to connect to Webserver.")
  226. return
  227. end
  228.  
  229. runWebsite(code)
  230. -- After running the website, return to the main menu
  231. term.clear()
  232. drawTitleBar()
  233. print("Returning to main menu...")
  234. os.sleep(2) -- Pause to allow the user to see the message
  235. break
  236. end
  237. elseif choice == 2 then
  238. -- Edit DNS Provider ID
  239. term.clear()
  240. drawTitleBar()
  241. print("Current DNS Provider ID: " .. (loadDNSProviderID() or "Not set"))
  242. print("Enter new DNS Provider ID:")
  243. local newID = tonumber(read())
  244. if newID then
  245. saveDNSProviderID(newID)
  246. else
  247. displayError("Invalid DNS Provider ID.")
  248. end
  249. elseif choice == 3 then
  250. print("Exiting...")
  251. shell.run("/disk/os/gui")
  252. return true -- Indicate exit
  253. else
  254. displayError("Invalid option. Please select 1, 2, or 3.")
  255. end
  256.  
  257. return false -- Continue showing menu
  258. end
  259.  
  260. -- Main execution loop
  261. while true do
  262. local exit = mainMenu()
  263. if exit then
  264. break -- Exit the loop if the user chose to exit
  265. end
  266. end
  267.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement