Advertisement
DOGGYWOOF

Secure browser

Sep 12th, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 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
  83. local function promptForPassword()
  84. term.clear()
  85. drawTitleBar()
  86. print("Enter DNS Password:")
  87. return read("*") -- Mask the input for security
  88. end
  89.  
  90. -- Connect to DNS and get IP address
  91. local function connectToDNS(url, dnsID)
  92. local modemSide = openModem()
  93. if not modemSide then return nil end
  94.  
  95. term.clear()
  96. drawTitleBar()
  97. print("Connecting to DNS...")
  98.  
  99. rednet.send(dnsID, url)
  100. local timer = os.startTimer(5)
  101. while true do
  102. local event, p1, p2 = os.pullEvent()
  103. if event == "rednet_message" and p1 == dnsID then
  104. local response = p2
  105. if response == "PASSWORD_REQUIRED" then
  106. local password = promptForPassword()
  107. rednet.send(dnsID, password) -- Send the password to the DNS server
  108. elseif response == "AUTH_FAILED" then
  109. rednet.close(modemSide)
  110. displayError("Authentication failed.")
  111. return nil
  112. elseif response == "TIMEOUT" then
  113. rednet.close(modemSide)
  114. displayError("Password entry timed out.")
  115. return nil
  116. else
  117. rednet.close(modemSide)
  118. return response
  119. end
  120. elseif event == "timer" and p1 == timer then
  121. rednet.close(modemSide)
  122. displayError("DNS Timeout.")
  123. return nil
  124. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  125. rednet.close(modemSide)
  126. return nil
  127. end
  128. end
  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 func, err = load(code, "website", "t", _ENV)
  161. if func then
  162. term.clear()
  163. drawTitleBar()
  164. func() -- Run the website code
  165. -- Continuously check for user interaction (mouse clicks) to close the browser
  166. while not waitForClick() do
  167. -- Keep checking for click events to close the browser
  168. end
  169. term.clear()
  170. else
  171. displayError("Error loading website: " .. err)
  172. end
  173. end
  174.  
  175. -- Main menu function
  176. local function mainMenu()
  177. term.clear()
  178. drawTitleBar()
  179. print("Select an option:")
  180. print("1. Enter URL")
  181. print("2. Edit DNS Provider ID")
  182. print("3. Exit")
  183. local choice = tonumber(read())
  184.  
  185. if choice == 1 then
  186. -- Enter URL
  187. while true do
  188. local url = getURL()
  189. if url == nil then
  190. break
  191. end
  192.  
  193. local dnsID = loadDNSProviderID()
  194. if not dnsID then
  195. displayError("Failed to load DNS Provider ID.")
  196. return
  197. end
  198.  
  199. local ip = connectToDNS(url, dnsID)
  200. if ip == "404_NOT_FOUND" then
  201. displayError("Error 404: Webpage cannot be found!")
  202. return
  203. elseif not ip then
  204. displayError("Failed to connect to DNS.")
  205. return
  206. end
  207.  
  208. local code = connectToWebserver(ip)
  209. if not code then
  210. displayError("Failed to connect to Webserver.")
  211. return
  212. end
  213.  
  214. -- Write code to file and then delete it
  215. local tempFile = "received_code.lua"
  216. local tempFileHandle = fs.open(tempFile, "w")
  217. tempFileHandle.write(code)
  218. tempFileHandle.close()
  219.  
  220. -- Immediately delete the file after writing
  221. local success, err = pcall(function() fs.delete(tempFile) end)
  222. if success then
  223. print("Temporary file deleted successfully.")
  224. else
  225. displayError("Error deleting temporary file: " .. err)
  226. end
  227.  
  228. runWebsite(code)
  229. -- After running the website, return to the main menu
  230. term.clear()
  231. drawTitleBar()
  232. print("Returning to main menu...")
  233. os.sleep(2) -- Pause to allow the user to see the message
  234. break
  235. end
  236. elseif choice == 2 then
  237. -- Edit DNS Provider ID
  238. term.clear()
  239. drawTitleBar()
  240. print("Current DNS Provider ID: " .. (loadDNSProviderID() or "Not set"))
  241. print("Enter new DNS Provider ID:")
  242. local newID = tonumber(read())
  243. if newID then
  244. saveDNSProviderID(newID)
  245. else
  246. displayError("Invalid DNS Provider ID.")
  247. end
  248. elseif choice == 3 then
  249. print("Exiting...")
  250. return true -- Indicate exit
  251. else
  252. displayError("Invalid option. Please select 1, 2, or 3.")
  253. end
  254.  
  255. return false -- Continue showing menu
  256. end
  257.  
  258. -- Main execution loop
  259. while true do
  260. local exit = mainMenu()
  261. if exit then
  262. break -- Exit the loop if the user chose to exit
  263. end
  264. end
  265.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement