Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- gFile server by Reinified (IGN Fifti)
- -- For client version 2.0
- -- Server version 2.0
- -- 22/4/18
- -- function check
- function check(request,id)
- if request.action ~= "u" and request.action ~= "d" then
- rednet.send(id, "gf_4") -- invalid request
- return false
- end
- if sUseU == "y" and users[request.name] == nil then
- print("Denied service to " .. request.name .. " (" .. id .. "), not whitelisted.")
- rednet.send(id, "gf_3_0")
- return false
- end
- if sUseK == "y" and request.code ~= sID then
- print("Denied service to " .. request.name .. " (" .. id .. "), incorrect code.")
- rednet.send(id, "gf_3_1")
- return false
- end
- if request.action == "u" then
- if string.find(request.fileName, "/") then
- rednet.send(id, "gf_5")
- return false
- end
- if string.sub(request.fileContent,1,3) == "gf_" then
- rednet.send(id, "gf_6")
- return false
- end
- if fs.exists(request.fileName) then
- rednet.send(id, "gf_9")
- return false
- end
- else -- action is d
- if not fs.exists(request.file) then
- rednet.send(id, "gf_2")
- return false
- end
- if request.file == "server-users" or request.file == "server-config" then
- rednet.send(id, "gf_7")
- return false
- end
- if fs.isDir(request.file) then
- rednet.send(id, "gf_8")
- return false
- end
- end
- return true
- end
- -- Server for gFile. By Reinified.
- print("Enter server key:")
- write("> ")
- sID = read()
- -- Reading config
- if fs.exists("server-config") then
- fC = fs.open("server-config", "r")
- sUseU = fC.readLine(1) -- this should always be y or n
- if sUseU ~= "y" and sUseU ~= "n" then
- print("Invalid config file. Deleting config file...")
- fs.delete("server-config")
- print("Restart the server to continue")
- fC.close()
- return false
- end
- sUseK = fC.readLine(2) -- also y or n
- if sUseK ~= "y" and sUseK ~= "n" then
- print("Invalid config file. Deleting config file...")
- fs.delete("server-config")
- print("Restart the server to continue")
- fC.close()
- return false
- end
- else
- fU = fs.open("server-config", "w")
- fU.writeLine("n")
- fU.writeLine("n")
- fU.close()
- end
- -- Getting users
- if fs.exists("server-users") then
- fU = fs.open("server-users", "r")
- local users = textutils.unserialize(fU.readAll())
- fU.close()
- else local users = {}
- fU = fs.open("server-users", "w")
- users["tom"] = true
- fU.write(textutils.serialize(users))
- fU.close()
- end
- log = fs.open("log", "a")
- rednet.open("bottom")
- aTime = os.time().."-"..os.day()
- log.writeLine(aTime.." - Starting server using key "..sID)
- term.clear()
- term.setCursorPos(1,1)
- print("Hosting server. CTRL + T to stop")
- -- Actually running the server
- while true do
- id, req = rednet.receive("gfp")
- local request = req
- -- Checking whole request
- ans = check(request, id)
- if ans == true then
- log = fs.open("log", "a")
- if request.action == "u" then
- fT = fs.open(request.fileName, "w")
- fT.write(request.fileContent)
- fT.close()
- print("User " .. request.name .. " (" .. id .. ")" .. " Uploaded file " .. request.fileName)
- log.writeLine("User " .. request.name .. " (" .. id .. ")" .. " Uploaded file " .. request.fileName)
- rednet.send(id, "gf_1")
- else -- request is download if this runs
- fTd = fs.open(request.file, "r")
- local content = fTd.readAll()
- rednet.send(id, content) -- sending file content to user
- print("Sent file " .. request.file .. " to user " .. request.name .. " (" .. id .. ")")
- log.writeLine("Sent file " .. request.file .. " to user " .. request.name .. " (" .. id .. ")")
- end -- end of request handler
- log.close()
- end -- end of check answer if
- end -- end of while true loop at line 40
Add Comment
Please, Sign In to add comment