Advertisement
Redxone

[CC - Logsys] - Server

May 17th, 2016
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.77 KB | None | 0 0
  1. if(not fs.exists("sha256"))then
  2.     print("sha256 is required to run this program.")
  3.     print("download? [Y][N]")
  4.     local _, key = os.pullEvent("key")
  5.     if(key == keys.y)then
  6.         --]] Attempt to download SHA256...
  7.         if(http.checkURL("http://pastebin.com/raw/gsFrNjbt") and http)then
  8.             local file = http.get("http://pastebin.com/raw/gsFrNjbt")
  9.             local contents = file.readAll()
  10.             file.close()
  11.             local file = fs.open("sha256","w")
  12.             file.write(contents)
  13.             file.close()
  14.             print("Sha256 downloaded successfully!")
  15.             print("You may have to edit sha256 and remove the 'local' from the function sha256()")
  16.             os.loadAPI("sha256")
  17.         else
  18.             error("Failed to retrieve sha256 (Invaild URL) or HTTP not enabled. ")
  19.         end
  20.     else
  21.         error("Program needs sha256 to run. ")
  22.     end
  23. else
  24.     os.loadAPI("sha256")
  25. end
  26.  
  27. --]] Setup config.
  28. local function checkConfig()
  29.     if(not fs.exists("syslog_config"))then
  30.         print("No config file detected. ")
  31.         write("Enter a server request code: ")
  32.         sreq = read("*")
  33.         write("Where should user accounts be stored: ")
  34.         smethod = read()
  35.         f = fs.open("syslog_config","w")
  36.         f.writeLine( sha256.sha256(sreq) )
  37.         f.writeLine(smethod)
  38.         f.close()
  39.         print("Config file created, starting server...")
  40.         sleep(0.2)
  41.     end
  42. end
  43. --]] Read config and get request code
  44. local function getConfig()
  45.     local f = fs.open("syslog_config","r")
  46.     local reqcode = f.readLine()
  47.     local storagemethod = f.readLine()
  48.     f.close()
  49.     return reqcode, storagemethod
  50. end
  51.  
  52. local function say(msg)
  53.     print("[LOGSYS(" .. textutils.formatTime(os.time(),false) ..  ")] - " .. msg)
  54. end
  55.  
  56. local function getModemSide()
  57.     local listOfSides = rs.getSides()
  58.    
  59.     for i = 1, #listOfSides do
  60.         if(peripheral.isPresent(listOfSides[i]) and peripheral.getType(listOfSides[i]) == "modem")then
  61.             return listOfSides[i]
  62.         end
  63.     end
  64.  
  65.     error("No modem found, please attach a modem. ")
  66. end
  67.  
  68. --]] the "Meat" of the server, its update loop. [[--
  69. checkConfig()
  70. local srcode,store = getConfig()
  71.  
  72. local running = true
  73.  
  74. local requests = {
  75.     ['checkLogin']  = {run=function(margs)
  76.         if(#margs ~= 2)then
  77.             say("Expected 2 arguments got " .. #margs .. ".")
  78.             return false
  79.         end
  80.  
  81.         local uname = margs[1]
  82.         local upass = margs[2]
  83.  
  84.         --]] check for username
  85.         if(fs.exists(store .. "/" .. uname))then
  86.             local cpf = fs.open(store .. "/" .. uname,"r")
  87.             local cpass = cpf.readLine()
  88.             cpf.close()
  89.             --]] compare passwords
  90.             if(upass ~= cpass)then
  91.                 say("Incorrect password for: " .. uname)
  92.             else
  93.                 say("Sent info on User: " .. uname)
  94.             end
  95.  
  96.             return upass == cpass
  97.         else
  98.             say("No such user: " .. uname)
  99.             return false
  100.         end
  101.  
  102.     end},
  103.     ['createLogin'] = {run=function(margs)
  104.         if(#margs ~= 2)then
  105.             say("Expected 2 arguments got " .. #margs .. ".")
  106.             return false
  107.         end
  108.  
  109.         local uname = margs[1]
  110.         local upass = margs[2]
  111.  
  112.         --]] check for username
  113.         if(fs.exists(store .. "/" .. uname))then
  114.             say("User already exists: " .. uname)
  115.             return 'username-taken'
  116.         else
  117.             local acc = fs.open(store .. "/" .. uname, "w")
  118.             acc.write(upass)
  119.             acc.close()
  120.             say("Account created: " .. uname .. ".")
  121.             return true
  122.         end
  123.  
  124.     end},
  125.     ['deleteLogin'] = {run=function(margs)
  126.         if(#margs ~= 2)then
  127.             say("Expected 2 arguments got " .. #margs .. ".")
  128.             return false
  129.         end
  130.  
  131.         local uname = margs[1]
  132.         local upass = margs[2]
  133.  
  134.         --]] check for username
  135.         if(fs.exists(store .. "/" .. uname))then
  136.             local cpf = fs.open(store .. "/" .. uname,"r")
  137.             local cpass = cpf.readLine()
  138.             cpf.close()
  139.             --]] compare passwords
  140.             if(upass ~= cpass)then
  141.                 say("Incorrect password for user: " .. uname)
  142.                 return false
  143.             end
  144.             fs.delete(store .. "/" .. uname)
  145.             say("User deleted: " .. uname)
  146.             return true
  147.         else
  148.             say("No such user: " .. uname)
  149.             return false
  150.         end
  151.     end},
  152. }
  153.  
  154. local function getRequestInfo()
  155.     local cid, message, protocol = rednet.receive(srcode,2)
  156.     return message
  157. end
  158.  
  159. local w,h = term.getSize()
  160. local function clearlog()
  161.     term.setBackgroundColor(colors.lightBlue)
  162.     term.clear()
  163.     term.setTextColor(colors.white)
  164.     paintutils.drawLine(1,1,w,1,colors.blue)
  165.     term.setCursorPos(1,1)
  166.     write("SysLog Database Server. ")
  167.     term.setBackgroundColor(colors.lightBlue)
  168.     term.setCursorPos(1,2)
  169.     term.setTextColor(colors.blue)
  170. end
  171. clearlog()
  172.  
  173. rednet.open(getModemSide())
  174. while true do
  175.     local cid, request, protocol = rednet.receive(srcode)
  176.         say("Received verification code.")
  177.         rednet.send(cid,"[LOGSYS]_HELLO",srcode)
  178.         local request = getRequestInfo()
  179.         local argtable = getRequestInfo()
  180.  
  181.         if(type(argtable) == "table")then
  182.             for k,v in pairs(requests) do
  183.                 if(request == k)then
  184.                     say("Received request: " .. k .. ".")
  185.                     rednet.send(cid,v.run(argtable),srcode)
  186.                 end
  187.             end
  188.         else
  189.             say("Invalid request type. ")      
  190.         end
  191. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement