Advertisement
wow0

userAPI: Serveur

Oct 21st, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.11 KB | None | 0 0
  1. --Var init
  2. local RP_BOOL = true
  3. if not fs.exists("/StringUtils") then shell.run("pastebin get ad3aUsVw StringUtils") end
  4. os.loadAPI("StringUtils")
  5. local modem = peripheral.find("modem")
  6. local MODEM_CHANNEL = 644
  7.  
  8.  
  9. --Users management
  10. local USER_FILE = "/users"
  11. local DB_SCHEM = {Users = {}, Password = {}, Biolock = {}, AccessLevel = {}, Token= {}, Where= {}}
  12.  
  13. local function USERS_CREATEDB()
  14.   local USER_TEMP = fs.open(USER_FILE, "w")
  15.   USER_TEMP.write(textutils.serialise(DB_SCHEM))
  16.   USER_TEMP.close()
  17. end
  18.  
  19. local function USERS_GET()
  20.   local OBJECT_UF = fs.open(USER_FILE, "r")
  21.   local USERS = textutils.unserialise(OBJECT_UF.readAll())
  22.   if USERS == nil then
  23.     USERS_CREATEDB()
  24.     USERS = textutils.unserialise(OBJECT_UF.readAll())
  25.   end
  26.   OBJECT_UF.close()
  27.   return USERS
  28. end
  29.  
  30. local function USERS_ID(users, tofind)
  31.   local ID = nil
  32.   for i = 1,#users["Users"] do
  33.     if tofind == nil then
  34.       ID = i
  35.     else
  36.       if users["Users"][i] == tofind then
  37.         ID = i
  38.       end
  39.     end
  40.   end
  41.   return ID
  42. end
  43.  
  44. local function USERS_BIOLOCK(users, tofind)
  45.   local ID = nil
  46.   for i = 1,#users["Biolock"] do
  47.     if tofind == nil then return 0x1
  48.     else
  49.       if users["Biolock"][i] == tofind then
  50.         ID = i
  51.       end
  52.     end
  53.   end
  54.   return ID
  55. end
  56.  
  57. local function USERS_TOKEN(users, tofind)
  58.   local ID = nil
  59.   for i = 1,#users["Token"] do
  60.     if tofind == nil then return 0x1
  61.     else
  62.       if users["Token"][i] == tofind then
  63.         ID = i
  64.       end
  65.     end
  66.   end
  67.   return ID
  68. end
  69.  
  70. local function USERS_WHERE(users, tofind)
  71.   local ID = nil
  72.   for i = 1,#users["Where"] do
  73.     if tofind == nil then return 0x1
  74.     else
  75.       if users["Where"][i] == tofind then
  76.         ID = i
  77.       end
  78.     end
  79.   end
  80.   return ID
  81. end
  82.  
  83. --Encryption and modems
  84. local function SEND(sID, content)
  85.     if type(sID) ~= "number" or content == nil then return 0x1 end
  86.     local Users = USERS_GET()
  87.     --local serialised = textutils.serialise(content)
  88.     --local FinalTable = StringUtils.encrypt(serialised) DO NOT WORK
  89.     modem.transmit(MODEM_CHANNEL, MODEM_CHANNEL, content)
  90.     return 0
  91. end
  92.  
  93. local function USERS_CREATE(name, pass, biolock, accesslevel)
  94.  
  95.  
  96.   local oUSERS = USERS_GET()
  97.   local fUSERS = oUSERS
  98.   local lastID = USERS_ID(oUSERS)
  99.  
  100.   --Check if user already exist because or AL will not fuck my script ;(
  101.   if USERS_ID(oUSERS, name) ~= nil then return 0x1 end
  102.   if accesslevel > 5 or accesslevel < 1 then return 0x1 end
  103.   if lastID == nil then lastID = 1 else lastID = lastID + 1 end
  104.   if name == nil or pass == nil or accesslevel == nil then
  105.     return 0x1
  106.   end
  107.   if biolock == nil then biolock = "unknown" end
  108.   --end of check
  109.  
  110.   fUSERS["Users"][lastID] = name
  111.   fUSERS["Password"][lastID] = StringUtils.SHA1(pass)
  112.   fUSERS["Biolock"][lastID] = biolock
  113.   fUSERS["Token"][lastID] = "unknown"
  114.   fUSERS["AccessLevel"][lastID] = accesslevel
  115.   fUSERS["Where"][lastID] = "unknown"
  116.   local TO_SAVE = fs.open(USER_FILE,"w")
  117.   TO_SAVE.write(textutils.serialise(fUSERS))
  118.   TO_SAVE.close()
  119.   return 0
  120. end
  121.  
  122. local function USERS_UPDATE(toUpdate, userID, newUpdate)
  123.     if type(toUpdate) ~= "string" or type(userID) ~= "number" or newUpdate == nil then return 0x1 end
  124.     local oUSERS = USERS_GET()
  125.     --Check if id exist
  126.     local lastID = USERS_ID(oUSERS)
  127.     if userID > lastID then return 0x2 end
  128.    
  129.     if toUpdate == "token" then
  130.         oUSERS["Token"][userID] = newUpdate
  131.         local TO_SAVE = fs.open(USER_FILE,"w")
  132.         TO_SAVE.write(textutils.serialise(oUSERS))
  133.         TO_SAVE.close()
  134.         return 0
  135.     elseif toUpdate == "accesslevel" then
  136.         if type(newUpdate) ~= "number" then return 0x1 end
  137.         oUSERS["AccessLevel"][userID] = newUpdate
  138.         local TO_SAVE = fs.open(USER_FILE,"w")
  139.         TO_SAVE.write(textutils.serialise(oUSERS))
  140.         TO_SAVE.close()
  141.         return 0
  142.     end
  143.    
  144. end
  145.  
  146.  
  147.  
  148.  
  149. --Token functions
  150.  
  151. local function TOKEN_GEN()
  152.     local TOKEN_LENGHT = 25
  153.     local TOKEN = ""
  154.     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","#", "$", "*", "!"}
  155.  local x    = 0
  156.  while TOKEN_LENGHT > 0 do
  157.   x=x+1
  158.         local mRandom = math.random(1,#TOKEN_TABLE)
  159.         TOKEN = TOKEN..TOKEN_TABLE[mRandom]
  160.         TOKEN_LENGHT = TOKEN_LENGHT - 1
  161.   print("Generating Token: "..tostring(x).."/25")
  162.     end
  163.     return TOKEN
  164. end
  165.  
  166. local function TOKEN_CREATE(id)
  167.     local USERS = USERS_GET()
  168.     if type(id) ~= "number" then return 0x1 end
  169.     local newToken = TOKEN_GEN()
  170.     --Check if token already exist
  171.     local YES = false
  172.     while YES do
  173.         if USERS_TOKEN(USERS, newToken) == nil then YES = false else
  174.         newToken = TOKEN_GEN() end
  175.     end
  176.     --Write the new token
  177.     local retu = USERS_UPDATE("token", id, newToken)
  178.     if retu then
  179.         return newToken
  180.     else
  181.         return retu
  182.     end
  183. end
  184.  
  185. local function execute(sTable,sID)
  186.     local uLevel = 0
  187.     if sTable.type == "ping" then
  188.         return SEND(sID,0x0)
  189.     end
  190.  
  191.     if sTable.todo ~= "getToken" and sTable.todo ~= "createToken" then
  192.         if sTable["tokenUsername"] == nil or sTable["tokenCheck"] == nil then return SEND(sID, 0x3) end
  193.         --Check account
  194.         local uTable = USERS_GET()
  195.         local uID = USERS_ID(uTable, sTable["tokenUsername"])
  196.         if uID == nil then return SEND(sID, 0x2) end
  197.         local uTOKEN = uTable["Token"][uID]
  198.         if StringUtils.decrypt(sTable["tokenCheck"], uTOKEN) ~= "true" then return SEND(sID, 0x4) end
  199.         uLevel = uTable["AccessLevel"][uID]
  200.     end
  201.  
  202.   if sTable.type == "token" then
  203.     if sTable.todo == "createToken" then
  204.       if type(sTable.enc) ~= "string" or type(sTable.Username) ~= "string" or type(sTable.math) ~= "string" then SEND(sID, 0x1) else
  205.       local oTable = USERS_GET()
  206.       local uID = USERS_ID(oTable,sTable.Username)  
  207.       if uID == nil then SEND(sID, 0x2) return false end
  208.       local SHA1Pass = oTable["Password"][uID]
  209.       local crypted = StringUtils.decrypt(sTable.enc, SHA1Pass)
  210.       if crypted == sTable.math then
  211.         --He got it we need to give a token to him
  212.         local Token = TOKEN_CREATE(uID)
  213.         SEND(sID, Token)
  214.         print("Token created for: "..sTable["Username"])
  215.         print("New Token hash: "..StringUtils.SHA1(Token))
  216.       else
  217.         SEND(sID, 0x5)  
  218.       end
  219.       end
  220.      
  221.     elseif sTable.todo == "getToken" then
  222.       if type(sTable.enc) ~= "string" or type(sTable.Username) ~= "string" or type(sTable.math) ~= "string" then SEND(sID, "nil entry") else
  223.       local oTable = USERS_GET()
  224.       local uID = USERS_ID(oTable,sTable.Username)  
  225.       if uID == nil then SEND(sID, 0x2) return false end
  226.       local SHA1Pass = oTable["Password"][uID]
  227.       local crypted = StringUtils.decrypt(sTable.enc, SHA1Pass)
  228.       if crypted == sTable.math then
  229.         --He got it we need to give a token to him
  230.         SEND(sID, oTable["Token"][uID])
  231.       else
  232.         SEND(sID, 0x5)  
  233.       end
  234.       end
  235.     end
  236.        
  237.   elseif sTable.type == "ping" then
  238.     SEND(sID, "true")
  239.     log(sID, sTable, "ping")
  240.    
  241.   elseif sTable.type == "database" then
  242.     if sTable.todo == "createUser" then
  243.     if uLevel < 4 then return SEND(sID, 0x6) end
  244.       if sTable.Username == nil or sTable.Password == nil or sTable.Biolock == nil or type(sTable.accesslevel) == number then
  245.         print("User creation failed")
  246.         SEND(sID, 0x1)
  247.       else
  248.         if USERS_CREATE(sTable.Username,sTable.Password,sTable.Biolock,sTable.accesslevel) then
  249.           print("User Created")
  250.           SEND(sID, 0x0)
  251.         else
  252.           print("Failed to create")
  253.           SEND(sID, 0x7)
  254.         end
  255.       end
  256.      
  257.     elseif sTable.todo == "findBL" then
  258.         if uLevel ~= 0 and uLevel ~= 5 then return SEND(sID, "Bad accessLevel") end
  259.         if type(sTable.toFind) ~= "string" then return SEND(sID, 0x1) end
  260.         local uTable = USERS_GET()
  261.         local uID = USERS_BIOLOCK(uTable, sTable.toFind)
  262.         if uID == nil then return SEND(sID, 0x0) end -- 0x0 <- Unknown user
  263.         return SEND(sID, uTable["AccessLevel"][uID])
  264.        
  265.     elseif sTable.todo == "reset" then
  266.         if uLevel < 4 then return SEND(sID, "Bad accessLevel") end
  267.       USERS_CREATEDB()
  268.       print("Database reseted") --Pls add a Terminal confirm
  269.       SEND(sID, 0x0)
  270.      
  271.     elseif sTable.todo == "Userinfo" then
  272.     if uLevel ~= 0 and uLevel ~= 5 then return SEND(sID, 0x6) end
  273.       if sTable.Username == nil then
  274.       SEND(sID, 0x1)
  275.  
  276.       else
  277.         local TUserTable = USERS_GET()
  278.         local Tid = USERS_ID(TUserTable, sTable.Username)
  279.         if Tid == nil then SEND(sID, 0x2) else
  280.             local Tbiolock = TUserTable["Biolock"][Tid]
  281.             local TAccessL = TUserTable["AccessLevel"][Tid]
  282.             local TEMP_TABLE = {id = Tid, biolockid = Tbiolock, accesslevel = TAccessL}
  283.             SEND(sID, TEMP_TABLE)
  284.            
  285.         end
  286.       end
  287.     end
  288.    
  289.   else
  290.     SEND(sID, 0x1)
  291.   end
  292. end
  293.  
  294. --Starting
  295. print("Modem state: "..tostring(modem.isOpen(MODEM_CHANNEL)))
  296. if modem.isOpen(MODEM_CHANNEL) ~= true then print("Modem opened\nID: ".. os.computerID()) modem.open(MODEM_CHANNEL) end
  297.  
  298. local function receiveo()
  299.   while RP_BOOL do
  300.     local event, modemSide, sID, rID, msg, distance = os.pullEvent("modem_message")
  301.     if type(msg) ~= "table" then SEND(sID, "Please use API") else
  302.       execute(msg,sID)
  303.     end
  304.   end
  305. end
  306.  
  307. parallel.waitForAll(receiveo)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement