Advertisement
DOGGYWOOF

Secure DNS

Sep 12th, 2024 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. -- Function to load DNS entries from the /DNS directory
  2. local function loadDNS()
  3. local dns = {}
  4. for _, file in ipairs(fs.list("/DNS")) do
  5. local f = fs.open("/DNS/" .. file, "r")
  6. local ip = f.readAll()
  7. f.close()
  8. dns[file] = ip
  9. end
  10. return dns
  11. end
  12.  
  13. -- Function to log DNS connections
  14. local function logConnection(url, ip)
  15. local logFile = fs.open("dns_log.txt", "a")
  16. logFile.writeLine("URL: " .. url .. ", IP: " .. ip .. ", Time: " .. textutils.formatTime(os.time(), true))
  17. logFile.close()
  18. end
  19.  
  20. -- Function to load the stored password from /DNS/locked.cfg
  21. local function loadPassword()
  22. local filePath = "/DNS/locked.cfg"
  23. if fs.exists(filePath) then
  24. local file = fs.open(filePath, "r")
  25. local password = file.readAll()
  26. file.close()
  27. return password
  28. end
  29. return nil -- No password file found
  30. end
  31.  
  32. -- Function to handle DNS requests
  33. local function handleDNSRequest(dns, senderID, url)
  34. local storedPassword = loadPassword()
  35. if storedPassword then
  36. rednet.send(senderID, "PASSWORD_REQUIRED") -- Ask for the password
  37.  
  38. local timer = os.startTimer(10) -- Wait for 10 seconds for a response
  39. while true do
  40. local event, id, message = os.pullEvent()
  41. if event == "rednet_message" and id == senderID then
  42. if message == storedPassword then
  43. if dns[url] then
  44. rednet.send(senderID, dns[url])
  45. logConnection(url, dns[url])
  46. else
  47. rednet.send(senderID, "404_NOT_FOUND")
  48. end
  49. else
  50. rednet.send(senderID, "AUTH_FAILED")
  51. end
  52. break
  53. elseif event == "timer" and id == timer then
  54. rednet.send(senderID, "TIMEOUT")
  55. break
  56. end
  57. end
  58. else
  59. if dns[url] then
  60. rednet.send(senderID, dns[url])
  61. logConnection(url, dns[url])
  62. else
  63. rednet.send(senderID, "404_NOT_FOUND")
  64. end
  65. end
  66. end
  67.  
  68. -- Automatically find and open the first available modem
  69. local function openModem()
  70. local sides = {"top", "bottom", "left", "right", "front", "back"}
  71. for _, side in ipairs(sides) do
  72. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  73. rednet.open(side)
  74. return side
  75. end
  76. end
  77. print("Error: No modem found.")
  78. return nil
  79. end
  80.  
  81. -- Main DNS server function
  82. local function runDNSServer()
  83. local modemSide = openModem()
  84. if not modemSide then return end
  85.  
  86. local dns = loadDNS()
  87. print("DNS Server is running on " .. modemSide .. " modem. Listening for requests...")
  88.  
  89. while true do
  90. local senderID, url = rednet.receive()
  91. handleDNSRequest(dns, senderID, url)
  92. end
  93. end
  94.  
  95. -- Main execution
  96. runDNSServer()
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement