SHOW:
|
|
- or go back to the newest paste.
1 | - | local fs = require "filesystem" |
1 | + | local fs = require "filesystem" |
2 | - | local text = require "text" |
2 | + | local text = require "text" |
3 | - | |
3 | + | |
4 | - | local dns = {} |
4 | + | local dns = {} |
5 | - | |
5 | + | |
6 | - | function dns.read_hosts(path) |
6 | + | function dns.read_hosts(path) |
7 | - | local file = io.open(path, "r") |
7 | + | local file = io.open(path, "r") |
8 | - | local t = {} |
8 | + | local t = {} |
9 | - | |
9 | + | |
10 | - | if not file then return nil end |
10 | + | if not file then return nil end |
11 | - | for line in file:lines() do |
11 | + | for line in file:lines() do |
12 | - | table.insert(t, line) |
12 | + | table.insert(t, line) |
13 | - | end |
13 | + | end |
14 | - | io.close(file) |
14 | + | io.close(file) |
15 | - | return t |
15 | + | return t |
16 | end | |
17 | - | |
17 | + | |
18 | - | function dns.get(path, name) |
18 | + | function dns.get(path, name) |
19 | - | local hosts = dns.read_hosts(path) |
19 | + | local hosts = dns.read_hosts(path) |
20 | - | |
20 | + | |
21 | - | if not hosts then return nil end |
21 | + | if not hosts then return nil end |
22 | - | |
22 | + | |
23 | - | for _,v in pairs(hosts) do |
23 | + | for _,v in pairs(hosts) do |
24 | - | local tab = text.tokenize(v) |
24 | + | local tab = text.tokenize(v) |
25 | - | if tab[2] == name then return tab[1] end |
25 | + | if tab[2] == name then return tab[1] end |
26 | - | end |
26 | + | end |
27 | - | return nil |
27 | + | return nil |
28 | end | |
29 | - | |
29 | + | |
30 | - | function dns.write_hosts(path, list) |
30 | + | function dns.write_hosts(path, list) |
31 | - | local file = io.open(path, "w") |
31 | + | local file = io.open(path, "w") |
32 | - | |
32 | + | |
33 | - | for _,v in pairs(list) do |
33 | + | for _,v in pairs(list) do |
34 | - | file:write(v) |
34 | + | file:write(v) |
35 | - | file:write("\n") |
35 | + | file:write("\n") |
36 | - | end |
36 | + | end |
37 | - | |
37 | + | |
38 | - | io.close(file) |
38 | + | io.close(file) |
39 | end | |
40 | - | |
40 | + | |
41 | - | function dns.unregister(path, hosts, addr) |
41 | + | function dns.unregister(path, hosts, addr) |
42 | - | local file = io.open(path, "w") |
42 | + | local file = io.open(path, "w") |
43 | - | |
43 | + | |
44 | - | for _,v in pairs(hosts) do |
44 | + | for _,v in pairs(hosts) do |
45 | - | if (text.tokenize(v))[1] ~= addr then |
45 | + | if (text.tokenize(v))[1] ~= addr then |
46 | - | file:write(v) |
46 | + | file:write(v) |
47 | - | file:write("\n") |
47 | + | file:write("\n") |
48 | - | end |
48 | + | end |
49 | - | end |
49 | + | end |
50 | - | io.close(file) |
50 | + | io.close(file) |
51 | end | |
52 | - | |
52 | + | |
53 | - | function dns.register(path, hosts, addr, name) |
53 | + | function dns.register(path, hosts, addr, name) |
54 | - | table.insert(hosts, addr .." ".. name) |
54 | + | table.insert(hosts, addr .." ".. name) |
55 | - | dns.write_hosts(path, hosts) |
55 | + | dns.write_hosts(path, hosts) |
56 | end | |
57 | - | |
57 | + | |
58 | - | function dns.check_errors(hosts, addr, name) |
58 | + | function dns.check_errors(hosts, addr, name) |
59 | - | if not addr then return "dns_addr_error" end |
59 | + | if not addr then return "dns_addr_error" end |
60 | - | if not name then return "dns_name_error" end |
60 | + | if not name then return "dns_name_error" end |
61 | - | for _,v in pairs(hosts) do |
61 | + | for _,v in pairs(hosts) do |
62 | - | v = text.tokenize(v) |
62 | + | v = text.tokenize(v) |
63 | - | if addr == v[1] then return "dns_addr_error" end |
63 | + | if addr == v[1] then return "dns_addr_error" end |
64 | - | if name == v[2] then return "dns_name_error" end |
64 | + | if name == v[2] then return "dns_name_error" end |
65 | - | end |
65 | + | end |
66 | - | return nil |
66 | + | return nil |
67 | end | |
68 | - | |
68 | + | |
69 | - | function dns.is_registered(hosts, addr) |
69 | + | function dns.is_registered(hosts, addr) |
70 | - | for _,v in pairs(hosts) do |
70 | + | for _,v in pairs(hosts) do |
71 | - | v = text.tokenize(v) |
71 | + | v = text.tokenize(v) |
72 | - | if addr == v[1] then return true end |
72 | + | if addr == v[1] then return true end |
73 | - | end |
73 | + | end |
74 | - | return false |
74 | + | return false |
75 | end | |
76 | - | |
76 | + | |
77 | - | -- [[ DNS DAEMON ]] |
77 | + | -- [[ DNS DAEMON ]] |
78 | - | |
78 | + | |
79 | - | local event = require "event" |
79 | + | local event = require "event" |
80 | - | local modem = require("component").modem |
80 | + | local modem = require("component").modem |
81 | - | |
81 | + | |
82 | - | local state = false |
82 | + | local state = false |
83 | - | local path = "/etc/hosts.dns" |
83 | + | local path = "/etc/hosts.dns" |
84 | - | |
84 | + | |
85 | - | local function callback(_, to, from, port, dist, msg, name) |
85 | + | local function callback(_, to, from, port, dist, msg, name) |
86 | - | if port == 2 then |
86 | + | if port == 2 then |
87 | - | local res = "dns_error" |
87 | + | local res = "dns_error" |
88 | - | if msg == "dns_update" then |
88 | + | if msg == "dns_update" then |
89 | - | res = dns.read_hosts(path) |
89 | + | res = dns.read_hosts(path) |
90 | - | res = table.concat(res, "\n") |
90 | + | res = table.concat(res, "\n") |
91 | - | elseif msg == "dns_register" then |
91 | + | elseif msg == "dns_register" then |
92 | - | local hosts = dns.read_hosts(path) |
92 | + | local hosts = dns.read_hosts(path) |
93 | - | local err = dns.check_errors(hosts, from, name) |
93 | + | local err = dns.check_errors(hosts, from, name) |
94 | - | if err then |
94 | + | if err then |
95 | - | res = err |
95 | + | res = err |
96 | - | else |
96 | + | else |
97 | - | dns.register(path, hosts, from, name) |
97 | + | dns.register(path, hosts, from, name) |
98 | - | res = "dns_register_ok" |
98 | + | res = "dns_register_ok" |
99 | - | end |
99 | + | end |
100 | - | elseif msg == "dns_unregister" then |
100 | + | elseif msg == "dns_unregister" then |
101 | - | local hosts = dns.read_hosts(path) |
101 | + | local hosts = dns.read_hosts(path) |
102 | - | if dns.is_registered(hosts, from) then |
102 | + | if dns.is_registered(hosts, from) then |
103 | - | dns.unregister(path, hosts, from) |
103 | + | dns.unregister(path, hosts, from) |
104 | - | res = "dns_unregister_ok" |
104 | + | res = "dns_unregister_ok" |
105 | - | else res = "dns_unregister_error" |
105 | + | else res = "dns_unregister_error" |
106 | - | end |
106 | + | end |
107 | - | end |
107 | + | end |
108 | - | modem.send(from, 2, res) |
108 | + | modem.send(from, 2, res) |
109 | - | end |
109 | + | end |
110 | end | |
111 | - | |
111 | + | |
112 | - | function start(_) |
112 | + | function start(_) |
113 | - | if not fs.exists(path) then io.close(io.open(path, "w")) end |
113 | + | if not fs.exists(path) then io.close(io.open(path, "w")) end |
114 | - | if state then |
114 | + | if state then |
115 | - | print("Server is already started.") |
115 | + | print("Server is already started.") |
116 | - | return |
116 | + | return |
117 | - | end |
117 | + | end |
118 | - | modem.open(2) |
118 | + | modem.open(2) |
119 | - | event.listen("modem_message", callback) |
119 | + | event.listen("modem_message", callback) |
120 | - | print("DNS Server started.") |
120 | + | print("DNS Server started.") |
121 | - | state = true |
121 | + | state = true |
122 | end | |
123 | - | |
123 | + | |
124 | - | function stop(_) |
124 | + | function stop(_) |
125 | - | if not state then |
125 | + | if not state then |
126 | - | print("Server is already stopped.") |
126 | + | print("Server is already stopped.") |
127 | - | return |
127 | + | return |
128 | - | end |
128 | + | end |
129 | - | modem.close(2) |
129 | + | modem.close(2) |
130 | - | event.ignore("modem_message", callback) |
130 | + | event.ignore("modem_message", callback) |
131 | - | print("DNS Server stopped.") |
131 | + | print("DNS Server stopped.") |
132 | - | state = false |
132 | + | state = false |
133 | end | |
134 | - | |
134 | + | |
135 | - | function status(_) |
135 | + | function status(_) |
136 | - | if state == true then |
136 | + | if state == true then |
137 | - | print("DNS Server is ON") |
137 | + | print("DNS Server is ON") |
138 | - | else |
138 | + | else |
139 | - | print("DNS Server is OFF") |
139 | + | print("DNS Server is OFF") |
140 | - | end |
140 | + | end |
141 | end |