Advertisement
sammie287

DNSProgram.lua

Aug 16th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.29 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local fs = require("filesystem")
  4. local modem = component.modem
  5. local keyboard = require("keyboard")
  6. local addressFile
  7. local addressFileString
  8. local addressFilePath = "/home/AddressDirectory"
  9. local addressDirectoryPath = "/home/Addresses/"
  10. local modemStrength
  11. local requestedGUID
  12. local wireless = true
  13. local timeOut = 15
  14. local chosenPort = 42
  15. local addComputerPort = 50
  16. local retrieveGUIDPort = 60
  17.  
  18. local function IsWireless(wireless)
  19.   local signal
  20.   if wireless then
  21.     signal = 5000
  22.   else
  23.     signal = 0
  24.   end
  25.   return signal
  26. end
  27.  
  28. local function OpenModem(modemStrength, addComputerPort, retrieveGUIDPort, oldPort)
  29.   modem.open(addComputerPort)
  30.   modem.open(retrieveGUIDPort)
  31.   modem.open(oldPort)
  32.   modemStrength = IsWireless(wireless)
  33.   modem.setStrength(modemStrength)
  34.   print("Wireless modem strength has been set to " .. modemStrength)
  35.   if modem.isOpen(oldPort) and modem.isOpen(addComputerPort) and modem.isOpen(retrieveGUIDPort) then
  36.     print("Successfully opened ports.")
  37.   else
  38.     print("Could not open ports, exiting program...")
  39.     os.exit()
  40.   end
  41. end
  42.  
  43. local function Quit() --need to add variables to this and not be a scrub and use numbers
  44.   print("Closing ports.")
  45.   modem.close(42)
  46.   modem.close(50)
  47.   modem.close(60)
  48.   if modem.isOpen(50) == false and modem.isOpen(60) == false and modem.isOpen(42) == false then
  49.     print("Successfully closed ports, shutting down server...")
  50.     os.exit()
  51.   else
  52.     print("Ports could not close, contact admin to resolve security hole.")
  53.     os.exit()
  54.   end
  55. end
  56.  
  57. local function OpenFile(addressFilePath)
  58.   if fs.exists(addressFilePath) ~= true then
  59.     addressFile = io.open(addressFilePath, "w")
  60.     addressFile:close()
  61.     print("DNS file not found, generating a new one.")
  62.   end
  63.   addressFile = io.open(addressFilePath, "r")
  64.   fileSize, _ = addressFile:seek("end")
  65.   addressFile:seek("set")
  66.   addressFileString = addressFile:read(fileSize)
  67.   if addressFileString == nil then
  68.     addressFileString = ""
  69.   end
  70.   print("Address file string is " .. addressFileString)
  71.   addressFile:close()
  72.   print("Server address file loaded.")
  73. end
  74.  
  75. local function SaveToFile(text, addressFilePath)
  76.   newAddressFile = io.open(addressFilePath, "w")
  77.   newAddressFile:write(text)
  78.   newAddressFile:close()
  79. end
  80.  
  81. local function CreateFile(fileName, filePath, GUID)
  82.   file = io.open(filePath .. fileName, "w")
  83.   file:write(GUID)
  84.   file:close()
  85.   print("New GUID file has been created with the filepath " .. filePath .. fileName)
  86. end
  87.  
  88. local function RemoveDashes(guid)
  89.   return guid:sub(1, 8) .. guid:sub(10, 13) .. guid:sub(15, 18) .. guid:sub(20, 23) .. guid:sub(25, 38)
  90. end
  91.  
  92. local function AddDashes(guid)
  93.   return guid:sub(1, 8) .. "-" .. guid:sub(9, 12) .. "-" .. guid:sub(13, 16) .. "-" .. guid:sub(17, 20) .. "-" .. guid:sub(21, 34)
  94. end
  95.  
  96. local function EventPull(timeOut)
  97.   local type, _, foreignAddress, port, distance, message = event.pull(timeOut)
  98.   local pullArray = {}
  99.   if type == "key_down" then
  100.     if keyboard.isAltDown() then
  101.       Quit()
  102.     end
  103.   elseif type == "modem_message" then
  104.     pullArray[1] = type
  105.     pullArray[2] = foreignAddress
  106.     pullArray[3] = port
  107.     pullArray[4] = distance
  108.     pullArray[5] = message
  109.     print("Message has been received from " .. pullArray[2] .. " on port " .. pullArray[3])
  110.   end
  111.   return pullArray
  112. end
  113.  
  114. local function SendACK(remoteAddress, port)
  115.   modem.send(remoteAddress, port, "ACK")
  116.   print("ACK has been sent to " .. remoteAddress .. " on port " .. port)
  117. end
  118.  
  119. local function SendTimeout(remoteAddress, port, message, timeOut)
  120.   local timeoutPull = {}
  121.   loopActive = true
  122.   while loopActive do
  123.     modem.send(remoteAddress, port, message)
  124.     timeoutPull = EventPull(timeOut)
  125.     if timeoutPull[1] ~= nil then
  126.       loopActive = false
  127.     end
  128.   end
  129. end
  130.  
  131. local function AddComputer(remoteAddress, port, addressFileString, addressFilePath, GUIDFilePath, computerName)
  132.   SendACK(remoteAddress, port)
  133.   addressFileString = addressFileString .. "\n" .. RemoveDashes(remoteAddress) .. " = " .. computerName
  134.   SaveToFile(addressFileString, addressFilePath)
  135.   print("Remote address: " .. remoteAddress .. " has been added under the name " .. computerName .. ".")
  136.   local storedFormat = RemoveDashes(remoteAddress)
  137.   CreateFile(computerName, GUIDFilePath, storedFormat)
  138. end
  139.  
  140. local function RetrieveGUID(name, directoryPath)
  141.   local path = directoryPath .. name
  142.   if fs.exists(path) then
  143.     local file = io.open(path, "r")
  144.     local fileSize, _ = file:seek("end")
  145.     file:seek("set")
  146.     local fileContents = file:read(fileSize)
  147.     file:close()
  148.     return fileContents
  149.   else
  150.     return "fail"
  151.   end
  152. end
  153.  
  154. local function DataRead() --method is deprecated
  155.   local continue = true
  156.   while continue do
  157.     local type, _, remoteAddress, port, _, message = event.pull()
  158.     if type == "key_down" then
  159.       continue = false
  160.     elseif type == "modem_message" and port == 42 then
  161.       print(message)
  162.     end
  163.   end
  164. end
  165.  
  166. OpenModem(modemStrength, addComputerPort, retrieveGUIDPort, chosenPort)
  167. print("Press ALT to exit")
  168. OpenFile(addressFilePath)
  169. while true do
  170.   local firstArray = EventPull(timeout)
  171.   if firstArray[3] == retrieveGUIDPort then
  172.     isMatching = addressFileString:match(RemoveDashes(firstArray[2]))
  173.     if isMatching then
  174.       SendACK(firstArray[2], firstArray[3])
  175.       requestedGUID = RetrieveGUID(firstArray[5], addressDirectoryPath)
  176.       if requestedGUID ~= "fail" then
  177.         SendTimeout(firstArray[2], firstArray[3], AddDashes(requestedGUID), timeOut)
  178.         print("GUID: " .. AddDashes(requestedGUID) .. " has been sent to " .. firstArray[2])
  179.       else
  180.         SendTimeout(firstArray[2], firstArray[3], "500", timeOut)
  181.         print(firstArray[2] .. " attempted to query file " .. firstArray[5] .. ", which does not exist.")
  182.       end
  183.     end
  184.   elseif firstArray[3] == addComputerPort then
  185.     AddComputer(firstArray[2], firstArray[3], addressFileString, addressFilePath, addressDirectoryPath, firstArray[5])
  186.     OpenFile(addressFilePath)    
  187.   else
  188.     if firstArray[1] == "modem_message" then
  189.       modem.send(firstArray[2], firstArray[3], "DNS does not recognize this port.")
  190.       print("Receieved unauthorized request from " .. firstArray[2])
  191.     end
  192.   end
  193. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement