Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("top")
- term.clear()
- term.setCursorPos(1,1)
- passcode = "test"
- if not fs.exists("network") then
- fs.makeDir("network/devices")
- end
- function createDevice(name, dType, sender, cmds)
- commands = {}
- commands = cmds
- fs.makeDir("network/devices/"..name)
- file = fs.open("network/devices/"..name.."/dType","w")
- file.write(dType)
- file.close()
- file = fs.open("network/devices/"..name.."/commands","w")
- file.write(textutils.serialize(commands))
- file.close()
- file = fs.open("network/devices/"..name.."/id","w")
- file.write(sender)
- file.close()
- end
- function getDevices()
- list = fs.list("network/devices")
- return list
- end
- function getCommands(device)
- file = fs.open("network/devices/"..device.."/commands","r")
- returnCommands = file.readAll()
- file.close()
- return textutils.unserialize(returnCommands)
- end
- function getDType(device)
- file = fs.open("network/devices/"..device.."/dType","r")
- returnType = file.readAll()
- file.close()
- return returnType
- end
- function sendCommand(device,command, argumentList)
- commandList = getCommands(device)
- arguments = {}
- arguments = argumentList
- commandFound = false
- for i,v in pairs(commandList) do
- if v == command then
- commandFound = true
- end
- end
- if commandFound then
- file = fs.open("network/devices/"..device.."/id","r")
- target = file.readAll()
- file.close()
- target = target + 0
- fullCommand = {command,arguments}
- fullCommand = textutils.serialize(fullCommand)
- rednet.send(target, fullCommand)
- end
- end
- function getAllTypes()
- allTypes = {}
- allDevices = getDevices()
- for i,v in pairs(allDevices) do
- dType = getDType(v)
- inArray = false
- for i,v in pairs(allTypes) do
- if string.upper(v) == string.upper(dType) then
- inArray = true
- end
- end
- if not inArray then
- table.insert(allTypes, dType)
- end
- end
- return allTypes
- end
- function getAllOfType(dType)
- allOfType = {}
- allDevices = getDevices()
- for i,v in pairs(allDevices) do
- vdType = getDType(v)
- if string.upper(vdType) == string.upper(dType) then
- table.insert(allOfType, v)
- end
- end
- return allOfType
- end
- function checkForDevice(nDevice)
- if type(nDevice) == "string" and fs.exists("network/devices/"..nDevice) then
- return true
- else
- return false
- end
- end
- while true do
- id, msg, d = rednet.receive()
- if type(msg) ~= "table" then
- message = textutils.unserialize(msg)
- if type(message) ~= "nil" then
- if message[1] ~= nil then
- protocol = message[1]
- code = message[2]
- print("Protocol: "..protocol)
- if code == passcode then
- if protocol == "getDevices" then
- devices = getDevices()
- devices = textutils.serialize(devices)
- rednet.send(id, devices)
- print("DEVICES RETURNED")
- print("-----------------")
- elseif protocol == "getTypes" then
- dTypes = getAllTypes()
- dTypes = textutils.serialize(dTypes)
- rednet.send(id, dTypes)
- print("TYPES RETURNED")
- print("-----------------")
- elseif protocol == "getAllOfType" then
- deviceType = message[3]
- if type(deviceType) == "string" then
- print("Type: "..deviceType)
- devices = getAllOfType(deviceType)
- devices = textutils.serialize(devices)
- rednet.send(id, devices)
- print("DEVICES RETURNED")
- print("-----------------")
- else
- print("NETWORK MAY BE UNDER ATTACK: BAD TYPE")
- print("ATTACKER ID: "..id)
- print("-----------------")
- end
- elseif protocol == "getDeviceSpecs" then
- dev = message[3]
- if checkForDevice(dev) then
- print("Device Name: "..dev)
- commandList = getCommands(dev)
- print("Command List: "..textutils.serialize(commandList))
- dType = getDType(dev)
- print("Type: "..dType)
- specs = {dType,commandList}
- specs = textutils.serialize(specs)
- print("Specs: "..specs)
- rednet.send(id, specs)
- print("SPECS RETURNED")
- print("-----------------")
- else
- print("NETWORK MAY BE UNDER ATTACK: FALSE DEVICE")
- print("ATTACKER ID: "..id)
- print("FALSE DEVICE: "..textutils.serialize(dev))
- print("-----------------")
- end
- elseif protocol == "createDevice" then
- name = message[3]
- print("Name: "..name)
- dType = message[4]
- print("Type: "..dType)
- commands = message[5]
- print("Commands: "..textutils.serialize(commands))
- createDevice(name,dType,id,commands)
- print("DEVICE CREATED")
- print("-----------------")
- elseif protocol == "sendCommand" then
- device = message[3]
- if checkForDevice(device) then
- print("Device: "..device)
- if type(message[4]) == "string" and type(message[5]) == "table" then
- command = message[4]
- print("Command: "..command)
- params = message[5]
- print("Parameters: "..textutils.serialize(params))
- sendCommand(device, command,params)
- print("COMMAND SENT")
- print("-----------------")
- else
- print("NETWORK MAY BE UNDER ATTACK: BAD ARGUMENTS")
- print("ATTACKER ID: "..id)
- print("-----------------")
- end
- else
- print("NETWORK MAY BE UNDER ATTACK: FALSE DEVICE")
- print("ATTACKER ID: "..id)
- print("FALSE DEVICE: "..textutils.serialize(device))
- print("-----------------")
- end
- elseif protocol == "detatchDevice" then
- device = message[3]
- if checkForDevice(device) then
- file = fs.open("network/devices/"..device.."/id", "r")
- checkId = file.readAll()
- file.close()
- checkId = checkId + 0
- if id == checkId then
- print("Device: "..device)
- if fs.exists("network/devices/"..device) then
- fs.delete("network/devices/"..device)
- end
- print("-----------------")
- else
- print("NETWORK MAY BE UNDER ATTACK: ATTEMPTED DETATCHMENT")
- print("ATTACKER ID: "..id)
- print("-----------------")
- end
- else
- print("NETWORK MAY BE UNDER ATTACK: FALSE DEVICE")
- print("ATTACKER ID: "..id)
- print("FALSE DEVICE: "..textutils.serialize(device))
- print("-----------------")
- end
- end
- else
- print("NETWORK MAY BE UNDER ATTACK: BAD PASSWORD")
- print("ATTACKER ID: "..id)
- print("-------------------")
- end
- else
- print("NETWORK MAY BE UNDER ATTACK: message[1] is nil")
- print("ATTACKER ID: "..id)
- print("MESSAGE: "..textutils.serialize(msg))
- print("-------------------")
- end
- else
- print("NEWORK MAY BE UNDER ATTACK: BAD MESSAGE")
- print("ATTACKER ID: "..id)
- print("MESSAGE: "..textutils.serialize(msg))
- print("-------------------")
- end
- else
- print("NETWORK MAY BE UNDER ATTACK: TABLE MESSAGE")
- print("ATTACKER ID: "..id)
- print("TABLE: "..textutils.serialize(msg))
- print("-------------------")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement