· 6 years ago · Oct 19, 2019, 12:38 AM
1-- Derived from SuPeRMiNoR2's DNS program
2-- https://github.com/OpenPrograms/SuPeRMiNoR2-Programs/blob/master/networking/dns.lua
3
4-- Config Options
5local port = 16
6local version = 1.0
7
8local dnsDir = "/home/dns"
9local dnsFile = "dns.lua"
10local rdnsFile = "rdns.lua"
11
12local antheus = require("antheus")
13local component = require("component")
14local event = require("event")
15local term = require("term")
16local fs = require("filesystem")
17
18local modem = component.modem
19
20-- Create the dns table
21dns = {}
22
23-- Create the reverse dns table
24rdns = {}
25
26-- Function to send messages
27function send(addr, data)
28 modem.send(addr, port, data)
29end
30
31-- Function to register new things
32local function register(name, addr)
33 dns[name] = addr
34 rdns[addr] = name
35 antheus.file.write(dnsDir.."/"..dnsFile, antheus.util.encode(dns), true)
36 antheus.file.write(dnsDir.."/"..rdnsFile, antheus.util.encode(rdns), true)
37end
38
39-- Stuff to lookup
40
41function _lookup(name)
42 return dns[name]
43end
44
45local function _rlookup(addr)
46 return rdns[addr]
47end
48
49local function lookup(name)
50 result, addr = pcall(_lookup, name)
51 return addr
52end
53
54local function rlookup(addr)
55 result, name = pcall(_rlookup, addr)
56 if result then return name else return false end
57end
58
59-- Begin DNS Server
60term.clear()
61antheus.text.format.justify(1, "OpenDNS Server", "center")
62antheus.text.format.justify(2, "Version "..version, "center")
63term.setCursor(1, 3)
64antheus.text.hline("-")
65::FCHK::
66print("Checking if DNS Directory Exists")
67if fs.exists(dnsDir) ~= true then
68 antheus.error.notice("[WARN] DNS Directory Does Not Exist")
69 print("Attempting to Make Directory")
70 fs.makeDirectory(dnsDir)
71 goto FCHK
72elseif fs.exists(dnsDir) == true then
73 print("Directory Exists")
74end
75
76if fs.exists(dnsDir.."/"..dnsFile) == true then
77 result, tmp = antheus.util.decode(antheus.file.read(dnsDir.."/"..dnsFile))
78 if result and tmp ~= false then
79 print("Sucessfully Found DNS File")
80 dns = tmp
81 print("DNS File Contents:")
82 for k, v in pairs(dns) do
83 print(k, v)
84 end
85
86 end
87else
88 antheus.error.notice("[WARN] No DNS File Found.")
89end
90
91if fs.exists(dnsDir.."/"..rdnsFile) == true then
92 result, tmp = antheus.util.decode(antheus.file.read(dnsDir.."/"..rdnsFile))
93 if result and tmp ~= false then
94 print("Sucessfully Found Reverse DNS File")
95 rdns = tmp
96 print("Reverse DNS File Contents:")
97 for k, v in pairs(rdns) do
98 print(k, v)
99 end
100 end
101else
102 antheus.error.notice("[WARN] No Reverse DNS File Found.")
103end
104
105print("Starting Server Loop")
106
107while true do
108 modem.open(port)
109 local e, _, address, port, distance, message = event.pull("modem_message")
110 result, message = antheus.util.decode(message)
111 if result then
112 if message.action == "register" then
113 print("Registering "..message.name.." to "..address)
114 register(message.name, address)
115 end
116 if message.action == "lookup" then
117 n = lookup(message.name) or nil
118 print(address.." Looked Up "..message.name)
119 print("Response: "..n)
120 send(address, antheus.util.encode({action = "lookup", response = n}))
121 end
122 if message.action == "rlookup" then
123 a = rlookup(message.addr) or nil
124 print(address.." Reverse Looked Up "..message.addr)
125 print("Response: "..a)
126 send(address, antheus.util.encode({action = "rlookup", response = a}))
127 end
128 end
129end