Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Var init
- local RP_BOOL = true
- os.loadAPI("StringUtils")
- local modem = peripheral.find("modem")
- local MODEM_CHANNEL = 644
- --Log
- local function log(ID, oMSG, reason)
- local FILE = fs.open("/log", "a")
- if type(oMSG) == "string" then
- toMSG = "bad type(string)"
- elseif type(oMSG) == "table" then
- if oMSG["type"] ~= nil then
- toMSG = "bad table"
- else
- toMSG = oMSG["type"]
- end
- else
- toMSG = "unknown"
- end
- local TEMP = "[LOG] ID:"..ID.." toMSG: "..toMSG.." Reason: "..reason
- print(TEMP)
- FILE.writeLine(TEMP)
- FILE.close()
- return true
- end
- --Users management
- local USER_FILE = "/users"
- local DB_SCHEM = {Users = {}, Password = {}, Biolock = {}, AccessLevel = {}, Token= {}, Where= {}}
- local function USERS_CREATEDB()
- local USER_TEMP = fs.open(USER_FILE, "w")
- USER_TEMP.write(textutils.serialise(DB_SCHEM))
- USER_TEMP.close()
- end
- local function USERS_GET()
- local OBJECT_UF = fs.open(USER_FILE, "r")
- local USERS = textutils.unserialise(OBJECT_UF.readAll())
- if USERS == nil then
- USERS_CREATEDB()
- USERS = textutils.unserialise(OBJECT_UF.readAll())
- end
- OBJECT_UF.close()
- return USERS
- end
- local function USERS_ID(users, tofind)
- local ID = nil
- for i = 1,#users["Users"] do
- if tofind == nil then
- ID = i
- else
- if users["Users"][i] == tofind then
- ID = i
- end
- end
- end
- return ID
- end
- local function USERS_BIOLOCK(users, tofind)
- local ID = nil
- for i = 1,#users["Biolock"] do
- if tofind == nil then return false
- else
- if users["Biolock"][i] == tofind then
- ID = i
- end
- end
- end
- return ID
- end
- local function USERS_TOKEN(users, tofind)
- local ID = nil
- for i = 1,#users["Token"] do
- if tofind == nil then return false
- else
- if users["Token"][i] == tofind then
- ID = i
- end
- end
- end
- return ID
- end
- local function USERS_WHERE(users, tofind)
- local ID = nil
- for i = 1,#users["Where"] do
- if tofind == nil then return false
- else
- if users["Where"][i] == tofind then
- ID = i
- end
- end
- end
- return ID
- end
- local function USERS_CREATE(name, pass, biolock, accesslevel)
- local oUSERS = USERS_GET()
- local fUSERS = oUSERS
- local lastID = USERS_ID(oUSERS)
- --Check if user already exist because or AL will not fuck my script ;(
- if USERS_ID(oUSERS, name) ~= nil then return false end
- if accesslevel > 5 or accesslevel < 1 then return false end
- if lastID == nil then lastID = 1 else lastID = lastID + 1 end
- if name == nil or pass == nil or accesslevel == nil then
- return false
- end
- if biolock == nil then biolock = "unknown" end
- --end of check
- fUSERS["Users"][lastID] = name
- fUSERS["Password"][lastID] = StringUtils.SHA1(pass)
- fUSERS["Biolock"][lastID] = biolock
- fUSERS["Token"][lastID] = "unknown"
- fUSERS["AccessLevel"][lastID] = accesslevel
- fUSERS["Where"][lastID] = "unknown"
- local TO_SAVE = fs.open(USER_FILE,"w")
- TO_SAVE.write(textutils.serialise(fUSERS))
- TO_SAVE.close()
- return true
- end
- local function USERS_UPDATE(toUpdate, userID, newUpdate)
- if type(toUpdate) ~= "string" or type(userID) ~= "number" or newUpdate == nil then return false end
- local oUSERS = USERS_GET()
- --Check if id exist
- local lastID = USERS_ID(oUSERS)
- if userID > lastID then return false end
- if toUpdate == "token" then
- oUSERS["Token"][userID] = newUpdate
- local TO_SAVE = fs.open(USER_FILE,"w")
- TO_SAVE.write(textutils.serialise(oUSERS))
- TO_SAVE.close()
- return true
- elseif toUpdate == "accesslevel" then
- if type(newUpdate) ~= "number" then return false end
- oUSERS["AccessLevel"][userID] = newUpdate
- local TO_SAVE = fs.open(USER_FILE,"w")
- TO_SAVE.write(textutils.serialise(oUSERS))
- TO_SAVE.close()
- return true
- end
- end
- --Encryption and modems
- local function SEND(sID, content, uID)
- if type(sID) ~= "number" or content == nil or type(uID) ~= "number" then return false end
- local Users = USERS_GET()
- local Token = Users[Tokens][uID]
- --local serialised = textutils.serialise(content)
- --local FinalTable = StringUtils.encrypt(serialised) DO NOT WORK
- modem.transmit(MODEM_CHANNEL, MODEM_CHANNEL, content)
- end
- --Token functions
- local function TOKEN_GEN()
- local TOKEN_LENGHT = 25
- local TOKEN = ""
- 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","#", "$", "*"}
- local x = 0
- while TOKEN_LENGHT > 0 do
- x=x+1
- local mRandom = math.random(1,#TOKEN_TABLE)
- TOKEN = TOKEN..TOKEN_TABLE[mRandom]
- TOKEN_LENGHT = TOKEN_LENGHT - 1
- print("Generating Token: "..tostring(x).."/25")
- end
- return TOKEN
- end
- local function TOKEN_CREATE(id)
- local USERS = USERS_GET()
- if type(id) ~= "number" then return false end
- local newToken = TOKEN_GEN()
- --Check if token already exist
- local YES = false
- while YES do
- if USERS_TOKEN(USERS, newToken) == nil then YES = false else
- newToken = TOKEN_GEN() end
- end
- --Write the new token
- if USERS_UPDATE("token", id, newToken) then
- return newToken
- else
- return false
- end
- end
- --We need the famous Gitano Parser !
- local function gitanoParser(sTable,sID)
- print(sTable.type)
- if sTable.type == "mdr" then
- log(sID, sTable, "bad type")
- return false
- elseif sTable.type == "token" then
- if sTable.todo == "createToken" then
- if type(sTable.enc) ~= "string" or type(sTable.Username) ~= "string" or type(sTable.math) ~= "string" then SEND(sID, "nil entry") else
- local oTable = USERS_GET()
- local uID = USERS_ID(oTable,sTable.Username)
- if uID == nil then send(sID, "Unknown user") return false end
- local SHA1Pass = oTable["Password"][uID]
- local crypted = StringUtils.decrypt(sTable.enc, SHA1Pass)
- if crypted == sTable.math then
- --He got it we need to give a token to him
- local Token = TOKEN_CREATE(uID)
- SEND(sID, Token)
- print("Token created for: "..sTable["Username"])
- print("New Token hash: "..StringUtils.SHA1(Token))
- else
- SEND(sID, "Bad password")
- end
- end
- elseif sTable.todo == "getToken" then
- if type(sTable.enc) ~= "string" or type(sTable.Username) ~= "string" or type(sTable.math) ~= "string" then SEND(sID, "nil entry") else
- local oTable = USERS_GET()
- local uID = USERS_ID(oTable,sTable.Username)
- if uID == nil then SEND(sID, "Unknown user") return false end
- local SHA1Pass = oTable["Password"][uID]
- local crypted = StringUtils.decrypt(sTable.enc, SHA1Pass)
- if crypted == sTable.math then
- --He got it we need to give a token to him
- SEND(sID, oTable["Token"][uID])
- else
- SEND(sID, "Bad password")
- end
- end
- end
- elseif sTable.type == "log" then
- elseif sTable.type == "ping" then
- SEND(sID, "true")
- log(sID, sTable, "ping")
- elseif sTable.type == "WAS" then --Wow address system
- elseif sTable.type == "database" then
- if sTable.todo == "createUser" then
- if sTable.Username == nil or sTable.Password == nil or sTable.Biolock == nil or type(sTable.accesslevel) == number then
- print("User creation failed")
- SEND(sID, "nil in table")
- else
- if USERS_CREATE(sTable.Username,sTable.Password,sTable.Biolock,sTable.accesslevel) then
- print("User Created")
- SEND(sID, "true")
- else
- print("Failed to create")
- SEND(sID, "error")
- end
- end
- elseif sTable.todo == "reset" then
- USERS_CREATEDB()
- print("Database reseted") --Pls add a Terminal confirm
- SEND(sID, "true")
- elseif sTable.todo == "Userinfo" then
- if sTable.Username == nil then
- SEND(sID, false)
- --finish error
- else
- local TUserTable = USERS_GET()
- local Tid = USERS_ID(TUserTable, sTable.Username)
- if Tid == nil then print("Nil TID") SEND(sID, "nil id") else
- local Tbiolock = TUserTable["Biolock"][Tid]
- local TAccessL = TUserTable["AccessLevel"][Tid]
- local TEMP_TABLE = {id = Tid, biolockid = Tbiolock, accesslevel = TAccessL}
- SEND(sID, TEMP_TABLE)
- print("Succefuly sended")
- end
- end
- end
- elseif sTable.type == "network" then
- else
- SEND(sID, "bad TableType")
- end
- end
- --Starting
- term.write("Modem state: ")
- if modem.isOpen(MODEM_CHANNEL) then print("True") else print("False") end
- if modem.isOpen(MODEM_CHANNEL) ~= true then modem.open(MODEM_CHANNEL) end
- print("Modem opened\nID: ".. os.computerID())
- local function receiveparse()
- while RP_BOOL do
- local modemSide, sID, rID, msg, distance = rednet.receive()
- if type(msg) ~= "table" then SEND(sID, "Please use API") log(sID, msg, "Dont use a table") else
- gitanoParser(msg,sID) -- redirect to gitanoParser
- end
- end
- end
- parallel.waitForAll(receiveparse)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement