Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --DNS Server by Cynagen (for CC1.6+)
- --Settings:
- --Honor zone transfer requests (default: false) (Setting: true = Permit this instance of dnsserver to share records in bulk)
- local allowZoneTransfers=false
- --Only transfer authorized records (default: true) (Setting: false = Permit the sharing of records which were copied from other servers)
- local onlyAuthZone=true
- --Find and open all modems
- for _,v in ipairs(peripheral.getNames()) do
- if peripheral.getType(v) == "modem" then
- rednet.open(v)
- end
- end
- --Configuring static host records; String(hostname)={Boolean(local),Int(computerID)}
- hosts={}
- if fs.exists("data/hosts") then
- file=fs.open("data/hosts","r")
- if file then
- write("Importing records...")
- imports=0
- line=""
- while true do
- line=file.readLine()
- if not line then break end
- splitAt=string.find(line,":")
- if string.len(line) > 0 and splitAt > 1 then
- hosts[tostring(string.sub(line,0,splitAt-1))]={true,tonumber(string.sub(line,splitAt+1))}
- imports=imports+1
- end
- end
- print("imported "..imports.." records")
- end
- file.close()
- end
- --Checking for any other root DNS servers
- write("Searching for other DNS servers... ")
- rednet.broadcast({root=true},"dns")
- servers={}
- id,msg = rednet.receive("dns",2)
- while id do
- write(id.." ")
- if type(msg) == "table" and msg["root"] == true then servers[#servers+1]=id end
- id,msg = rednet.receive("dns",2)
- end
- print()
- --Scanning other DNS servers for additional records
- for _,v in ipairs(servers) do
- rednet.send(v,{zonetrans="request"},"dns")
- write("Polling server @ "..v)
- id,msg = rednet.receive("dns",2)
- if id == v and type(msg) == "table" then
- recvRecords=0
- if type(msg.zonetrans) == "table" then
- for hname,htable in pairs(msg.zonetrans) do
- if not hosts[hname] then
- htable[1]=false --Flip authoritative flag to "no" since we received a copy of this record
- hosts[hname]=htable --Inject into hosts table
- recvRecords=recvRecords+1
- end
- end
- end
- print(" - Received "..recvRecords.." records")
- end
- sleep(1)
- end
- --Daemon loop, listen to rednet, etc, etc...
- while true do
- term.clear()
- term.setCursorPos(1,1)
- hostcount=0
- for _,_ in pairs(hosts) do hostcount=hostcount+1 end
- term.setTextColor(colors.green)
- print("Known hosts: "..hostcount)
- if hostcount > 0 then
- hostTable={}
- hostTable[#hostTable+1]=colors.white
- hostTable[#hostTable+1]={"Hostname","Address","Static"}
- i = 1
- for name,record in pairs(hosts) do
- if i == 1 then
- hostTable[#hostTable+1]=colors.lightGray
- i = 2
- else
- hostTable[#hostTable+1]=colors.gray
- i = 1
- end
- hostTable[#hostTable+1]={name,record[2],tostring(record[1])}
- end
- i = nil
- textutils.tabulate(unpack(hostTable))
- end
- id,msg = rednet.receive("dns",1)
- if id and type(msg) == "table" then
- if msg.query then
- if not hosts[msg.query.host] then
- rednet.send(id,{query=false},"dns")
- else
- rednet.send(id,{query=hosts[msg.query.host]},"dns")
- end
- elseif msg.create then
- if hosts[msg.create.host] then
- rednet.send(id,{create=false},"dns")
- else
- hosts[msg.create.host] = {false,id}
- rednet.send(id,{create=hosts[msg.create.host]},"dns")
- end
- elseif msg.remove then
- if type(hosts[msg.remove.host]) ~= "table" then
- rednet.send(id,{remove=false},"dns")
- elseif hosts[msg.remove.host][1] or hosts[msg.remove.host][2] ~= id then
- rednet.send(id,{remove=false},"dns")
- else
- hosts[msg.remove.host]=nil
- rednet.send(id,{remove={}},"dns")
- end
- elseif msg.zonetrans == "request" then
- if not allowZoneTransfers then
- rednet.send(id,{zonetrans=false},"dns")
- else
- if not onlyAuthZone then
- rednet.send(id,{zonetrans=hosts},"dns")
- else
- transfer={}
- for hostname,record in pairs(hosts) do
- if record[1] then transfer[hostname]=record end
- end
- rednet.send(id,{zonetrans=transfer},"dns")
- end
- end
- elseif msg.root then
- rednet.send(id,{root=true},"dns")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement