Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local fs = require("filesystem")
- local modem = component.modem
- local keyboard = require("keyboard")
- local addressFile
- local addressFileString
- local addressFilePath = "/home/AddressDirectory"
- local addressDirectoryPath = "/home/Addresses/"
- local modemStrength
- local requestedGUID
- local wireless = true
- local timeOut = 15
- local chosenPort = 42
- local addComputerPort = 50
- local retrieveGUIDPort = 60
- local function IsWireless(wireless)
- local signal
- if wireless then
- signal = 5000
- else
- signal = 0
- end
- return signal
- end
- local function OpenModem(modemStrength, addComputerPort, retrieveGUIDPort, oldPort)
- modem.open(addComputerPort)
- modem.open(retrieveGUIDPort)
- modem.open(oldPort)
- modemStrength = IsWireless(wireless)
- modem.setStrength(modemStrength)
- print("Wireless modem strength has been set to " .. modemStrength)
- if modem.isOpen(oldPort) and modem.isOpen(addComputerPort) and modem.isOpen(retrieveGUIDPort) then
- print("Successfully opened ports.")
- else
- print("Could not open ports, exiting program...")
- os.exit()
- end
- end
- local function Quit() --need to add variables to this and not be a scrub and use numbers
- print("Closing ports.")
- modem.close(42)
- modem.close(50)
- modem.close(60)
- if modem.isOpen(50) == false and modem.isOpen(60) == false and modem.isOpen(42) == false then
- print("Successfully closed ports, shutting down server...")
- os.exit()
- else
- print("Ports could not close, contact admin to resolve security hole.")
- os.exit()
- end
- end
- local function OpenFile(addressFilePath)
- if fs.exists(addressFilePath) ~= true then
- addressFile = io.open(addressFilePath, "w")
- addressFile:close()
- print("DNS file not found, generating a new one.")
- end
- addressFile = io.open(addressFilePath, "r")
- fileSize, _ = addressFile:seek("end")
- addressFile:seek("set")
- addressFileString = addressFile:read(fileSize)
- if addressFileString == nil then
- addressFileString = ""
- end
- print("Address file string is " .. addressFileString)
- addressFile:close()
- print("Server address file loaded.")
- end
- local function SaveToFile(text, addressFilePath)
- newAddressFile = io.open(addressFilePath, "w")
- newAddressFile:write(text)
- newAddressFile:close()
- end
- local function CreateFile(fileName, filePath, GUID)
- file = io.open(filePath .. fileName, "w")
- file:write(GUID)
- file:close()
- print("New GUID file has been created with the filepath " .. filePath .. fileName)
- end
- local function RemoveDashes(guid)
- return guid:sub(1, 8) .. guid:sub(10, 13) .. guid:sub(15, 18) .. guid:sub(20, 23) .. guid:sub(25, 38)
- end
- local function AddDashes(guid)
- return guid:sub(1, 8) .. "-" .. guid:sub(9, 12) .. "-" .. guid:sub(13, 16) .. "-" .. guid:sub(17, 20) .. "-" .. guid:sub(21, 34)
- end
- local function EventPull(timeOut)
- local type, _, foreignAddress, port, distance, message = event.pull(timeOut)
- local pullArray = {}
- if type == "key_down" then
- if keyboard.isAltDown() then
- Quit()
- end
- elseif type == "modem_message" then
- pullArray[1] = type
- pullArray[2] = foreignAddress
- pullArray[3] = port
- pullArray[4] = distance
- pullArray[5] = message
- print("Message has been received from " .. pullArray[2] .. " on port " .. pullArray[3])
- end
- return pullArray
- end
- local function SendACK(remoteAddress, port)
- modem.send(remoteAddress, port, "ACK")
- print("ACK has been sent to " .. remoteAddress .. " on port " .. port)
- end
- local function SendTimeout(remoteAddress, port, message, timeOut)
- local timeoutPull = {}
- loopActive = true
- while loopActive do
- modem.send(remoteAddress, port, message)
- timeoutPull = EventPull(timeOut)
- if timeoutPull[1] ~= nil then
- loopActive = false
- end
- end
- end
- local function AddComputer(remoteAddress, port, addressFileString, addressFilePath, GUIDFilePath, computerName)
- SendACK(remoteAddress, port)
- addressFileString = addressFileString .. "\n" .. RemoveDashes(remoteAddress) .. " = " .. computerName
- SaveToFile(addressFileString, addressFilePath)
- print("Remote address: " .. remoteAddress .. " has been added under the name " .. computerName .. ".")
- local storedFormat = RemoveDashes(remoteAddress)
- CreateFile(computerName, GUIDFilePath, storedFormat)
- end
- local function RetrieveGUID(name, directoryPath)
- local path = directoryPath .. name
- if fs.exists(path) then
- local file = io.open(path, "r")
- local fileSize, _ = file:seek("end")
- file:seek("set")
- local fileContents = file:read(fileSize)
- file:close()
- return fileContents
- else
- return "fail"
- end
- end
- local function DataRead() --method is deprecated
- local continue = true
- while continue do
- local type, _, remoteAddress, port, _, message = event.pull()
- if type == "key_down" then
- continue = false
- elseif type == "modem_message" and port == 42 then
- print(message)
- end
- end
- end
- OpenModem(modemStrength, addComputerPort, retrieveGUIDPort, chosenPort)
- print("Press ALT to exit")
- OpenFile(addressFilePath)
- while true do
- local firstArray = EventPull(timeout)
- if firstArray[3] == retrieveGUIDPort then
- isMatching = addressFileString:match(RemoveDashes(firstArray[2]))
- if isMatching then
- SendACK(firstArray[2], firstArray[3])
- requestedGUID = RetrieveGUID(firstArray[5], addressDirectoryPath)
- if requestedGUID ~= "fail" then
- SendTimeout(firstArray[2], firstArray[3], AddDashes(requestedGUID), timeOut)
- print("GUID: " .. AddDashes(requestedGUID) .. " has been sent to " .. firstArray[2])
- else
- SendTimeout(firstArray[2], firstArray[3], "500", timeOut)
- print(firstArray[2] .. " attempted to query file " .. firstArray[5] .. ", which does not exist.")
- end
- end
- elseif firstArray[3] == addComputerPort then
- AddComputer(firstArray[2], firstArray[3], addressFileString, addressFilePath, addressDirectoryPath, firstArray[5])
- OpenFile(addressFilePath)
- else
- if firstArray[1] == "modem_message" then
- modem.send(firstArray[2], firstArray[3], "DNS does not recognize this port.")
- print("Receieved unauthorized request from " .. firstArray[2])
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement