Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.58 KB | None | 0 0
  1. -- Derived from SuPeRMiNoR2's DNS program
  2. -- https://github.com/OpenPrograms/SuPeRMiNoR2-Programs/blob/master/networking/dns.lua
  3.  
  4. -- Config Options
  5. local port = 16
  6. local version = 1.0
  7.  
  8. local dnsDir = "/home/dns"
  9. local dnsFile = "dns.lua"
  10. local rdnsFile = "rdns.lua"
  11.  
  12. local antheus = require("antheus")
  13. local component = require("component")
  14. local event = require("event")
  15. local term = require("term")
  16. local fs = require("filesystem")
  17.  
  18. local modem = component.modem
  19.  
  20. -- Create the dns table
  21. dns = {}
  22.  
  23. -- Create the reverse dns table
  24. rdns = {}
  25.  
  26. -- Function to send messages
  27. function send(addr, data)
  28.     modem.send(addr, port, data)
  29. end
  30.  
  31. -- Function to register new things
  32. local 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)
  37. end
  38.  
  39. -- Stuff to lookup
  40.  
  41. function _lookup(name)
  42.     return dns[name]
  43. end
  44.  
  45. local function _rlookup(addr)
  46.     return rdns[addr]
  47. end
  48.  
  49. local function lookup(name)
  50.     result, addr = pcall(_lookup, name)
  51.     return addr
  52. end
  53.  
  54. local function rlookup(addr)
  55.     result, name = pcall(_rlookup, addr)
  56.     if result then return name else return false end
  57. end
  58.  
  59. -- Begin DNS Server
  60. term.clear()
  61. antheus.text.format.justify(1, "OpenDNS Server", "center")
  62. antheus.text.format.justify(2, "Version "..version, "center")
  63. term.setCursor(1, 3)
  64. antheus.text.hline("-")
  65. ::FCHK::
  66. print("Checking if DNS Directory Exists")
  67. if 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
  72. elseif fs.exists(dnsDir) == true then
  73.     print("Directory Exists")
  74. end
  75.  
  76. if 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
  87. else
  88.     antheus.error.notice("[WARN] No DNS File Found.")
  89. end
  90.  
  91. if 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
  101. else
  102.     antheus.error.notice("[WARN] No Reverse DNS File Found.")
  103. end
  104.  
  105. print("Starting Server Loop")
  106.  
  107. while 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
  129. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement