iteration
This commit is contained in:
130
packages/auth.lua
Normal file
130
packages/auth.lua
Normal file
@ -0,0 +1,130 @@
|
||||
args = { ... }
|
||||
|
||||
if args[1] == nil then
|
||||
io.write("Usage: auth <action>\n")
|
||||
return
|
||||
end
|
||||
|
||||
local action = args[1]
|
||||
|
||||
settings.define("auth.token", {
|
||||
description = "Authentication token",
|
||||
type = "number",
|
||||
default = -1
|
||||
})
|
||||
|
||||
settings.define("auth.server", {
|
||||
description = "Authentication server",
|
||||
type = "string",
|
||||
default = "auth.box"
|
||||
})
|
||||
|
||||
local server = settings.get("auth.server")
|
||||
|
||||
function lns_lookup(hostname)
|
||||
local data = {
|
||||
["action"] = "lookup",
|
||||
["hostname"] = hostname
|
||||
}
|
||||
|
||||
lns_server_id = rednet.lookup("lns", lns_server)
|
||||
rednet.send(lns_server_id, data, "lns")
|
||||
|
||||
while true do
|
||||
id, msg = rednet.receive("lns")
|
||||
if id == lns_server_id then
|
||||
if msg == nil then
|
||||
return nil
|
||||
else
|
||||
return msg
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
auth_server_id = lns_lookup(server)
|
||||
|
||||
if action == "login" then
|
||||
io.write("Username: ")
|
||||
local username = io.read()
|
||||
io.write("Password: ")
|
||||
local password = read("*")
|
||||
|
||||
local data = {
|
||||
["action"] = "login",
|
||||
["username"] = username,
|
||||
["password"] = password
|
||||
}
|
||||
|
||||
rednet.send(auth_server_id, data, "auth")
|
||||
|
||||
while true do
|
||||
id, msg = rednet.receive("auth")
|
||||
if id == auth_server_id then
|
||||
if msg == "invalid request" then
|
||||
io.write("Invalid request\n")
|
||||
return
|
||||
elseif msg == "user not found" then
|
||||
io.write("User not found\n")
|
||||
return
|
||||
elseif msg == "invalid password" then
|
||||
io.write("Invalid password\n")
|
||||
return
|
||||
else
|
||||
token = msg
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
settings.set("auth.token", token)
|
||||
settings.save()
|
||||
io.write("Logged in as " .. username .. "\n")
|
||||
return
|
||||
end
|
||||
|
||||
if action == "logout" then
|
||||
settings.set("auth.token", -1)
|
||||
settings.save()
|
||||
io.write("Logged out\n")
|
||||
return
|
||||
end
|
||||
|
||||
if action == "register" then
|
||||
io.write("Username: ")
|
||||
local username = io.read()
|
||||
io.write("Password: ")
|
||||
local password = read("*")
|
||||
io.write("Confirm Password: ")
|
||||
local confirm_password = read("*")
|
||||
|
||||
if password ~= confirm_password then
|
||||
io.write("Passwords do not match\n")
|
||||
return
|
||||
end
|
||||
|
||||
local data = {
|
||||
["action"] = "register",
|
||||
["username"] = username,
|
||||
["password"] = password
|
||||
}
|
||||
|
||||
rednet.send(auth_server_id, data, "auth")
|
||||
|
||||
while true do
|
||||
id, msg = rednet.receive("auth")
|
||||
if id == auth_server_id then
|
||||
if msg == "invalid request" then
|
||||
io.write("Invalid request\n")
|
||||
return
|
||||
elseif msg == "user already exists" then
|
||||
io.write("User already exists\n")
|
||||
return
|
||||
else
|
||||
io.write("Registered user " .. username .. "\n")
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
161
packages/auth_server.lua
Normal file
161
packages/auth_server.lua
Normal file
@ -0,0 +1,161 @@
|
||||
|
||||
|
||||
data = {
|
||||
users = {
|
||||
["admin"] = {
|
||||
password = "admin"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function save_data()
|
||||
db = fs.open("auth.db", "w")
|
||||
db.write(textutils.serialize(data))
|
||||
db.close()
|
||||
end
|
||||
|
||||
function load_data()
|
||||
if fs.exists("auth.db") then
|
||||
db = fs.open("auth.db", "r")
|
||||
db_contents = db.readAll()
|
||||
db.close()
|
||||
|
||||
data = textutils.unserialize(db_contents)
|
||||
end
|
||||
end
|
||||
|
||||
function generate_token(user)
|
||||
local token = hash(user .. os.time() .. math.random())
|
||||
data.users[user].token = token
|
||||
|
||||
return token
|
||||
end
|
||||
|
||||
function convert_password(password)
|
||||
return hash(password .. "3m&LmNm7")
|
||||
end
|
||||
|
||||
-- Does a complex hash of a string to make it harder to guess
|
||||
function hash(str)
|
||||
local hash = 0
|
||||
local len = string.len(str)
|
||||
local byte = 0
|
||||
|
||||
for i = 1, len do
|
||||
byte = string.byte(str, i)
|
||||
hash = bit32.band(hash * 31 + byte, 0xFFFFFFFF)
|
||||
end
|
||||
|
||||
return hash
|
||||
end
|
||||
|
||||
function log(str)
|
||||
io.write("[" .. os.time() .. "] " .. str .. "\n")
|
||||
end
|
||||
|
||||
load_data()
|
||||
math.randomseed(os.time())
|
||||
|
||||
while true do
|
||||
client_id, msg = rednet.receive("auth")
|
||||
request = msg
|
||||
|
||||
if request.action == nil then
|
||||
rednet.send(client_id, "invalid request", "auth")
|
||||
end
|
||||
|
||||
if request.action == "register" then
|
||||
if request.username == nil or request.password == nil then
|
||||
rednet.send(client_id, "invalid request", "auth")
|
||||
end
|
||||
|
||||
if data.users[request.username] ~= nil then
|
||||
rednet.send(client_id, "user already exists", "auth")
|
||||
end
|
||||
|
||||
data.users[request.username] = {
|
||||
password = convert_password(request.password)
|
||||
}
|
||||
|
||||
save_data()
|
||||
|
||||
rednet.send(client_id, "ok", "auth")
|
||||
log(request.username .. " registered")
|
||||
end
|
||||
|
||||
if request.action == "login" then
|
||||
if request.username == nil or request.password == nil then
|
||||
rednet.send(client_id, "invalid request", "auth")
|
||||
|
||||
elseif data.users[request.username] == nil then
|
||||
rednet.send(client_id, "user not found", "auth")
|
||||
|
||||
log(request.username .. " failed log in attempt")
|
||||
elseif convert_password(request.password) == data.users[request.username].password then
|
||||
local token = generate_token(request.username)
|
||||
|
||||
rednet.send(client_id, token, "auth")
|
||||
|
||||
log(request.username .. " logged in")
|
||||
else
|
||||
rednet.send(client_id, "invalid password", "auth")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
if request.action == "token" then
|
||||
if request.token == nil or request.token == "" or request.token == -1 then
|
||||
rednet.send(client_id, "invalid request", "auth")
|
||||
else
|
||||
|
||||
local found = false
|
||||
for user, userdata in pairs(data.users) do
|
||||
if userdata.token == request.token then
|
||||
rednet.send(client_id, user, "auth")
|
||||
found = true
|
||||
end
|
||||
end
|
||||
|
||||
if not found then
|
||||
rednet.send(client_id, "invalid token", "auth")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if request.action == "profile" then
|
||||
if request.username == nil then
|
||||
rednet.send(client_id, "invalid request", "auth")
|
||||
end
|
||||
|
||||
local profile = {}
|
||||
|
||||
if data.users[request.username].profile_data ~= nil then
|
||||
profile = data.users[request.username].profile_data
|
||||
else
|
||||
profile.display_name = request.username
|
||||
end
|
||||
|
||||
profile.username = request.username
|
||||
|
||||
rednet.send(client_id, data.users[request.username], "auth")
|
||||
end
|
||||
|
||||
if request.action == "update_profile" then
|
||||
if request.username == nil or request.token == nil then
|
||||
rednet.send(client_id, "invalid request", "auth")
|
||||
end
|
||||
|
||||
if data.users[request.username].token == request.token then
|
||||
if request.profile_data ~= nil then
|
||||
data.users[request.username].profile_data = request.profile_data
|
||||
end
|
||||
|
||||
save_data()
|
||||
|
||||
rednet.send(client_id, "ok")
|
||||
log(request.username .. " updated their profile", "auth")
|
||||
else
|
||||
rednet.send(client_id, "invalid token", "auth")
|
||||
end
|
||||
end
|
||||
end
|
1
packages/clown.lua
Normal file
1
packages/clown.lua
Normal file
@ -0,0 +1 @@
|
||||
io.write("Its ya boi, clown.lua")
|
8
packages/dig.lua
Normal file
8
packages/dig.lua
Normal file
@ -0,0 +1,8 @@
|
||||
local args = { ... }
|
||||
|
||||
if args[1] == nil then
|
||||
print("Usage: dig <hostname>")
|
||||
return
|
||||
end
|
||||
|
||||
os.write(lns.lookup(args[1]) .. "\n")
|
147
packages/lns_server.lua
Normal file
147
packages/lns_server.lua
Normal file
@ -0,0 +1,147 @@
|
||||
|
||||
settings.define("lns_server.modem_location", {
|
||||
description = "Modem location",
|
||||
type = "string",
|
||||
default = "right"
|
||||
})
|
||||
modem_location = settings.get("lns_server.modem_location")
|
||||
|
||||
settings.define("lns_server.rednet_hostname", {
|
||||
description = "Rednet hostname",
|
||||
type = "string",
|
||||
default = "test"
|
||||
})
|
||||
|
||||
settings.define("lns_server.require_auth", {
|
||||
description = "Require authentication",
|
||||
type = "boolean",
|
||||
default = false
|
||||
})
|
||||
require_auth = settings.get("lns_server.require_auth")
|
||||
|
||||
settings.define("lns_server.auth_server", {
|
||||
description = "Authentication server",
|
||||
type = "string",
|
||||
default = "auth.box"
|
||||
})
|
||||
auth_server = settings.get("lns_server.auth_server")
|
||||
|
||||
settings.define("lns_server.log_level", {
|
||||
description = "Log Level",
|
||||
type = "string",
|
||||
default = "info"
|
||||
})
|
||||
log_level = settings.get("lns_server.log_level")
|
||||
|
||||
rednet.host("lns", settings.get("lns_server.rednet_hostname"))
|
||||
rednet.open(modem_location)
|
||||
|
||||
local data = {}
|
||||
|
||||
|
||||
function log(str)
|
||||
io.write("[" .. os.time() .. "] " .. str .. "\n")
|
||||
end
|
||||
|
||||
function log_debug(str)
|
||||
if log_level == "debug" then
|
||||
log(str)
|
||||
end
|
||||
end
|
||||
|
||||
function save_data()
|
||||
db = fs.open("lns.db", "w")
|
||||
db.write(textutils.serialize(data))
|
||||
db.close()
|
||||
end
|
||||
|
||||
function load_data()
|
||||
if fs.exists("lns.db") then
|
||||
db = fs.open("lns.db", "r")
|
||||
db_contents = db.readAll()
|
||||
db.close()
|
||||
|
||||
data = textutils.unserialize(db_contents)
|
||||
end
|
||||
end
|
||||
|
||||
function split (inputstr, sep)
|
||||
if sep == nil then
|
||||
sep = "%s"
|
||||
end
|
||||
local t={}
|
||||
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
|
||||
table.insert(t, str)
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
load_data()
|
||||
|
||||
while true do
|
||||
client_id, msg = rednet.receive("lns")
|
||||
request = msg
|
||||
|
||||
if request.action == nil or request.hostname == nil then
|
||||
rednet.send(client_id, "invalid request", "lns")
|
||||
end
|
||||
|
||||
if request.action == "lookup" then
|
||||
|
||||
-- check if hostname in data
|
||||
if request.hostname == nil then
|
||||
rednet.send(client_id, nil, "lns")
|
||||
log("Sent nil to " .. client_id)
|
||||
else
|
||||
rednet.send(client_id, data[request.hostname], "lns")
|
||||
log("Sent " .. request.hostname .. " to " .. client_id)
|
||||
end
|
||||
end
|
||||
|
||||
if request.action == "reload" then
|
||||
load_data()
|
||||
rednet.send(client_id, "ok", "lns")
|
||||
log("Reloaded data")
|
||||
end
|
||||
|
||||
if request.action == "register" then
|
||||
|
||||
local auth_passed = false
|
||||
if require_auth then
|
||||
local authID = data[auth_server]
|
||||
if request.token == nil then
|
||||
rednet.send(client_id, "invalid auth", "lns")
|
||||
elseif authID ~= nil then
|
||||
rednet.send(authID, {
|
||||
["action"] = "token",
|
||||
["token"] = request.token
|
||||
}, "auth")
|
||||
|
||||
local responseServerID = nil
|
||||
while responseServerID ~= authID do
|
||||
responseServerID, auth_response = rednet.receive("auth")
|
||||
end
|
||||
|
||||
local errorPos, errorEnd = string.find(auth_response, "invalid")
|
||||
if errorPos then
|
||||
log("Error: " .. auth_response)
|
||||
else
|
||||
auth_passed = true
|
||||
end
|
||||
else
|
||||
log("Error: Auth server not found to " .. client_id)
|
||||
end
|
||||
else
|
||||
auth_passed = true
|
||||
end
|
||||
|
||||
if auth_passed then
|
||||
data[request.hostname] = client_id
|
||||
log("Registered " .. request.hostname .. " as " .. client_id)
|
||||
save_data()
|
||||
rednet.send(client_id, "ok", "lns")
|
||||
else
|
||||
rednet.send(client_id, "auth required", "lns")
|
||||
end
|
||||
end
|
||||
end
|
38
packages/set_lns.lua
Normal file
38
packages/set_lns.lua
Normal file
@ -0,0 +1,38 @@
|
||||
|
||||
io.write("Enter the hostname: ")
|
||||
hostname = io.read()
|
||||
|
||||
io.write("Enter the modem location: ")
|
||||
modem_location = io.read()
|
||||
|
||||
rednet.open(modem_location)
|
||||
receiverID = rednet.lookup("lns", lns_server)
|
||||
current_time = os.time()
|
||||
|
||||
|
||||
data = {
|
||||
["action"] = "register",
|
||||
["hostname"] = hostname,
|
||||
["token"] = settings.get("auth.token", nil)
|
||||
}
|
||||
|
||||
rednet.send(receiverID, data, "lns")
|
||||
|
||||
while true do
|
||||
id, msg = rednet.receive("lns")
|
||||
if id == receiverID then
|
||||
if msg == "ok" then
|
||||
print("Registered with LNS Server Successfully!")
|
||||
break
|
||||
else
|
||||
print("Failed to register with LNS Server! Error: " .. msg)
|
||||
break
|
||||
end
|
||||
|
||||
-- timeout after 5 seconds
|
||||
if os.time() - current_time > 5 then
|
||||
print("Failed to register with LNS Server! Timeout!")
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user