Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.46 KB | None | 0 0
  1. --Var init
  2. local RP_BOOL = true
  3. os.loadAPI("StringUtils")
  4. local modem = peripheral.find("modem")
  5. local MODEM_CHANNEL = 644
  6.  
  7. --Log
  8. local function log(ID, oMSG, reason)
  9. local FILE = fs.open("/log", "a")
  10.  
  11. if type(oMSG) == "string" then
  12. toMSG = "bad type(string)"
  13. elseif type(oMSG) == "table" then
  14. if oMSG["type"] ~= nil then
  15. toMSG = "bad table"
  16. else
  17. toMSG = oMSG["type"]
  18. end
  19. else
  20. toMSG = "unknown"
  21. end
  22.  
  23. local TEMP = "[LOG] ID:"..ID.." toMSG: "..toMSG.." Reason: "..reason
  24. print(TEMP)
  25. FILE.writeLine(TEMP)
  26. FILE.close()
  27.  
  28. return true
  29. end
  30.  
  31.  
  32. --Users management
  33. local USER_FILE = "/users"
  34. local DB_SCHEM = {Users = {}, Password = {}, Biolock = {}, AccessLevel = {}, Token= {}, Where= {}}
  35.  
  36. local function USERS_CREATEDB()
  37. local USER_TEMP = fs.open(USER_FILE, "w")
  38. USER_TEMP.write(textutils.serialise(DB_SCHEM))
  39. USER_TEMP.close()
  40. end
  41.  
  42. local function USERS_GET()
  43. local OBJECT_UF = fs.open(USER_FILE, "r")
  44. local USERS = textutils.unserialise(OBJECT_UF.readAll())
  45. if USERS == nil then
  46. USERS_CREATEDB()
  47. USERS = textutils.unserialise(OBJECT_UF.readAll())
  48. end
  49. OBJECT_UF.close()
  50. return USERS
  51. end
  52.  
  53. local function USERS_ID(users, tofind)
  54. local ID = nil
  55. for i = 1,#users["Users"] do
  56. if tofind == nil then
  57. ID = i
  58. else
  59. if users["Users"][i] == tofind then
  60. ID = i
  61. end
  62. end
  63. end
  64. return ID
  65. end
  66.  
  67. local function USERS_BIOLOCK(users, tofind)
  68. local ID = nil
  69. for i = 1,#users["Biolock"] do
  70. if tofind == nil then return false
  71. else
  72. if users["Biolock"][i] == tofind then
  73. ID = i
  74. end
  75. end
  76. end
  77. return ID
  78. end
  79.  
  80. local function USERS_TOKEN(users, tofind)
  81. local ID = nil
  82. for i = 1,#users["Token"] do
  83. if tofind == nil then return false
  84. else
  85. if users["Token"][i] == tofind then
  86. ID = i
  87. end
  88. end
  89. end
  90. return ID
  91. end
  92.  
  93. local function USERS_WHERE(users, tofind)
  94. local ID = nil
  95. for i = 1,#users["Where"] do
  96. if tofind == nil then return false
  97. else
  98. if users["Where"][i] == tofind then
  99. ID = i
  100. end
  101. end
  102. end
  103. return ID
  104. end
  105.  
  106. --Encryption and modems
  107. local function SEND(sID, content)
  108. if type(sID) ~= "number" or content == nil then return false end
  109. local Users = USERS_GET()
  110. --local serialised = textutils.serialise(content)
  111. --local FinalTable = StringUtils.encrypt(serialised) DO NOT WORK
  112. modem.transmit(MODEM_CHANNEL, MODEM_CHANNEL, content)
  113. end
  114.  
  115. local function USERS_CREATE(name, pass, biolock, accesslevel)
  116.  
  117.  
  118. local oUSERS = USERS_GET()
  119. local fUSERS = oUSERS
  120. local lastID = USERS_ID(oUSERS)
  121.  
  122. --Check if user already exist because or AL will not fuck my script ;(
  123. if USERS_ID(oUSERS, name) ~= nil then return false end
  124. if accesslevel > 5 or accesslevel < 1 then return false end
  125. if lastID == nil then lastID = 1 else lastID = lastID + 1 end
  126. if name == nil or pass == nil or accesslevel == nil then
  127. return false
  128. end
  129. if biolock == nil then biolock = "unknown" end
  130. --end of check
  131.  
  132. fUSERS["Users"][lastID] = name
  133. fUSERS["Password"][lastID] = StringUtils.SHA1(pass)
  134. fUSERS["Biolock"][lastID] = biolock
  135. fUSERS["Token"][lastID] = "unknown"
  136. fUSERS["AccessLevel"][lastID] = accesslevel
  137. fUSERS["Where"][lastID] = "unknown"
  138. local TO_SAVE = fs.open(USER_FILE,"w")
  139. TO_SAVE.write(textutils.serialise(fUSERS))
  140. TO_SAVE.close()
  141. return true
  142. end
  143.  
  144. local function USERS_UPDATE(toUpdate, userID, newUpdate)
  145. if type(toUpdate) ~= "string" or type(userID) ~= "number" or newUpdate == nil then return false end
  146. local oUSERS = USERS_GET()
  147. --Check if id exist
  148. local lastID = USERS_ID(oUSERS)
  149. if userID > lastID then return false end
  150.  
  151. if toUpdate == "token" then
  152. oUSERS["Token"][userID] = newUpdate
  153. local TO_SAVE = fs.open(USER_FILE,"w")
  154. TO_SAVE.write(textutils.serialise(oUSERS))
  155. TO_SAVE.close()
  156. return true
  157. elseif toUpdate == "accesslevel" then
  158. if type(newUpdate) ~= "number" then return false end
  159. oUSERS["AccessLevel"][userID] = newUpdate
  160. local TO_SAVE = fs.open(USER_FILE,"w")
  161. TO_SAVE.write(textutils.serialise(oUSERS))
  162. TO_SAVE.close()
  163. return true
  164. end
  165.  
  166. end
  167.  
  168.  
  169.  
  170.  
  171. --Token functions
  172.  
  173. local function TOKEN_GEN()
  174. local TOKEN_LENGHT = 25
  175. local TOKEN = ""
  176. 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","#", "$", "*"}
  177. local x = 0
  178. while TOKEN_LENGHT > 0 do
  179. x=x+1
  180. local mRandom = math.random(1,#TOKEN_TABLE)
  181. TOKEN = TOKEN..TOKEN_TABLE[mRandom]
  182. TOKEN_LENGHT = TOKEN_LENGHT - 1
  183. print("Generating Token: "..tostring(x).."/25")
  184. end
  185. return TOKEN
  186. end
  187.  
  188. local function TOKEN_CREATE(id)
  189. local USERS = USERS_GET()
  190. if type(id) ~= "number" then return false end
  191. local newToken = TOKEN_GEN()
  192. --Check if token already exist
  193. local YES = false
  194. while YES do
  195. if USERS_TOKEN(USERS, newToken) == nil then YES = false else
  196. newToken = TOKEN_GEN() end
  197. end
  198. --Write the new token
  199. if USERS_UPDATE("token", id, newToken) then
  200. return newToken
  201. else
  202. return false
  203. end
  204. end
  205.  
  206. --We need the famous Gitano Parser !
  207. local function gitanoParser(sTable,sID)
  208. local uLevel = 0
  209. if sTable.todo ~= "getToken" or sTable.todo ~= "createToken" then
  210. if sTable["tokenUsername"] == nil or sTable["tokenCheck"] == nil then return SEND(sID, "Nil entry") end
  211. --Check account
  212. local uTable = USERS_GET()
  213. local uID = USERS_ID(uTable, sTable["tokenUsername"])
  214. if uID == nil then return SEND(sID, "Bad Username") end
  215. local uTOKEN = uTable["Token"][uID]
  216. if StringUtils.decrypt(sTable["tokenCheck"], uTOKEN) ~= "true" then return SEND(sID, "Bad token") end
  217. uLevel = uTable["AccessLevel"][uID]
  218. end
  219. if sTable.type == "mdr" then
  220. log(sID, sTable, "bad type")
  221. return false
  222.  
  223. elseif sTable.type == "token" then
  224. if sTable.todo == "createToken" then
  225. if type(sTable.enc) ~= "string" or type(sTable.Username) ~= "string" or type(sTable.math) ~= "string" then SEND(sID, "nil entry") else
  226. local oTable = USERS_GET()
  227. local uID = USERS_ID(oTable,sTable.Username)
  228. if uID == nil then send(sID, "Unknown user") return false end
  229. local SHA1Pass = oTable["Password"][uID]
  230. local crypted = StringUtils.decrypt(sTable.enc, SHA1Pass)
  231. if crypted == sTable.math then
  232. --He got it we need to give a token to him
  233. local Token = TOKEN_CREATE(uID)
  234. SEND(sID, Token)
  235. print("Token created for: "..sTable["Username"])
  236. print("New Token hash: "..StringUtils.SHA1(Token))
  237. else
  238. SEND(sID, "Bad password")
  239. end
  240. end
  241.  
  242. elseif sTable.todo == "getToken" then
  243. if type(sTable.enc) ~= "string" or type(sTable.Username) ~= "string" or type(sTable.math) ~= "string" then SEND(sID, "nil entry") else
  244. local oTable = USERS_GET()
  245. local uID = USERS_ID(oTable,sTable.Username)
  246. if uID == nil then SEND(sID, "Unknown user") return false end
  247. local SHA1Pass = oTable["Password"][uID]
  248. local crypted = StringUtils.decrypt(sTable.enc, SHA1Pass)
  249. if crypted == sTable.math then
  250. --He got it we need to give a token to him
  251. SEND(sID, oTable["Token"][uID])
  252. else
  253. SEND(sID, "Bad password")
  254. end
  255. end
  256. end
  257.  
  258. elseif sTable.type == "ping" then
  259. SEND(sID, "true")
  260. log(sID, sTable, "ping")
  261.  
  262. elseif sTable.type == "database" then
  263.  
  264. if sTable.todo == "createUser" then
  265. if uLevel < 4 then return SEND(sID, "Bad accessLevel") end
  266. if sTable.Username == nil or sTable.Password == nil or sTable.Biolock == nil or type(sTable.accesslevel) == number then
  267. print("User creation failed")
  268. SEND(sID, "nil in table")
  269. else
  270. if USERS_CREATE(sTable.Username,sTable.Password,sTable.Biolock,sTable.accesslevel) then
  271. print("User Created")
  272. SEND(sID, "true")
  273. else
  274. print("Failed to create")
  275. SEND(sID, "error")
  276. end
  277. end
  278.  
  279. elseif sTable.todo == "reset" then
  280. if uLevel < 4 then return SEND(sID, "Bad accessLevel") end
  281. USERS_CREATEDB()
  282. print("Database reseted") --Pls add a Terminal confirm
  283. SEND(sID, "true")
  284.  
  285. elseif sTable.todo == "Userinfo" then
  286. if uLevel ~= 0 or uLevel ~= 5 then return SEND(sID, "Bad accessLevel") end
  287. if sTable.Username == nil then
  288. SEND(sID, "Nil entry")
  289.  
  290. else
  291. local TUserTable = USERS_GET()
  292. local Tid = USERS_ID(TUserTable, sTable.Username)
  293. if Tid == nil then print("Nil TID") SEND(sID, "nil id") else
  294. local Tbiolock = TUserTable["Biolock"][Tid]
  295. local TAccessL = TUserTable["AccessLevel"][Tid]
  296. local TEMP_TABLE = {id = Tid, biolockid = Tbiolock, accesslevel = TAccessL}
  297. SEND(sID, TEMP_TABLE)
  298. print("Succefuly sended")
  299. end
  300. end
  301. end
  302.  
  303. else
  304. SEND(sID, "bad TableType")
  305. end
  306. end
  307.  
  308. --Starting
  309. print("Modem state: "..tostring(modem.isOpen(MODEM_CHANNEL)))
  310. if modem.isOpen(MODEM_CHANNEL) ~= true then modem.open(MODEM_CHANNEL) end
  311. print("Modem opened\nID: ".. os.computerID())
  312.  
  313. local function receiveparse()
  314. while RP_BOOL do
  315. local event, modemSide, sID, rID, msg, distance = os.pullEvent("modem_message")
  316. if type(msg) ~= "table" then SEND(sID, "Please use API") log(sID, msg, "Dont use a table") else
  317. gitanoParser(msg,sID) -- redirect to gitanoParser
  318. end
  319. end
  320. end
  321.  
  322. parallel.waitForAll(receiveparse)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement