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 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 authenticate request
- local function authenticateRequest(token)
- local validToken = "YOUR_SECURE_TOKEN" -- Replace with your secure token
- return token == validToken
- end
- -- Open modem
- local modemSide = "back" -- Change to the side your modem is connected to
- rednet.open(modemSide)
- print("DNS server is running...")
- while true do
- -- Wait for a DNS request
- local senderID, request = rednet.receive()
- logConnection(senderID, "Requested: " .. request)
- -- Extract token and URL from the request
- local token, url = string.match(request, "([^:]+):(.+)")
- -- Validate and authenticate request
- if not authenticateRequest(token) then
- rednet.send(senderID, "ERROR 403: Unauthorized Access")
- logConnection(senderID, "Unauthorized access")
- elseif 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement