abstract_abstract

dns_server.lua

Nov 6th, 2024 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 KB | None | 0 0
  1. shell.setPath(shell.path() .. ":/bin")
  2. rednet.open("right")
  3.  
  4. print("DNS server started")
  5.  
  6. local dnsTable = {}
  7.  
  8.  
  9. local function saveToFile(filename)
  10.     local file = fs.open(filename, 'w')
  11.     if file then
  12.         file.write(textutils.serialize(dnsTable))
  13.         file.close()
  14.         print("DNS table saved to " .. filename)
  15.     else
  16.         print("Error: unable to open file for saving")
  17.     end
  18. end
  19.  
  20.  
  21. local function loadFromFile(filename)
  22.     if fs.exists(filename) then
  23.         local file = fs.open(filename, 'r')
  24.         if file then
  25.             local content = file.readAll()
  26.             dnsTable = textutils.unserialise(content) or {}
  27.             file.close()
  28.             print("DNS table loaded from " .. filename)
  29.         else
  30.             print("Error: Unable to open file for loading")
  31.         end
  32.     else
  33.         print("No existing DNS table found, starting fresh")
  34.     end
  35. end
  36.  
  37.  
  38. local function registerComputer(name, id)    
  39.     dnsTable[id] = name
  40.    
  41.     print("Registation successful")
  42.     print("PC: " .. name .. " ID: " .. id)
  43.  
  44.     saveToFile("dns_table.txt")
  45.  
  46.     return true
  47. end
  48.  
  49.  
  50. local function getAllComputers()
  51.     return dnsTable
  52. end
  53.  
  54.  
  55. loadFromFile("dns_table.txt")
  56.  
  57.  
  58. while true do
  59.     local senderID, message, protocol = rednet.receive()
  60.     print("Something query")
  61.     if type(message) == "string" then
  62.         local data = textutils.unserialize(message)
  63.         if protocol == "dns_register" then
  64.             if data and data.name and data.id then
  65.                 registerComputer(data.name, data.id)                
  66.                 rednet.send(senderID, "Registation has been successful", "dns_ack")
  67.             end    
  68.         elseif protocol == "dns_list" then
  69.             print("Protocol dns_list")
  70.             local allComputers = getAllComputers()
  71.             local listMessage = textutils.serialize(allComputers)
  72.             rednet.send(senderID, listMessage, "dns_list_response")
  73.         end
  74.     elseif type(message) == "table" then
  75.         if protocol == "log" then
  76.             print("Protocol log")
  77.             local file = fs.open("teleport.log", "a")
  78.  
  79.             if file then
  80.                 file.write(senderID .. ";" ..message.id .. ";" .. message.name .. "\n")
  81.                 file.close()
  82.             else
  83.                 print("Something strange with file")
  84.             end
  85.         end
  86.     else
  87.         rednet.send(senderID, "something strange", "dns_list_response")
  88.     end
  89. end
  90.  
  91.  
Advertisement
Add Comment
Please, Sign In to add comment