LDDestroier

Plisk encrypted file/folder sender

Apr 24th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.33 KB | None | 0 0
  1. -- "plisk" by LDDestroier
  2. -- thanks for the AES, SquidDev
  3. -- pastebin get G9fsXzGt plisk
  4.  
  5. local tArg = {...}
  6. local port, id = 257, os.getComputerID()
  7. local absolutePath = false
  8.  
  9. assert(type(id)=="number", "why is your ID a "..type(id).."...?")
  10. assert(id>=0, "IDs can't be below zero, plebian!")
  11. assert(id<=65535, "IDs can't be above 65535, fool!")
  12.  
  13. local modem = {}
  14.  
  15. local waitForModem = function(time)
  16.     local tID, evt, rID = os.startTimer(time or 1)
  17.     while true do
  18.         evt, rID = os.pullEventRaw()
  19.         if evt == "timer" then
  20.             if rID == tID then
  21.                 return false
  22.             end
  23.         elseif evt == "terminate" then
  24.             return false
  25.         elseif evt == "peripheral" then
  26.             if peripheral.getType(rID) == "modem" then
  27.                 return true
  28.             end
  29.         end
  30.     end
  31. end
  32.  
  33. local getModem = function(timeout) --returns a working modem, prioritizes use of wireless modem
  34.     if modem.open then return modem end
  35.     local modems = {peripheral.find("modem")}
  36.     if #modems == 0 then
  37.         if waitForModem(timeout or 3) then
  38.             modems = {peripheral.find("modem")}
  39.         else
  40.             error("No modem found...")
  41.         end
  42.     end
  43.     for a = 1, #modems do
  44.         if modems[a].isWireless() then
  45.             modems[a].open(port)
  46.             modems[a].open(id)
  47.             return modems[a]
  48.         end
  49.     end
  50.     modems[1].open(port)
  51.     modems[1].open(id)
  52.     return modems[1]
  53. end
  54.  
  55. modem = getModem()
  56.  
  57. local readFile = function(path)
  58.     if fs.exists(path) and (not fs.isDir(path)) then
  59.         local file = fs.open(path,"r")
  60.         return file.readAll(), file.close()
  61.     end
  62. end
  63.  
  64. local writeFile = function(path,contents)
  65.     local file = fs.open(path,"w")
  66.     return file.write(contents), file.close()
  67. end
  68.  
  69. local initAES = function()
  70.     local apipath = "aes"
  71.     if (not aes) and (not fs.exists(apipath)) then
  72.         print("AES API not found! Downloading...")
  73.         local prog = http.get("https://gist.githubusercontent.com/SquidDev/86925e07cbabd70773e53d781bd8b2fe/raw/ccea5f652cc33a979de02d6e0fe193db0c5bdfb1/aeslua.min.lua")
  74.         if not prog then error("FAIL!") end
  75.         writeFile(apipath, prog.readAll())
  76.     end
  77.     if not aes then
  78.         local res = os.loadAPI(apipath)
  79.         if not res then error("Couldn't load AES API!") end
  80.     end
  81. end
  82.  
  83. initAES()
  84.  
  85. plisk = {
  86.     send = function(rID, key, content)
  87.         local output = {
  88.             id = id,
  89.             rID = rID,
  90.             content = content
  91.         }
  92.         output = aes.encrypt(key, textutils.serialize(output))
  93.         modem = getModem()
  94.         modem.transmit(port,id,output)
  95.         return true, output
  96.     end,
  97.     receive = function(rID, key)
  98.         assert(type(key) == "string", "key must be string!")
  99.         local evt, msg
  100.         while true do
  101.             evt = {os.pullEventRaw()}
  102.             if evt[1] == "modem_message" then
  103.                 if type(evt[5]) == "string" then
  104.                     msg = textutils.unserialize(aes.decrypt(key,evt[5]))
  105.                     if type(msg) == "table" then
  106.                         if (not rID) or (msg.id == rID) then
  107.                             if (not msg.id) or (msg.id == id) then
  108.                                 break
  109.                             end
  110.                         end
  111.                     end
  112.                 end
  113.                 if msg.content then
  114.                     return msg.id, msg.content
  115.                 end
  116.             elseif evt[1] == "terminate" then
  117.                 return false, evt[1]
  118.             end
  119.         end
  120.     end
  121. }
  122.  
  123. local function listAll(_path, _files, noredundant)
  124.     local path = _path or ""
  125.     local files = _files or {}
  126.     if #path > 1 then table.insert(files, path) end
  127.     for _, file in ipairs(fs.list(path)) do
  128.         local path = fs.combine(path, file)
  129.         if (file ~= thisProgram) then
  130.             local guud = true
  131.             if guud then
  132.                 if fs.isDir(path) then
  133.                     listAll(path, files, noredundant)
  134.                 else
  135.                     table.insert(files, path)
  136.                 end
  137.             end
  138.         end
  139.     end
  140.     if noredundant then
  141.         for a = 1, #files do
  142.             if fs.isDir(tostring(files[a])) then
  143.                 if #fs.list(tostring(files[a])) ~= 0 then
  144.                     table.remove(files,a)
  145.                 end
  146.             end
  147.         end
  148.     end
  149.     return files
  150. end
  151.  
  152. if shell then
  153.  
  154.     local command = tArg[1]
  155.     local filename = tArg[2] and fs.combine(absolutePath and "" or shell.dir(), tArg[2])
  156.     local key = tArg[3] or "scissors61"
  157.     local rID = tonumber(tArg[4])
  158.    
  159.     if rID then
  160.         if (rID < 0) or (rID > 65535) then
  161.             error("recipient ID must be betwixt 0-65535")
  162.         end
  163.         if rID ~= math.floor(rID) then
  164.             error("recipient ID can't be a decimal, fool")
  165.         end
  166.     end
  167.    
  168.     local displayHelp = function()
  169.         local name = fs.getName(shell.getRunningProgram() or "plisk")
  170.         print(name.." get <path> [key] [ID filter]")
  171.         print(name.." send <file> [key] [ID filter]")
  172.     end
  173.    
  174.     local goodPrint = function(text)
  175.         local ct = term.getTextColor()
  176.         if term.isColor() then
  177.             term.setTextColor(colors.lime)
  178.         else
  179.             term.setTextColor(colors.lightGray)
  180.         end
  181.         local res = {print(text)}
  182.         term.setTextColor(ct)
  183.         return table.unpack(res)
  184.     end
  185.    
  186.     if not filename then
  187.         return displayHelp()
  188.     end
  189.    
  190.     if command == "send" then
  191.         if not fs.exists(filename) then
  192.             error("no such file exists")
  193.         else
  194.             local content = {}
  195.             if fs.isDir(filename) then
  196.                 local fList = listAll(filename)
  197.                 for k,v in pairs(fList) do
  198.                     if v:find("/") then
  199.                         content[v:sub(v:find("/")+1)] = readFile(v)
  200.                     end
  201.                 end
  202.             else
  203.                 content = readFile(filename)
  204.             end
  205.             write("Transmitting...")
  206.             local result = plisk.send(rID, key, content)
  207.             if result then
  208.                 goodPrint("gucci!")
  209.             else
  210.                 printError("failed!?")
  211.             end
  212.         end
  213.     elseif command == "get" then
  214.         if fs.isReadOnly(filename) then
  215.             error("file path is read-only.")
  216.         else
  217.             if fs.exists(filename) then
  218.                 if fs.isDir(filename) then
  219.                     print("Overwrite folder? (Y/N)")
  220.                 else
  221.                     print("Overwrite? (Y/N)")
  222.                 end
  223.                 local evt
  224.                 repeat
  225.                     evt = {os.pullEventRaw()}
  226.                 until ((evt[1] == "char") and string.find("yn",evt[2])) or evt[1] == "terminate"
  227.                 if (evt[2] == "n") or (evt[1] == "terminate") then
  228.                     error("Oh, uh, alright.")
  229.                 end
  230.             end
  231.            
  232.             write("Waiting for file") if rID then write(" from "..tostring(rID)) end write("...")
  233.             local recipID, content = plisk.receive(rID, key)
  234.            
  235.             if content then
  236.                 if fs.exists(filename) then fs.delete(filename) end
  237.                 if type(content) == "string" then
  238.                     writeFile(filename, content)
  239.                 elseif type(content) == "table" then
  240.                     for name,cont in pairs(content) do
  241.                         writeFile(fs.combine(filename, name), cont)
  242.                     end
  243.                 else
  244.                     printError("failed...")
  245.                 end
  246.                 if recipID then
  247.                     if rID then
  248.                         goodPrint("gucci!")
  249.                     else
  250.                         goodPrint("gucci from "..tostring(recipID).."!")
  251.                     end
  252.                 else
  253.                     printError("failed...")
  254.                 end
  255.             else
  256.                 printError("failed...")
  257.             end
  258.         end
  259.     end
  260. end
Add Comment
Please, Sign In to add comment