Advertisement
Guest User

Untitled

a guest
Aug 10th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. rednet.open("top")
  2. term.clear()
  3. term.setCursorPos(1,1)
  4.  
  5. passcode = "test"
  6.  
  7. if not fs.exists("network") then
  8. fs.makeDir("network/devices")
  9. end
  10.  
  11. function createDevice(name, dType, sender, cmds)
  12. commands = {}
  13. commands = cmds
  14.  
  15. fs.makeDir("network/devices/"..name)
  16. file = fs.open("network/devices/"..name.."/dType","w")
  17. file.write(dType)
  18. file.close()
  19.  
  20. file = fs.open("network/devices/"..name.."/commands","w")
  21. file.write(textutils.serialize(commands))
  22. file.close()
  23.  
  24. file = fs.open("network/devices/"..name.."/id","w")
  25. file.write(sender)
  26. file.close()
  27. end
  28.  
  29. function getDevices()
  30. list = fs.list("network/devices")
  31. return list
  32. end
  33.  
  34. function getCommands(device)
  35. file = fs.open("network/devices/"..device.."/commands","r")
  36. returnCommands = file.readAll()
  37. file.close()
  38. return textutils.unserialize(returnCommands)
  39. end
  40.  
  41. function getDType(device)
  42. file = fs.open("network/devices/"..device.."/dType","r")
  43. returnType = file.readAll()
  44. file.close()
  45. return returnType
  46. end
  47.  
  48. function sendCommand(device,command, argumentList)
  49. commandList = getCommands(device)
  50.  
  51. arguments = {}
  52. arguments = argumentList
  53.  
  54. commandFound = false
  55.  
  56. for i,v in pairs(commandList) do
  57. if v == command then
  58. commandFound = true
  59. end
  60. end
  61.  
  62. if commandFound then
  63. file = fs.open("network/devices/"..device.."/id","r")
  64. target = file.readAll()
  65. file.close()
  66. target = target + 0
  67. fullCommand = {command,arguments}
  68. fullCommand = textutils.serialize(fullCommand)
  69. rednet.send(target, fullCommand)
  70. end
  71. end
  72.  
  73. function getAllTypes()
  74. allTypes = {}
  75.  
  76. allDevices = getDevices()
  77.  
  78. for i,v in pairs(allDevices) do
  79. dType = getDType(v)
  80.  
  81. inArray = false
  82. for i,v in pairs(allTypes) do
  83. if string.upper(v) == string.upper(dType) then
  84. inArray = true
  85. end
  86. end
  87.  
  88. if not inArray then
  89. table.insert(allTypes, dType)
  90. end
  91. end
  92.  
  93. return allTypes
  94. end
  95.  
  96. function getAllOfType(dType)
  97.  
  98. allOfType = {}
  99.  
  100. allDevices = getDevices()
  101.  
  102. for i,v in pairs(allDevices) do
  103. vdType = getDType(v)
  104.  
  105. if string.upper(vdType) == string.upper(dType) then
  106. table.insert(allOfType, v)
  107. end
  108. end
  109.  
  110. return allOfType
  111. end
  112.  
  113. function checkForDevice(nDevice)
  114. if type(nDevice) == "string" and fs.exists("network/devices/"..nDevice) then
  115. return true
  116. else
  117. return false
  118. end
  119. end
  120.  
  121. while true do
  122. id, msg, d = rednet.receive()
  123. if type(msg) ~= "table" then
  124. message = textutils.unserialize(msg)
  125. if type(message) ~= "nil" then
  126. if message[1] ~= nil then
  127. protocol = message[1]
  128. code = message[2]
  129. print("Protocol: "..protocol)
  130.  
  131. if code == passcode then
  132.  
  133. if protocol == "getDevices" then
  134. devices = getDevices()
  135. devices = textutils.serialize(devices)
  136. rednet.send(id, devices)
  137. print("DEVICES RETURNED")
  138. print("-----------------")
  139. elseif protocol == "getTypes" then
  140. dTypes = getAllTypes()
  141. dTypes = textutils.serialize(dTypes)
  142. rednet.send(id, dTypes)
  143. print("TYPES RETURNED")
  144. print("-----------------")
  145. elseif protocol == "getAllOfType" then
  146. deviceType = message[3]
  147. if type(deviceType) == "string" then
  148. print("Type: "..deviceType)
  149. devices = getAllOfType(deviceType)
  150. devices = textutils.serialize(devices)
  151. rednet.send(id, devices)
  152. print("DEVICES RETURNED")
  153. print("-----------------")
  154. else
  155. print("NETWORK MAY BE UNDER ATTACK: BAD TYPE")
  156. print("ATTACKER ID: "..id)
  157. print("-----------------")
  158. end
  159. elseif protocol == "getDeviceSpecs" then
  160. dev = message[3]
  161. if checkForDevice(dev) then
  162. print("Device Name: "..dev)
  163. commandList = getCommands(dev)
  164. print("Command List: "..textutils.serialize(commandList))
  165. dType = getDType(dev)
  166. print("Type: "..dType)
  167.  
  168. specs = {dType,commandList}
  169. specs = textutils.serialize(specs)
  170. print("Specs: "..specs)
  171.  
  172. rednet.send(id, specs)
  173. print("SPECS RETURNED")
  174. print("-----------------")
  175. else
  176. print("NETWORK MAY BE UNDER ATTACK: FALSE DEVICE")
  177. print("ATTACKER ID: "..id)
  178. print("FALSE DEVICE: "..textutils.serialize(dev))
  179. print("-----------------")
  180. end
  181. elseif protocol == "createDevice" then
  182. name = message[3]
  183. print("Name: "..name)
  184. dType = message[4]
  185. print("Type: "..dType)
  186. commands = message[5]
  187. print("Commands: "..textutils.serialize(commands))
  188. createDevice(name,dType,id,commands)
  189. print("DEVICE CREATED")
  190. print("-----------------")
  191. elseif protocol == "sendCommand" then
  192. device = message[3]
  193. if checkForDevice(device) then
  194. print("Device: "..device)
  195. if type(message[4]) == "string" and type(message[5]) == "table" then
  196. command = message[4]
  197. print("Command: "..command)
  198. params = message[5]
  199. print("Parameters: "..textutils.serialize(params))
  200.  
  201. sendCommand(device, command,params)
  202. print("COMMAND SENT")
  203. print("-----------------")
  204. else
  205. print("NETWORK MAY BE UNDER ATTACK: BAD ARGUMENTS")
  206. print("ATTACKER ID: "..id)
  207. print("-----------------")
  208. end
  209. else
  210. print("NETWORK MAY BE UNDER ATTACK: FALSE DEVICE")
  211. print("ATTACKER ID: "..id)
  212. print("FALSE DEVICE: "..textutils.serialize(device))
  213. print("-----------------")
  214. end
  215. elseif protocol == "detatchDevice" then
  216. device = message[3]
  217.  
  218. if checkForDevice(device) then
  219. file = fs.open("network/devices/"..device.."/id", "r")
  220. checkId = file.readAll()
  221. file.close()
  222.  
  223. checkId = checkId + 0
  224.  
  225. if id == checkId then
  226. print("Device: "..device)
  227. if fs.exists("network/devices/"..device) then
  228. fs.delete("network/devices/"..device)
  229. end
  230. print("-----------------")
  231. else
  232. print("NETWORK MAY BE UNDER ATTACK: ATTEMPTED DETATCHMENT")
  233. print("ATTACKER ID: "..id)
  234. print("-----------------")
  235. end
  236. else
  237. print("NETWORK MAY BE UNDER ATTACK: FALSE DEVICE")
  238. print("ATTACKER ID: "..id)
  239. print("FALSE DEVICE: "..textutils.serialize(device))
  240. print("-----------------")
  241. end
  242. end
  243. else
  244. print("NETWORK MAY BE UNDER ATTACK: BAD PASSWORD")
  245. print("ATTACKER ID: "..id)
  246. print("-------------------")
  247. end
  248. else
  249. print("NETWORK MAY BE UNDER ATTACK: message[1] is nil")
  250. print("ATTACKER ID: "..id)
  251. print("MESSAGE: "..textutils.serialize(msg))
  252. print("-------------------")
  253. end
  254. else
  255. print("NEWORK MAY BE UNDER ATTACK: BAD MESSAGE")
  256. print("ATTACKER ID: "..id)
  257. print("MESSAGE: "..textutils.serialize(msg))
  258. print("-------------------")
  259. end
  260. else
  261. print("NETWORK MAY BE UNDER ATTACK: TABLE MESSAGE")
  262. print("ATTACKER ID: "..id)
  263. print("TABLE: "..textutils.serialize(msg))
  264. print("-------------------")
  265. end
  266. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement