Advertisement
Guest User

tak2

a guest
May 18th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1)
  3.  
  4. enc1 = {29, 58, 93, 28, 27}
  5.  
  6. os.loadAPI("TextUtils")
  7. os.loadAPI("FileUtils")
  8.  
  9. rednet.open("back")
  10.  
  11. function genAuthCode(str, rem, s)
  12. enc = TextUtils.encrypt(str, enc1)
  13. encSub = string.sub(enc,1,15)
  14. authCode = rem..tostring(s)..encSub
  15. return authCode
  16. end
  17.  
  18. users = {}
  19. if fs.exists("Users") then
  20. users = FileUtils.readVariableFile("Users")
  21. end
  22.  
  23. function getUserIndex(username)
  24. for i=1, #users do
  25. if users[i][1] == username then
  26. return i
  27. end
  28. end
  29. return 0
  30. end
  31.  
  32. print("User Server (ID: " .. os.getComputerID() .. ")")
  33.  
  34. while true do
  35. redstone.setOuput("bottom", true)
  36. s, m = rednet.receive()
  37. redstone.setOuput("bottom", false)
  38. -- CAN ACCEPT CREATE, LOGIN or AUTH
  39. -- SENDS PASS OR FAIL
  40. ms = TextUtils.split(m, ":")
  41. action = ms[1]
  42. if action == "CREATE" then
  43. username = ms[2]
  44. password = TextUtils.encrypt(ms[3], enc1)
  45. remPas = ms[3]
  46. thisUser = getUserIndex(username)
  47. if thisUser == 0 then
  48. newUser = {username, password, remPas, s}
  49. table.insert(users, newUser)
  50. rednet.send(s, "PASS:"..genAuthCode(username, remPas, s))
  51. else
  52. rednet.send(s, "FAIL:User already exists.")
  53. end
  54. elseif action == "LOGIN" then
  55. username = ms[2]
  56. password = TextUtils.encrypt(ms[3], enc1)
  57. thisUser = getUserIndex(username)
  58. if thisUser == 0 then
  59. rednet.send(s, "FAIL:User not found.")
  60. else
  61. if users[thisUser][2] == password then
  62. tempuser = users[thisUser]
  63. tempuser[4] = s
  64. rednet.send(s, "PASS:"..genAuthCode(username,tempuser[3],s))
  65. users[thisUser] = tempuser
  66. else
  67. rednet.send(s, "FAIL:Wrong password.")
  68. end
  69. end
  70. elseif action == "AUTH" then
  71. username = ms[2]
  72. attemptedAuth = ms[3]
  73. rem = string.sub(attemptedAuth,1,1)
  74. if rem == "0" then
  75. rednet.send(s, "FAIL:User must reenter password.")
  76. else
  77. thisUser = getUserIndex(username)
  78. if thisUser == 0 then
  79. rednet.send(s, "FAIL:User could not be found")
  80. else
  81. actualAuth = genAuthCode(username, rem, users[thisUser][4])
  82. if actualAuth == attemptedAuth then
  83. rednet.send(s, "PASS:"..actualAuth)
  84. else
  85. rednet.send(s, "FAIL:Auth code not valid.")
  86. end
  87. end
  88. end
  89. else
  90. rednet.send(s, "FAIL:No such protocol.")
  91. end
  92. FileUtils.writeVariableFile("Users", users)
  93. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement