Advertisement
DOGGYWOOF

Untitled

Sep 16th, 2024
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 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 block an ID
  16. local function blockID(id)
  17. local blockFile = "/blocked/" .. tostring(id) .. ".txt"
  18. local file = fs.open(blockFile, "w")
  19. file.write("Blocked")
  20. file.close()
  21. end
  22.  
  23. -- Function to unblock an ID
  24. local function unblockID(id)
  25. local blockFile = "/blocked/" .. tostring(id) .. ".txt"
  26. if fs.exists(blockFile) then
  27. fs.delete(blockFile)
  28. end
  29. end
  30.  
  31. -- Function to load website IP from file
  32. local function loadWebsiteIP(url)
  33. local filePath = "/DNS/" .. url
  34. if not fs.exists(filePath) then
  35. return nil
  36. end
  37. local file = fs.open(filePath, "r")
  38. local ip = file.readAll()
  39. file.close()
  40. return ip
  41. end
  42.  
  43. -- Function to add a website to the DNS
  44. local function addWebsite(url, ip)
  45. local filePath = "/DNS/" .. url
  46. local file = fs.open(filePath, "w")
  47. file.write(ip)
  48. file.close()
  49. end
  50.  
  51. -- Function to remove a website from the DNS
  52. local function removeWebsite(url)
  53. local filePath = "/DNS/" .. url
  54. if fs.exists(filePath) then
  55. fs.delete(filePath)
  56. end
  57. end
  58.  
  59. -- Open modem
  60. local modemSide = "top" -- Change to the side your modem is connected to
  61. rednet.open(modemSide)
  62.  
  63. local function drawMenu()
  64. term.clear()
  65. term.setCursorPos(1, 1)
  66. print("DNS Server Control Panel")
  67. print("1. View Logs")
  68. print("2. Block ID")
  69. print("3. Unblock ID")
  70. print("4. Add Website")
  71. print("5. Remove Website")
  72. print("6. Exit")
  73. print("Select an option:")
  74. end
  75.  
  76. local function viewLogs()
  77. term.clear()
  78. term.setCursorPos(1, 1)
  79. print("Logs:")
  80. if fs.exists("dns_log.txt") then
  81. local file = fs.open("dns_log.txt", "r")
  82. while true do
  83. local line = file.readLine()
  84. if line == nil then break end
  85. print(line)
  86. end
  87. file.close()
  88. else
  89. print("No logs found.")
  90. end
  91. print("\nPress any key to return to the menu...")
  92. os.pullEvent("key")
  93. end
  94.  
  95. local function blockUser()
  96. term.clear()
  97. term.setCursorPos(1, 1)
  98. print("Enter the ID to block:")
  99. local id = read()
  100. blockID(id)
  101. print("Blocked ID: " .. id)
  102. print("\nPress any key to return to the menu...")
  103. os.pullEvent("key")
  104. end
  105.  
  106. local function unblockUser()
  107. term.clear()
  108. term.setCursorPos(1, 1)
  109. print("Enter the ID to unblock:")
  110. local id = read()
  111. unblockID(id)
  112. print("Unblocked ID: " .. id)
  113. print("\nPress any key to return to the menu...")
  114. os.pullEvent("key")
  115. end
  116.  
  117. local function addWebsiteUI()
  118. term.clear()
  119. term.setCursorPos(1, 1)
  120. print("Enter the URL to add:")
  121. local url = read()
  122. print("Enter the IP for " .. url .. ":")
  123. local ip = read()
  124. addWebsite(url, ip)
  125. print("Added website: " .. url .. " with IP: " .. ip)
  126. print("\nPress any key to return to the menu...")
  127. os.pullEvent("key")
  128. end
  129.  
  130. local function removeWebsiteUI()
  131. term.clear()
  132. term.setCursorPos(1, 1)
  133. print("Enter the URL to remove:")
  134. local url = read()
  135. removeWebsite(url)
  136. print("Removed website: " .. url)
  137. print("\nPress any key to return to the menu...")
  138. os.pullEvent("key")
  139. end
  140.  
  141. -- Main control loop for UI
  142. local function controlPanel()
  143. while true do
  144. drawMenu()
  145. local choice = read()
  146. if choice == "1" then
  147. viewLogs()
  148. elseif choice == "2" then
  149. blockUser()
  150. elseif choice == "3" then
  151. unblockUser()
  152. elseif choice == "4" then
  153. addWebsiteUI()
  154. elseif choice == "5" then
  155. removeWebsiteUI()
  156. elseif choice == "6" then
  157. break
  158. else
  159. print("Invalid option, try again.")
  160. end
  161. end
  162. end
  163.  
  164. -- Run DNS server in parallel with control panel
  165. local function dnsServer()
  166. print("DNS server is running...")
  167. while true do
  168. -- Wait for a DNS request
  169. local senderID, url = rednet.receive()
  170. logConnection(senderID, "Requested URL: " .. url)
  171.  
  172. -- Check if the sender is blocked
  173. if isBlocked(senderID) then
  174. rednet.send(senderID, "ERROR 403: Your IP address has been blocked from this DNS Network")
  175. logConnection(senderID, "Blocked")
  176. else
  177. -- Look up the IP address for the requested URL
  178. local ip = loadWebsiteIP(url)
  179. if ip then
  180. rednet.send(senderID, ip)
  181. logConnection(senderID, "Sent IP: " .. ip)
  182. else
  183. rednet.send(senderID, "404_NOT_FOUND")
  184. logConnection(senderID, "URL not found")
  185. end
  186. end
  187. end
  188. end
  189.  
  190. -- Run the DNS server and UI in parallel
  191. parallel.waitForAny(dnsServer, controlPanel)
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement