Advertisement
DOGGYWOOF

Untitled

Sep 12th, 2024 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. -- Function to draw the title bar with "DNS Server"
  2. local function drawTitleBar()
  3. term.setBackgroundColor(colors.blue)
  4. term.setTextColor(colors.white)
  5. term.setCursorPos(1, 1)
  6. term.write("DNS Server")
  7. term.setBackgroundColor(colors.black)
  8. term.setTextColor(colors.white)
  9. end
  10.  
  11. -- Function to load DNS mappings from the /DNS/URLS/ directory
  12. local function loadDNS()
  13. local dns = {}
  14. if fs.exists("/DNS/URLS") then
  15. for _, file in ipairs(fs.list("/DNS/URLS")) do
  16. local f = fs.open("/DNS/URLS/" .. file, "r")
  17. local ip = f.readAll()
  18. f.close()
  19. dns[file] = ip
  20. end
  21. else
  22. fs.makeDir("/DNS/URLS") -- Create the directory if it doesn't exist
  23. end
  24. return dns
  25. end
  26.  
  27. -- Function to log DNS events
  28. local function logConnection(message)
  29. local logFile = fs.open("dns_log.txt", "a")
  30. logFile.writeLine(message)
  31. logFile.close()
  32. print(message)
  33. end
  34.  
  35. -- Function to prompt for and verify the password
  36. local function promptForPassword()
  37. term.clear()
  38. drawTitleBar()
  39. print("Enter DNS Password:")
  40. return read("*") -- Mask the input for security
  41. end
  42.  
  43. -- Function to authenticate password
  44. local function authenticate(password)
  45. local filePath = "/DNS/locked.cfg"
  46. if fs.exists(filePath) then
  47. local file = fs.open(filePath, "r")
  48. local storedPassword = file.readAll()
  49. file.close()
  50. return password == storedPassword
  51. end
  52. return true -- No password required if file does not exist
  53. end
  54.  
  55. -- Function to display and manage DNS settings
  56. local function dnsSettingsMenu()
  57. while true do
  58. term.clear()
  59. drawTitleBar()
  60. print("DNS Settings Menu:")
  61. print("1. View DNS Mappings")
  62. print("2. Manage DNS Mappings")
  63. print("3. Change Password")
  64. print("4. Back to Main Menu")
  65.  
  66. local choice = tonumber(read())
  67. if choice == 1 then
  68. -- View DNS Mappings
  69. term.clear()
  70. drawTitleBar()
  71. print("Current DNS Mappings:")
  72. local dns = loadDNS()
  73. if next(dns) == nil then
  74. print("No DNS mappings found.")
  75. else
  76. for url, ip in pairs(dns) do
  77. print(url .. " -> " .. ip)
  78. end
  79. end
  80. print("Press any key to return to the menu.")
  81. os.pullEvent("key")
  82. elseif choice == 2 then
  83. -- Manage DNS Mappings
  84. term.clear()
  85. drawTitleBar()
  86. print("Manage DNS Mappings:")
  87. print("1. Add DNS Mapping")
  88. print("2. Delete DNS Mapping")
  89. print("3. Back to DNS Settings Menu")
  90.  
  91. local subChoice = tonumber(read())
  92. if subChoice == 1 then
  93. -- Add DNS Mapping
  94. term.clear()
  95. drawTitleBar()
  96. print("Enter URL to map:")
  97. local url = read()
  98. print("Enter IP address for " .. url .. ":")
  99. local ip = read()
  100.  
  101. if url ~= "" and ip ~= "" then
  102. local file = fs.open("/DNS/URLS/" .. url, "w")
  103. file.write(ip)
  104. file.close()
  105. print("Mapping added successfully.")
  106. else
  107. print("Invalid input. Mapping not added.")
  108. end
  109. print("Press any key to return to the menu.")
  110. os.pullEvent("key")
  111. elseif subChoice == 2 then
  112. -- Delete DNS Mapping
  113. term.clear()
  114. drawTitleBar()
  115. print("Enter URL to delete:")
  116. local url = read()
  117.  
  118. local filePath = "/DNS/URLS/" .. url
  119. if fs.exists(filePath) then
  120. fs.delete(filePath)
  121. print("Mapping deleted successfully.")
  122. else
  123. print("URL not found. Mapping not deleted.")
  124. end
  125. print("Press any key to return to the menu.")
  126. os.pullEvent("key")
  127. elseif subChoice == 3 then
  128. -- Return to DNS Settings Menu
  129. break
  130. else
  131. print("Invalid option. Press any key to return to the menu.")
  132. os.pullEvent("key")
  133. end
  134. elseif choice == 3 then
  135. -- Change Password
  136. term.clear()
  137. drawTitleBar()
  138. print("Enter new DNS password (leave blank to remove password):")
  139. local newPassword = read("*")
  140. local filePath = "/DNS/locked.cfg"
  141. if newPassword == "" then
  142. if fs.exists(filePath) then
  143. fs.delete(filePath)
  144. print("Password removed.")
  145. else
  146. print("No password set.")
  147. end
  148. else
  149. local file = fs.open(filePath, "w")
  150. file.write(newPassword)
  151. file.close()
  152. print("Password updated.")
  153. end
  154. print("Press any key to return to the menu.")
  155. os.pullEvent("key")
  156. elseif choice == 4 then
  157. -- Return to main DNS request handling
  158. break
  159. else
  160. print("Invalid option. Press any key to return to the menu.")
  161. os.pullEvent("key")
  162. end
  163. end
  164. end
  165.  
  166. -- Function to handle incoming DNS requests
  167. local function handleRequests()
  168. rednet.open("top") -- Assuming the modem is on the top
  169. print("DNS Server running... Press F1 for settings.")
  170.  
  171. while true do
  172. local event, param1, param2 = os.pullEvent()
  173.  
  174. if event == "rednet_message" then
  175. local senderID, url = param1, param2
  176. local dns = loadDNS()
  177. local ip = dns[url]
  178. logConnection("Received request for URL: " .. url)
  179.  
  180. if not ip then
  181. rednet.send(senderID, "404_NOT_FOUND")
  182. logConnection("URL not found: " .. url)
  183. else
  184. rednet.send(senderID, ip)
  185. logConnection("URL resolved to IP: " .. ip)
  186. end
  187.  
  188. elseif event == "key" and param1 == keys.f1 then
  189. -- Open DNS settings menu when F1 is pressed
  190. dnsSettingsMenu()
  191. -- Redisplay server status after returning from the menu
  192. term.clear()
  193. drawTitleBar()
  194. print("DNS Server running... Press F1 for settings.")
  195. end
  196. end
  197. end
  198.  
  199. -- Main function to run the DNS server
  200. local function main()
  201. handleRequests()
  202. end
  203.  
  204. main()
  205.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement