Advertisement
MrHG

FTPServer 3.1

Jan 20th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 KB | None | 0 0
  1. -- RedNet FTP Server
  2. protocol = "FTP"
  3. hostname = "FTPServer"
  4. uploadPass = ""
  5. dirName = "files/"
  6. programName = "File Transfer Server "
  7. versionNumber = "3.1"
  8. modemSide = ""
  9.  
  10. -- Find Wireless Modem
  11. function FindModem()
  12.  modemFound = false
  13.  side = ""
  14.  local pList = peripheral.getNames()
  15.  for i = 1, #pList do
  16.   if peripheral.getType(pList[i]) == "modem" then
  17.    modemFound = true
  18.    side = pList[i]
  19.   end
  20.  end
  21.  if modemFound then
  22.   return side
  23.  else
  24.   return false
  25.  end
  26. end
  27.  
  28. function WriteFile(name,filecontents)
  29.  if fs.exists(dirName..name) then
  30.   fs.delete(dirName..name)
  31.  end
  32.  file = fs.open(dirName..name,"w")
  33.  file.write(filecontents)
  34.  file.close()
  35. end
  36.  
  37. function TransmitFile(id,filename)
  38.  file = fs.open(dirName..filename,"r")
  39.  rednet.send(id,file.readAll(),protocol)
  40.  file.close()
  41. end
  42.  
  43. if FindModem() == false then
  44.  print("Modem not Found")
  45.  return
  46. else
  47.  modemSide = FindModem()
  48. end
  49.  
  50. if not fs.exists(dirName) then
  51.  fs.makeDir(dirName)
  52. end
  53.  
  54. rednet.open(modemSide)
  55. rednet.host(protocol,hostname)
  56.  
  57. -- Wait for Request
  58. term.clear()
  59. term.setCursorPos(1,1)
  60. print(programName..versionNumber)
  61. while true do
  62. id, msg = rednet.receive(protocol)
  63.  -- If Request for List
  64.  if msg == "LIST" then
  65.   Flist = fs.list(dirName)
  66.   HRlist = "\n"
  67.    for i=1, #Flist do
  68.     if not fs.isDir(Flist[i]) then
  69.      HRlist = HRlist..tostring(Flist[i]).."\n"
  70.     end
  71.    end
  72.   print("LIST:"..id)
  73.   rednet.send(id,HRlist,protocol)
  74.  end
  75.  -- If Request to upload file
  76.   -- REQUEST 1
  77.  if msg == "SEND" then
  78.   -- REQUEST 2
  79.   id2, cPass = rednet.receive(protocol)
  80.   if not id2 == id then
  81.    print("CONNECTION BREAK.")
  82.    return
  83.   end
  84.   if cPass == uploadPass then
  85.    -- REQUEST 3
  86.    rednet.send(id,"GRANTED",protocol)
  87.    -- REQUEST 4
  88.    local id1, pName = rednet.receive(protocol)
  89.    -- REQUEST 5
  90.    local id2, pContents = rednet.receive(protocol)
  91.    WriteFile(pName,pContents)
  92.    print("UPLOAD:"..pName..":"..id1)
  93.   else
  94.    rednet.send(id,"DENIED",protocol)
  95.    print("DENY:"..id)
  96.   end
  97.  end
  98.  
  99.  -- If Request for File
  100.  if fs.exists(dirName..msg) then
  101.   rednet.send(id,"true",protocol)
  102.   TransmitFile(id,msg)
  103.   print("SEND:"..msg..":"..tostring(id))
  104.  else
  105.   rednet.send(id,"false",protocol)
  106.  end
  107. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement