Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args={...}
- local fbuffer={} -- File buffer for memoization
- local chunks={} -- Chunk count
- local w,h=term.getSize()
- local CSIZE=500 -- Base chunk size, in bytes
- local setUp=false
- local trailingSpace=string.rep(" ",w)
- local getSize=fs.getSize
- local readFile,loadTable,scanFile,markFile,dirtree,fastSleep,printThreat,makeChunk,nextChunk,printGUI,setUpGUI
- local known,scanPath
- function setUpGUI()
- term.setBackgroundColour(colours.white)
- term.setTextColour(colours.grey)
- if setUp then return end
- term.clear()
- term.setTextColour(colours.black)
- term.setCursorPos(3,2)
- term.write("Files scanned: ")
- term.setCursorPos(3,3)
- term.write("Threats found: ")
- term.setCursorPos(2,h-1)
- term.setBackgroundColour(colours.grey)
- term.write(string.rep(" ",w-2))
- term.setBackgroundColour(colours.white)
- term.setTextColour(colours.grey)
- setUp=true
- end
- --TODO: Rewrite this! Looks horrible :(
- function printGUI(path, percentage, scanned, found)
- setUpGUI()
- term.setCursorPos(2,5)
- term.write(path..trailingSpace)
- term.setCursorPos(w/4,h*2/3)
- term.write(tostring(percentage).."%"..trailingSpace)
- term.setTextColour(colours.black)
- term.setCursorPos(18,2)
- term.write(scanned)
- term.setCursorPos(18,3)
- term.setTextColour(found==0 and colours.black or colours.red)
- term.write(found)
- -- Print progress bar
- term.setCursorPos(2,h-1)
- term.setBackgroundColour(colours.green)
- term.write(string.rep(" ",(w-2)*percentage/100))
- end
- function printThreat(...)
- local args={...}
- if #args<1 then return print() end
- local _,_,family=tostring(args[1]):gsub("db%.",""):find("(%a+)%.")
- if family=="Suspicious" then
- term.setTextColour(colours.orange)
- else
- term.setTextColour(colours.red)
- end
- return print(...)
- end
- function fastSleep()
- os.queueEvent("fastSleep")
- return coroutine.yield("fastSleep")
- end
- function dirtree(dir, includeDirectories)
- assert(dir and dir ~= "", "directory parameter is missing or empty")
- if string.sub(dir, -1) == "/" then
- dir=string.sub(dir, 1, -2)
- end
- local function yieldtree(dir)
- for i,entry in ipairs(fs.list(dir)) do
- if entry ~= "." and entry ~= ".." then
- entry=dir.."/"..entry
- if includeDirectories or not fs.isDir(entry) then
- coroutine.yield(entry)
- end
- if fs.isDir(entry) then
- yieldtree(entry)
- end
- end
- end
- end
- return coroutine.wrap(function() yieldtree(dir) end)
- end
- function readFile(path, absolute, force)
- path=(absolute and "" or shell.resolve(".").."/")..path
- if fbuffer[path] and not force then
- return fbuffer[path]
- end
- local f,err=fs.open(path,"r")
- if not f then error("Could not open file '"..tostring(path).."': "..tostring(err),0) end
- local content=f.readAll()
- f.close()
- fbuffer[path]=content
- return content
- end
- --WARN: Consecutive chunk has the last character of the previous chunk, therefore its max size is CSIZE+1
- function makeChunk(str, offset)
- if offset>#str then
- return
- end
- local chunkSize=math.min(#str-offset, CSIZE)
- return str:sub(offset, offset+chunkSize)
- end
- function nextChunk(path)
- if not chunks[path] then
- chunks[path]=-1
- end
- chunks[path]=chunks[path]+1
- local minSize=math.min(CSIZE,getSize(path))
- if chunks[path]*minSize>getSize(path)*2+CSIZE/2+1 then
- return nil
- end
- local offset=(chunks[path]*minSize)>getSize(path) and minSize/2 or 0
- return makeChunk(readFile(path), CSIZE*chunks[path]+offset)
- end
- function loadTable(path, absolute)
- return textutils.unserialize(readFile(path,absolute))
- end
- function markFile(path, threats)
- --TODO: calculate a checksum and save to threat database
- end
- function scanFile(path, db)
- local iter,scan
- local position="db"
- local res={}
- function iter(tbl)
- for k,v in pairs(tbl) do
- local myPos=position
- position=position.."."..k
- if type(v)=="table" and not v[0] then
- iter(v)
- else
- scan(v)
- end
- position=myPos
- end
- end
- function scan(tbl)
- for i,pattern in ipairs(tbl) do
- --TODO: rewrite nextChunk to iterator
- local chunk=nextChunk(path)
- while chunk do
- if chunk:find(pattern) then
- table.insert(res,position.."."..i)
- end
- fastSleep()
- chunk=nextChunk(path)
- end
- chunks[path]=nil
- end
- end
- iter(db)
- return res
- end
- -- Load known threats
- known,err=loadTable("threatDatabase.ltbl")
- if not known then error("Failed to load threat database: "..tostring(err))end
- local results={}
- local totalFound=0
- local totalScanned=0
- local totalBytes=0
- local currentBytes=0
- scanPath="/"
- if args[1] and fs.exists(args[1]) and fs.isDir(args[1]) then
- scanPath=args[1]
- end
- for path in dirtree(scanPath) do
- totalBytes=totalBytes+getSize(path)
- end
- -- Test
- for path in dirtree(scanPath) do
- currentBytes=currentBytes+getSize(path)
- local percentage=currentBytes/totalBytes*100
- printGUI(path,percentage,totalScanned,totalFound)
- results[path]=scanFile(path,known)
- totalFound=totalFound+#results[path]
- totalScanned=totalScanned+1
- end
- term.setBackgroundColour(colours.white)
- term.setTextColour(colours.black)
- term.clear()
- term.setCursorPos(1,1)
- print("Scan results:")
- print(" Files scanned: "..totalScanned)
- term.write(" Threats found: ")
- term.setTextColour(totalFound==0 and colours.black or colours.red)
- print(totalFound)
- term.setTextColour(colours.lightGrey)
- print(" (Click to scroll)")
- print()
- for file,threats in pairs(results) do
- if #threats>0 then
- term.setTextColour(colours.black)
- print("Infected file: "..file)
- for i,threat in ipairs(threats) do
- printThreat(" "..threat)
- end
- os.pullEvent("mouse_click")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment