Advertisement
DOGGYWOOF

Untitled

Sep 13th, 2024 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. -- Function to check if DNS is locked by the existence of the lock file
  2. local function isLocked()
  3. return fs.exists("/DNS/locked.cfg")
  4. end
  5.  
  6. -- Function to get the correct password from a file (stored on DNS side)
  7. local function getPassword()
  8. if not fs.exists("/DNS/password.txt") then
  9. return nil -- If no password is set, return nil
  10. end
  11. local file = fs.open("/DNS/password.txt", "r")
  12. local password = file.readAll()
  13. file.close()
  14. return password
  15. end
  16.  
  17. -- Function to load URL mappings from file
  18. local function loadMappings()
  19. local mappings = {}
  20. if fs.exists("/DNS/URLS/") then
  21. for _, file in ipairs(fs.list("/DNS/URLS/")) do
  22. local f = fs.open("/DNS/URLS/" .. file, "r")
  23. local ip = f.readAll()
  24. f.close()
  25. mappings[file] = ip
  26. end
  27. end
  28. return mappings
  29. end
  30.  
  31. -- Function to handle incoming requests
  32. local function handleRequest(senderID, message)
  33. local url = message.url
  34. local password = message.password
  35.  
  36. -- Check if DNS is locked and a password is required
  37. if isLocked() then
  38. local correctPassword = getPassword()
  39. if password ~= correctPassword then
  40. rednet.send(senderID, "ACCESS_DENIED")
  41. return
  42. end
  43. end
  44.  
  45. local mappings = loadMappings()
  46. if mappings[url] then
  47. rednet.send(senderID, mappings[url])
  48. else
  49. rednet.send(senderID, "404_NOT_FOUND")
  50. end
  51. end
  52.  
  53. -- Function to add a new URL mapping
  54. local function addMapping(url, ip)
  55. local filePath = "/DNS/URLS/" .. url
  56. local file = fs.open(filePath, "w")
  57. file.write(ip)
  58. file.close()
  59. end
  60.  
  61. -- Function to delete a URL mapping
  62. local function deleteMapping(url)
  63. local filePath = "/DNS/URLS/" .. url
  64. if fs.exists(filePath) then
  65. fs.delete(filePath)
  66. else
  67. print("URL mapping does not exist.")
  68. end
  69. end
  70.  
  71. -- UI for managing mappings (add/delete)
  72. local function manageMappingsUI()
  73. while true do
  74. term.clear()
  75. print("Manage URL Mappings:")
  76. print("1. Add a new mapping")
  77. print("2. Delete an existing mapping")
  78. print("3. Exit")
  79. local choice = tonumber(read())
  80.  
  81. if choice == 1 then
  82. print("Enter URL:")
  83. local url = read()
  84. print("Enter IP address:")
  85. local ip = read()
  86. addMapping(url, ip)
  87. print("Mapping added successfully!")
  88. sleep(2)
  89. elseif choice == 2 then
  90. print("Enter URL to delete:")
  91. local url = read()
  92. deleteMapping(url)
  93. print("Mapping deleted successfully!")
  94. sleep(2)
  95. elseif choice == 3 then
  96. break
  97. else
  98. print("Invalid option. Try again.")
  99. sleep(2)
  100. end
  101. end
  102. end
  103.  
  104. -- Main loop to listen for DNS requests
  105. local function runDNS()
  106. -- Automatically find and open the first available modem
  107. local sides = {"top", "bottom", "left", "right", "front", "back"}
  108. for _, side in ipairs(sides) do
  109. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  110. rednet.open(side)
  111. print("Modem opened on side " .. side)
  112. break
  113. end
  114. end
  115.  
  116. print("DNS server is running... Press F1 for management.")
  117.  
  118. while true do
  119. local event = os.pullEvent()
  120.  
  121. -- Check if F1 is pressed to open the management UI
  122. if event == "key" and keys.getName(os.pullEvent("key")) == "f1" then
  123. manageMappingsUI()
  124. elseif event == "rednet_message" then
  125. local senderID, message = rednet.receive()
  126.  
  127. -- Check if the message is a table (should contain `url` and `password`)
  128. if type(message) == "table" and message.url and message.password then
  129. handleRequest(senderID, message)
  130. else
  131. print("Invalid message received from ID " .. senderID)
  132. rednet.send(senderID, "INVALID_REQUEST")
  133. end
  134. end
  135. end
  136. end
  137.  
  138. -- Run the DNS server
  139. runDNS()
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement