iteration
This commit is contained in:
parent
22743c248c
commit
a58ca7b94c
12
auth.lua
12
auth.lua
@ -28,10 +28,10 @@ function lns_lookup(hostname)
|
|||||||
}
|
}
|
||||||
|
|
||||||
lns_server_id = rednet.lookup("lns", lns_server)
|
lns_server_id = rednet.lookup("lns", lns_server)
|
||||||
rednet.send(lns_server_id, data)
|
rednet.send(lns_server_id, data, "lns")
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
id, msg = rednet.receive()
|
id, msg = rednet.receive("lns")
|
||||||
if id == lns_server_id then
|
if id == lns_server_id then
|
||||||
if msg == nil then
|
if msg == nil then
|
||||||
return nil
|
return nil
|
||||||
@ -57,10 +57,10 @@ if action == "login" then
|
|||||||
["password"] = password
|
["password"] = password
|
||||||
}
|
}
|
||||||
|
|
||||||
rednet.send(auth_server_id, data)
|
rednet.send(auth_server_id, data, "auth")
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
id, msg = rednet.receive()
|
id, msg = rednet.receive("auth")
|
||||||
if id == auth_server_id then
|
if id == auth_server_id then
|
||||||
if msg == "invalid request" then
|
if msg == "invalid request" then
|
||||||
io.write("Invalid request\n")
|
io.write("Invalid request\n")
|
||||||
@ -103,10 +103,10 @@ if action == "register" then
|
|||||||
["password"] = password
|
["password"] = password
|
||||||
}
|
}
|
||||||
|
|
||||||
rednet.send(auth_server_id, data)
|
rednet.send(auth_server_id, data, "auth")
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
id, msg = rednet.receive()
|
id, msg = rednet.receive("auth")
|
||||||
if id == auth_server_id then
|
if id == auth_server_id then
|
||||||
if msg == "invalid request" then
|
if msg == "invalid request" then
|
||||||
io.write("Invalid request\n")
|
io.write("Invalid request\n")
|
||||||
|
@ -57,20 +57,20 @@ load_data()
|
|||||||
math.randomseed(os.time())
|
math.randomseed(os.time())
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
client_id, msg = rednet.receive()
|
client_id, msg = rednet.receive("auth")
|
||||||
request = msg
|
request = msg
|
||||||
|
|
||||||
if request.action == nil then
|
if request.action == nil then
|
||||||
rednet.send(client_id, "invalid request")
|
rednet.send(client_id, "invalid request", "auth")
|
||||||
end
|
end
|
||||||
|
|
||||||
if request.action == "register" then
|
if request.action == "register" then
|
||||||
if request.username == nil or request.password == nil then
|
if request.username == nil or request.password == nil then
|
||||||
rednet.send(client_id, "invalid request")
|
rednet.send(client_id, "invalid request", "auth")
|
||||||
end
|
end
|
||||||
|
|
||||||
if data.users[request.username] ~= nil then
|
if data.users[request.username] ~= nil then
|
||||||
rednet.send(client_id, "user already exists")
|
rednet.send(client_id, "user already exists", "auth")
|
||||||
end
|
end
|
||||||
|
|
||||||
data.users[request.username] = {
|
data.users[request.username] = {
|
||||||
@ -79,16 +79,16 @@ while true do
|
|||||||
|
|
||||||
save_data()
|
save_data()
|
||||||
|
|
||||||
rednet.send(client_id, "ok")
|
rednet.send(client_id, "ok", "auth")
|
||||||
log(request.username .. " registered")
|
log(request.username .. " registered")
|
||||||
end
|
end
|
||||||
|
|
||||||
if request.action == "login" then
|
if request.action == "login" then
|
||||||
if request.username == nil or request.password == nil 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
|
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")
|
log(request.username .. " failed log in attempt")
|
||||||
elseif convert_password(request.password) == data.users[request.username].password then
|
elseif convert_password(request.password) == data.users[request.username].password then
|
||||||
@ -98,29 +98,29 @@ while true do
|
|||||||
|
|
||||||
log(request.username .. " logged in")
|
log(request.username .. " logged in")
|
||||||
else
|
else
|
||||||
rednet.send(client_id, "invalid password")
|
rednet.send(client_id, "invalid password", "auth")
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if request.action == "token" then
|
if request.action == "token" then
|
||||||
if request.token == nil then
|
if request.token == nil then
|
||||||
rednet.send(client_id, "invalid request")
|
rednet.send(client_id, "invalid request", "auth")
|
||||||
else
|
else
|
||||||
|
|
||||||
for user, userdata in pairs(data.users) do
|
for user, userdata in pairs(data.users) do
|
||||||
if userdata.token == request.token then
|
if userdata.token == request.token then
|
||||||
rednet.send(client_id, user)
|
rednet.send(client_id, user, "auth")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
rednet.send(client_id, "invalid token")
|
rednet.send(client_id, "invalid token", "auth")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if request.action == "profile" then
|
if request.action == "profile" then
|
||||||
if request.username == nil then
|
if request.username == nil then
|
||||||
rednet.send(client_id, "invalid request")
|
rednet.send(client_id, "invalid request", "auth")
|
||||||
end
|
end
|
||||||
|
|
||||||
local profile = {}
|
local profile = {}
|
||||||
@ -133,12 +133,12 @@ while true do
|
|||||||
|
|
||||||
profile.username = request.username
|
profile.username = request.username
|
||||||
|
|
||||||
rednet.send(client_id, data.users[request.username])
|
rednet.send(client_id, data.users[request.username], "auth")
|
||||||
end
|
end
|
||||||
|
|
||||||
if request.action == "update_profile" then
|
if request.action == "update_profile" then
|
||||||
if request.username == nil or request.token == nil then
|
if request.username == nil or request.token == nil then
|
||||||
rednet.send(client_id, "invalid request")
|
rednet.send(client_id, "invalid request", "auth")
|
||||||
end
|
end
|
||||||
|
|
||||||
if data.users[request.username].token == request.token then
|
if data.users[request.username].token == request.token then
|
||||||
@ -149,9 +149,9 @@ while true do
|
|||||||
save_data()
|
save_data()
|
||||||
|
|
||||||
rednet.send(client_id, "ok")
|
rednet.send(client_id, "ok")
|
||||||
log(request.username .. " updated their profile")
|
log(request.username .. " updated their profile", "auth")
|
||||||
else
|
else
|
||||||
rednet.send(client_id, "invalid token")
|
rednet.send(client_id, "invalid token", "auth")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
4
lns.lua
4
lns.lua
@ -48,10 +48,10 @@ function lns_lookup(remote_hostname)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
rednet.send(lns_server_id, data)
|
rednet.send(lns_server_id, data, "lns")
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
id, msg = rednet.receive()
|
id, msg = rednet.receive("lns")
|
||||||
if id == lns_server_id then
|
if id == lns_server_id then
|
||||||
if msg == nil then
|
if msg == nil then
|
||||||
return nil
|
return nil
|
||||||
|
@ -79,28 +79,28 @@ function split (inputstr, sep)
|
|||||||
load_data()
|
load_data()
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
client_id, msg = rednet.receive()
|
client_id, msg = rednet.receive("lns")
|
||||||
request = msg
|
request = msg
|
||||||
|
|
||||||
if request.action == nil or request.hostname == nil then
|
if request.action == nil or request.hostname == nil then
|
||||||
rednet.send(client_id, "invalid request")
|
rednet.send(client_id, "invalid request", "lns")
|
||||||
end
|
end
|
||||||
|
|
||||||
if request.action == "lookup" then
|
if request.action == "lookup" then
|
||||||
|
|
||||||
-- check if hostname in data
|
-- check if hostname in data
|
||||||
if request.hostname == nil then
|
if request.hostname == nil then
|
||||||
rednet.send(client_id, nil)
|
rednet.send(client_id, nil, "lns")
|
||||||
log("Sent nil to " .. client_id)
|
log("Sent nil to " .. client_id)
|
||||||
else
|
else
|
||||||
rednet.send(client_id, data[request.hostname])
|
rednet.send(client_id, data[request.hostname], "lns")
|
||||||
log("Sent " .. request.hostname .. " to " .. client_id)
|
log("Sent " .. request.hostname .. " to " .. client_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if request.action == "reload" then
|
if request.action == "reload" then
|
||||||
load_data()
|
load_data()
|
||||||
rednet.send(client_id, "ok")
|
rednet.send(client_id, "ok", "lns")
|
||||||
log("Reloaded data")
|
log("Reloaded data")
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -113,9 +113,9 @@ while true do
|
|||||||
rednet.send(authID, {
|
rednet.send(authID, {
|
||||||
action = "token",
|
action = "token",
|
||||||
token = request.token
|
token = request.token
|
||||||
})
|
}, "auth")
|
||||||
|
|
||||||
local auth_response = rednet.receive()
|
local auth_response = rednet.receive("auth")
|
||||||
log_debug("Auth response: " .. auth_response)
|
log_debug("Auth response: " .. auth_response)
|
||||||
local errorPos, errorEnd = string.find(auth_response, "invalid")
|
local errorPos, errorEnd = string.find(auth_response, "invalid")
|
||||||
if errorPos then
|
if errorPos then
|
||||||
@ -134,9 +134,9 @@ while true do
|
|||||||
data[request.hostname] = client_id
|
data[request.hostname] = client_id
|
||||||
log("Registered " .. request.hostname .. " as " .. client_id)
|
log("Registered " .. request.hostname .. " as " .. client_id)
|
||||||
save_data()
|
save_data()
|
||||||
rednet.send(client_id, "ok")
|
rednet.send(client_id, "ok", "lns")
|
||||||
else
|
else
|
||||||
rednet.send(client_id, "auth required")
|
rednet.send(client_id, "auth required", "lns")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
@ -25,10 +25,10 @@ data = {
|
|||||||
["token"] = settings.get("auth.token", nil)
|
["token"] = settings.get("auth.token", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
rednet.send(receiverID, data)
|
rednet.send(receiverID, data, "lns")
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
id, msg = rednet.receive()
|
id, msg = rednet.receive("lns")
|
||||||
if id == receiverID then
|
if id == receiverID then
|
||||||
if msg == "ok" then
|
if msg == "ok" then
|
||||||
print("Registered with LNS Server Successfully!")
|
print("Registered with LNS Server Successfully!")
|
||||||
|
Loading…
Reference in New Issue
Block a user