Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(not fs.exists("sha256"))then
- print("sha256 is required to run this program.")
- print("download? [Y][N]")
- local _, key = os.pullEvent("key")
- if(key == keys.y)then
- --]] Attempt to download SHA256...
- if(http.checkURL("http://pastebin.com/raw/gsFrNjbt") and http)then
- local file = http.get("http://pastebin.com/raw/gsFrNjbt")
- local contents = file.readAll()
- file.close()
- local file = fs.open("sha256","w")
- file.write(contents)
- file.close()
- print("Sha256 downloaded successfully!")
- print("You may have to edit sha256 and remove the 'local' from the function sha256()")
- os.loadAPI("sha256")
- else
- error("Failed to retrieve sha256 (Invaild URL) or HTTP not enabled. ")
- end
- else
- error("Program needs sha256 to run. ")
- end
- else
- os.loadAPI("sha256")
- end
- --]] Setup config.
- local function checkConfig()
- if(not fs.exists("syslog_config"))then
- print("No config file detected. ")
- write("Enter a server request code: ")
- sreq = read("*")
- write("Where should user accounts be stored: ")
- smethod = read()
- f = fs.open("syslog_config","w")
- f.writeLine( sha256.sha256(sreq) )
- f.writeLine(smethod)
- f.close()
- print("Config file created, starting server...")
- sleep(0.2)
- end
- end
- --]] Read config and get request code
- local function getConfig()
- local f = fs.open("syslog_config","r")
- local reqcode = f.readLine()
- local storagemethod = f.readLine()
- f.close()
- return reqcode, storagemethod
- end
- local function say(msg)
- print("[LOGSYS(" .. textutils.formatTime(os.time(),false) .. ")] - " .. msg)
- end
- local function getModemSide()
- local listOfSides = rs.getSides()
- for i = 1, #listOfSides do
- if(peripheral.isPresent(listOfSides[i]) and peripheral.getType(listOfSides[i]) == "modem")then
- return listOfSides[i]
- end
- end
- error("No modem found, please attach a modem. ")
- end
- --]] the "Meat" of the server, its update loop. [[--
- checkConfig()
- local srcode,store = getConfig()
- local running = true
- local requests = {
- ['checkLogin'] = {run=function(margs)
- if(#margs ~= 2)then
- say("Expected 2 arguments got " .. #margs .. ".")
- return false
- end
- local uname = margs[1]
- local upass = margs[2]
- --]] check for username
- if(fs.exists(store .. "/" .. uname))then
- local cpf = fs.open(store .. "/" .. uname,"r")
- local cpass = cpf.readLine()
- cpf.close()
- --]] compare passwords
- if(upass ~= cpass)then
- say("Incorrect password for: " .. uname)
- else
- say("Sent info on User: " .. uname)
- end
- return upass == cpass
- else
- say("No such user: " .. uname)
- return false
- end
- end},
- ['createLogin'] = {run=function(margs)
- if(#margs ~= 2)then
- say("Expected 2 arguments got " .. #margs .. ".")
- return false
- end
- local uname = margs[1]
- local upass = margs[2]
- --]] check for username
- if(fs.exists(store .. "/" .. uname))then
- say("User already exists: " .. uname)
- return 'username-taken'
- else
- local acc = fs.open(store .. "/" .. uname, "w")
- acc.write(upass)
- acc.close()
- say("Account created: " .. uname .. ".")
- return true
- end
- end},
- ['deleteLogin'] = {run=function(margs)
- if(#margs ~= 2)then
- say("Expected 2 arguments got " .. #margs .. ".")
- return false
- end
- local uname = margs[1]
- local upass = margs[2]
- --]] check for username
- if(fs.exists(store .. "/" .. uname))then
- local cpf = fs.open(store .. "/" .. uname,"r")
- local cpass = cpf.readLine()
- cpf.close()
- --]] compare passwords
- if(upass ~= cpass)then
- say("Incorrect password for user: " .. uname)
- return false
- end
- fs.delete(store .. "/" .. uname)
- say("User deleted: " .. uname)
- return true
- else
- say("No such user: " .. uname)
- return false
- end
- end},
- }
- local function getRequestInfo()
- local cid, message, protocol = rednet.receive(srcode,2)
- return message
- end
- local w,h = term.getSize()
- local function clearlog()
- term.setBackgroundColor(colors.lightBlue)
- term.clear()
- term.setTextColor(colors.white)
- paintutils.drawLine(1,1,w,1,colors.blue)
- term.setCursorPos(1,1)
- write("SysLog Database Server. ")
- term.setBackgroundColor(colors.lightBlue)
- term.setCursorPos(1,2)
- term.setTextColor(colors.blue)
- end
- clearlog()
- rednet.open(getModemSide())
- while true do
- local cid, request, protocol = rednet.receive(srcode)
- say("Received verification code.")
- rednet.send(cid,"[LOGSYS]_HELLO",srcode)
- local request = getRequestInfo()
- local argtable = getRequestInfo()
- if(type(argtable) == "table")then
- for k,v in pairs(requests) do
- if(request == k)then
- say("Received request: " .. k .. ".")
- rednet.send(cid,v.run(argtable),srcode)
- end
- end
- else
- say("Invalid request type. ")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement