Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to check if DNS is locked by the existence of the lock file
- local function isLocked()
- return fs.exists("/DNS/locked.cfg")
- end
- -- Function to get the correct password from a file (stored on DNS side)
- local function getPassword()
- if not fs.exists("/DNS/password.txt") then
- return nil -- If no password is set, return nil
- end
- local file = fs.open("/DNS/password.txt", "r")
- local password = file.readAll()
- file.close()
- return password
- end
- -- Function to load URL mappings from file
- local function loadMappings()
- local mappings = {}
- if fs.exists("/DNS/URLS/") then
- for _, file in ipairs(fs.list("/DNS/URLS/")) do
- local f = fs.open("/DNS/URLS/" .. file, "r")
- local ip = f.readAll()
- f.close()
- mappings[file] = ip
- end
- end
- return mappings
- end
- -- Function to handle incoming requests
- local function handleRequest(senderID, message)
- local url = message.url
- local password = message.password
- -- Check if DNS is locked and a password is required
- if isLocked() then
- local correctPassword = getPassword()
- if password ~= correctPassword then
- rednet.send(senderID, "ACCESS_DENIED")
- return
- end
- end
- local mappings = loadMappings()
- if mappings[url] then
- rednet.send(senderID, mappings[url])
- else
- rednet.send(senderID, "404_NOT_FOUND")
- end
- end
- -- Function to add a new URL mapping
- local function addMapping(url, ip)
- local filePath = "/DNS/URLS/" .. url
- local file = fs.open(filePath, "w")
- file.write(ip)
- file.close()
- end
- -- Function to delete a URL mapping
- local function deleteMapping(url)
- local filePath = "/DNS/URLS/" .. url
- if fs.exists(filePath) then
- fs.delete(filePath)
- else
- print("URL mapping does not exist.")
- end
- end
- -- UI for managing mappings (add/delete)
- local function manageMappingsUI()
- while true do
- term.clear()
- print("Manage URL Mappings:")
- print("1. Add a new mapping")
- print("2. Delete an existing mapping")
- print("3. Exit")
- local choice = tonumber(read())
- if choice == 1 then
- print("Enter URL:")
- local url = read()
- print("Enter IP address:")
- local ip = read()
- addMapping(url, ip)
- print("Mapping added successfully!")
- sleep(2)
- elseif choice == 2 then
- print("Enter URL to delete:")
- local url = read()
- deleteMapping(url)
- print("Mapping deleted successfully!")
- sleep(2)
- elseif choice == 3 then
- break
- else
- print("Invalid option. Try again.")
- sleep(2)
- end
- end
- end
- -- Main loop to listen for DNS requests
- local function runDNS()
- -- Automatically find and open the first available modem
- local sides = {"top", "bottom", "left", "right", "front", "back"}
- for _, side in ipairs(sides) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
- rednet.open(side)
- print("Modem opened on side " .. side)
- break
- end
- end
- print("DNS server is running... Press F1 for management.")
- while true do
- local event = os.pullEvent()
- -- Check if F1 is pressed to open the management UI
- if event == "key" and keys.getName(os.pullEvent("key")) == "f1" then
- manageMappingsUI()
- elseif event == "rednet_message" then
- local senderID, message = rednet.receive()
- -- Check if the message is a table (should contain `url` and `password`)
- if type(message) == "table" and message.url and message.password then
- handleRequest(senderID, message)
- else
- print("Invalid message received from ID " .. senderID)
- rednet.send(senderID, "INVALID_REQUEST")
- end
- end
- end
- end
- -- Run the DNS server
- runDNS()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement