Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------------------------
- --Copyright 2018 by Dusk the Dutch Angel Dragon
- -----------------------------------------------
- sides = {"top", "back", "left", "right", "bottom"}
- allowedID = {}
- username = {}
- password = {}
- modem = "null"
- --Define Functions
- function init()
- print("Initalizing Server...")
- print("Initalizing modem...")
- for i=1, #sides do
- if peripheral.getType(sides[i]) == "modem" then
- modem = sides[i]
- print("Modem found on "..sides[i].. " side.")
- rednet.open(modem)
- break
- elseif i == #sides and modem == "null" then
- error("Fatal Error: No Modem found.")
- end
- end
- username = loadTable("uname")
- password = loadTable("pword")
- allowedID = loadTable("whitelist")
- end
- function checkTable(table, chkv)
- for i=1, #table do
- if table[i] == chkv then
- return i
- elseif i == #table and table[i] ~= chkv then
- return "nomatch"
- end
- end
- end
- function loadTable(file)
- --Load table contents
- if fs.exists(file) then
- f = fs.open(file, "r")
- d = f.readAll()
- f.close()
- return(textutils.unserialise(d))
- else
- error("File not Found")
- end
- end
- function saveTable(file, table)
- --Save table contents
- f = fs.open(file, "w")
- f.write(textutils.serialise(table))
- f.close()
- end
- function filterResponse(filterID)
- rid = -1
- while rid ~= filterID do
- rid, msg = rednet.receive()
- end
- return msg
- end
- function checkWhitelist(compID)
- for i=1, #allowedID do
- if allowedID[i] == compID then
- return true
- elseif i == #allowedID and allowedID[i] ~= compID then
- return false
- end
- end
- end
- --Main Code
- init()
- while true do
- term.clear()
- term.setCursorPos(1,1)
- print("Choose an Option:")
- print("1: Start Server")
- print("2: Add User")
- print("3: Remove User")
- print("4: Add Whitelist ID")
- print("5: Revoke Whitelist ID")
- print("6: Exit Program")
- print("")
- write("Your Choice: ")
- x = read()
- --------------------------------------------------
- if tonumber(x) == 1 then
- --Server Code
- print("Server started...")
- while true do
- id, msg = rednet.receive()
- if checkWhitelist(id) then
- sleep(0.1)
- rednet.send(id, "ready")
- subUname = filterResponse(id)
- subPword = filterResponse(id)
- chkUname = checkTable(username,subUname)
- if password[chkUname] == subPword then
- rednet.send(id, "auth")
- else
- rednet.send(id, "nauth")
- end
- else
- rednet.send(id, 400)
- end
- end
- --------------------------------------------------
- elseif tonumber(x) == 2 then
- write("Username?: ")
- uname = read()
- print("")
- write("Password?: ")
- pword = read()
- print("")
- print("Creating user...")
- table.insert(username, uname)
- table.insert(password, pword)
- print("User created, saving tables...")
- saveTable("uname", username)
- saveTable("pword", password)
- print("Tables saved successfully!")
- elseif tonumber(x) == 3 then
- write("Username of user to delete?: ")
- uname = read()
- for i=1, #username do
- if username[i] == uname then
- table.remove(username, i)
- table.remove(password, i)
- print("User removed, saving tables...")
- saveTable("uname", username)
- saveTable("pword", password)
- print("Tables saved successfully!")
- elseif i == #username and uname ~= username[i] then
- print("Error: User not found.")
- end
- end
- --
- elseif tonumber(x) == 4 then
- write("Computer ID to whitelist?: ")
- compID = tonumber(read())
- table.insert(allowedID, compID)
- saveTable("whitelist", allowedID)
- print("Computer whitelisted successfully!")
- --
- elseif tonumber(x) == 5 then
- write("Computer ID to dewhitelist?: ")
- compID = tonumber(read())
- for i=1, #allowedID do
- if allowedID[i] == compID then
- table.remove(allowedID, i)
- saveTable("whitelist", allowedID)
- print("Computer dewhitelisted successfully!")
- elseif i == #allowedID and allowedID[i] ~= compID then
- print("Error: Computer ID not found.")
- end
- end
- --
- elseif tonumber(x) == 6 then
- print("Exiting...")
- error()
- end
- sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement