Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local fs = require "filesystem"
- local text = require "text"
- local dns = {}
- function dns.read_hosts(path)
- local file = io.open(path, "r")
- local t = {}
- if not file then return nil end
- for line in file:lines() do
- table.insert(t, line)
- end
- io.close(file)
- return t
- end
- function dns.get(path, name)
- local hosts = dns.read_hosts(path)
- if not hosts then return nil end
- for _,v in pairs(hosts) do
- local tab = text.tokenize(v)
- if tab[2] == name then return tab[1] end
- end
- return nil
- end
- function dns.write_hosts(path, list)
- local file = io.open(path, "w")
- for _,v in pairs(list) do
- file:write(v)
- file:write("\n")
- end
- io.close(file)
- end
- function dns.unregister(path, hosts, addr)
- local file = io.open(path, "w")
- for _,v in pairs(hosts) do
- if (text.tokenize(v))[1] ~= addr then
- file:write(v)
- file:write("\n")
- end
- end
- io.close(file)
- end
- function dns.register(path, hosts, addr, name)
- table.insert(hosts, addr .." ".. name)
- dns.write_hosts(path, hosts)
- end
- function dns.check_errors(hosts, addr, name)
- if not addr then return "dns_addr_error" end
- if not name then return "dns_name_error" end
- for _,v in pairs(hosts) do
- v = text.tokenize(v)
- if addr == v[1] then return "dns_addr_error" end
- if name == v[2] then return "dns_name_error" end
- end
- return nil
- end
- function dns.is_registered(hosts, addr)
- for _,v in pairs(hosts) do
- v = text.tokenize(v)
- if addr == v[1] then return true end
- end
- return false
- end
- -- [[ DNS DAEMON ]]
- local event = require "event"
- local modem = require("component").modem
- local state = false
- local path = "/etc/hosts.dns"
- local function callback(_, to, from, port, dist, msg, name)
- if port == 2 then
- local res = "dns_error"
- if msg == "dns_update" then
- res = dns.read_hosts(path)
- res = table.concat(res, "\n")
- elseif msg == "dns_register" then
- local hosts = dns.read_hosts(path)
- local err = dns.check_errors(hosts, from, name)
- if err then
- res = err
- else
- dns.register(path, hosts, from, name)
- res = "dns_register_ok"
- end
- elseif msg == "dns_unregister" then
- local hosts = dns.read_hosts(path)
- if dns.is_registered(hosts, from) then
- dns.unregister(path, hosts, from)
- res = "dns_unregister_ok"
- else res = "dns_unregister_error"
- end
- end
- modem.send(from, 2, res)
- end
- end
- function start(_)
- if not fs.exists(path) then io.close(io.open(path, "w")) end
- if state then
- print("Server is already started.")
- return
- end
- modem.open(2)
- event.listen("modem_message", callback)
- print("DNS Server started.")
- state = true
- end
- function stop(_)
- if not state then
- print("Server is already stopped.")
- return
- end
- modem.close(2)
- event.ignore("modem_message", callback)
- print("DNS Server stopped.")
- state = false
- end
- function status(_)
- if state == true then
- print("DNS Server is ON")
- else
- print("DNS Server is OFF")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement