Advertisement
DOGGYWOOF

Secure DNS

Jul 26th, 2024 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. -- Function to log connections
  2. local function logConnection(id, message)
  3. local logFile = "dns_log.txt"
  4. local file = fs.open(logFile, "a")
  5. file.writeLine("ID: " .. id .. " - " .. message .. " - " .. textutils.formatTime(os.time(), true))
  6. file.close()
  7. end
  8.  
  9. -- Function to check if an ID is blocked
  10. local function isBlocked(id)
  11. local blockFile = "/blocked/" .. tostring(id) .. ".txt"
  12. return fs.exists(blockFile)
  13. end
  14.  
  15. -- Function to load website IP from file
  16. local function loadWebsiteIP(url)
  17. local filePath = "/DNS/" .. url
  18. if not fs.exists(filePath) then
  19. return nil
  20. end
  21. local file = fs.open(filePath, "r")
  22. local ip = file.readAll()
  23. file.close()
  24. return ip
  25. end
  26.  
  27. -- Function to authenticate request
  28. local function authenticateRequest(token)
  29. local validToken = "YOUR_SECURE_TOKEN" -- Replace with your secure token
  30. return token == validToken
  31. end
  32.  
  33. -- Open modem
  34. local modemSide = "back" -- Change to the side your modem is connected to
  35. rednet.open(modemSide)
  36.  
  37. print("DNS server is running...")
  38.  
  39. while true do
  40. -- Wait for a DNS request
  41. local senderID, request = rednet.receive()
  42. logConnection(senderID, "Requested: " .. request)
  43.  
  44. -- Extract token and URL from the request
  45. local token, url = string.match(request, "([^:]+):(.+)")
  46.  
  47. -- Validate and authenticate request
  48. if not authenticateRequest(token) then
  49. rednet.send(senderID, "ERROR 403: Unauthorized Access")
  50. logConnection(senderID, "Unauthorized access")
  51. elseif isBlocked(senderID) then
  52. rednet.send(senderID, "ERROR 403: Your IP address has been blocked from this DNS Network")
  53. logConnection(senderID, "Blocked")
  54. else
  55. -- Look up the IP address for the requested URL
  56. local ip = loadWebsiteIP(url)
  57. if ip then
  58. rednet.send(senderID, ip)
  59. logConnection(senderID, "Sent IP: " .. ip)
  60. else
  61. rednet.send(senderID, "404_NOT_FOUND")
  62. logConnection(senderID, "URL not found")
  63. end
  64. end
  65. end
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement