Advertisement
DOGGYWOOF

Untitled

Jan 6th, 2025
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 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. local timer = os.startTimer(5)
  93. while true do
  94. local event, p1, p2 = os.pullEvent()
  95. if event == "rednet_message" and p1 == dnsID then
  96. rednet.close(modemSide)
  97. return p2
  98. elseif event == "timer" and p1 == timer then
  99. rednet.close(modemSide)
  100. displayError("DNS Timeout.")
  101. return nil
  102. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  103. rednet.close(modemSide)
  104. return nil
  105. end
  106. end
  107. end
  108.  
  109. -- Check for banned scripts in the code
  110. local function checkForBannedScripts(code)
  111. local bannedPatterns = {
  112. {pattern = "fs%.", allowed = {"exists"}},
  113. {pattern = "shell%.run"},
  114. {pattern = "fs%.open"},
  115. {pattern = "fs%.delete"},
  116. {pattern = "fs%.write"},
  117. {pattern = "fs%.move"},
  118. {pattern = "fs%.copy"}
  119. }
  120.  
  121. for _, entry in ipairs(bannedPatterns) do
  122. for line in code:gmatch("([^"]+)") do
  123. if line:match(entry.pattern) and not (entry.allowed and line:match(entry.allowed[1])) then
  124. return false, "Banned operation found: " .. line
  125. end
  126. end
  127. end
  128. return true
  129. end
  130.  
  131. -- Connect to webserver and get website code
  132. local function connectToWebserver(ip)
  133. local modemSide = openModem()
  134. if not modemSide then return nil end
  135.  
  136. term.clear()
  137. drawTitleBar()
  138. print("Connecting to Webserver...")
  139.  
  140. rednet.send(tonumber(ip), "GET")
  141. local timer = os.startTimer(5)
  142. while true do
  143. local event, p1, p2 = os.pullEvent()
  144. if event == "rednet_message" and p1 == tonumber(ip) then
  145. rednet.close(modemSide)
  146. return p2
  147. elseif event == "timer" and p1 == timer then
  148. rednet.close(modemSide)
  149. displayError("Connection timed out.")
  150. return nil
  151. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  152. rednet.close(modemSide)
  153. return nil
  154. end
  155. end
  156. end
  157.  
  158. -- Execute website code and render it
  159. local function runWebsite(code)
  160. local isSafe, errorMessage = checkForBannedScripts(code)
  161. if not isSafe then
  162. displayError(errorMessage)
  163. return
  164. end
  165.  
  166. local func, err = load(code, "website", "t", _ENV)
  167. if func then
  168. term.clear()
  169. drawTitleBar()
  170. func() -- Run the website code
  171. -- Continuously check for user interaction (mouse clicks) to close the browser
  172. while not waitForClick() do
  173. -- Keep checking for click events to close the browser
  174. end
  175. term.clear()
  176. else
  177. displayError("Error loading website: " .. err)
  178. end
  179. end
  180.  
  181. -- Main menu function
  182. local function mainMenu()
  183. term.clear()
  184. drawTitleBar()
  185. print("Select an option:")
  186. print("1. Enter URL")
  187. print("2. Edit DNS Provider ID")
  188. print("3. Exit")
  189. local choice = tonumber(read())
  190.  
  191. if choice == 1 then
  192. -- Enter URL
  193. while true do
  194. local url = getURL()
  195. if url == nil then
  196. break
  197. end
  198.  
  199. local dnsID = loadDNSProviderID()
  200. if not dnsID then
  201. displayError("Failed to load DNS Provider ID.")
  202. return
  203. end
  204.  
  205. local ip = connectToDNS(url, dnsID)
  206. if ip == "404_NOT_FOUND" then
  207. displayError("Error 404: Webpage cannot be found!")
  208. return
  209. elseif not ip then
  210. displayError("Failed to connect to DNS.")
  211. return
  212. end
  213.  
  214. local code = connectToWebserver(ip)
  215. if not code then
  216. displayError("Failed to connect to Webserver.")
  217. return
  218. end
  219.  
  220. -- Write code to file and then delete it
  221. local tempFile = "received_code.lua"
  222. local tempFileHandle = fs.open(tempFile, "w")
  223. tempFileHandle.write(code)
  224. tempFileHandle.close()
  225.  
  226. -- Immediately delete the file after writing
  227. local success, err = pcall(function() fs.delete(tempFile) end)
  228. if success then
  229. print("Temporary file deleted successfully.")
  230. else
  231. displayError("Error deleting temporary file: " .. err)
  232. end
  233.  
  234. runWebsite(code)
  235. -- After running the website, return to the main menu
  236. term.clear()
  237. drawTitleBar()
  238. print("Returning to main menu...")
  239. os.sleep(2) -- Pause to allow the user to see the message
  240. break
  241. end
  242. elseif choice == 2 then
  243. -- Edit DNS Provider ID
  244. term.clear()
  245. drawTitleBar()
  246. print("Current DNS Provider ID: " .. (loadDNSProviderID() or "Not set"))
  247. print("Enter new DNS Provider ID:")
  248. local newID = tonumber(read())
  249. if newID then
  250. saveDNSProviderID(newID)
  251. else
  252. displayError("Invalid DNS Provider ID.")
  253. end
  254. elseif choice == 3 then
  255. print("Exiting...")
  256. shell.run("/disk/os/gui")
  257. return true -- Indicate exit
  258. else
  259. displayError("Invalid option. Please select 1, 2, or 3.")
  260. end
  261.  
  262. return false -- Continue showing menu
  263. end
  264.  
  265. -- Main execution loop
  266. while true do
  267. local exit = mainMenu()
  268. if exit then
  269. break -- Exit the loop if the user chose to exit
  270. end
  271. end
  272.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement