Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function getURL()
- print("Enter URL:")
- return read()
- end
- local function loadDNSProviderID()
- local filePath = "DNS-Provider.txt"
- if not fs.exists(filePath) then
- print("DNS-Provider.txt not found.")
- return nil
- end
- local file = fs.open(filePath, "r")
- local id = file.readAll()
- file.close()
- return tonumber(id)
- end
- local function connectToDNS(url, dnsID)
- rednet.open("top") -- Assuming the modem is on the top
- print("Connecting to DNS...")
- rednet.send(dnsID, url)
- local timer = os.startTimer(5)
- while true do
- local event, p1, p2 = os.pullEvent()
- if event == "rednet_message" and p1 == dnsID then
- rednet.close("top")
- return p2
- elseif event == "timer" and p1 == timer then
- rednet.close("top")
- print("DNS Timeout.")
- return nil
- end
- end
- end
- local function connectToWebserver(ip)
- rednet.open("top") -- Assuming the modem is on the top
- print("Connecting to Webserver...")
- rednet.send(tonumber(ip), "GET")
- local timer = os.startTimer(5)
- while true do
- local event, p1, p2 = os.pullEvent()
- if event == "rednet_message" and p1 == tonumber(ip) then
- rednet.close("top")
- return p2
- elseif event == "timer" and p1 == timer then
- rednet.close("top")
- print("Connection timed out.")
- return nil
- end
- end
- end
- local function runWebsite(code)
- local func, err = load(code, "website", "t", _ENV)
- if func then
- func()
- else
- print("Error loading website: " .. err)
- end
- end
- -- Main execution
- local url = getURL()
- local dnsID = loadDNSProviderID()
- if not dnsID then
- print("Failed to load DNS Provider ID.")
- return
- end
- local ip = connectToDNS(url, dnsID)
- if ip == "404_NOT_FOUND" then
- print("Error 404: Webpage cannot be found!")
- return
- elseif not ip then
- print("Failed to connect to DNS.")
- return
- end
- local code = connectToWebserver(ip)
- if not code then
- print("Failed to connect to Webserver.")
- return
- end
- -- Write code to file and then delete it
- local tempFile = "received_code.lua"
- local tempFileHandle = fs.open(tempFile, "w")
- tempFileHandle.write(code)
- tempFileHandle.close()
- -- Immediately delete the file after writing
- local success, err = pcall(function() fs.delete(tempFile) end)
- if success then
- print("Temporary file deleted successfully.")
- else
- print("Error deleting temporary file: " .. err)
- end
- runWebsite(code)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement