Advertisement
Rayzin

Rayzin's DISCOVERY

Apr 8th, 2020 (edited)
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.68 KB | None | 0 0
  1. local component = require("component")
  2. local computer = require("computer")
  3. local fs = require("filesystem")
  4. local term = require("term")
  5. local md = component.modem
  6. local netAddr = component.modem.address
  7. local event = require("event")
  8. local databasePath = "/home/discov.dat"
  9. local ftpPath = "/home/ftplist.dat"
  10. --local tnl = component.tunnel
  11.  
  12.  
  13. local ftpNum = 0
  14. local ftpsession = 1
  15.  
  16. local discovPort = 0
  17. local ftpPort = 0
  18. local dnsRead = 0
  19. local dnsListen = 0
  20. local alarmPort = 0
  21. local reactorStatus = 0
  22. --SET THESE VARIABLES IN THE FILE LOCATED AT databasePath variable
  23.  
  24.  
  25.  
  26. --dns reverse lookup
  27. function reverseLookup(addrQuery)
  28. md.close(1)
  29. md.open(dnsSend)
  30. md.broadcast(dnsListen,"GET_REVERSE",addrQuery)
  31. local _,_,from,port,_,dnsRes= event.pull("modem_message")
  32. md.close(dnsSend)
  33. md.open(1)
  34. dnsRes = tostring(dnsRes)
  35. return dnsRes
  36. end
  37.  
  38. --file read
  39. function readDisc(value)
  40. print("Reading " .. value .. " from DISCOVERY database.")
  41. local nextLine = ""
  42. local fileContents = ""
  43. local newNextLine = ""
  44. local file = io.open(databasePath, "r")
  45. repeat
  46. local nextLine = file : read("*l")
  47. newNextLine = nextLine
  48. if nextLine == nil then break end
  49. if newNextLine == nil then break break end
  50. --print("Checking entry: " .. nextLine)
  51. until string.match(nextLine, value)
  52. nextLine = newNextLine
  53. print("Found: ".. newNextLine)
  54. nextLine = string.gsub(nextLine, value, "")
  55. nextLine = string.gsub(nextLine, " ", "")
  56. --print("Resolved: "..nextLine)
  57. file : close()
  58. return nextLine
  59.  
  60. end
  61.  
  62.  
  63. --Number getter
  64. function getNameNum (name)
  65. local file = io.open(databasePath,"r")
  66. contents = file : read(1000)
  67. local i,j = string.find(contents, name)
  68. local endNum = string.sub(contents,j+1,j+3)
  69. endNum = tonumber(endNum)
  70. file : close()
  71. return endNum
  72. end
  73.  
  74. --address getter  DEPRECIATED  (Replaced by readDisc function)
  75. function getAddr (addrId)
  76. local file = io.open(databasePath,"r")
  77. contents = file : read(1000)
  78. local i,j = string.find(contents,addrId)
  79. local addrLen = tonumber(string.sub(contents,j+1,j+2))
  80. j = j + 3
  81. local addr = string.sub(contents,j,j+addLen)
  82. return tostring(addr)
  83. end
  84.  
  85. --ftp logger
  86. function ftpLog(numftp,addr)
  87. addr = string.gsub(addr,"-"," ")
  88. print("Checking if address already present...")
  89. --check if address is in ftp list
  90. local file = io.open(ftpPath,"r")
  91. print("Reading file")
  92. contents = file : read(1000)
  93. contents = string.gsub(contents, "-"," ")
  94. file : close()
  95. print("Closed file")
  96. findResult = string.find(contents,addr)
  97. print(findResult)
  98. if findResult == nil then
  99. print("No result")
  100. addr = string.gsub(addr," ", "-")
  101. ftpId = ("ftpHost" .. numftp.. " " .. addr)
  102. local file = io.open(ftpPath,"a")
  103. file : write(ftpId.." \n")
  104. print("Writing ftphost to file")
  105. file : close()
  106. result = ("Ftphost added to ftp list")
  107. else
  108. print("Ftphost already exists")
  109. result = ("Ftphost already exists")
  110. end
  111. return result
  112. end
  113.  
  114. --number to id
  115. function numToId(num)
  116. num = tostring(num)
  117. if num < 10 then
  118. num = ("00" .. num)
  119. elseif num < 100 then
  120. num = ("0" .. num)
  121. elseif num < 1000 then
  122. num = ("" .. num)
  123. end
  124. return result
  125. end
  126.  
  127. --end of functions
  128.  
  129. term.clear()
  130. print("Initialize DISCOVERY server")
  131. --check for database
  132. print("Checking for database...")
  133. dExists = fs.exists(databasePath)
  134. ftpExists = fs.exists(ftpPath)
  135. dExists = tostring(dExists)
  136. ftpExists = tostring(ftpExists)
  137.  
  138. if dExists == "true" then
  139. print("Database found!")
  140. end
  141. if ftpExists == "true" then
  142. print("FTP hostlist found!")
  143. end
  144. if dExists == "false" then
  145. print("Database missing!")
  146. print("Creating new database...")
  147. local file = io.open(databasePath,"w")
  148. file : write("Please enter system variables as so: Variable_idNum\ndiscoveryPort1\nftpNum1\nftpPort80\nalarmPort40\ndnsListen52\ndnsSend53\n")
  149. file : close()
  150. print("Database created!")
  151. end
  152. if ftpExists == "false" then
  153. print("FTP hostlist missing!")
  154. print("Creating new hostlist...")
  155. local file = io.open(ftpPath,"w")
  156. file : write("FTP_HOSTLIST\n")
  157. file : close()
  158. print("FTP hostlist created!")
  159. end
  160.  
  161. --get ftphost num
  162. ftpNum = tonumber(readDisc("ftpNum"))
  163.  
  164. --get ftp port
  165. ftpPort = tonumber(readDisc("ftpPort"))
  166. print("FTP Port open! "..ftpPort)
  167.  
  168. --get dns port
  169. dnsListen = tonumber(readDisc("dnsListen"))
  170. print("DNS Listen Port found! "..dnsListen)
  171. dnsSend = tonumber(readDisc("dnsSend"))
  172. print("DNS Send Port found! " .. dnsSend)
  173.  
  174. --open DISCOVERY port
  175. discovPort = tonumber(readDisc("discoveryPort"))
  176. md.open(discovPort)
  177. print("Discovery port open! " .. discovPort)
  178.  
  179.  
  180. --get alarm port
  181. alarmPort = tonumber(readDisc("alarmPort"))
  182.  
  183. --begin main loop
  184. print("Initialized!")
  185. while true do
  186.  
  187. --listen for incoming requests
  188. term.write("Listening for requests...\n")
  189. local _,_,from,port,_,message = event.pull("modem_message")
  190. from = tostring(from)
  191. port = tonumber(port)
  192. messsage = tostring(messsage)
  193. local dnsResolved = reverseLookup(from)
  194. term.write("Received " .. message .. " from ("..dnsResolved..") " .. from .. "\n")
  195. os.sleep(0.2)
  196.  
  197. --check if request is a DISCOVERY request
  198. if message == "DISCOVERY" then
  199. md.send(from,discovPort,"1")
  200. md.send(from, discovPort , "DISCOVERY_RECEIVE")
  201. term.write("Sent net address to " .. from .. "\n")
  202. end
  203.  
  204. --check if request is a FTP_INFO request
  205. if message == "FTP_INFO" then
  206. md.send(from, discovPort,  "2")
  207. print("Sending FTP_INFO to " .. from)
  208. md.send(from, discovPort, ftpPort)
  209. md.send(from,discovPort, ftpAddr)
  210. print("FTP_INFO transfer complete!")
  211. end
  212.  
  213. --check for a clear command
  214. if message == "CLEAR_DATABASE" then
  215. md.send(from,discovPort,"2")
  216. md.send(from, discovPort, "Clearing database")
  217. print("CLEARING DATABASE..")
  218. fs.remove(databasePath)
  219. fs.remove(ftpPath)
  220. print("DONE")
  221. md.send(from,discovPort,"DONE!")
  222. computer.shutdown(true)
  223. end
  224.  
  225. if message == "FTP_LOG" then
  226. local ftpResult = tostring(ftpLog(ftpNum,from))
  227. md.send(from,discovPort,"1")
  228. md.send(from, discovPort, ftpResult)
  229. if ftpResult == "Ftphost added to ftp list" then
  230. ftpNum = ftpNum + 1
  231. end
  232.  
  233. end
  234.  
  235. if message == "TERMINATE" then
  236. print("Terminating process!")
  237. md.send(from,discovPort,"4")
  238. md.send(from,discovPort,"Terminating process!")
  239. print("END!")
  240. os.sleep(1)
  241. md.send(from,discovPort,"END!")
  242. os.exit()
  243. end
  244.  
  245. if message == "FTP_GET" then
  246. numIds = -1
  247. print("FTP session: ".. ftpsession)
  248. file = io.open("ftplist.dat","r")
  249. contents = file : read(1000)
  250. contents = tostring(contents)
  251. for word in string.gmatch(contents, "%\n") do
  252. numIds = numIds + 1
  253. end
  254. file : close()
  255. firstMessage = ("There are ".. numIds .. " ftp hosts on file.")
  256. print(firstMessage)
  257. md.send(from,discovPort, numIds)
  258.  
  259. words = {}
  260. i = 0
  261. for word in string.gmatch(contents, "%S+") do
  262. os.sleep(0.2)
  263. i = i + 1
  264. words[i] = word
  265. if (i % 2 == 1) and (i > 1) then
  266. print("Sending "..words[i].. " to ".. from)
  267. md.send(from,discovPort,words[i])
  268. end
  269.  
  270. end
  271.  
  272. print("Creating new FTP session...")
  273. ftpsession = ftpsession + 1
  274. file = io.open("ftplist.dat","w")
  275. file : write("FTP_HOSTLIST\n")
  276. file : close()
  277. print("FTP Session " .. ftpsession .. " created!")
  278. end
  279.  
  280. if message == "REACTOR_DOWN" then
  281. md.broadcast(alarmPort,"ALARM_BEGIN")
  282. print("Reactor Down!")
  283. reactorStatus = 0
  284. end
  285.  
  286. if message == "REACTOR_UP" then
  287. md.broadcast(alarmPort,"ALARM_END")
  288. print("Reactor Up!")
  289. reactorStatus = 1
  290. end
  291.  
  292. if message == "REACTOR_STATUS" then
  293. if reactorStatus == 1 then
  294. md.send(from,discovPort,"1")
  295. md.send(from,discovPort,"Reactor Online")
  296. end
  297. if reactorStatus == 0 then
  298. md.send(from,discovPort,"1")
  299. md.send(from,discovPort,"Reactor Offline")
  300. end
  301.  
  302. end
  303.  
  304. if message == "INFO" then
  305. print("Getting Info...")
  306. print("Sending command list")
  307. md.send(from,discovPort,"DISCOVERY FTP_INFO CLEAR_DATABASE FTP_LOG TERMINATE FTP_GET REACTOR_STATUS")
  308. end
  309.  
  310. if message == "DNS" then
  311. md.send(from,discovPort,dnsListen,dnsSend)
  312. end
  313.  
  314. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement