Advertisement
programcreator

FTP: server [WIP][Requires IPnet]

Feb 9th, 2015
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.76 KB | None | 0 0
  1. -------------FTP Server v1.0---------
  2. ---------------Program---------------
  3. --------------by Creator-------------
  4.  
  5. --Variables--
  6. local ftpDir = "disk/FTPdata"
  7. local textutilsserialize = textutils.serialize
  8. local textutilsunserialize = textutils.unserialize
  9. local openSessions = {}
  10. local userConfig = {}
  11. local userData = "users"
  12. local w,h = term.getSize()
  13. local oldPullEvent = os.pullEvent
  14. local pullEvent = pullEventRaw
  15. local openSessions = {}
  16. local bgColor = colors.black
  17. --APIs--
  18.  
  19. os.loadAPI("apis/sha")
  20. os.loadAPI("IPclient")
  21. os.loadAPI("apis/encrypt")
  22. os.loadAPI("apis/aes")
  23. IPclient.toReturn()
  24.  
  25. --Functions--
  26. function clear()
  27.     term.setBackgroundColor(bgColor)
  28.     term.setCursorPos(1,1)
  29.     term.clear()
  30. end
  31.  
  32. function dialogBox(tableInput)
  33.     clear()
  34.     local nTable = {}
  35.     dialogBoxWindow = window.create(tableInput[1],tableInput[2],tableInput[3],tableInput[4],tableInput[5])
  36.     dialogBoxWindow.setBackgroundColor(tableInput[9])
  37.     dialogBoxWindow.setCursorPos(2,1)
  38.     dialogBoxWindow.clearLine()
  39.     dialogBoxWindow.setTextColor(tableInput[8])
  40.     dialogBoxWindow.write(tableInput[7])
  41.     dialogBoxWindow.setCursorPos(1,2)
  42.     dialogBoxWindow.setBackgroundColor(tableInput[12])
  43.     for i = 2 , tableInput[5] do
  44.         dialogBoxWindow.setCursorPos(1,i)
  45.         dialogBoxWindow.clearLine()
  46.     end
  47.     dialogBoxWindow.setCursorPos(1,2)
  48.     dialogBoxWindow.setTextColor(tableInput[11])
  49.     for token in string.gmatch(tableInput[10],"[^%%]+") do
  50.         nTable[#nTable+1] = token
  51.     end
  52.     for i,v in pairs(nTable) do
  53.         dialogBoxWindow.setCursorPos(2,1+i)
  54.         dialogBoxWindow.write(v)
  55.     end
  56.     local ratio = math.floor(tableInput[4]/#tableInput[13])
  57.     for i,v in pairs(tableInput[13]) do
  58.         local iHelp = 2+(i-1)*ratio
  59.         dialogBoxWindow.setCursorPos(iHelp,tableInput[5]-1)
  60.         dialogBoxWindow.setTextColor(tableInput[13][i][2])
  61.         dialogBoxWindow.setBackgroundColor(tableInput[13][i][3])
  62.         dialogBoxWindow.write(" "..tableInput[13][i][1].." ")
  63.     end
  64.    
  65.     local buttonNotClicked = true
  66.     while buttonNotClicked do
  67.         local event, button, xPos, yPos = os.pullEvent("mouse_click")
  68.         local yPosToCompare = tableInput[3] + tableInput[5] - 2
  69.         local xPosToCompareMin = tableInput[2]
  70.         local xPosToCompareMax = tableInput[2] + tableInput[4] - 1
  71.         term.setCursorPos(1,1)
  72.         if tonumber(yPos) == tonumber(yPosToCompare) then
  73.             if xPosToCompareMin <= xPos and xPos <= xPosToCompareMax then
  74.                 for i,v in pairs(tableInput[13]) do
  75.                     local iHelp = 2+(i-1)*ratio
  76.                     if iHelp + tableInput[2] - 1 <= xPos and xPos <= iHelp + #tableInput[13][i][1] + tableInput[2] then
  77.                         buttonNotClicked = false
  78.                         toReturn = tableInput[13][i][4]
  79.                         return toReturn
  80.                     end
  81.                 end
  82.             end
  83.         end
  84.     end
  85. end
  86.  
  87. function loadUsers()
  88.     if not fs.exists(ftpDir) then
  89.         fs.makeDir(ftpDir)
  90.     end
  91.     if fs.exists(ftpDir.."/"..userData) then
  92.         local userList = fs.open(ftpDir.."/"..userData,"r")
  93.         local tempData = userList.readAll()
  94.         userList.close()
  95.         if pcall(textutilsunserialize,tempData) then
  96.             userConfig = textutilsunserialize(tempData)
  97.             term.write("Loading users...")
  98.             print(userList.readAll())
  99.         else
  100.             local answer = dialogBox({
  101.                 term.current(),
  102.                 math.floor((w-30)/2),
  103.                 math.floor((h-6)/2),
  104.                 30,
  105.                 6,
  106.                 true,
  107.                 "Error",
  108.                 colors.red,
  109.                 colors.lightBlue,
  110.                 "The file "..ftpDir.."/"..userData.." has been corrupted.%Do you want to delete it? ",
  111.                 colors.black,
  112.                 colors.gray,
  113.                 {
  114.                     {
  115.                         "Yes",
  116.                         colors.black,
  117.                         colors.blue,
  118.                         "yes",
  119.                     },
  120.                     {
  121.                         "No",
  122.                         colors.black,
  123.                         colors.blue,
  124.                         "no",
  125.                     },
  126.                 },
  127.             })
  128.             if answer == "yes" then
  129.                 fs.delete(ftpDir.."/"..userData)
  130.             end
  131.         end
  132.     end
  133. end
  134.  
  135. function saveUsers()
  136.     local userList = fs.open(ftpDir.."/"..userData, "w")
  137.     userList.write(textutilsserialize(userConfig))
  138.     userList.close()
  139. end
  140.  
  141. function getFiles(dir)
  142.     local bufferFiles = {}
  143.     for i,v in pairs(fs.list(dir)) do
  144.         if fs.isDir(dir.."/"..v) then
  145.             bufferFiles[#bufferFiles+1] = {v,"folder"}
  146.         else
  147.             bufferFiles[#bufferFiles+1] = {v,"file"}
  148.         end
  149.     end
  150.     return bufferFiles
  151. end
  152.  
  153. function login(sender,message)
  154.     local success = false
  155.     print(textutilsserialize(userConfig))
  156.     if userConfig ~= nil then
  157.         for i,v in pairs(userConfig) do
  158.             if v[1] == message[3][1] then
  159.                 if v[2] == message[3][2] then
  160.                     openSessions[#openSessions + 1] = {sender,v[1],v[2]}
  161.                     success = true
  162.                 end
  163.             end
  164.         end
  165.     end
  166.     if userConfig == nil then
  167.         IPclient.send(sender,{"ftp",0,{"fail"}})
  168.         print("Sent fail")
  169.     end
  170.     if success == true then
  171.         IPclient.send(sender,{"ftp",0,{"success"}})
  172.     else
  173.         IPclient.send(sender,{"ftp",0,{"fail"}})
  174.     end
  175. end
  176.  
  177. function register(sender,message)
  178.     local checkIfValid = true
  179.     for token in string.gmatch(message[3][1],"%p") do
  180.         if token ~= nil then
  181.             checkIfValid = false
  182.         end
  183.     end
  184.     for token in string.gmatch(message[3][1],"%s") do
  185.         if token ~= nil then
  186.             checkIfValid = false
  187.         end
  188.     end
  189.     if checkIfValid then
  190.     local userExists = false
  191.     if userConfig ~= nil then
  192.         for i,v in pairs(userConfig) do
  193.             if v[1] == message[3][1] then
  194.                 userExists = true
  195.             end
  196.         end
  197.     end
  198.     if userExists then
  199.         IPclient.send(sender,{"ftp",22,{"fail"}})
  200.     else
  201.         if userConfig ~= nil then
  202.             userConfig[#userConfig+1] = {message[3][1],message[3][2]}
  203.         else
  204.             userConfig = {{message[3][1],message[3][2]}}
  205.         end
  206.         IPclient.send(sender,{"ftp",22,{"success"}})
  207.         saveUsers()
  208.     end
  209.     else
  210.         IPclient.send(sender,{"ftp",22,{"fail"}})
  211.     end
  212.     fs.makeDir(ftpDir.."/userData/"..message[3][1])
  213. end
  214.  
  215. function requestDirCont(sender,message)
  216.     local user
  217.     if openSessions == nil then
  218.         IPclient.send(sender,{"ftp",4,{"fail"}})
  219.     elseif #openSessions == 0 then
  220.         IPclient.send(sender,{"ftp",4,{"fail"}})
  221.     else
  222.         for i,v in pairs(openSessions) do
  223.             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
  224.                 user = v[2]
  225.             end
  226.         end
  227.     end
  228.     if message[3][1] == nil or message[3][1] == "" then
  229.         message[3][1] = "/"
  230.     end
  231.     local files = getFiles(ftpDir.."/userData/"..user..message[3][1])
  232.     IPclient.send(sender,{"ftp",4,{files}})
  233. end
  234.  
  235. function deleteFile(sender,message)
  236.     local user
  237.     if openSessions == nil then
  238.         IPclient.send(sender,{"ftp",4,{"fail"}})
  239.     elseif #openSessions == 0 then
  240.         IPclient.send(sender,{"ftp",4,{"fail"}})
  241.     else
  242.         for i,v in pairs(openSessions) do
  243.             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
  244.                 user = v[2]
  245.             end
  246.         end
  247.     end
  248.     if message[3][1] == nil or message[3][1] == "" then
  249.         message[3][1] = "/"
  250.     end
  251.     fs.delete(ftpDir.."/userData/"..user..message[3][1])
  252. end
  253.  
  254. function makeFolder(sender,message)
  255.     local user
  256.     if openSessions == nil then
  257.         IPclient.send(sender,{"ftp",4,{"fail"}})
  258.     elseif #openSessions == 0 then
  259.         IPclient.send(sender,{"ftp",4,{"fail"}})
  260.     else
  261.         for i,v in pairs(openSessions) do
  262.             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
  263.                 user = v[2]
  264.             end
  265.         end
  266.     end
  267.     if message[3][1] == nil or message[3][1] == "" then
  268.         message[3][1] = "/"
  269.     end
  270.     fs.makeDir(ftpDir.."/userData/"..user..message[3][1])
  271. end
  272.  
  273. function saveFile(sender,message)
  274.     local user
  275.     if openSessions == nil then
  276.         IPclient.send(sender,{"ftp",4,{"fail"}})
  277.     elseif #openSessions == 0 then
  278.         IPclient.send(sender,{"ftp",4,{"fail"}})
  279.     else
  280.         for i,v in pairs(openSessions) do
  281.             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
  282.                 user = v[2]
  283.             end
  284.         end
  285.     end
  286.     if message[3][1] == nil or message[3][1] == "" then
  287.         message[3][1] = "/"
  288.     end
  289.     file = fs.open(ftpDir.."/userData/"..user..message[3][1],"w")
  290.     file.write(message[3][2])
  291.     file.close()
  292. end
  293.  
  294. function downloadFile(sender,message)
  295.     local user
  296.     if openSessions == nil then
  297.         IPclient.send(sender,{"ftp",4,{"fail"}})
  298.     elseif #openSessions == 0 then
  299.         IPclient.send(sender,{"ftp",4,{"fail"}})
  300.     else
  301.         for i,v in pairs(openSessions) do
  302.             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
  303.                 user = v[2]
  304.             end
  305.         end
  306.     end
  307.     if message[3][1] == nil or message[3][1] == "" then
  308.         message[3][1] = "/"
  309.     end
  310.     file = fs.open(ftpDir.."/userData/"..user..message[3][1],"r")
  311.     IPclient.send(sender,{"ftp",12,{file.readAll()}})
  312.     file.close()
  313. end
  314.  
  315. function moveFileOrFolder(sender,message)
  316.     local user
  317.     if openSessions == nil then
  318.         IPclient.send(sender,{"ftp",4,{"fail"}})
  319.     elseif #openSessions == 0 then
  320.         IPclient.send(sender,{"ftp",4,{"fail"}})
  321.     else
  322.         for i,v in pairs(openSessions) do
  323.             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
  324.                 user = v[2]
  325.             end
  326.         end
  327.     end
  328.     if message[3][1] == nil or message[3][1] == "" then
  329.         message[3][1] = "/"
  330.     end
  331.     fs.move(ftpDir.."/userData/"..user..message[3][1],ftpDir.."/userData/"..user..message[3][2])
  332. end
  333.  
  334. function serveRequests()
  335.     while true do
  336.         messageToParse = IPclient.receive()
  337.         if pcall(textutilsunserialize,messageToParse) then
  338.             messageTable = textutilsunserialize(messageToParse)
  339.             sender = messageTable[2]
  340.             message = messageTable[3]
  341.             if message[1] == "ftp" then
  342.                 print("ftp protocol")
  343.                 if message[2] == 1 then
  344.                     login(sender,message)
  345.                 elseif message[2] == 5 then
  346.                     pcall(requestDirCont,sender,message)
  347.                 elseif message[2] == 7 then
  348.                     pcall(deleteFile,sender,message)
  349.                 elseif message[2] == 9 then
  350.                     pcall(makeFolder,sender,message)
  351.                 elseif message[2] == 11 then
  352.                     pcall(saveFile,sender,message)
  353.                 elseif message[2] == 13 then
  354.                     pcall(downloadFile,sender,message)
  355.                 elseif message[2] == 15 then
  356.                     pcall(moveFileOrFolder,sender,message)
  357.                 elseif message[2] == 23 then
  358.                     register(sender,message)
  359.                 end
  360.             end
  361.         end
  362.     end
  363. end
  364.  
  365. clear()
  366. loadUsers()
  367. serveRequests()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement