Advertisement
Guest User

startup

a guest
Nov 4th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.45 KB | None | 0 0
  1. --[[
  2.  
  3. Bank sever - Fork of userAPI by wow
  4.  
  5.  
  6.  
  7. All of the bank's servers and API are made by wow.
  8.  
  9. All of the bank's graphics are made by Kuruyia.
  10.  
  11. ]]
  12.  
  13.  
  14.  
  15. --Var init
  16.  
  17. local RP_BOOL = true
  18.  
  19. if not fs.exists("/StringUtils") then shell.run("pastebin get ad3aUsVw StringUtils") end
  20.  
  21. os.loadAPI("StringUtils")
  22.  
  23. local modem = peripheral.find("modem")
  24.  
  25. local MODEM_CHANNEL = 644
  26.  
  27.  
  28.  
  29. --ProLog 2017 (TM)
  30.  
  31.  
  32.  
  33. local LOG_FILE = "/proLog.log"
  34.  
  35. local LOG_SCHEM = {}
  36.  
  37.  
  38.  
  39. local function LOGS_GET()
  40.  
  41.   local OBJECT_UF = fs.open(LOG_FILE, "r")
  42.  
  43.   local LOGS
  44.  
  45.   LOGS = textutils.unserialise(OBJECT_UF.readAll())
  46.  
  47.   if LOGS == nil then
  48.  
  49.     LOGS = {}
  50.  
  51.   end
  52.  
  53.   return LOGS
  54.  
  55. end
  56.  
  57.  
  58.  
  59. local function proLog(usertoLog, exchangeName, amount)
  60.  
  61.   --Table creation
  62.  
  63.   local LOGS = LOGS_GET()
  64.  
  65.   local newTable = {id = exchangeName, amount = amount}
  66.  
  67.   if LOGS[usertoLog] == nil then LOGS[usertoLog] = {} end
  68.  
  69.   local num =  #LOGS[usertoLog]+1
  70.  
  71.   LOGS[usertoLog][num] = newTable
  72.  
  73.   local USER_TEMP = fs.open(LOG_FILE, "w")
  74.  
  75.   USER_TEMP.write(textutils.serialise(LOGS))
  76.  
  77.   USER_TEMP.close()
  78.  
  79. end
  80.  
  81.  
  82.  
  83. --Users management
  84.  
  85. local USER_FILE = "/users"
  86.  
  87. local DB_SCHEM = {Users = {}, Password = {}, Biolock = {}, AccessLevel = {}, Money = {}}
  88.  
  89.  
  90.  
  91. local function USERS_CREATEDB()
  92.  
  93.   local USER_TEMP = fs.open(USER_FILE, "w")
  94.  
  95.   USER_TEMP.write(textutils.serialise(DB_SCHEM))
  96.  
  97.   USER_TEMP.close()
  98.  
  99. end
  100.  
  101.  
  102.  
  103. local function USERS_GET()
  104.  
  105.   local OBJECT_UF = fs.open(USER_FILE, "r")
  106.  
  107.   local USERS = textutils.unserialise(OBJECT_UF.readAll())
  108.  
  109.   if USERS == nil then
  110.  
  111.     USERS_CREATEDB()
  112.  
  113.     USERS = textutils.unserialise(OBJECT_UF.readAll())
  114.  
  115.   end
  116.  
  117.   OBJECT_UF.close()
  118.  
  119.   return USERS
  120.  
  121. end
  122.  
  123.  
  124.  
  125. local function USERS_ID(users, tofind)
  126.  
  127.   local ID = nil
  128.  
  129.   for i = 1,#users["Users"] do
  130.  
  131.     if tofind == nil then
  132.  
  133.       ID = i
  134.  
  135.     else
  136.  
  137.       if users["Users"][i] == tofind then
  138.  
  139.         ID = i
  140.  
  141.       end
  142.  
  143.     end
  144.  
  145.   end
  146.  
  147.   return ID
  148.  
  149. end
  150.  
  151.  
  152.  
  153. local function USERS_BIOLOCK(users, tofind)
  154.  
  155.   local ID = nil
  156.  
  157.   for i = 1,#users["Biolock"] do
  158.  
  159.     if tofind == nil then return 0x1
  160.  
  161.     else
  162.  
  163.       if users["Biolock"][i] == tofind then
  164.  
  165.         ID = i
  166.  
  167.       end
  168.  
  169.     end
  170.  
  171.   end
  172.  
  173.   return ID
  174.  
  175. end
  176.  
  177.  
  178.  
  179. local function USERS_WHERE(users, tofind)
  180.  
  181.   local ID = nil
  182.  
  183.   for i = 1,#users["Where"] do
  184.  
  185.     if tofind == nil then return 0x1
  186.  
  187.     else
  188.  
  189.       if users["Where"][i] == tofind then
  190.  
  191.         ID = i
  192.  
  193.       end
  194.  
  195.     end
  196.  
  197.   end
  198.  
  199.   return ID
  200.  
  201. end
  202.  
  203.  
  204.  
  205. --Encryption and modems
  206.  
  207. local function SEND(sID, content)
  208.  
  209.     if type(sID) ~= "number" or content == nil then return 0x1 end
  210.  
  211.     local Users = USERS_GET()
  212.  
  213.     --local serialised = textutils.serialise(content)
  214.  
  215.     --local FinalTable = StringUtils.encrypt(serialised) DO NOT WORK
  216.  
  217.     modem.transmit(MODEM_CHANNEL, MODEM_CHANNEL, content)
  218.  
  219.     return 0
  220.  
  221. end
  222.  
  223.  
  224.  
  225. local function USERS_CREATE(name, pass, biolock, accesslevel)
  226.  
  227.  
  228.  
  229.   local oUSERS = USERS_GET()
  230.  
  231.   local fUSERS = oUSERS
  232.  
  233.   local lastID = USERS_ID(oUSERS)
  234.  
  235.  
  236.  
  237.   --Check if user already exist because or AL will not fuck my script ;(
  238.  
  239.   if USERS_ID(oUSERS, name) ~= nil then return 0x1 end
  240.  
  241.   if accesslevel > 5 or accesslevel < 1 then return 0x1 end
  242.  
  243.   if lastID == nil then lastID = 1 else lastID = lastID + 1 end
  244.  
  245.   if name == nil or pass == nil or accesslevel == nil then
  246.  
  247.     return 0x1
  248.  
  249.   end
  250.  
  251.   if biolock == nil then biolock = "unknown" end
  252.  
  253.   --end of check
  254.  
  255.  
  256.  
  257.   fUSERS["Users"][lastID] = name
  258.  
  259.   fUSERS["Password"][lastID] = StringUtils.SHA1(pass)
  260.  
  261.   fUSERS["Biolock"][lastID] = biolock
  262.  
  263.   fUSERS["Money"][lastID] = 50
  264.  
  265.   fUSERS["AccessLevel"][lastID] = accesslevel
  266.  
  267.   local TO_SAVE = fs.open(USER_FILE,"w")
  268.  
  269.   TO_SAVE.write(textutils.serialise(fUSERS))
  270.  
  271.   TO_SAVE.close()
  272.  
  273.   return 0
  274.  
  275. end
  276.  
  277.  
  278.  
  279. local function USERS_UPDATE(toUpdate, userID, newUpdate)
  280.  
  281.     if type(toUpdate) ~= "string" or type(userID) ~= "number" or newUpdate == nil then return 0x1 end
  282.  
  283.     local oUSERS = USERS_GET()
  284.  
  285.     --Check if id exist
  286.  
  287.     local lastID = USERS_ID(oUSERS)
  288.  
  289.     if userID > lastID then return 0x2 end
  290.  
  291.  
  292.  
  293.     if toUpdate == "password" then
  294.  
  295.     if type(newUpdate) ~= "string" then return 0x1 end
  296.  
  297.     oUSERS["Password"][userID] = newUpdate
  298.  
  299.     local TO_SAVE = fs.open(USER_FILE,"w")
  300.  
  301.       TO_SAVE.write(textutils.serialise(oUSERS))
  302.  
  303.       TO_SAVE.close()
  304.  
  305.     return 0
  306.  
  307.   elseif toUpdate == "Money" then
  308.  
  309.     if type(newUpdate) ~= "number" then return 0x1 end
  310.  
  311.     oUSERS["Money"][userID] = newUpdate
  312.  
  313.     local TO_SAVE = fs.open(USER_FILE,"w")
  314.  
  315.       TO_SAVE.write(textutils.serialise(oUSERS))
  316.  
  317.       TO_SAVE.close()
  318.  
  319.     return 0
  320.  
  321.     elseif toUpdate == "accesslevel" then
  322.  
  323.         if type(newUpdate) ~= "number" then return 0x1 end
  324.  
  325.         oUSERS["AccessLevel"][userID] = newUpdate
  326.  
  327.         local TO_SAVE = fs.open(USER_FILE,"w")
  328.  
  329.         TO_SAVE.write(textutils.serialise(oUSERS))
  330.  
  331.         TO_SAVE.close()
  332.  
  333.         return 0
  334.  
  335.     end
  336.  
  337.  
  338.  
  339. end
  340.  
  341.  
  342.  
  343. --EXEC
  344.  
  345.  
  346.  
  347. local function execute(sTable,sID)
  348.  
  349.     local uLevel = -1
  350.  
  351.   local uID = 0
  352.  
  353.     if sTable.type ~= "ping" then
  354.  
  355.         if sTable["fromUsername"] == nil or sTable["fromPassword"] == nil then return SEND(sID, 0x3) end
  356.  
  357.         --Check account
  358.  
  359.         local uTable = USERS_GET()
  360.  
  361.         uID = USERS_ID(uTable, sTable.fromUsername)
  362.  
  363.         if uID == nil then return SEND(sID, 0x2) end
  364.  
  365.     --Encryption password is: "1" (Encryption of the SHA-1 password with a password and decryption server side)
  366.  
  367.     if StringUtils.decrypt(sTable.fromPassword, uTable["Password"][uID]) == "1" then
  368.  
  369.       print(uTable["Users"][uID].." Logged")
  370.  
  371.       uLevel = uTable["AccessLevel"][uID]
  372.  
  373.     else
  374.  
  375.       return SEND(sID, 0x5)
  376.  
  377.     end
  378.  
  379.   end
  380.  
  381.   if sTable.type == "ping" then
  382.  
  383.     SEND(sID,0x0)
  384.  
  385.  
  386.  
  387.   elseif sTable.type == "transaction" then
  388.  
  389.     --if you see this: buy Life.exe or Life.bin if you are on Linux. Thank you ! :p
  390.  
  391.     if sTable["amount"] == nil or sTable["to"] == nil then return SEND(sID, 0x1) end
  392.  
  393.     local uTable = USERS_GET()
  394.  
  395.     local toID = USERS_ID(uTable, sTable.to)
  396.  
  397.     if toID == nil then SEND(sID, 0x9) end
  398.  
  399.     --Check money
  400.  
  401.     if uTable["Money"][uID] >= sTable["amount"] then else return SEND(sID, 0x8) end
  402.  
  403.     --Transfering
  404.  
  405.     USERS_UPDATE("Money", uID, uTable["Money"][uID] - sTable["amount"])
  406.  
  407.     USERS_UPDATE("Money", toID, uTable["Money"][toID] + sTable["amount"])
  408.  
  409.     proLog(uTable["Users"][uID], uTable["Users"][toID], sTable["amount"])
  410.  
  411.     SEND(sID, 0x0)
  412.  
  413.  
  414.  
  415.   elseif sTable.type == "database" then
  416.  
  417.     if sTable.todo == "createUser" then
  418.  
  419.         if uLevel < 4 then return SEND(sID, 0x6) end
  420.  
  421.       if sTable.Username == nil or sTable.Password == nil or sTable.Biolock == nil or type(sTable.accesslevel) == number then
  422.  
  423.         print("User creation failed")
  424.  
  425.         SEND(sID, 0x1)
  426.  
  427.       else
  428.  
  429.         if USERS_CREATE(sTable.Username,sTable.Password,sTable.Biolock,sTable.accesslevel) then
  430.  
  431.           print("User Created")
  432.  
  433.           SEND(sID, 0x0)
  434.  
  435.         else
  436.  
  437.           print("Failed to create")
  438.  
  439.           SEND(sID, 0x7)
  440.  
  441.         end
  442.  
  443.       end
  444.  
  445.  
  446.  
  447.       elseif sTable.todo == "findBL" then
  448.  
  449.             if uLevel ~= 0 and uLevel ~= 5 then return SEND(sID, "Bad accessLevel") end
  450.  
  451.             if type(sTable.toFind) ~= "string" then return SEND(sID, 0x1) end
  452.  
  453.             local uTable = USERS_GET()
  454.  
  455.             local uID = USERS_BIOLOCK(uTable, sTable.toFind)
  456.  
  457.             if uID == nil then return SEND(sID, 0x0) end -- 0x0 <- Unknown user
  458.  
  459.             return SEND(sID, uTable["AccessLevel"][uID])
  460.  
  461.  
  462.  
  463.     elseif sTable.todo == "reset" then
  464.  
  465.         if uLevel < 4 then return SEND(sID, "Bad accessLevel") end
  466.  
  467.       USERS_CREATEDB()
  468.  
  469.       print("Database reseted") --Pls add a Terminal confirm
  470.  
  471.       SEND(sID, 0x0)
  472.  
  473.  
  474.  
  475.       elseif sTable.todo == "Userinfo" then
  476.  
  477.       local TUserTable = USERS_GET()
  478.  
  479.       local Tid = uID
  480.  
  481.       if Tid == nil then SEND(sID, 0x2) else
  482.  
  483.         local Tbiolock = TUserTable["Biolock"][Tid]
  484.  
  485.         local TAccessL = TUserTable["AccessLevel"][Tid]
  486.  
  487.         local TMoney = TUserTable["Money"][Tid]
  488.  
  489.         local TEMP_TABLE = {id = Tid, biolockid = Tbiolock, accesslevel = TAccessL, money = TMoney}
  490.  
  491.         SEND(sID, TEMP_TABLE)
  492.  
  493.       end
  494.  
  495.     end
  496.  
  497.     else
  498.  
  499.         SEND(sID, 0x1)
  500.  
  501.   end
  502.  
  503. end
  504.  
  505.  
  506.  
  507. --Starting
  508.  
  509. print("Modem state: "..tostring(modem.isOpen(MODEM_CHANNEL)))
  510.  
  511. if modem.isOpen(MODEM_CHANNEL) ~= true then print("Modem opened\nID: ".. os.computerID()) modem.open(MODEM_CHANNEL) end
  512.  
  513.  
  514.  
  515. local function receiveo()
  516.  
  517.   while RP_BOOL do
  518.  
  519.     local event, modemSide, sID, rID, msg, distance = os.pullEvent("modem_message")
  520.  
  521.     if type(msg) ~= "table" then SEND(sID, "Please use API") else
  522.  
  523.       execute(msg,sID)
  524.  
  525.     end
  526.  
  527.   end
  528.  
  529. end
  530.  
  531.  
  532.  
  533. parallel.waitForAll(receiveo)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement