Advertisement
DOGGYWOOF

Doggy OS Browser

Jul 25th, 2024 (edited)
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. local function getURL()
  2. print("Enter URL:")
  3. return read()
  4. end
  5.  
  6. local function loadDNSProviderID()
  7. local filePath = "DNS-Provider.txt"
  8. if not fs.exists(filePath) then
  9. print("DNS-Provider.txt not found.")
  10. return nil
  11. end
  12. local file = fs.open(filePath, "r")
  13. local id = file.readAll()
  14. file.close()
  15. return tonumber(id)
  16. end
  17.  
  18. local function connectToDNS(url, dnsID)
  19. rednet.open("top") -- Assuming the modem is on the top
  20. print("Connecting to DNS...")
  21.  
  22. rednet.send(dnsID, url)
  23. local timer = os.startTimer(5)
  24. while true do
  25. local event, p1, p2 = os.pullEvent()
  26. if event == "rednet_message" and p1 == dnsID then
  27. rednet.close("top")
  28. return p2
  29. elseif event == "timer" and p1 == timer then
  30. rednet.close("top")
  31. print("DNS Timeout.")
  32. return nil
  33. end
  34. end
  35. end
  36.  
  37. local function connectToWebserver(ip)
  38. rednet.open("top") -- Assuming the modem is on the top
  39. print("Connecting to Webserver...")
  40.  
  41. rednet.send(tonumber(ip), "GET")
  42. local timer = os.startTimer(5)
  43. while true do
  44. local event, p1, p2 = os.pullEvent()
  45. if event == "rednet_message" and p1 == tonumber(ip) then
  46. rednet.close("top")
  47. return p2
  48. elseif event == "timer" and p1 == timer then
  49. rednet.close("top")
  50. print("Connection timed out.")
  51. return nil
  52. end
  53. end
  54. end
  55.  
  56. local function runWebsite(code)
  57. local func, err = load(code, "website", "t", _ENV)
  58. if func then
  59. func()
  60. else
  61. print("Error loading website: " .. err)
  62. end
  63. end
  64.  
  65. -- Main execution
  66. local url = getURL()
  67. local dnsID = loadDNSProviderID()
  68.  
  69. if not dnsID then
  70. print("Failed to load DNS Provider ID.")
  71. return
  72. end
  73.  
  74. local ip = connectToDNS(url, dnsID)
  75. if ip == "404_NOT_FOUND" then
  76. print("Error 404: Webpage cannot be found!")
  77. return
  78. elseif not ip then
  79. print("Failed to connect to DNS.")
  80. return
  81. end
  82.  
  83. local code = connectToWebserver(ip)
  84. if not code then
  85. print("Failed to connect to Webserver.")
  86. return
  87. end
  88.  
  89. -- Write code to file and then delete it
  90. local tempFile = "received_code.lua"
  91. local tempFileHandle = fs.open(tempFile, "w")
  92. tempFileHandle.write(code)
  93. tempFileHandle.close()
  94.  
  95. -- Immediately delete the file after writing
  96. local success, err = pcall(function() fs.delete(tempFile) end)
  97. if success then
  98. print("Temporary file deleted successfully.")
  99. else
  100. print("Error deleting temporary file: " .. err)
  101. end
  102.  
  103. runWebsite(code)
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement