Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local computer = require("computer")
- local fs = require("filesystem")
- local term = require("term")
- local md = component.modem
- local netAddr = component.modem.address
- local event = require("event")
- local databasePath = "/home/discov.dat"
- local ftpPath = "/home/ftplist.dat"
- --local tnl = component.tunnel
- local ftpNum = 0
- local ftpsession = 1
- local discovPort = 0
- local ftpPort = 0
- local dnsRead = 0
- local dnsListen = 0
- local alarmPort = 0
- local reactorStatus = 0
- --SET THESE VARIABLES IN THE FILE LOCATED AT databasePath variable
- --dns reverse lookup
- function reverseLookup(addrQuery)
- md.close(1)
- md.open(dnsSend)
- md.broadcast(dnsListen,"GET_REVERSE",addrQuery)
- local _,_,from,port,_,dnsRes= event.pull("modem_message")
- md.close(dnsSend)
- md.open(1)
- dnsRes = tostring(dnsRes)
- return dnsRes
- end
- --file read
- function readDisc(value)
- print("Reading " .. value .. " from DISCOVERY database.")
- local nextLine = ""
- local fileContents = ""
- local newNextLine = ""
- local file = io.open(databasePath, "r")
- repeat
- local nextLine = file : read("*l")
- newNextLine = nextLine
- if nextLine == nil then break end
- if newNextLine == nil then break break end
- --print("Checking entry: " .. nextLine)
- until string.match(nextLine, value)
- nextLine = newNextLine
- print("Found: ".. newNextLine)
- nextLine = string.gsub(nextLine, value, "")
- nextLine = string.gsub(nextLine, " ", "")
- --print("Resolved: "..nextLine)
- file : close()
- return nextLine
- end
- --Number getter
- function getNameNum (name)
- local file = io.open(databasePath,"r")
- contents = file : read(1000)
- local i,j = string.find(contents, name)
- local endNum = string.sub(contents,j+1,j+3)
- endNum = tonumber(endNum)
- file : close()
- return endNum
- end
- --address getter DEPRECIATED (Replaced by readDisc function)
- function getAddr (addrId)
- local file = io.open(databasePath,"r")
- contents = file : read(1000)
- local i,j = string.find(contents,addrId)
- local addrLen = tonumber(string.sub(contents,j+1,j+2))
- j = j + 3
- local addr = string.sub(contents,j,j+addLen)
- return tostring(addr)
- end
- --ftp logger
- function ftpLog(numftp,addr)
- addr = string.gsub(addr,"-"," ")
- print("Checking if address already present...")
- --check if address is in ftp list
- local file = io.open(ftpPath,"r")
- print("Reading file")
- contents = file : read(1000)
- contents = string.gsub(contents, "-"," ")
- file : close()
- print("Closed file")
- findResult = string.find(contents,addr)
- print(findResult)
- if findResult == nil then
- print("No result")
- addr = string.gsub(addr," ", "-")
- ftpId = ("ftpHost" .. numftp.. " " .. addr)
- local file = io.open(ftpPath,"a")
- file : write(ftpId.." \n")
- print("Writing ftphost to file")
- file : close()
- result = ("Ftphost added to ftp list")
- else
- print("Ftphost already exists")
- result = ("Ftphost already exists")
- end
- return result
- end
- --number to id
- function numToId(num)
- num = tostring(num)
- if num < 10 then
- num = ("00" .. num)
- elseif num < 100 then
- num = ("0" .. num)
- elseif num < 1000 then
- num = ("" .. num)
- end
- return result
- end
- --end of functions
- term.clear()
- print("Initialize DISCOVERY server")
- --check for database
- print("Checking for database...")
- dExists = fs.exists(databasePath)
- ftpExists = fs.exists(ftpPath)
- dExists = tostring(dExists)
- ftpExists = tostring(ftpExists)
- if dExists == "true" then
- print("Database found!")
- end
- if ftpExists == "true" then
- print("FTP hostlist found!")
- end
- if dExists == "false" then
- print("Database missing!")
- print("Creating new database...")
- local file = io.open(databasePath,"w")
- file : write("Please enter system variables as so: Variable_idNum\ndiscoveryPort1\nftpNum1\nftpPort80\nalarmPort40\ndnsListen52\ndnsSend53\n")
- file : close()
- print("Database created!")
- end
- if ftpExists == "false" then
- print("FTP hostlist missing!")
- print("Creating new hostlist...")
- local file = io.open(ftpPath,"w")
- file : write("FTP_HOSTLIST\n")
- file : close()
- print("FTP hostlist created!")
- end
- --get ftphost num
- ftpNum = tonumber(readDisc("ftpNum"))
- --get ftp port
- ftpPort = tonumber(readDisc("ftpPort"))
- print("FTP Port open! "..ftpPort)
- --get dns port
- dnsListen = tonumber(readDisc("dnsListen"))
- print("DNS Listen Port found! "..dnsListen)
- dnsSend = tonumber(readDisc("dnsSend"))
- print("DNS Send Port found! " .. dnsSend)
- --open DISCOVERY port
- discovPort = tonumber(readDisc("discoveryPort"))
- md.open(discovPort)
- print("Discovery port open! " .. discovPort)
- --get alarm port
- alarmPort = tonumber(readDisc("alarmPort"))
- --begin main loop
- print("Initialized!")
- while true do
- --listen for incoming requests
- term.write("Listening for requests...\n")
- local _,_,from,port,_,message = event.pull("modem_message")
- from = tostring(from)
- port = tonumber(port)
- messsage = tostring(messsage)
- local dnsResolved = reverseLookup(from)
- term.write("Received " .. message .. " from ("..dnsResolved..") " .. from .. "\n")
- os.sleep(0.2)
- --check if request is a DISCOVERY request
- if message == "DISCOVERY" then
- md.send(from,discovPort,"1")
- md.send(from, discovPort , "DISCOVERY_RECEIVE")
- term.write("Sent net address to " .. from .. "\n")
- end
- --check if request is a FTP_INFO request
- if message == "FTP_INFO" then
- md.send(from, discovPort, "2")
- print("Sending FTP_INFO to " .. from)
- md.send(from, discovPort, ftpPort)
- md.send(from,discovPort, ftpAddr)
- print("FTP_INFO transfer complete!")
- end
- --check for a clear command
- if message == "CLEAR_DATABASE" then
- md.send(from,discovPort,"2")
- md.send(from, discovPort, "Clearing database")
- print("CLEARING DATABASE..")
- fs.remove(databasePath)
- fs.remove(ftpPath)
- print("DONE")
- md.send(from,discovPort,"DONE!")
- computer.shutdown(true)
- end
- if message == "FTP_LOG" then
- local ftpResult = tostring(ftpLog(ftpNum,from))
- md.send(from,discovPort,"1")
- md.send(from, discovPort, ftpResult)
- if ftpResult == "Ftphost added to ftp list" then
- ftpNum = ftpNum + 1
- end
- end
- if message == "TERMINATE" then
- print("Terminating process!")
- md.send(from,discovPort,"4")
- md.send(from,discovPort,"Terminating process!")
- print("END!")
- os.sleep(1)
- md.send(from,discovPort,"END!")
- os.exit()
- end
- if message == "FTP_GET" then
- numIds = -1
- print("FTP session: ".. ftpsession)
- file = io.open("ftplist.dat","r")
- contents = file : read(1000)
- contents = tostring(contents)
- for word in string.gmatch(contents, "%\n") do
- numIds = numIds + 1
- end
- file : close()
- firstMessage = ("There are ".. numIds .. " ftp hosts on file.")
- print(firstMessage)
- md.send(from,discovPort, numIds)
- words = {}
- i = 0
- for word in string.gmatch(contents, "%S+") do
- os.sleep(0.2)
- i = i + 1
- words[i] = word
- if (i % 2 == 1) and (i > 1) then
- print("Sending "..words[i].. " to ".. from)
- md.send(from,discovPort,words[i])
- end
- end
- print("Creating new FTP session...")
- ftpsession = ftpsession + 1
- file = io.open("ftplist.dat","w")
- file : write("FTP_HOSTLIST\n")
- file : close()
- print("FTP Session " .. ftpsession .. " created!")
- end
- if message == "REACTOR_DOWN" then
- md.broadcast(alarmPort,"ALARM_BEGIN")
- print("Reactor Down!")
- reactorStatus = 0
- end
- if message == "REACTOR_UP" then
- md.broadcast(alarmPort,"ALARM_END")
- print("Reactor Up!")
- reactorStatus = 1
- end
- if message == "REACTOR_STATUS" then
- if reactorStatus == 1 then
- md.send(from,discovPort,"1")
- md.send(from,discovPort,"Reactor Online")
- end
- if reactorStatus == 0 then
- md.send(from,discovPort,"1")
- md.send(from,discovPort,"Reactor Offline")
- end
- end
- if message == "INFO" then
- print("Getting Info...")
- print("Sending command list")
- md.send(from,discovPort,"DISCOVERY FTP_INFO CLEAR_DATABASE FTP_LOG TERMINATE FTP_GET REACTOR_STATUS")
- end
- if message == "DNS" then
- md.send(from,discovPort,dnsListen,dnsSend)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement