Advertisement
Guest User

DNS Server by PrinzJuliano

a guest
Nov 25th, 2015
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.56 KB | None | 0 0
  1. local component = require("component")
  2. local fs = component.filesystem
  3. local io = require("io")
  4. local modem = component.modem
  5. local ser = require("serialization")
  6.  
  7. local dnsPort = 12
  8. local fetchPort = 13
  9. local dnsList = {}
  10. local dnsPath = "/etc/dnsList.cfg"
  11. if not fs.exists(dnsPath) then
  12.   print("Creating DB")
  13.   local crate = io.open(dnsPath, "w")
  14.   crate:write("{}")
  15.   crate:close()
  16. end
  17. ------------------------------------------
  18.  
  19. function cls()
  20.   require("term").clear()
  21. end
  22.  
  23. function showInfo()
  24.   print("+----------------------------+")
  25.   print("| DNS Server by PrinzJuliano |")
  26.   print("+----------------------------+")
  27.   print("Starting DNS Server on port: "..dnsPort)
  28.   print("Reply channel is: "..fetchPort)
  29. end
  30.  
  31. function readDB()
  32.   local str = ""
  33.   local file = io.open(dnsPath, "r")
  34.   for line in file:lines() do
  35.     str = str .. line
  36.   end
  37.   dnsList = ser.unserialize(str)
  38. end
  39.  
  40. function addEntry(domain, addr, port)
  41.   local crate = {}
  42.   crate.addr = addr
  43.   crate.port = port
  44.   if dnsList[domain] == nil then
  45.     dnsList[domain] = crate
  46.     return true
  47.   else
  48.     return false
  49.   end
  50. end
  51.  
  52. function deleteEntry(domain)
  53.   dnsList:remove(domain)
  54. end
  55.  
  56. function writeDB()
  57.   local file = io.open(dnsPath, "w")
  58.   file:write(ser.serialize(dnsList))
  59.   file:close()
  60. end
  61.  
  62. showInfo()
  63. readDB()
  64. print("Add pjog once: " .. tostring(addEntry("pjog.de", "FICK DICH", -1)))
  65. print("Add pjog twice: " .. tostring(addEntry("pjog.de", "hallo, ich bin der julian", 5)))
  66.  
  67. for k,v in pairs(dnsList) do
  68.   print(k,v.addr, v.port)
  69. end
  70. writeDB()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement