Advertisement
DOGGYWOOF

Secure Browser

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