Advertisement
cynagen

dnsserver

Aug 19th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.17 KB | None | 0 0
  1. --DNS Server by Cynagen (for CC1.6+)
  2.  
  3. --Settings:
  4. --Honor zone transfer requests (default: false) (Setting: true = Permit this instance of dnsserver to share records in bulk)
  5. local allowZoneTransfers=false
  6. --Only transfer authorized records (default: true) (Setting: false = Permit the sharing of records which were copied from other servers)
  7. local onlyAuthZone=true
  8.  
  9. --Find and open all modems
  10. for _,v in ipairs(peripheral.getNames()) do
  11.   if peripheral.getType(v) == "modem" then
  12.     rednet.open(v)
  13.   end
  14. end
  15.  
  16. --Configuring static host records; String(hostname)={Boolean(local),Int(computerID)}
  17. hosts={}
  18. if fs.exists("data/hosts") then
  19.   file=fs.open("data/hosts","r")
  20.   if file then
  21.     write("Importing records...")
  22.     imports=0
  23.     line=""
  24.     while true do
  25.       line=file.readLine()
  26.       if not line then break end
  27.       splitAt=string.find(line,":")
  28.       if string.len(line) > 0 and splitAt > 1 then
  29.         hosts[tostring(string.sub(line,0,splitAt-1))]={true,tonumber(string.sub(line,splitAt+1))}
  30.         imports=imports+1
  31.       end
  32.     end
  33.     print("imported "..imports.." records")
  34.   end
  35.   file.close()
  36. end
  37.  
  38. --Checking for any other root DNS servers
  39. write("Searching for other DNS servers... ")
  40. rednet.broadcast({root=true},"dns")
  41. servers={}
  42. id,msg = rednet.receive("dns",2)
  43. while id do
  44.   write(id.." ")
  45.   if type(msg) == "table" and msg["root"] == true then servers[#servers+1]=id end
  46.   id,msg = rednet.receive("dns",2)
  47. end
  48. print()
  49.  
  50. --Scanning other DNS servers for additional records
  51. for _,v in ipairs(servers) do
  52.   rednet.send(v,{zonetrans="request"},"dns")
  53.   write("Polling server @ "..v)
  54.   id,msg = rednet.receive("dns",2)
  55.   if id == v and type(msg) == "table" then
  56.     recvRecords=0
  57.     if type(msg.zonetrans) == "table" then
  58.       for hname,htable in pairs(msg.zonetrans) do
  59.         if not hosts[hname] then
  60.           htable[1]=false --Flip authoritative flag to "no" since we received a copy of this record
  61.           hosts[hname]=htable --Inject into hosts table
  62.           recvRecords=recvRecords+1
  63.         end
  64.       end
  65.     end
  66.     print(" - Received "..recvRecords.." records")
  67.   end
  68.   sleep(1)
  69. end
  70.  
  71. --Daemon loop, listen to rednet, etc, etc...
  72. while true do
  73.   term.clear()
  74.   term.setCursorPos(1,1)
  75.   hostcount=0
  76.   for _,_ in pairs(hosts) do hostcount=hostcount+1 end
  77.   term.setTextColor(colors.green)
  78.   print("Known hosts: "..hostcount)
  79.   if hostcount > 0 then
  80.     hostTable={}
  81.     hostTable[#hostTable+1]=colors.white
  82.     hostTable[#hostTable+1]={"Hostname","Address","Static"}
  83.     i = 1
  84.     for name,record in pairs(hosts) do
  85.       if i == 1 then
  86.         hostTable[#hostTable+1]=colors.lightGray
  87.         i = 2
  88.       else
  89.         hostTable[#hostTable+1]=colors.gray
  90.         i = 1
  91.       end
  92.       hostTable[#hostTable+1]={name,record[2],tostring(record[1])}
  93.     end
  94.     i = nil
  95.     textutils.tabulate(unpack(hostTable))
  96.   end
  97.   id,msg = rednet.receive("dns",1)
  98.   if id and type(msg) == "table" then
  99.     if msg.query then
  100.       if not hosts[msg.query.host] then
  101.         rednet.send(id,{query=false},"dns")
  102.       else
  103.         rednet.send(id,{query=hosts[msg.query.host]},"dns")
  104.       end
  105.     elseif msg.create then
  106.       if hosts[msg.create.host] then
  107.         rednet.send(id,{create=false},"dns")
  108.       else
  109.         hosts[msg.create.host] = {false,id}
  110.         rednet.send(id,{create=hosts[msg.create.host]},"dns")
  111.       end
  112.     elseif msg.remove then
  113.       if type(hosts[msg.remove.host]) ~= "table" then
  114.         rednet.send(id,{remove=false},"dns")
  115.       elseif hosts[msg.remove.host][1] or hosts[msg.remove.host][2] ~= id then
  116.         rednet.send(id,{remove=false},"dns")
  117.       else
  118.         hosts[msg.remove.host]=nil
  119.         rednet.send(id,{remove={}},"dns")
  120.       end
  121.     elseif msg.zonetrans == "request" then
  122.       if not allowZoneTransfers then
  123.         rednet.send(id,{zonetrans=false},"dns")
  124.       else
  125.         if not onlyAuthZone then
  126.           rednet.send(id,{zonetrans=hosts},"dns")
  127.         else
  128.           transfer={}
  129.           for hostname,record in pairs(hosts) do
  130.             if record[1] then transfer[hostname]=record end
  131.           end
  132.           rednet.send(id,{zonetrans=transfer},"dns")
  133.         end
  134.       end
  135.     elseif msg.root then
  136.       rednet.send(id,{root=true},"dns")
  137.     end
  138.   end
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement