Advertisement
wow0

Untitled

Nov 5th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 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. if LOGS ~= nil then LOGS = textutils.unserialise(OBJECT_UF.readAll()) end
  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 users = USERS_GET()
  65. local ID = nil
  66. for i = 1,#users["Users"] do
  67. if tofind == nil then
  68. ID = i
  69. else
  70. if users["Users"][i] == tofind then
  71. ID = i
  72. end
  73. end
  74. end
  75. return ID
  76. end
  77.  
  78. local function USERS_BIOLOCK(users, tofind)
  79. local ID = nil
  80. for i = 1,#users["Biolock"] do
  81. if tofind == nil then return 0x1
  82. else
  83. if users["Biolock"][i] == tofind then
  84. ID = i
  85. end
  86. end
  87. end
  88. return ID
  89. end
  90.  
  91. local function USERS_WHERE(users, tofind)
  92. local ID = nil
  93. for i = 1,#users["Where"] do
  94. if tofind == nil then return 0x1
  95. else
  96. if users["Where"][i] == tofind then
  97. ID = i
  98. end
  99. end
  100. end
  101. return ID
  102. end
  103.  
  104. --Encryption and modems
  105. local function SEND(sID, content)
  106. if type(sID) ~= "number" or content == nil then return 0x1 end
  107. local Users = USERS_GET()
  108. --local serialised = textutils.serialise(content)
  109. --local FinalTable = StringUtils.encrypt(serialised) DO NOT WORK
  110. modem.transmit(MODEM_CHANNEL, MODEM_CHANNEL, content)
  111. return 0
  112. end
  113.  
  114. local function USERS_CREATE(name, pass, biolock, accesslevel)
  115.  
  116. local oUSERS = USERS_GET()
  117. local fUSERS = oUSERS
  118. local lastID = USERS_ID(oUSERS)
  119.  
  120. --Check if user already exist because or AL will not fuck my script ;(
  121. if USERS_ID(oUSERS, name) ~= nil then return 0x1 end
  122. if accesslevel > 5 or accesslevel < 1 then return 0x1 end
  123. if lastID == nil then lastID = 1 else lastID = lastID + 1 end
  124. if name == nil or pass == nil or accesslevel == nil then
  125. return 0x1
  126. end
  127. if biolock == nil then biolock = "unknown" end
  128. --end of check
  129.  
  130. fUSERS["Users"][lastID] = name
  131. fUSERS["Password"][lastID] = StringUtils.SHA1(pass)
  132. fUSERS["Biolock"][lastID] = biolock
  133. fUSERS["Money"][lastID] = 50
  134. fUSERS["AccessLevel"][lastID] = accesslevel
  135. local TO_SAVE = fs.open(USER_FILE,"w")
  136. TO_SAVE.write(textutils.serialise(fUSERS))
  137. TO_SAVE.close()
  138. return 0
  139. end
  140.  
  141. local function USERS_UPDATE(toUpdate, userID, newUpdate)
  142. if type(toUpdate) ~= "string" or type(userID) ~= "number" or newUpdate == nil then return 0x1 end
  143. local oUSERS = USERS_GET()
  144. --Check if id exist
  145. local lastID = USERS_ID(oUSERS)
  146. if userID > lastID then return 0x2 end
  147.  
  148. if toUpdate == "password" then
  149. if type(newUpdate) ~= "string" then return 0x1 end
  150. oUSERS["Password"][userID] = newUpdate
  151. local TO_SAVE = fs.open(USER_FILE,"w")
  152. TO_SAVE.write(textutils.serialise(oUSERS))
  153. TO_SAVE.close()
  154. return 0
  155. elseif toUpdate == "Money" then
  156. if type(newUpdate) ~= "number" then return 0x1 end
  157. oUSERS["Money"][userID] = newUpdate
  158. local TO_SAVE = fs.open(USER_FILE,"w")
  159. TO_SAVE.write(textutils.serialise(oUSERS))
  160. TO_SAVE.close()
  161. return 0
  162. elseif toUpdate == "accesslevel" then
  163. if type(newUpdate) ~= "number" then return 0x1 end
  164. oUSERS["AccessLevel"][userID] = newUpdate
  165. local TO_SAVE = fs.open(USER_FILE,"w")
  166. TO_SAVE.write(textutils.serialise(oUSERS))
  167. TO_SAVE.close()
  168. return 0
  169. end
  170.  
  171. end
  172.  
  173. --EXEC
  174.  
  175. local function execute(sTable,sID)
  176. local uLevel = -1
  177. local uID = 0
  178. if sTable.type ~= "ping" then
  179. if sTable["fromUsername"] == nil or sTable["fromPassword"] == nil then return SEND(sID, 0x3) end
  180. --Check account
  181. local uTable = USERS_GET()
  182. uID = USERS_ID(uTable, sTable.fromUsername)
  183. if uID == nil then return SEND(sID, 0x2) end
  184. --Encryption password is: "1" (Encryption of the SHA-1 password with a password and decryption server side)
  185. if StringUtils.decrypt(sTable.fromPassword, uTable["Password"][uID]) == "1" then
  186. print(uTable["Users"][uID].." Logged")
  187. uLevel = uTable["AccessLevel"][uID]
  188. else
  189. return SEND(sID, 0x5)
  190. end
  191. end
  192. if sTable.type == "ping" then
  193. SEND(sID,0x0)
  194.  
  195. elseif sTable.type == "update" then
  196. if sTable["toUpdate"] == nil or sTable["newUpdate"] == nil or sTable["who"] == nil then return SEND(sID, 0x1) end
  197. local nikID = USERS_ID( USERS_GET(), sTable.who)
  198. if nikID == nil then return SEND(sID, 0x9) end
  199. return SEND(sID, USERS_UPDATE(sTable.toUpdate, nikID, sTable.newUpdate))
  200.  
  201.  
  202. elseif sTable.type == "transaction" then
  203. --if you see this: buy Life.exe or Life.bin if you are on Linux. Thank you ! :p
  204. if sTable["amount"] == nil or sTable["to"] == nil then return SEND(sID, 0x1) end
  205. local uTable = USERS_GET()
  206. local toID = USERS_ID(uTable, sTable.to)
  207. if toID == nil then return SEND(sID, 0x9) end
  208. --Check money
  209. if toID == uID then return SEND(sID, 0x7) end
  210. if uTable["Money"][uID] >= sTable["amount"] then else return SEND(sID, 0x8) end
  211. --Transfering
  212. USERS_UPDATE("Money", uID, uTable["Money"][uID] - sTable["amount"])
  213. USERS_UPDATE("Money", toID, uTable["Money"][toID] + sTable["amount"])
  214. proLog(uTable["Users"][uID], uTable["Users"][toID], sTable["amount"])
  215. SEND(sID, 0x0)
  216.  
  217. elseif sTable.type == "database" then
  218. if sTable.todo == "createUser" then
  219. if uLevel < 4 then return SEND(sID, 0x6) end
  220. if sTable.Username == nil or sTable.Password == nil or sTable.Biolock == nil or type(sTable.accesslevel) == number then
  221. print("User creation failed")
  222. SEND(sID, 0x1)
  223. else
  224. if USERS_CREATE(sTable.Username,sTable.Password,sTable.Biolock,sTable.accesslevel) then
  225. print("User Created")
  226. SEND(sID, 0x0)
  227. else
  228. print("Failed to create")
  229. SEND(sID, 0x7)
  230. end
  231. end
  232.  
  233. elseif sTable.todo == "findBL" then
  234. if uLevel ~= 0 and uLevel ~= 5 then return SEND(sID, "Bad accessLevel") end
  235. if type(sTable.toFind) ~= "string" then return SEND(sID, 0x1) end
  236. local uTable = USERS_GET()
  237. local uID = USERS_BIOLOCK(uTable, sTable.toFind)
  238. if uID == nil then return SEND(sID, 0x0) end -- 0x0 <- Unknown user
  239. return SEND(sID, uTable["AccessLevel"][uID])
  240.  
  241. elseif sTable.todo == "reset" then
  242. if uLevel < 4 then return SEND(sID, "Bad accessLevel") end
  243. USERS_CREATEDB()
  244. print("Database reseted") --Pls add a Terminal confirm
  245. SEND(sID, 0x0)
  246.  
  247. elseif sTable.todo == "Userinfo" then
  248. local TUserTable = USERS_GET()
  249. local Tid = uID
  250. if Tid == nil then SEND(sID, 0x2) else
  251. local Tbiolock = TUserTable["Biolock"][Tid]
  252. local TAccessL = TUserTable["AccessLevel"][Tid]
  253. local TMoney = TUserTable["Money"][Tid]
  254. local TEMP_TABLE = {id = Tid, biolockid = Tbiolock, accesslevel = TAccessL, money = TMoney}
  255. SEND(sID, TEMP_TABLE)
  256. end
  257. end
  258. else
  259. SEND(sID, 0x1)
  260. end
  261. end
  262.  
  263. --Starting
  264. print("Modem state: "..tostring(modem.isOpen(MODEM_CHANNEL)))
  265. if modem.isOpen(MODEM_CHANNEL) ~= true then print("Modem opened\nID: ".. os.computerID()) modem.open(MODEM_CHANNEL) end
  266.  
  267. local function receiveo()
  268. while RP_BOOL do
  269. local event, modemSide, sID, rID, msg, distance = os.pullEvent("modem_message")
  270. if type(msg) ~= "table" then SEND(sID, "Please use API") else
  271. execute(msg,sID)
  272. end
  273. end
  274. end
  275.  
  276. parallel.waitForAll(receiveo)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement