Advertisement
Guest User

startup

a guest
Oct 1st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.87 KB | None | 0 0
  1. --Var init
  2. local RP_BOOL = true
  3. os.loadAPI("StringUtils")
  4.  
  5. --Log
  6. local function log(ID, oMSG, reason)
  7.   local FILE = fs.open("/log", "a")
  8.  
  9.   if type(oMSG) == "string" then
  10.     toMSG = "bad type(string)"
  11.   elseif type(oMSG) == "table" then
  12.     if oMSG["type"] ~= nil then
  13.       toMSG = "bad table"
  14.     else
  15.       toMSG = oMSG["type"]
  16.     end
  17.   else
  18.     toMSG = "unknown"
  19.   end
  20.  
  21.   local TEMP = "[LOG] ID:"..ID.." toMSG: "..toMSG.." Reason: "..reason
  22.   print(TEMP)
  23.   FILE.writeLine(TEMP)
  24.   FILE.close()
  25.  
  26.   return true
  27. end
  28.  
  29. --Users management
  30. local USER_FILE = "/users"
  31. local DB_SCHEM = {Users = {}, Password = {}, Biolock = {}, AccessLevel = {}, Token= {}}
  32. local function USERS_CREATEDB()
  33.   local USER_TEMP = fs.open(USER_FILE, "w")
  34.   USER_TEMP.write(textutils.serialise(DB_SCHEM))
  35.   USER_TEMP.close()
  36. end
  37.  
  38. local function USERS_GET()
  39.   local OBJECT_UF = fs.open(USER_FILE, "r")
  40.   local USERS = textutils.unserialise(OBJECT_UF.readAll())
  41.   if USERS == nil then
  42.     USERS_CREATEDB()
  43.     USERS = textutils.unserialise(OBJECT_UF.readAll())
  44.   end
  45.   OBJECT_UF.close()
  46.   return USERS
  47. end
  48.  
  49. local function USERS_ID(users, tofind)
  50.   local ID = nil
  51.   for i = 1,#users["Users"] do
  52.     if tofind == nil then
  53.       ID = i
  54.     else
  55.       if users["Users"][i] == tofind then
  56.         ID = i
  57.       end
  58.     end
  59.   end
  60.   return ID
  61. end
  62.  
  63. local function USERS_BIOLOCK(users, tofind)
  64.   local ID = nil
  65.   for i = 1,#users["Biolock"] do
  66.     if tofind == nil then return false
  67.     else
  68.       if users["Biolock"][i] == tofind then
  69.         ID = i
  70.       end
  71.     end
  72.   end
  73.   return ID
  74. end
  75.  
  76. local function USERS_TOKEN(users, tofind)
  77.   local ID = nil
  78.   for i = 1,#users["Token"] do
  79.     if tofind == nil then return false
  80.     else
  81.       if users["Token"][i] == tofind then
  82.         ID = i
  83.       end
  84.     end
  85.   end
  86.   return ID
  87. end
  88.  
  89. local function USERS_CREATE(name, pass, biolock, accesslevel)
  90.   local oUSERS = USERS_GET()
  91.   local fUSERS = oUSERS
  92.   local lastID = USERS_ID(oUSERS)
  93.  
  94.   --Check if user already exist because or AL will not fuck my script ;(
  95.   if USERS_ID(oUSERS, name) ~= nil then return false end
  96.   if accesslevel > 5 or accesslevel < 1 then return false end
  97.   if lastID == nil then lastID = 1 else lastID = lastID + 1 end
  98.   if name == nil or pass == nil or accesslevel == nil then
  99.     return false
  100.   end
  101.   if biolock == nil then biolock = "unknown" end
  102.   --end of check
  103.  
  104.   fUSERS["Users"][lastID] = name
  105.   fUSERS["Password"][lastID] = StringUtils.SHA1(pass)
  106.   fUSERS["Biolock"][lastID] = biolock
  107.   fUSERS["Token"][lastID] = "unknown"
  108.   fUSERS["AccessLevel"][lastID] = accesslevel
  109.   local TO_SAVE = fs.open(USER_FILE,"w")
  110.   TO_SAVE.write(textutils.serialise(fUSERS))
  111.   TO_SAVE.close()
  112.   return true
  113. end
  114.  
  115. local function USERS_UPDATE(toUpdate, userID, newUpdate)
  116.     if type(toUpdate) ~= "string" or type(userID) ~= "number" or newUpdate == nil then return false end
  117.     local oUSERS = USERS_GET()
  118.     --Check if id exist
  119.     local lastID = USERS_ID(oUSERS)
  120.     if userID > lastID then return false end
  121.    
  122.     if toUpdate == "token" then
  123.         oUSERS["Token"][userID] = newUpdate
  124.         local TO_SAVE = fs.open(USER_FILE,"w")
  125.         TO_SAVE.write(textutils.serialise(oUSERS))
  126.         TO_SAVE.close()
  127.         return true
  128.     elseif toUpdate == "accesslevel" then
  129.         if type(newUpdate) ~= "number" then return false end
  130.         oUSERS["AccessLevel"][userID] = newUpdate
  131.         local TO_SAVE = fs.open(USER_FILE,"w")
  132.         TO_SAVE.write(textutils.serialise(oUSERS))
  133.         TO_SAVE.close()
  134.         return true
  135.     end
  136.    
  137. end
  138.  
  139. --Token functions
  140.  
  141. local function TOKEN_GEN()
  142.     local TOKEN_LENGHT = 25
  143.     local TOKEN = ""
  144.     local TOKEN_TABLE = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9","#", "$", "*"}
  145.     local x = 0
  146.  while TOKEN_LENGHT >= 1 do
  147.   x = x + 1
  148.         local mRandom = math.random(1,#TOKEN_TABLE)
  149.         TOKEN = TOKEN..TOKEN_TABLE[mRandom]
  150.         TOKEN_LENGHT = TOKEN_LENGHT - 1
  151.   sleep(0.1)
  152.   print("Token generation "..tostring(x).."/25")
  153.     end
  154.     return TOKEN
  155. end
  156.  
  157. local function TOKEN_CREATE(id)
  158.     local USERS = USERS_GET()
  159.     if type(id) ~= "number" then return false end
  160.     local newToken = TOKEN_GEN()
  161.     --Check if token already exist
  162.     local YES = false
  163.     while YES do
  164.         newToken = TOKEN_GEN()
  165.   sleep(0.1)
  166.   print("Generating a new token..")
  167.     end
  168.     --Write the new token
  169.     if USERS_UPDATE("token", id, newToken) then
  170.         return newToken
  171.     else
  172.         return false
  173.     end
  174. end
  175.  
  176. --We need the famous Gitano Parser !
  177. local function gitanoParser(sTable,sID)
  178.   if sTable.type == "mdr" then
  179.     log(sID, sTable, "bad type")
  180.     return false
  181.   elseif sTable.type == "token" then
  182.     if sTable.todo == "createToken" then
  183.       if type(sTable.enc) ~= "string" or type(sTable.Username) ~= "string" or type(sTable.math) ~= "string" then rednet.send(sID, "nil entry") else
  184.       local oTable = USERS_GET()
  185.       local uID = USERS_ID(oTable,sTable.Username)  
  186.       if uID == nil then rednet.send(sID, "Unknown user") return false end
  187.       local SHA1Pass = oTable["Password"][uID]
  188.       local crypted = StringUtils.decrypt(sTable.enc, SHA1Pass)
  189.       if crypted == sTable.math then
  190.         --He got it we need to give a token to him
  191.         print("Going to generate token")
  192.         local token = TOKEN_CREATE(uID)
  193.         print("Token generated for: "..sTable["Username"])
  194.         print("SHA1: "..StringUtils.SHA1(token))
  195.         rednet.send(sID, token)
  196.       else
  197.         rednet.send(sID, "Bad password")  
  198.       end
  199.       end  
  200.     elseif sTable.todo == "reset" then
  201.       USERS_CREATEDB()
  202.       print("Database reseted") --Pls add a Terminal confirm
  203.       rednet.send(sID, "true")
  204.     end
  205.   elseif sTable.type == "log" then
  206.   elseif sTable.type == "ping" then
  207.     rednet.send(sID, "true")
  208.     log(sID, sTable, "ping")
  209.   elseif sTable.type == "WAS" then --Wow address system
  210.   elseif sTable.type == "database" then
  211.     if sTable.todo == "createUser" then
  212.       if sTable.Username == nil or sTable.Password == nil or sTable.Biolock == nil or type(sTable.accesslevel) == number then
  213.         print("User creation failed")
  214.         rednet.send(sID, "nil in table")
  215.       else
  216.         if USERS_CREATE(sTable.Username,sTable.Password,sTable.Biolock,sTable.accesslevel) then
  217.           print("User Created")
  218.           rednet.send(sID, "true")
  219.         else
  220.           print("Failed to create")
  221.           rednet.send(sID, "error")
  222.         end
  223.       end
  224.     elseif sTable.todo == "Userinfo" then
  225.       if sTable.Username == nil then
  226.       rednet.send(sID, false)
  227.       --finish error
  228.       else
  229.         --[[
  230.         Table to resend:
  231.         id
  232.         biolockid
  233.         accesslevel
  234.         ]]
  235.         local TUserTable = USERS_GET()
  236.         local Tid = USERS_ID(TUserTable, sTable.Username)
  237.         if Tid == nil then print("Nil TID") rednet.send(sID, "nil id") else
  238.         local Tbiolock = TUserTable["Biolock"][Tid]
  239.         local TAccessL = TUserTable["AccessLevel"][Tid]
  240.         local TEMP_TABLE = {id = Tid, biolockid = Tbiolock, accesslevel = TAccessL}
  241.         --print(textutils.serialise(TEMP_TABLE))
  242.         rednet.send(sID, TEMP_TABLE)
  243.         print("Succefuly sended")
  244.         end
  245.       end
  246.     end
  247.   elseif sTable.type == "network" then
  248.   else
  249.     rednet.send(sID, "bad TableType")
  250.   end
  251. end
  252. --Database
  253. term.write("Rednet state: ")
  254. if rednet.isOpen() then print("true") else print("false") end
  255. if rednet.isOpen() ~= true then rednet.open("top") end
  256. rednet.host("HomePI", "main")
  257. print("Rednet opened\nID: ".. os.computerID())
  258.  
  259. --USERS_CREATEDB()
  260. --print(USERS_CREATE("admin", "test", "unknown", 5)
  261.  
  262. local function receiveparse()
  263.   while RP_BOOL do
  264.     local sID, msg = rednet.receive()
  265.     if type(msg) ~= "table" then rednet.send(sID, "Please use API") log(sID, msg, "Dont use a table") else
  266.       gitanoParser(msg,sID, "received")       -- redirect to gitanoParser
  267.     end
  268.   end
  269. end
  270.  
  271. parallel.waitForAll(receiveparse)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement