iteration

This commit is contained in:
Layla 2023-10-08 02:44:20 -04:00
parent 22743c248c
commit a58ca7b94c
5 changed files with 35 additions and 35 deletions

View File

@ -28,10 +28,10 @@ function lns_lookup(hostname)
}
lns_server_id = rednet.lookup("lns", lns_server)
rednet.send(lns_server_id, data)
rednet.send(lns_server_id, data, "lns")
while true do
id, msg = rednet.receive()
id, msg = rednet.receive("lns")
if id == lns_server_id then
if msg == nil then
return nil
@ -57,10 +57,10 @@ if action == "login" then
["password"] = password
}
rednet.send(auth_server_id, data)
rednet.send(auth_server_id, data, "auth")
while true do
id, msg = rednet.receive()
id, msg = rednet.receive("auth")
if id == auth_server_id then
if msg == "invalid request" then
io.write("Invalid request\n")
@ -103,10 +103,10 @@ if action == "register" then
["password"] = password
}
rednet.send(auth_server_id, data)
rednet.send(auth_server_id, data, "auth")
while true do
id, msg = rednet.receive()
id, msg = rednet.receive("auth")
if id == auth_server_id then
if msg == "invalid request" then
io.write("Invalid request\n")

View File

@ -57,20 +57,20 @@ load_data()
math.randomseed(os.time())
while true do
client_id, msg = rednet.receive()
client_id, msg = rednet.receive("auth")
request = msg
if request.action == nil then
rednet.send(client_id, "invalid request")
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")
rednet.send(client_id, "invalid request", "auth")
end
if data.users[request.username] ~= nil then
rednet.send(client_id, "user already exists")
rednet.send(client_id, "user already exists", "auth")
end
data.users[request.username] = {
@ -79,16 +79,16 @@ while true do
save_data()
rednet.send(client_id, "ok")
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")
rednet.send(client_id, "invalid request", "auth")
elseif data.users[request.username] == nil then
rednet.send(client_id, "user not found")
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
@ -98,29 +98,29 @@ while true do
log(request.username .. " logged in")
else
rednet.send(client_id, "invalid password")
rednet.send(client_id, "invalid password", "auth")
end
end
if request.action == "token" then
if request.token == nil then
rednet.send(client_id, "invalid request")
rednet.send(client_id, "invalid request", "auth")
else
for user, userdata in pairs(data.users) do
if userdata.token == request.token then
rednet.send(client_id, user)
rednet.send(client_id, user, "auth")
end
end
rednet.send(client_id, "invalid token")
rednet.send(client_id, "invalid token", "auth")
end
end
if request.action == "profile" then
if request.username == nil then
rednet.send(client_id, "invalid request")
rednet.send(client_id, "invalid request", "auth")
end
local profile = {}
@ -133,12 +133,12 @@ while true do
profile.username = request.username
rednet.send(client_id, data.users[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")
rednet.send(client_id, "invalid request", "auth")
end
if data.users[request.username].token == request.token then
@ -149,9 +149,9 @@ while true do
save_data()
rednet.send(client_id, "ok")
log(request.username .. " updated their profile")
log(request.username .. " updated their profile", "auth")
else
rednet.send(client_id, "invalid token")
rednet.send(client_id, "invalid token", "auth")
end
end
end

View File

@ -48,10 +48,10 @@ function lns_lookup(remote_hostname)
end
end
rednet.send(lns_server_id, data)
rednet.send(lns_server_id, data, "lns")
while true do
id, msg = rednet.receive()
id, msg = rednet.receive("lns")
if id == lns_server_id then
if msg == nil then
return nil

View File

@ -79,28 +79,28 @@ function split (inputstr, sep)
load_data()
while true do
client_id, msg = rednet.receive()
client_id, msg = rednet.receive("lns")
request = msg
if request.action == nil or request.hostname == nil then
rednet.send(client_id, "invalid request")
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)
rednet.send(client_id, nil, "lns")
log("Sent nil to " .. client_id)
else
rednet.send(client_id, data[request.hostname])
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")
rednet.send(client_id, "ok", "lns")
log("Reloaded data")
end
@ -113,9 +113,9 @@ while true do
rednet.send(authID, {
action = "token",
token = request.token
})
}, "auth")
local auth_response = rednet.receive()
local auth_response = rednet.receive("auth")
log_debug("Auth response: " .. auth_response)
local errorPos, errorEnd = string.find(auth_response, "invalid")
if errorPos then
@ -134,9 +134,9 @@ while true do
data[request.hostname] = client_id
log("Registered " .. request.hostname .. " as " .. client_id)
save_data()
rednet.send(client_id, "ok")
rednet.send(client_id, "ok", "lns")
else
rednet.send(client_id, "auth required")
rednet.send(client_id, "auth required", "lns")
end
end
end

View File

@ -25,10 +25,10 @@ data = {
["token"] = settings.get("auth.token", nil)
}
rednet.send(receiverID, data)
rednet.send(receiverID, data, "lns")
while true do
id, msg = rednet.receive()
id, msg = rednet.receive("lns")
if id == receiverID then
if msg == "ok" then
print("Registered with LNS Server Successfully!")