Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to log connections
- local function logConnection(id, message)
- local logFile = "dns_log.txt"
- local file = fs.open(logFile, "a")
- file.writeLine("ID: " .. id .. " - " .. message .. " - " .. textutils.formatTime(os.time(), true))
- file.close()
- end
- -- Function to check if an ID is blocked
- local function isBlocked(id)
- local blockFile = "/blocked/" .. tostring(id) .. ".txt"
- return fs.exists(blockFile)
- end
- -- Function to block an ID
- local function blockID(id)
- local blockFile = "/blocked/" .. tostring(id) .. ".txt"
- local file = fs.open(blockFile, "w")
- file.write("Blocked")
- file.close()
- end
- -- Function to unblock an ID
- local function unblockID(id)
- local blockFile = "/blocked/" .. tostring(id) .. ".txt"
- if fs.exists(blockFile) then
- fs.delete(blockFile)
- end
- end
- -- Function to load website IP from file
- local function loadWebsiteIP(url)
- local filePath = "/DNS/" .. url
- if not fs.exists(filePath) then
- return nil
- end
- local file = fs.open(filePath, "r")
- local ip = file.readAll()
- file.close()
- return ip
- end
- -- Function to add a website to the DNS
- local function addWebsite(url, ip)
- local filePath = "/DNS/" .. url
- local file = fs.open(filePath, "w")
- file.write(ip)
- file.close()
- end
- -- Function to remove a website from the DNS
- local function removeWebsite(url)
- local filePath = "/DNS/" .. url
- if fs.exists(filePath) then
- fs.delete(filePath)
- end
- end
- -- Open modem
- local modemSide = "top" -- Change to the side your modem is connected to
- rednet.open(modemSide)
- local function drawMenu()
- term.clear()
- term.setCursorPos(1, 1)
- print("DNS Server Control Panel")
- print("1. View Logs")
- print("2. Block ID")
- print("3. Unblock ID")
- print("4. Add Website")
- print("5. Remove Website")
- print("6. Exit")
- print("Select an option:")
- end
- local function viewLogs()
- term.clear()
- term.setCursorPos(1, 1)
- print("Logs:")
- if fs.exists("dns_log.txt") then
- local file = fs.open("dns_log.txt", "r")
- while true do
- local line = file.readLine()
- if line == nil then break end
- print(line)
- end
- file.close()
- else
- print("No logs found.")
- end
- print("\nPress any key to return to the menu...")
- os.pullEvent("key")
- end
- local function blockUser()
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter the ID to block:")
- local id = read()
- blockID(id)
- print("Blocked ID: " .. id)
- print("\nPress any key to return to the menu...")
- os.pullEvent("key")
- end
- local function unblockUser()
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter the ID to unblock:")
- local id = read()
- unblockID(id)
- print("Unblocked ID: " .. id)
- print("\nPress any key to return to the menu...")
- os.pullEvent("key")
- end
- local function addWebsiteUI()
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter the URL to add:")
- local url = read()
- print("Enter the IP for " .. url .. ":")
- local ip = read()
- addWebsite(url, ip)
- print("Added website: " .. url .. " with IP: " .. ip)
- print("\nPress any key to return to the menu...")
- os.pullEvent("key")
- end
- local function removeWebsiteUI()
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter the URL to remove:")
- local url = read()
- removeWebsite(url)
- print("Removed website: " .. url)
- print("\nPress any key to return to the menu...")
- os.pullEvent("key")
- end
- -- Main control loop for UI
- local function controlPanel()
- while true do
- drawMenu()
- local choice = read()
- if choice == "1" then
- viewLogs()
- elseif choice == "2" then
- blockUser()
- elseif choice == "3" then
- unblockUser()
- elseif choice == "4" then
- addWebsiteUI()
- elseif choice == "5" then
- removeWebsiteUI()
- elseif choice == "6" then
- break
- else
- print("Invalid option, try again.")
- end
- end
- end
- -- Run DNS server in parallel with control panel
- local function dnsServer()
- print("DNS server is running...")
- while true do
- -- Wait for a DNS request
- local senderID, url = rednet.receive()
- logConnection(senderID, "Requested URL: " .. url)
- -- Check if the sender is blocked
- if isBlocked(senderID) then
- rednet.send(senderID, "ERROR 403: Your IP address has been blocked from this DNS Network")
- logConnection(senderID, "Blocked")
- else
- -- Look up the IP address for the requested URL
- local ip = loadWebsiteIP(url)
- if ip then
- rednet.send(senderID, ip)
- logConnection(senderID, "Sent IP: " .. ip)
- else
- rednet.send(senderID, "404_NOT_FOUND")
- logConnection(senderID, "URL not found")
- end
- end
- end
- end
- -- Run the DNS server and UI in parallel
- parallel.waitForAny(dnsServer, controlPanel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement