Guest User

sarvar

a guest
Jun 20th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.58 KB | None | 0 0
  1. log = fs.open("log", "a")
  2. rednet.open("bottom")
  3. term.clear()
  4. term.setCursorPos(1,1)
  5. print("Hosting server. CTRL + T to stop.")
  6. while true do
  7. local uploaded = false
  8. log.close()
  9. a1, a3 = rednet.receive("gfile")
  10.  
  11. -- Uploading
  12. if a3 == "upload" then
  13.     print(a1)
  14.     rednet.send(a1, "310", "gfile")
  15.     print("Sent continue 310")
  16.     id, msg = rednet.receive(15)
  17.     if msg then
  18.         if fs.exists(msg) then
  19.             rednet.send(a1, "560", "gfile")
  20.         else
  21.             rednet.send(a1, "311", "gfile")
  22.             fName = msg
  23.             id, msg = rednet.receive(5)
  24.             hUP = fs.open(fName, "w")
  25.             hUP.write(msg)
  26.             hUP.close()
  27.             rednet.send(a1, "200", "gfile")
  28.             print(a1.." Successfully uploaded file "..fName)
  29.             uploaded = true
  30.             end
  31.     elseif id ~= nil then
  32.         print("Disconnected from "..a1.." due to inactivity.")
  33.         end
  34.    
  35. -- Downloading
  36. else
  37. log = fs.open("log", "a")
  38. if uploaded == false then
  39. aTime = os.time().."-"..os.day()
  40. print(aTime.." - Serving request from "..a1.." for file "..a3)
  41. reqfile = a3
  42. if fs.exists(reqfile) then
  43. if not fs.isDir(reqfile) then
  44. hRF = fs.open(reqfile, "r")
  45. cont = hRF.readAll()
  46. hRF.close()
  47. rednet.send(a1, cont, "gfile")
  48. log.writeLine(aTime.." - Successfully served "..a1.." file "..a3)
  49. else
  50. rednet.send(a1, "404a", "gfile")
  51. log.writeLine(aTime.." - Denied service to "..a1..", Tried to request directory")
  52. end
  53. else
  54. rednet.send(a1, "404", "gfile")
  55. log.writeLine(aTime.." - Denied service to "..a1..", Reason 404")
  56. end
  57. end
  58. end
  59. end
Advertisement
Add Comment
Please, Sign In to add comment