Reinified

(SERVER) gFile-2.0

Apr 22nd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. -- gFile server by Reinified (IGN Fifti)
  2. -- For client version 2.0
  3. -- Server version 2.0
  4. -- 22/4/18
  5.  
  6.  
  7. -- function check
  8. function check(request,id)
  9.  
  10. if request.action ~= "u" and request.action ~= "d" then
  11. rednet.send(id, "gf_4") -- invalid request
  12. return false
  13. end
  14.  
  15. if sUseU == "y" and users[request.name] == nil then
  16. print("Denied service to " .. request.name .. " (" .. id .. "), not whitelisted.")
  17. rednet.send(id, "gf_3_0")
  18. return false
  19. end
  20.  
  21. if sUseK == "y" and request.code ~= sID then
  22. print("Denied service to " .. request.name .. " (" .. id .. "), incorrect code.")
  23. rednet.send(id, "gf_3_1")
  24. return false
  25. end
  26.  
  27. if request.action == "u" then
  28.  
  29. if string.find(request.fileName, "/") then
  30. rednet.send(id, "gf_5")
  31. return false
  32. end
  33.  
  34. if string.sub(request.fileContent,1,3) == "gf_" then
  35. rednet.send(id, "gf_6")
  36. return false
  37. end
  38.  
  39. if fs.exists(request.fileName) then
  40. rednet.send(id, "gf_9")
  41. return false
  42. end
  43.  
  44. else -- action is d
  45.  
  46. if not fs.exists(request.file) then
  47. rednet.send(id, "gf_2")
  48. return false
  49. end
  50.  
  51. if request.file == "server-users" or request.file == "server-config" then
  52. rednet.send(id, "gf_7")
  53. return false
  54. end
  55.  
  56. if fs.isDir(request.file) then
  57. rednet.send(id, "gf_8")
  58. return false
  59. end
  60.  
  61.  
  62. end
  63.  
  64. return true
  65. end
  66. -- Server for gFile. By Reinified.
  67. print("Enter server key:")
  68. write("> ")
  69. sID = read()
  70.  
  71.  
  72. -- Reading config
  73. if fs.exists("server-config") then
  74. fC = fs.open("server-config", "r")
  75. sUseU = fC.readLine(1) -- this should always be y or n
  76. if sUseU ~= "y" and sUseU ~= "n" then
  77. print("Invalid config file. Deleting config file...")
  78. fs.delete("server-config")
  79. print("Restart the server to continue")
  80. fC.close()
  81. return false
  82. end
  83. sUseK = fC.readLine(2) -- also y or n
  84. if sUseK ~= "y" and sUseK ~= "n" then
  85. print("Invalid config file. Deleting config file...")
  86. fs.delete("server-config")
  87. print("Restart the server to continue")
  88. fC.close()
  89. return false
  90. end
  91. else
  92. fU = fs.open("server-config", "w")
  93. fU.writeLine("n")
  94. fU.writeLine("n")
  95. fU.close()
  96. end
  97.  
  98. -- Getting users
  99. if fs.exists("server-users") then
  100. fU = fs.open("server-users", "r")
  101. local users = textutils.unserialize(fU.readAll())
  102. fU.close()
  103. else local users = {}
  104. fU = fs.open("server-users", "w")
  105. users["tom"] = true
  106. fU.write(textutils.serialize(users))
  107. fU.close()
  108. end
  109.  
  110. log = fs.open("log", "a")
  111. rednet.open("bottom")
  112. aTime = os.time().."-"..os.day()
  113. log.writeLine(aTime.." - Starting server using key "..sID)
  114. term.clear()
  115. term.setCursorPos(1,1)
  116. print("Hosting server. CTRL + T to stop")
  117.  
  118. -- Actually running the server
  119. while true do
  120. id, req = rednet.receive("gfp")
  121. local request = req
  122.  
  123. -- Checking whole request
  124. ans = check(request, id)
  125.  
  126. if ans == true then
  127. log = fs.open("log", "a")
  128.  
  129. if request.action == "u" then
  130. fT = fs.open(request.fileName, "w")
  131. fT.write(request.fileContent)
  132. fT.close()
  133. print("User " .. request.name .. " (" .. id .. ")" .. " Uploaded file " .. request.fileName)
  134. log.writeLine("User " .. request.name .. " (" .. id .. ")" .. " Uploaded file " .. request.fileName)
  135. rednet.send(id, "gf_1")
  136.  
  137. else -- request is download if this runs
  138. fTd = fs.open(request.file, "r")
  139. local content = fTd.readAll()
  140. rednet.send(id, content) -- sending file content to user
  141. print("Sent file " .. request.file .. " to user " .. request.name .. " (" .. id .. ")")
  142. log.writeLine("Sent file " .. request.file .. " to user " .. request.name .. " (" .. id .. ")")
  143.  
  144. end -- end of request handler
  145. log.close()
  146.  
  147.  
  148. end -- end of check answer if
  149.  
  150.  
  151. end -- end of while true loop at line 40
Add Comment
Please, Sign In to add comment