Advertisement
Guest User

Untitled

a guest
Oct 11th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.42 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 Token = Users[Tokens][uID]
  111. --local serialised = textutils.serialise(content)
  112. --local FinalTable = StringUtils.encrypt(serialised) DO NOT WORK
  113. modem.transmit(MODEM_CHANNEL, MODEM_CHANNEL, content)
  114. end
  115.  
  116. local function USERS_CREATE(name, pass, biolock, accesslevel)
  117.  
  118.  
  119. local oUSERS = USERS_GET()
  120. local fUSERS = oUSERS
  121. local lastID = USERS_ID(oUSERS)
  122.  
  123. --Check if user already exist because or AL will not fuck my script ;(
  124. if USERS_ID(oUSERS, name) ~= nil then return false end
  125. if accesslevel > 5 or accesslevel < 1 then return false end
  126. if lastID == nil then lastID = 1 else lastID = lastID + 1 end
  127. if name == nil or pass == nil or accesslevel == nil then
  128. return false
  129. end
  130. if biolock == nil then biolock = "unknown" end
  131. --end of check
  132.  
  133. fUSERS["Users"][lastID] = name
  134. fUSERS["Password"][lastID] = StringUtils.SHA1(pass)
  135. fUSERS["Biolock"][lastID] = biolock
  136. fUSERS["Token"][lastID] = "unknown"
  137. fUSERS["AccessLevel"][lastID] = accesslevel
  138. fUSERS["Where"][lastID] = "unknown"
  139. local TO_SAVE = fs.open(USER_FILE,"w")
  140. TO_SAVE.write(textutils.serialise(fUSERS))
  141. TO_SAVE.close()
  142. return true
  143. end
  144.  
  145. local function USERS_UPDATE(toUpdate, userID, newUpdate)
  146. if type(toUpdate) ~= "string" or type(userID) ~= "number" or newUpdate == nil then return false end
  147. local oUSERS = USERS_GET()
  148. --Check if id exist
  149. local lastID = USERS_ID(oUSERS)
  150. if userID > lastID then return false end
  151.  
  152. if toUpdate == "token" then
  153. oUSERS["Token"][userID] = newUpdate
  154. local TO_SAVE = fs.open(USER_FILE,"w")
  155. TO_SAVE.write(textutils.serialise(oUSERS))
  156. TO_SAVE.close()
  157. return true
  158. elseif toUpdate == "accesslevel" then
  159. if type(newUpdate) ~= "number" then return false end
  160. oUSERS["AccessLevel"][userID] = newUpdate
  161. local TO_SAVE = fs.open(USER_FILE,"w")
  162. TO_SAVE.write(textutils.serialise(oUSERS))
  163. TO_SAVE.close()
  164. return true
  165. end
  166.  
  167. end
  168.  
  169.  
  170.  
  171.  
  172. --Token functions
  173.  
  174. local function TOKEN_GEN()
  175. local TOKEN_LENGHT = 25
  176. local TOKEN = ""
  177. 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","#", "$", "*"}
  178. local x = 0
  179. while TOKEN_LENGHT > 0 do
  180. x=x+1
  181. local mRandom = math.random(1,#TOKEN_TABLE)
  182. TOKEN = TOKEN..TOKEN_TABLE[mRandom]
  183. TOKEN_LENGHT = TOKEN_LENGHT - 1
  184. print("Generating Token: "..tostring(x).."/25")
  185. end
  186. return TOKEN
  187. end
  188.  
  189. local function TOKEN_CREATE(id)
  190. local USERS = USERS_GET()
  191. if type(id) ~= "number" then return false end
  192. local newToken = TOKEN_GEN()
  193. --Check if token already exist
  194. local YES = false
  195. while YES do
  196. if USERS_TOKEN(USERS, newToken) == nil then YES = false else
  197. newToken = TOKEN_GEN() end
  198. end
  199. --Write the new token
  200. if USERS_UPDATE("token", id, newToken) then
  201. return newToken
  202. else
  203. return false
  204. end
  205. end
  206.  
  207. --We need the famous Gitano Parser !
  208. local function gitanoParser(sTable,sID)
  209. if sTable["tokenUsername"] == nil or sTable["tokenCheck"] == nil then return SEND(sID, "Nil entry") end
  210. --Check account
  211. local uTable = USERS_GET()
  212. local uID = USERS_ID(uTable, sTable["tokenUsername"])
  213. if uID == nil return then SEND(sID, "Bad Username") end
  214. local uTOKEN = uTable["Token"][uID]
  215. if StringUtils.decrypt(sTable["tokenCheck"], uTOKEN) ~= "true" end return SEND(sID, "Bad token") end
  216. local uLevel = uTable["AccessLevel"][uID]
  217.  
  218. if sTable.type == "mdr" then
  219. log(sID, sTable, "bad type")
  220. return false
  221.  
  222. elseif sTable.type == "token" then
  223. if sTable.todo == "createToken" then
  224. if type(sTable.enc) ~= "string" or type(sTable.Username) ~= "string" or type(sTable.math) ~= "string" then SEND(sID, "nil entry") else
  225. local oTable = USERS_GET()
  226. local uID = USERS_ID(oTable,sTable.Username)
  227. if uID == nil then send(sID, "Unknown user") return false end
  228. local SHA1Pass = oTable["Password"][uID]
  229. local crypted = StringUtils.decrypt(sTable.enc, SHA1Pass)
  230. if crypted == sTable.math then
  231. --He got it we need to give a token to him
  232. local Token = TOKEN_CREATE(uID)
  233. SEND(sID, Token)
  234. print("Token created for: "..sTable["Username"])
  235. print("New Token hash: "..StringUtils.SHA1(Token))
  236. else
  237. SEND(sID, "Bad password")
  238. end
  239. end
  240.  
  241. elseif sTable.todo == "getToken" then
  242. if type(sTable.enc) ~= "string" or type(sTable.Username) ~= "string" or type(sTable.math) ~= "string" then SEND(sID, "nil entry") else
  243. local oTable = USERS_GET()
  244. local uID = USERS_ID(oTable,sTable.Username)
  245. if uID == nil then SEND(sID, "Unknown user") return false end
  246. local SHA1Pass = oTable["Password"][uID]
  247. local crypted = StringUtils.decrypt(sTable.enc, SHA1Pass)
  248. if crypted == sTable.math then
  249. --He got it we need to give a token to him
  250. SEND(sID, oTable["Token"][uID])
  251. else
  252. SEND(sID, "Bad password")
  253. end
  254. end
  255. end
  256.  
  257. elseif sTable.type == "ping" then
  258. SEND(sID, "true")
  259. log(sID, sTable, "ping")
  260.  
  261. elseif sTable.type == "database" then
  262.  
  263. if sTable.todo == "createUser" then
  264. if uLevel < 4 then return SEND(sID, "Bad accessLevel") end
  265. if sTable.Username == nil or sTable.Password == nil or sTable.Biolock == nil or type(sTable.accesslevel) == number then
  266. print("User creation failed")
  267. SEND(sID, "nil in table")
  268. else
  269. if USERS_CREATE(sTable.Username,sTable.Password,sTable.Biolock,sTable.accesslevel) then
  270. print("User Created")
  271. SEND(sID, "true")
  272. else
  273. print("Failed to create")
  274. SEND(sID, "error")
  275. end
  276. end
  277.  
  278. elseif sTable.todo == "reset" then
  279. if uLevel < 4 then return SEND(sID, "Bad accessLevel") end
  280. USERS_CREATEDB()
  281. print("Database reseted") --Pls add a Terminal confirm
  282. SEND(sID, "true")
  283.  
  284. elseif sTable.todo == "Userinfo" then
  285. if uLevel ~= 0 then return SEND(sID, "Bad accessLevel") end
  286. if sTable.Username == nil then
  287. SEND(sID, "Nil entry")
  288.  
  289. else
  290. local TUserTable = USERS_GET()
  291. local Tid = USERS_ID(TUserTable, sTable.Username)
  292. if Tid == nil then print("Nil TID") SEND(sID, "nil id") else
  293. local Tbiolock = TUserTable["Biolock"][Tid]
  294. local TAccessL = TUserTable["AccessLevel"][Tid]
  295. local TEMP_TABLE = {id = Tid, biolockid = Tbiolock, accesslevel = TAccessL}
  296. SEND(sID, TEMP_TABLE)
  297. print("Succefuly sended")
  298. end
  299. end
  300. end
  301.  
  302. else
  303. SEND(sID, "bad TableType")
  304. end
  305. end
  306.  
  307. --Starting
  308. term.write("Modem state: ")
  309. if modem.isOpen(MODEM_CHANNEL) then print("True") else print("False") end
  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