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