Advertisement
DOGGYWOOF

Untitled

Sep 16th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 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. -- Open modem
  28. local modemSide = "top" -- Change to the side your modem is connected to
  29. rednet.open(modemSide)
  30.  
  31. print("DNS server is running...")
  32.  
  33. while true do
  34. -- Wait for a DNS request
  35. local senderID, url = rednet.receive()
  36. logConnection(senderID, "Requested URL: " .. url)
  37.  
  38. -- Check if the sender is blocked
  39. if isBlocked(senderID) then
  40. rednet.send(senderID, "ERROR 403: Your IP address has been blocked from this DNS Network")
  41. logConnection(senderID, "Blocked")
  42. else
  43. -- Look up the IP address for the requested URL
  44. local ip = loadWebsiteIP(url)
  45. if ip then
  46. rednet.send(senderID, ip)
  47. logConnection(senderID, "Sent IP: " .. ip)
  48. else
  49. rednet.send(senderID, "404_NOT_FOUND")
  50. logConnection(senderID, "URL not found")
  51. end
  52. end
  53. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement