Advertisement
DOGGYWOOF

Untitled

Sep 12th, 2024
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. -- Function to load DNS records from files
  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 load stored passwords from file
  14. local function loadPasswords()
  15. local passwords = {}
  16. if fs.exists("passwords.txt") then
  17. local file = fs.open("passwords.txt", "r")
  18. for line in file.readLine do
  19. passwords[line] = true
  20. end
  21. file.close()
  22. end
  23. return passwords
  24. end
  25.  
  26. -- Function to load stored authorized computer IDs
  27. local function loadAuthorizedIDs()
  28. local ids = {}
  29. if fs.exists("authorized_ids.txt") then
  30. local file = fs.open("authorized_ids.txt", "r")
  31. for line in file.readLine do
  32. ids[line] = true
  33. end
  34. file.close()
  35. end
  36. return ids
  37. end
  38.  
  39. -- Function to save a new authorized computer ID
  40. local function saveAuthorizedID(id)
  41. local file = fs.open("authorized_ids.txt", "a")
  42. file.writeLine(id)
  43. file.close()
  44. end
  45.  
  46. -- Function to log DNS connections
  47. local function logConnection(url, ip)
  48. local logFile = fs.open("dns_log.txt", "a")
  49. local logMessage = "URL: " .. url .. ", IP: " .. (ip or "Not found")
  50. logFile.writeLine(logMessage)
  51. logFile.close()
  52. print(logMessage)
  53. end
  54.  
  55. -- Function to check if a computer ID is already authorized
  56. local function isAuthorizedID(id, ids)
  57. return ids[tostring(id)] ~= nil
  58. end
  59.  
  60. -- Function to authenticate a computer with a password
  61. local function authenticate(senderID, password)
  62. local authorizedPasswords = loadPasswords() -- Load passwords from file
  63. if authorizedPasswords[password] then
  64. saveAuthorizedID(senderID) -- Save computer ID if authentication is successful
  65. return true
  66. else
  67. return false
  68. end
  69. end
  70.  
  71. -- Main DNS Server Loop
  72. local authorizedIDs = loadAuthorizedIDs() -- Load authorized IDs
  73. rednet.open("top") -- Assuming the modem is on the top
  74. print("DNS Server running...")
  75.  
  76. while true do
  77. local senderID, urlOrPassword = rednet.receive()
  78. local dns = loadDNS()
  79.  
  80. if isAuthorizedID(senderID, authorizedIDs) then
  81. -- If the computer is already authorized, handle DNS request
  82. local ip = dns[urlOrPassword]
  83. logConnection(urlOrPassword, ip)
  84. if ip then
  85. rednet.send(senderID, ip)
  86. else
  87. rednet.send(senderID, "404_NOT_FOUND")
  88. end
  89. else
  90. -- If not authorized, require password
  91. rednet.send(senderID, "PASSWORD_REQUIRED")
  92.  
  93. local event, id, password = os.pullEvent("rednet_message")
  94. if id == senderID then
  95. if authenticate(senderID, password) then
  96. authorizedIDs[tostring(senderID)] = true -- Update the in-memory authorized IDs list
  97. rednet.send(senderID, "AUTH_SUCCESS")
  98. else
  99. rednet.send(senderID, "AUTH_FAILED")
  100. end
  101. end
  102. end
  103. end
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement