From 4767b0ea0170906571274a06853e5d916d9ac869 Mon Sep 17 00:00:00 2001 From: Layla Manley Date: Sun, 8 Oct 2023 01:09:44 -0400 Subject: [PATCH] iteration --- auth_server.lua | 31 ++++++++++++++-------- lns.lua | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 lns.lua diff --git a/auth_server.lua b/auth_server.lua index f4539a8..27f7ab1 100644 --- a/auth_server.lua +++ b/auth_server.lua @@ -49,6 +49,10 @@ function hash(str) return hash end +function log(str) + io.write("[" .. os.time() .. "] " .. str .. "\n") +end + load_data() math.randomseed(os.time()) @@ -76,21 +80,23 @@ while true do save_data() rednet.send(client_id, "ok") + 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") - end - - if data.users[request.username] == nil then + + else if data.users[request.username] == nil then rednet.send(client_id, "user not found") - end - if convert_password(request.password) == data.users[request.username].password then + log(request.username .. " failed log in attempt") + else if convert_password(request.password) == data.users[request.username].password then local token = generate_token(request.username) rednet.send(client_id, token) + + log(request.username .. " logged in") else rednet.send(client_id, "invalid password") end @@ -100,15 +106,17 @@ while true do if request.action == "token" then if request.token == nil then rednet.send(client_id, "invalid request") - end + else - for user, userdata in pairs(data.users) do - if userdata.token == request.token then - rednet.send(client_id, user) + for user, userdata in pairs(data.users) do + if userdata.token == request.token then + rednet.send(client_id, user) + end end - end - rednet.send(client_id, "invalid token") + rednet.send(client_id, "invalid token") + log(request.username .. " failed token check") + end end if request.action == "profile" then @@ -142,6 +150,7 @@ while true do save_data() rednet.send(client_id, "ok") + log(request.username .. " updated their profile") else rednet.send(client_id, "invalid token") end diff --git a/lns.lua b/lns.lua new file mode 100644 index 0000000..9b2600d --- /dev/null +++ b/lns.lua @@ -0,0 +1,68 @@ +args = { ... } + +cache = {} + +allow_dynamic_lns_hosts = false + +function lns_lookup(hostname) + local data = { + ["action"] = "lookup", + ["hostname"] = hostname + } + + local lns_server_id = 7 + if allow_dynamic_lns_hosts then + lns_server_id = rednet.lookup("lns", lns_server) + if lns_server_id == nil then + io.write("LNS Server not found!\n") + return + end + end + + rednet.send(lns_server_id, data) + + while true do + id, msg = rednet.receive() + if id == lns_server_id then + if msg == nil then + return nil + else + return msg + end + end + end +end + +local action = args[1] +if action == nil then + io.write("Usage: lns \n") + return +end + +if action == "lookup" then + local hostname = args[2] + if hostname == nil then + io.write("Usage: lns lookup \n") + return + end + + if cache[hostname] ~= nil then + io.write(cache[hostname]) + return + end + + local id = lns_lookup(hostname) + cache[hostname] = id + if id ~= nil then + io.write(id) + end + +else if action == "clear" then + local data = { + ["action"] = "reload" + } + + lns_server_id = rednet.lookup("lns", lns_server) + rednet.send(lns_server_id, data) + print("Reloaded LNS Server") +end \ No newline at end of file