SHARE
TWEET

Untitled

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