Advertisement
sammie287

DNSConnect2.0.lua

Aug 16th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.99 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 serverAddress = "9955471e-b28f-4001-9d66-79c28c75be2d"
  7. local modemStrength = 5000
  8. local fileName
  9. local computerName
  10. local sendSignal
  11. local serverResponse
  12. local GUID
  13. local chosenPort = 42
  14. local addComputerPort = 50
  15. local retrieveGUIDPort = 60
  16. local timeOut = 15
  17. local firstArray = {}
  18. local secondArray = {}
  19. local decision
  20. local filePath = "/home/"
  21.  
  22. local function OpenModem(port1, port2, port3, strength)
  23.   modem.open(port1)
  24.   modem.open(port2)
  25.   modem.open(port3)
  26.   modem.setStrength(strength)
  27.   if modem.isOpen(port1) and modem.isOpen(port2) and modem.isOpen(port3) then
  28.     print("Ports have been opened.")
  29.   else
  30.     print("Could not open ports, closing program.")
  31.   end
  32. end
  33.  
  34. local function Quit()
  35.   modem.close(42)
  36.   modem.close(50)
  37.   modem.close(60)
  38.   if modem.isOpen(42) == false and modem.isOpen(50) == false  and modem.isOpen(60) == false then
  39.     print("Ports have been closed. Exiting program.")
  40.     os.exit()
  41.   else
  42.     print("Could not close ports.")
  43.     os.exit()
  44.   end
  45. end
  46.  
  47. local function EventPull(timeOut)
  48.   local type, _, foreignAddress, port, distance, message = event.pull(timeOut)
  49.   local pullArray = {}
  50.   if type == "key_down" then
  51.     if keyboard.isAltDown() then
  52.       Quit()
  53.     end
  54.   elseif type == "modem_message" then
  55.     pullArray[1] = type
  56.     pullArray[2] = foreignAddress
  57.     pullArray[3] = port
  58.     pullArray[4] = distance
  59.     pullArray[5] = message
  60.     print("Message has been recieved from DNS on port " .. port)
  61.   end
  62.   return pullArray
  63. end  
  64.  
  65. local function SendACK(remoteAddress, port)
  66.   modem.send(remoteAddress, port, "ACK")
  67.   print("ACK has been sent to " .. remoteAddress .. " on port " .. port)
  68. end
  69.  
  70. local function SendTimeout(remoteAddress, port, message, timeOut)
  71.   local timeoutPull = {}
  72.   while true do
  73.     modem.send(remoteAddress, port, message)
  74.     timeoutPull = EventPull(timeOut)
  75.     if timeoutPull[1] ~= nil then
  76.       break
  77.     end
  78.   end
  79. end
  80.  
  81. local function AddComputer(name, serverAddress, port, timeOut)
  82.   SendTimeout(serverAddress, port, name, timeOut)
  83.   print("Computer has been successfully added to the DNS under the name " .. name)
  84. end
  85.  
  86. local function AddDashes(guid)
  87.   return guid:sub(1,8) .. "-" .. guid:sub(9,12) .. "-" .. guid:sub(13,16) .. "-" .. guid:sub(17,20) .. "-" .. guid:sub(21,34)
  88. end
  89.  
  90. local function RemoveDashes(guid)
  91.   return guid:sub(1,8) .. guid:sub(10,13) .. guid:sub(15,18) .. guid:sub(20,23) .. guid:sub(25,38)
  92. end
  93.  
  94. local function SaveID(GUID, filePath, fileName)
  95.   file = io.open(filePath .. fileName, "w")
  96.   file:write(GUID)
  97.   file:close()
  98.   print("GUID has been saved to " .. filePath .. fileName)
  99. end
  100.  
  101. OpenModem(chosenPort, addComputerPort, retrieveGUIDPort, modemStrength)
  102. print("Press ALT to exit.")
  103. while true do
  104.   print("Enter '1' to get a GUID or '2' to add this computer to the DNS.")
  105.   decision = tostring(io.read())
  106.   os.sleep(1)
  107.   if decision == "1" then
  108.     print("Enter the computer's name of the GUID you desire. The format is Name#, such as Matt1 or Luke2.")
  109.     fileName = io.read()
  110.     os.sleep(1)
  111.     SendTimeout(serverAddress, retrieveGUIDPort, fileName, timeOut)
  112.     firstArray = EventPull(timeOut)
  113.     SendACK(serverAddress, retrieveGUIDPort)
  114.     if tostring(firstArray[5]) ~= "500" then
  115.       GUID = firstArray[5]
  116.       print("The GUID of the requested computer is...")
  117.       print(GUID)
  118.       SaveID(GUID, filePath, fileName)
  119.       Quit()
  120.     else
  121.       print(fileName .. " does not exist as typed in the DNS server.")
  122.     end
  123.   elseif decision == "2" then
  124.     print("Type the name you want to give this computer. It should be your name with a number at the end, such as Luke1 or Luke2")
  125.     computerName = io.read()
  126.     os.sleep(1)
  127.     AddComputer(computerName, serverAddress, addComputerPort, timeOut)
  128.     Quit()
  129.   else
  130.     print("Not a decision, closing program.")
  131.   end
  132. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement