Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Bank sever - Fork of userAPI by wow
- All of the bank's servers and API are made by wow.
- All of the bank's graphics are made by Kuruyia.
- ]]
- --Var init
- local RP_BOOL = true
- if not fs.exists("/StringUtils") then shell.run("pastebin get ad3aUsVw StringUtils") end
- os.loadAPI("StringUtils")
- local modem = peripheral.find("modem")
- local MODEM_CHANNEL = 644
- --ProLog 2017 (TM)
- local LOG_FILE = "/proLog.log"
- local LOG_SCHEM = {}
- local function LOGS_GET()
- local OBJECT_UF = fs.open(LOG_FILE, "r")
- local LOGS
- if LOGS ~= nil then LOGS = textutils.unserialise(OBJECT_UF.readAll()) end
- if LOGS == nil then
- LOGS = {}
- end
- return LOGS
- end
- local function proLog(usertoLog, exchangeName, amount)
- --Table creation
- local LOGS = LOGS_GET()
- local newTable = {id = exchangeName, amount = amount}
- if LOGS[usertoLog] == nil then LOGS[usertoLog] = {} end
- local num = #LOGS[usertoLog]+1
- LOGS[usertoLog][num] = newTable
- local USER_TEMP = fs.open(LOG_FILE, "w")
- USER_TEMP.write(textutils.serialise(LOGS))
- USER_TEMP.close()
- end
- --Users management
- local USER_FILE = "/users"
- local DB_SCHEM = {Users = {}, Password = {}, Biolock = {}, AccessLevel = {}, Money = {}}
- 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 users = USERS_GET()
- 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 0x1
- else
- if users["Biolock"][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 0x1
- else
- if users["Where"][i] == tofind then
- ID = i
- end
- end
- end
- return ID
- end
- --Encryption and modems
- local function SEND(sID, content)
- if type(sID) ~= "number" or content == nil then return 0x1 end
- local Users = USERS_GET()
- --local serialised = textutils.serialise(content)
- --local FinalTable = StringUtils.encrypt(serialised) DO NOT WORK
- modem.transmit(MODEM_CHANNEL, MODEM_CHANNEL, content)
- return 0
- 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 0x1 end
- if accesslevel > 5 or accesslevel < 1 then return 0x1 end
- if lastID == nil then lastID = 1 else lastID = lastID + 1 end
- if name == nil or pass == nil or accesslevel == nil then
- return 0x1
- 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["Money"][lastID] = 50
- fUSERS["AccessLevel"][lastID] = accesslevel
- local TO_SAVE = fs.open(USER_FILE,"w")
- TO_SAVE.write(textutils.serialise(fUSERS))
- TO_SAVE.close()
- return 0
- end
- local function USERS_UPDATE(toUpdate, userID, newUpdate)
- if type(toUpdate) ~= "string" or type(userID) ~= "number" or newUpdate == nil then return 0x1 end
- local oUSERS = USERS_GET()
- --Check if id exist
- local lastID = USERS_ID(oUSERS)
- if userID > lastID then return 0x2 end
- if toUpdate == "password" then
- if type(newUpdate) ~= "string" then return 0x1 end
- oUSERS["Password"][userID] = newUpdate
- local TO_SAVE = fs.open(USER_FILE,"w")
- TO_SAVE.write(textutils.serialise(oUSERS))
- TO_SAVE.close()
- return 0
- elseif toUpdate == "Money" then
- if type(newUpdate) ~= "number" then return 0x1 end
- oUSERS["Money"][userID] = newUpdate
- local TO_SAVE = fs.open(USER_FILE,"w")
- TO_SAVE.write(textutils.serialise(oUSERS))
- TO_SAVE.close()
- return 0
- elseif toUpdate == "accesslevel" then
- if type(newUpdate) ~= "number" then return 0x1 end
- oUSERS["AccessLevel"][userID] = newUpdate
- local TO_SAVE = fs.open(USER_FILE,"w")
- TO_SAVE.write(textutils.serialise(oUSERS))
- TO_SAVE.close()
- return 0
- end
- end
- --EXEC
- local function execute(sTable,sID)
- local uLevel = -1
- local uID = 0
- if sTable.type ~= "ping" then
- if sTable["fromUsername"] == nil or sTable["fromPassword"] == nil then return SEND(sID, 0x3) end
- --Check account
- local uTable = USERS_GET()
- uID = USERS_ID(uTable, sTable.fromUsername)
- if uID == nil then return SEND(sID, 0x2) end
- --Encryption password is: "1" (Encryption of the SHA-1 password with a password and decryption server side)
- if StringUtils.decrypt(sTable.fromPassword, uTable["Password"][uID]) == "1" then
- print(uTable["Users"][uID].." Logged")
- uLevel = uTable["AccessLevel"][uID]
- else
- return SEND(sID, 0x5)
- end
- end
- if sTable.type == "ping" then
- SEND(sID,0x0)
- elseif sTable.type == "update" then
- if sTable["toUpdate"] == nil or sTable["newUpdate"] == nil or sTable["who"] == nil then return SEND(sID, 0x1) end
- local nikID = USERS_ID( USERS_GET(), sTable.who)
- if nikID == nil then return SEND(sID, 0x9) end
- return SEND(sID, USERS_UPDATE(sTable.toUpdate, nikID, sTable.newUpdate))
- elseif sTable.type == "transaction" then
- --if you see this: buy Life.exe or Life.bin if you are on Linux. Thank you ! :p
- if sTable["amount"] == nil or sTable["to"] == nil then return SEND(sID, 0x1) end
- local uTable = USERS_GET()
- local toID = USERS_ID(uTable, sTable.to)
- if toID == nil then return SEND(sID, 0x9) end
- --Check money
- if toID == uID then return SEND(sID, 0x7) end
- if uTable["Money"][uID] >= sTable["amount"] then else return SEND(sID, 0x8) end
- --Transfering
- USERS_UPDATE("Money", uID, uTable["Money"][uID] - sTable["amount"])
- USERS_UPDATE("Money", toID, uTable["Money"][toID] + sTable["amount"])
- proLog(uTable["Users"][uID], uTable["Users"][toID], sTable["amount"])
- SEND(sID, 0x0)
- elseif sTable.type == "database" then
- if sTable.todo == "createUser" then
- if uLevel < 4 then return SEND(sID, 0x6) end
- if sTable.Username == nil or sTable.Password == nil or sTable.Biolock == nil or type(sTable.accesslevel) == number then
- print("User creation failed")
- SEND(sID, 0x1)
- else
- if USERS_CREATE(sTable.Username,sTable.Password,sTable.Biolock,sTable.accesslevel) then
- print("User Created")
- SEND(sID, 0x0)
- else
- print("Failed to create")
- SEND(sID, 0x7)
- end
- end
- elseif sTable.todo == "findBL" then
- if uLevel ~= 0 and uLevel ~= 5 then return SEND(sID, "Bad accessLevel") end
- if type(sTable.toFind) ~= "string" then return SEND(sID, 0x1) end
- local uTable = USERS_GET()
- local uID = USERS_BIOLOCK(uTable, sTable.toFind)
- if uID == nil then return SEND(sID, 0x0) end -- 0x0 <- Unknown user
- return SEND(sID, uTable["AccessLevel"][uID])
- elseif sTable.todo == "reset" then
- if uLevel < 4 then return SEND(sID, "Bad accessLevel") end
- USERS_CREATEDB()
- print("Database reseted") --Pls add a Terminal confirm
- SEND(sID, 0x0)
- elseif sTable.todo == "Userinfo" then
- local TUserTable = USERS_GET()
- local Tid = uID
- if Tid == nil then SEND(sID, 0x2) else
- local Tbiolock = TUserTable["Biolock"][Tid]
- local TAccessL = TUserTable["AccessLevel"][Tid]
- local TMoney = TUserTable["Money"][Tid]
- local TEMP_TABLE = {id = Tid, biolockid = Tbiolock, accesslevel = TAccessL, money = TMoney}
- SEND(sID, TEMP_TABLE)
- end
- end
- else
- SEND(sID, 0x1)
- end
- end
- --Starting
- print("Modem state: "..tostring(modem.isOpen(MODEM_CHANNEL)))
- if modem.isOpen(MODEM_CHANNEL) ~= true then print("Modem opened\nID: ".. os.computerID()) modem.open(MODEM_CHANNEL) end
- local function receiveo()
- while RP_BOOL do
- local event, modemSide, sID, rID, msg, distance = os.pullEvent("modem_message")
- if type(msg) ~= "table" then SEND(sID, "Please use API") else
- execute(msg,sID)
- end
- end
- end
- parallel.waitForAll(receiveo)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement