Advertisement
wow0

Untitled

Nov 4th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. --[[
  2. Bank sever - Fork of userAPI by wow
  3.  
  4. All of the bank's servers and API are made by wow.
  5. All of the bank's graphics are made by Kuruyia.
  6. ]]
  7.  
  8. --Var init
  9. local RP_BOOL = true
  10. if not fs.exists("/StringUtils") then shell.run("pastebin get ad3aUsVw StringUtils") end
  11. os.loadAPI("StringUtils")
  12. local modem = peripheral.find("modem")
  13. local MODEM_CHANNEL = 644
  14.  
  15. --ProLog 2017 (TM)
  16.  
  17. local LOG_FILE = "/proLog.log"
  18. local LOG_SCHEM = {}
  19.  
  20. local function LOGS_GET()
  21. local OBJECT_UF = fs.open(LOG_FILE, "r")
  22. local LOGS
  23. LOGS = textutils.unserialise(OBJECT_UF.readAll())
  24. if LOGS == nil then
  25. LOGS = {}
  26. end
  27. return LOGS
  28. end
  29.  
  30. local function proLog(usertoLog, exchangeName, amount)
  31. --Table creation
  32. local LOGS = LOGS_GET()
  33. local newTable = {id = exchangeName, amount = amount}
  34. if LOGS[usertoLog] == nil then LOGS[usertoLog] = {} end
  35. local num = #LOGS[usertoLog]+1
  36. LOGS[usertoLog][num] = newTable
  37. local USER_TEMP = fs.open(LOG_FILE, "w")
  38. USER_TEMP.write(textutils.serialise(LOGS))
  39. USER_TEMP.close()
  40. end
  41.  
  42. --Users management
  43. local USER_FILE = "/users"
  44. local DB_SCHEM = {Users = {}, Password = {}, Biolock = {}, AccessLevel = {}, Money = {}}
  45.  
  46. local function USERS_CREATEDB()
  47. local USER_TEMP = fs.open(USER_FILE, "w")
  48. USER_TEMP.write(textutils.serialise(DB_SCHEM))
  49. USER_TEMP.close()
  50. end
  51.  
  52. local function USERS_GET()
  53. local OBJECT_UF = fs.open(USER_FILE, "r")
  54. local USERS = textutils.unserialise(OBJECT_UF.readAll())
  55. if USERS == nil then
  56. USERS_CREATEDB()
  57. USERS = textutils.unserialise(OBJECT_UF.readAll())
  58. end
  59. OBJECT_UF.close()
  60. return USERS
  61. end
  62.  
  63. local function USERS_ID(users, tofind)
  64. local ID = nil
  65. for i = 1,#users["Users"] do
  66. if tofind == nil then
  67. ID = i
  68. else
  69. if users["Users"][i] == tofind then
  70. ID = i
  71. end
  72. end
  73. end
  74. return ID
  75. end
  76.  
  77. local function USERS_BIOLOCK(users, tofind)
  78. local ID = nil
  79. for i = 1,#users["Biolock"] do
  80. if tofind == nil then return 0x1
  81. else
  82. if users["Biolock"][i] == tofind then
  83. ID = i
  84. end
  85. end
  86. end
  87. return ID
  88. end
  89.  
  90. local function USERS_WHERE(users, tofind)
  91. local ID = nil
  92. for i = 1,#users["Where"] do
  93. if tofind == nil then return 0x1
  94. else
  95. if users["Where"][i] == tofind then
  96. ID = i
  97. end
  98. end
  99. end
  100. return ID
  101. end
  102.  
  103. --Encryption and modems
  104. local function SEND(sID, content)
  105. if type(sID) ~= "number" or content == nil then return 0x1 end
  106. local Users = USERS_GET()
  107. --local serialised = textutils.serialise(content)
  108. --local FinalTable = StringUtils.encrypt(serialised) DO NOT WORK
  109. modem.transmit(MODEM_CHANNEL, MODEM_CHANNEL, content)
  110. return 0
  111. end
  112.  
  113. local function USERS_CREATE(name, pass, biolock, accesslevel)
  114.  
  115. local oUSERS = USERS_GET()
  116. local fUSERS = oUSERS
  117. local lastID = USERS_ID(oUSERS)
  118.  
  119. --Check if user already exist because or AL will not fuck my script ;(
  120. if USERS_ID(oUSERS, name) ~= nil then return 0x1 end
  121. if accesslevel > 5 or accesslevel < 1 then return 0x1 end
  122. if lastID == nil then lastID = 1 else lastID = lastID + 1 end
  123. if name == nil or pass == nil or accesslevel == nil then
  124. return 0x1
  125. end
  126. if biolock == nil then biolock = "unknown" end
  127. --end of check
  128.  
  129. fUSERS["Users"][lastID] = name
  130. fUSERS["Password"][lastID] = StringUtils.SHA1(pass)
  131. fUSERS["Biolock"][lastID] = biolock
  132. fUSERS["Money"][lastID] = 50
  133. fUSERS["AccessLevel"][lastID] = accesslevel
  134. local TO_SAVE = fs.open(USER_FILE,"w")
  135. TO_SAVE.write(textutils.serialise(fUSERS))
  136. TO_SAVE.close()
  137. return 0
  138. end
  139.  
  140. local function USERS_UPDATE(toUpdate, userID, newUpdate)
  141. if type(toUpdate) ~= "string" or type(userID) ~= "number" or newUpdate == nil then return 0x1 end
  142. local oUSERS = USERS_GET()
  143. --Check if id exist
  144. local lastID = USERS_ID(oUSERS)
  145. if userID > lastID then return 0x2 end
  146.  
  147. if toUpdate == "password" then
  148. if type(newUpdate) ~= "string" then return 0x1 end
  149. oUSERS["Password"][userID] = newUpdate
  150. local TO_SAVE = fs.open(USER_FILE,"w")
  151. TO_SAVE.write(textutils.serialise(oUSERS))
  152. TO_SAVE.close()
  153. return 0
  154. elseif toUpdate == "Money" then
  155. if type(newUpdate) ~= "number" then return 0x1 end
  156. oUSERS["Money"][userID] = newUpdate
  157. local TO_SAVE = fs.open(USER_FILE,"w")
  158. TO_SAVE.write(textutils.serialise(oUSERS))
  159. TO_SAVE.close()
  160. return 0
  161. elseif toUpdate == "accesslevel" then
  162. if type(newUpdate) ~= "number" then return 0x1 end
  163. oUSERS["AccessLevel"][userID] = newUpdate
  164. local TO_SAVE = fs.open(USER_FILE,"w")
  165. TO_SAVE.write(textutils.serialise(oUSERS))
  166. TO_SAVE.close()
  167. return 0
  168. end
  169.  
  170. end
  171.  
  172. --EXEC
  173.  
  174. local function execute(sTable,sID)
  175. local uLevel = -1
  176. local uID = 0
  177. if sTable.type ~= "ping" then
  178. if sTable["fromUsername"] == nil or sTable["fromPassword"] == nil then return SEND(sID, 0x3) end
  179. --Check account
  180. local uTable = USERS_GET()
  181. uID = USERS_ID(uTable, sTable.fromUsername)
  182. if uID == nil then return SEND(sID, 0x2) end
  183. --Encryption password is: "1" (Encryption of the SHA-1 password with a password and decryption server side)
  184. if StringUtils.decrypt(sTable.fromPassword, uTable["Password"][uID]) == "1" then
  185. print(uTable["Users"][uID].." Logged")
  186. uLevel = uTable["AccessLevel"][uID]
  187. else
  188. return SEND(sID, 0x5)
  189. end
  190. end
  191. if sTable.type == "ping" then
  192. SEND(sID,0x0)
  193.  
  194. elseif sTable.type == "transaction" then
  195. --if you see this: buy Life.exe or Life.bin if you are on Linux. Thank you ! :p
  196. if sTable["amount"] == nil or sTable["to"] == nil then return SEND(sID, 0x1) end
  197. local uTable = USERS_GET()
  198. local toID = USERS_ID(uTable, sTable.to)
  199. if toID == nil then SEND(sID, 0x9) end
  200. --Check money
  201. if uTable["Money"][uID] >= sTable["amount"] then else return SEND(sID, 0x8) end
  202. --Transfering
  203. USERS_UPDATE("Money", uID, uTable["Money"][uID] - sTable["amount"])
  204. USERS_UPDATE("Money", toID, uTable["Money"][toID] + sTable["amount"])
  205. proLog(uTable["Users"][uID], uTable["Users"][toID], sTable["amount"])
  206. SEND(sID, 0x0)
  207.  
  208. elseif sTable.type == "database" then
  209. if sTable.todo == "createUser" then
  210. if uLevel < 4 then return SEND(sID, 0x6) end
  211. if sTable.Username == nil or sTable.Password == nil or sTable.Biolock == nil or type(sTable.accesslevel) == number then
  212. print("User creation failed")
  213. SEND(sID, 0x1)
  214. else
  215. if USERS_CREATE(sTable.Username,sTable.Password,sTable.Biolock,sTable.accesslevel) then
  216. print("User Created")
  217. SEND(sID, 0x0)
  218. else
  219. print("Failed to create")
  220. SEND(sID, 0x7)
  221. end
  222. end
  223.  
  224. elseif sTable.todo == "findBL" then
  225. if uLevel ~= 0 and uLevel ~= 5 then return SEND(sID, "Bad accessLevel") end
  226. if type(sTable.toFind) ~= "string" then return SEND(sID, 0x1) end
  227. local uTable = USERS_GET()
  228. local uID = USERS_BIOLOCK(uTable, sTable.toFind)
  229. if uID == nil then return SEND(sID, 0x0) end -- 0x0 <- Unknown user
  230. return SEND(sID, uTable["AccessLevel"][uID])
  231.  
  232. elseif sTable.todo == "reset" then
  233. if uLevel < 4 then return SEND(sID, "Bad accessLevel") end
  234. USERS_CREATEDB()
  235. print("Database reseted") --Pls add a Terminal confirm
  236. SEND(sID, 0x0)
  237.  
  238. elseif sTable.todo == "Userinfo" then
  239. local TUserTable = USERS_GET()
  240. local Tid = uID
  241. if Tid == nil then SEND(sID, 0x2) else
  242. local Tbiolock = TUserTable["Biolock"][Tid]
  243. local TAccessL = TUserTable["AccessLevel"][Tid]
  244. local TMoney = TUserTable["Money"][Tid]
  245. local TEMP_TABLE = {id = Tid, biolockid = Tbiolock, accesslevel = TAccessL, money = TMoney}
  246. SEND(sID, TEMP_TABLE)
  247. end
  248. end
  249. else
  250. SEND(sID, 0x1)
  251. end
  252. end
  253.  
  254. --Starting
  255. print("Modem state: "..tostring(modem.isOpen(MODEM_CHANNEL)))
  256. if modem.isOpen(MODEM_CHANNEL) ~= true then print("Modem opened\nID: ".. os.computerID()) modem.open(MODEM_CHANNEL) end
  257.  
  258. local function receiveo()
  259. while RP_BOOL do
  260. local event, modemSide, sID, rID, msg, distance = os.pullEvent("modem_message")
  261. if type(msg) ~= "table" then SEND(sID, "Please use API") else
  262. execute(msg,sID)
  263. end
  264. end
  265. end
  266.  
  267. parallel.waitForAll(receiveo)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement