Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Changes
- -- * moved everything into the .authKey folder.
- -- the . makes the folder hidden
- --
- -- * added block to create .authKey folder if it
- -- does not exist
- --
- -- * cahnged backslashes in file path strings to
- -- forward slahes to prevent charecter escapes.
- --
- -- * renamed pass to passFile because having a
- -- pass and a password is confusing.
- --
- -- * changed password to a local variable so others
- -- cannot read it after program runs.
- --
- -- * added parenthesis to pass.close
- --
- -- * added return statement to first code block
- -- to get rid of the setup variable
- --
- -- * changed "password = pass.readAll()" to
- -- "password == pass.readAll()"
- --
- -- * Closed the passFile in password checking section.
- --
- -- * replaced the statement "goodPass = false" with
- -- a return statement. This eliminates the
- -- goodPass variable and removed a layer of
- -- indentation of the bulk of your program.
- --
- -- * Moved all the code that creates files if they
- -- dont exist into the beginning. Its good to put
- -- similair code together.
- --
- -- * Moved code into local functions to seperate the
- -- code that checks arguments and the code that
- -- actually does stuff. This also means all your
- -- code isn't in a gigantic if statement.
- --
- -- * renamed ranKey to randKey
- --
- -- * made pcID, user, and key local variables
- --
- -- * changed send functions to rednet.send functions
- --
- -- * changed "elseif fs.exists("authIDs\".. pcID) then"
- -- to "else". there is only to parts of your if
- -- and you dont have to check if the file exists twice.
- --
- -- * switched the sections in the " if not fs.exists("/.authKey/authIDs/".. pcID) then"
- -- statement. I like to avoid negative logic.
- --
- -- * moved code into a main function. I like to do this
- -- becuase all the main logic is up front and you
- -- dont have to worry about what order all your
- -- functions were declared. There is a main() at the
- -- bottom of the code.
- --
- -- Edited by: GhastTearz
- -- Original code at: www.pastebin.com/mGUecsv8
- local args = { ... }
- local function main()
- -- Make sure all needed files exist
- if not fs.exists("/.authKey") then
- fs.makeDir("/.authKey")
- end
- if not fs.exists("/.authKey/authKeys") then
- fs.makeDir("/.authKey/authKeys")
- end
- if not fs.exists("/.authKey/authIDs") then
- fs.makeDir("/.authKey/authIDs")
- end
- if not fs.exists("/.authKey/Settings") then
- fs.makeDir("/.authKey/Settings")
- print("Please set an admin password:")
- local password = read('*')
- local passFile = fs.open("/.authKey/Settings/password", "w")
- passFile.write(password)
- passFile.close()
- print("Thank you. You may now log in with that password in the future to make keys.")
- print("Keys can only be made from this server, and nowhere else.")
- return
- end
- print("Please enter your password:")
- local password = read('*')
- local passFile = fs.open("/.authKey/Settings/password", "r")
- local correctPassword = passFile.readAll()
- passFile.close()
- if password == correctPassword then
- print("Password correct, logging you in.")
- else
- printError("Password incorrect. Please try again.")
- return
- end
- if args[1] == "createKey" then
- if not args[2] then
- printError("Useage: authKey createKey <Username>")
- else
- createKey(args[2])
- end
- elseif args[1] == "startListener" then
- startListener()
- elseif args[1] == "changePass" then
- changePass()
- else
- printError("Usage:")
- printError("Add new user: authKey createKey <Username>")
- printError("Start listening for connections: authKey startListener")
- printError("Change admin password: authKey changePass")
- end
- end
- local function createKey(userName)
- math.randomseed(os.time())
- randKey = math.random(10000000)
- local keys = fs.open("/.authKey/authKeys/".. userName, "w")
- keys.write(randKey)
- keys.close()
- print("Your key is: ".. ranKey)
- print("User ".. userName.. " can log in with the client using their username and key.")
- print("This key is valid for a one time use, and will never be usable again.")
- end
- local function startListener()
- while true do
- local pcID
- local user
- local key
- pcID, user, key = rednet.receive()
- if fs.exists("/.authKey/authIDs/".. pcID) then
- userFile = fs.open("/.authKey/authIDs/".. pcID, "r")
- user = userFile.readAll()
- userFile.close()
- rednet.send(pcID, "Welcome back, ".. user.. "!")
- else
- if fs.exists("/.authKey/authKeys/".. user) then
- keyFile = fs.open("/.authKey/authKeys/".. user, "r")
- if key == keyFile.readAll() then
- keyFile.close()
- rednet.send(pcID, "Success")
- userPcID = fs.open("/.authKey/authIDs/".. pcID, "w")
- userPcID.write(user)
- userPcID.close()
- fs.delete("/.authKey/authKeys/".. user)
- else
- keyFile.close()
- rednet.send(pcID, "Failure: Key does not match user.")
- end
- else
- rednet.send(pcID, "Failire: User does not exist.")
- end
- end
- end
- end
- local function changePass()
- print("Please set a new admin password: ")
- local password = read('*')
- local passFile = fs.open("/.authFile/Settings/password", "w")
- pass.write(password)
- pass.close()
- print("Thank you. You may now log in with that password in the future to make keys.")
- print("Keys can only be made from this server, and nowhere else.")
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement