Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------FTP Server v1.0---------
- ---------------Program---------------
- --------------by Creator-------------
- --Variables--
- local ftpDir = "disk/FTPdata"
- local textutilsserialize = textutils.serialize
- local textutilsunserialize = textutils.unserialize
- local openSessions = {}
- local userConfig = {}
- local userData = "users"
- local w,h = term.getSize()
- local oldPullEvent = os.pullEvent
- local pullEvent = pullEventRaw
- local openSessions = {}
- local bgColor = colors.black
- --APIs--
- os.loadAPI("apis/sha")
- os.loadAPI("IPclient")
- os.loadAPI("apis/encrypt")
- os.loadAPI("apis/aes")
- IPclient.toReturn()
- --Functions--
- function clear()
- term.setBackgroundColor(bgColor)
- term.setCursorPos(1,1)
- term.clear()
- end
- function dialogBox(tableInput)
- clear()
- local nTable = {}
- dialogBoxWindow = window.create(tableInput[1],tableInput[2],tableInput[3],tableInput[4],tableInput[5])
- dialogBoxWindow.setBackgroundColor(tableInput[9])
- dialogBoxWindow.setCursorPos(2,1)
- dialogBoxWindow.clearLine()
- dialogBoxWindow.setTextColor(tableInput[8])
- dialogBoxWindow.write(tableInput[7])
- dialogBoxWindow.setCursorPos(1,2)
- dialogBoxWindow.setBackgroundColor(tableInput[12])
- for i = 2 , tableInput[5] do
- dialogBoxWindow.setCursorPos(1,i)
- dialogBoxWindow.clearLine()
- end
- dialogBoxWindow.setCursorPos(1,2)
- dialogBoxWindow.setTextColor(tableInput[11])
- for token in string.gmatch(tableInput[10],"[^%%]+") do
- nTable[#nTable+1] = token
- end
- for i,v in pairs(nTable) do
- dialogBoxWindow.setCursorPos(2,1+i)
- dialogBoxWindow.write(v)
- end
- local ratio = math.floor(tableInput[4]/#tableInput[13])
- for i,v in pairs(tableInput[13]) do
- local iHelp = 2+(i-1)*ratio
- dialogBoxWindow.setCursorPos(iHelp,tableInput[5]-1)
- dialogBoxWindow.setTextColor(tableInput[13][i][2])
- dialogBoxWindow.setBackgroundColor(tableInput[13][i][3])
- dialogBoxWindow.write(" "..tableInput[13][i][1].." ")
- end
- local buttonNotClicked = true
- while buttonNotClicked do
- local event, button, xPos, yPos = os.pullEvent("mouse_click")
- local yPosToCompare = tableInput[3] + tableInput[5] - 2
- local xPosToCompareMin = tableInput[2]
- local xPosToCompareMax = tableInput[2] + tableInput[4] - 1
- term.setCursorPos(1,1)
- if tonumber(yPos) == tonumber(yPosToCompare) then
- if xPosToCompareMin <= xPos and xPos <= xPosToCompareMax then
- for i,v in pairs(tableInput[13]) do
- local iHelp = 2+(i-1)*ratio
- if iHelp + tableInput[2] - 1 <= xPos and xPos <= iHelp + #tableInput[13][i][1] + tableInput[2] then
- buttonNotClicked = false
- toReturn = tableInput[13][i][4]
- return toReturn
- end
- end
- end
- end
- end
- end
- function loadUsers()
- if not fs.exists(ftpDir) then
- fs.makeDir(ftpDir)
- end
- if fs.exists(ftpDir.."/"..userData) then
- local userList = fs.open(ftpDir.."/"..userData,"r")
- local tempData = userList.readAll()
- userList.close()
- if pcall(textutilsunserialize,tempData) then
- userConfig = textutilsunserialize(tempData)
- term.write("Loading users...")
- print(userList.readAll())
- else
- local answer = dialogBox({
- term.current(),
- math.floor((w-30)/2),
- math.floor((h-6)/2),
- 30,
- 6,
- true,
- "Error",
- colors.red,
- colors.lightBlue,
- "The file "..ftpDir.."/"..userData.." has been corrupted.%Do you want to delete it? ",
- colors.black,
- colors.gray,
- {
- {
- "Yes",
- colors.black,
- colors.blue,
- "yes",
- },
- {
- "No",
- colors.black,
- colors.blue,
- "no",
- },
- },
- })
- if answer == "yes" then
- fs.delete(ftpDir.."/"..userData)
- end
- end
- end
- end
- function saveUsers()
- local userList = fs.open(ftpDir.."/"..userData, "w")
- userList.write(textutilsserialize(userConfig))
- userList.close()
- end
- function getFiles(dir)
- local bufferFiles = {}
- for i,v in pairs(fs.list(dir)) do
- if fs.isDir(dir.."/"..v) then
- bufferFiles[#bufferFiles+1] = {v,"folder"}
- else
- bufferFiles[#bufferFiles+1] = {v,"file"}
- end
- end
- return bufferFiles
- end
- function login(sender,message)
- local success = false
- print(textutilsserialize(userConfig))
- if userConfig ~= nil then
- for i,v in pairs(userConfig) do
- if v[1] == message[3][1] then
- if v[2] == message[3][2] then
- openSessions[#openSessions + 1] = {sender,v[1],v[2]}
- success = true
- end
- end
- end
- end
- if userConfig == nil then
- IPclient.send(sender,{"ftp",0,{"fail"}})
- print("Sent fail")
- end
- if success == true then
- IPclient.send(sender,{"ftp",0,{"success"}})
- else
- IPclient.send(sender,{"ftp",0,{"fail"}})
- end
- end
- function register(sender,message)
- local checkIfValid = true
- for token in string.gmatch(message[3][1],"%p") do
- if token ~= nil then
- checkIfValid = false
- end
- end
- for token in string.gmatch(message[3][1],"%s") do
- if token ~= nil then
- checkIfValid = false
- end
- end
- if checkIfValid then
- local userExists = false
- if userConfig ~= nil then
- for i,v in pairs(userConfig) do
- if v[1] == message[3][1] then
- userExists = true
- end
- end
- end
- if userExists then
- IPclient.send(sender,{"ftp",22,{"fail"}})
- else
- if userConfig ~= nil then
- userConfig[#userConfig+1] = {message[3][1],message[3][2]}
- else
- userConfig = {{message[3][1],message[3][2]}}
- end
- IPclient.send(sender,{"ftp",22,{"success"}})
- saveUsers()
- end
- else
- IPclient.send(sender,{"ftp",22,{"fail"}})
- end
- fs.makeDir(ftpDir.."/userData/"..message[3][1])
- end
- function requestDirCont(sender,message)
- local user
- if openSessions == nil then
- IPclient.send(sender,{"ftp",4,{"fail"}})
- elseif #openSessions == 0 then
- IPclient.send(sender,{"ftp",4,{"fail"}})
- else
- for i,v in pairs(openSessions) do
- if v[1][1] == sender[1] and v[1][2] == sender[2] and v[1][3] == sender[3] and v[1][4] == sender[4] then
- user = v[2]
- end
- end
- end
- if message[3][1] == nil or message[3][1] == "" then
- message[3][1] = "/"
- end
- local files = getFiles(ftpDir.."/userData/"..user..message[3][1])
- IPclient.send(sender,{"ftp",4,{files}})
- end
- function deleteFile(sender,message)
- local user
- if openSessions == nil then
- IPclient.send(sender,{"ftp",4,{"fail"}})
- elseif #openSessions == 0 then
- IPclient.send(sender,{"ftp",4,{"fail"}})
- else
- for i,v in pairs(openSessions) do
- if v[1][1] == sender[1] and v[1][2] == sender[2] and v[1][3] == sender[3] and v[1][4] == sender[4] then
- user = v[2]
- end
- end
- end
- if message[3][1] == nil or message[3][1] == "" then
- message[3][1] = "/"
- end
- fs.delete(ftpDir.."/userData/"..user..message[3][1])
- end
- function makeFolder(sender,message)
- local user
- if openSessions == nil then
- IPclient.send(sender,{"ftp",4,{"fail"}})
- elseif #openSessions == 0 then
- IPclient.send(sender,{"ftp",4,{"fail"}})
- else
- for i,v in pairs(openSessions) do
- if v[1][1] == sender[1] and v[1][2] == sender[2] and v[1][3] == sender[3] and v[1][4] == sender[4] then
- user = v[2]
- end
- end
- end
- if message[3][1] == nil or message[3][1] == "" then
- message[3][1] = "/"
- end
- fs.makeDir(ftpDir.."/userData/"..user..message[3][1])
- end
- function saveFile(sender,message)
- local user
- if openSessions == nil then
- IPclient.send(sender,{"ftp",4,{"fail"}})
- elseif #openSessions == 0 then
- IPclient.send(sender,{"ftp",4,{"fail"}})
- else
- for i,v in pairs(openSessions) do
- if v[1][1] == sender[1] and v[1][2] == sender[2] and v[1][3] == sender[3] and v[1][4] == sender[4] then
- user = v[2]
- end
- end
- end
- if message[3][1] == nil or message[3][1] == "" then
- message[3][1] = "/"
- end
- file = fs.open(ftpDir.."/userData/"..user..message[3][1],"w")
- file.write(message[3][2])
- file.close()
- end
- function downloadFile(sender,message)
- local user
- if openSessions == nil then
- IPclient.send(sender,{"ftp",4,{"fail"}})
- elseif #openSessions == 0 then
- IPclient.send(sender,{"ftp",4,{"fail"}})
- else
- for i,v in pairs(openSessions) do
- if v[1][1] == sender[1] and v[1][2] == sender[2] and v[1][3] == sender[3] and v[1][4] == sender[4] then
- user = v[2]
- end
- end
- end
- if message[3][1] == nil or message[3][1] == "" then
- message[3][1] = "/"
- end
- file = fs.open(ftpDir.."/userData/"..user..message[3][1],"r")
- IPclient.send(sender,{"ftp",12,{file.readAll()}})
- file.close()
- end
- function moveFileOrFolder(sender,message)
- local user
- if openSessions == nil then
- IPclient.send(sender,{"ftp",4,{"fail"}})
- elseif #openSessions == 0 then
- IPclient.send(sender,{"ftp",4,{"fail"}})
- else
- for i,v in pairs(openSessions) do
- if v[1][1] == sender[1] and v[1][2] == sender[2] and v[1][3] == sender[3] and v[1][4] == sender[4] then
- user = v[2]
- end
- end
- end
- if message[3][1] == nil or message[3][1] == "" then
- message[3][1] = "/"
- end
- fs.move(ftpDir.."/userData/"..user..message[3][1],ftpDir.."/userData/"..user..message[3][2])
- end
- function serveRequests()
- while true do
- messageToParse = IPclient.receive()
- if pcall(textutilsunserialize,messageToParse) then
- messageTable = textutilsunserialize(messageToParse)
- sender = messageTable[2]
- message = messageTable[3]
- if message[1] == "ftp" then
- print("ftp protocol")
- if message[2] == 1 then
- login(sender,message)
- elseif message[2] == 5 then
- pcall(requestDirCont,sender,message)
- elseif message[2] == 7 then
- pcall(deleteFile,sender,message)
- elseif message[2] == 9 then
- pcall(makeFolder,sender,message)
- elseif message[2] == 11 then
- pcall(saveFile,sender,message)
- elseif message[2] == 13 then
- pcall(downloadFile,sender,message)
- elseif message[2] == 15 then
- pcall(moveFileOrFolder,sender,message)
- elseif message[2] == 23 then
- register(sender,message)
- end
- end
- end
- end
- end
- clear()
- loadUsers()
- serveRequests()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement