iteration
This commit is contained in:
@ -49,6 +49,10 @@ function hash(str)
|
|||||||
return hash
|
return hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function log(str)
|
||||||
|
io.write("[" .. os.time() .. "] " .. str .. "\n")
|
||||||
|
end
|
||||||
|
|
||||||
load_data()
|
load_data()
|
||||||
math.randomseed(os.time())
|
math.randomseed(os.time())
|
||||||
|
|
||||||
@ -76,21 +80,23 @@ while true do
|
|||||||
save_data()
|
save_data()
|
||||||
|
|
||||||
rednet.send(client_id, "ok")
|
rednet.send(client_id, "ok")
|
||||||
|
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")
|
||||||
end
|
|
||||||
|
else if data.users[request.username] == nil then
|
||||||
if data.users[request.username] == nil then
|
|
||||||
rednet.send(client_id, "user not found")
|
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)
|
local token = generate_token(request.username)
|
||||||
|
|
||||||
rednet.send(client_id, token)
|
rednet.send(client_id, token)
|
||||||
|
|
||||||
|
log(request.username .. " logged in")
|
||||||
else
|
else
|
||||||
rednet.send(client_id, "invalid password")
|
rednet.send(client_id, "invalid password")
|
||||||
end
|
end
|
||||||
@ -100,15 +106,17 @@ while true do
|
|||||||
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")
|
||||||
end
|
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)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
rednet.send(client_id, "invalid token")
|
rednet.send(client_id, "invalid token")
|
||||||
|
log(request.username .. " failed token check")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if request.action == "profile" then
|
if request.action == "profile" then
|
||||||
@ -142,6 +150,7 @@ while true do
|
|||||||
save_data()
|
save_data()
|
||||||
|
|
||||||
rednet.send(client_id, "ok")
|
rednet.send(client_id, "ok")
|
||||||
|
log(request.username .. " updated their profile")
|
||||||
else
|
else
|
||||||
rednet.send(client_id, "invalid token")
|
rednet.send(client_id, "invalid token")
|
||||||
end
|
end
|
||||||
|
68
lns.lua
Normal file
68
lns.lua
Normal file
@ -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 <action>\n")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if action == "lookup" then
|
||||||
|
local hostname = args[2]
|
||||||
|
if hostname == nil then
|
||||||
|
io.write("Usage: lns lookup <hostname>\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
|
Reference in New Issue
Block a user