computercraft/packages/lns_server.lua

184 lines
4.7 KiB
Lua
Raw Permalink Normal View History

2023-10-08 04:57:23 +02:00
2023-10-08 08:18:41 +02:00
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")
2023-10-08 11:39:17 +02:00
settings.define("lns_server.auth_group", {
description = "Authentication group",
type = "string",
default = "admin"
})
local auth_group = settings.get("lns_server.auth_group")
2023-10-08 08:18:41 +02:00
settings.define("lns_server.auth_server", {
description = "Authentication server",
type = "string",
default = "auth.box"
})
2023-10-08 08:28:59 +02:00
auth_server = settings.get("lns_server.auth_server")
2023-10-08 08:18:41 +02:00
2023-10-08 08:36:02 +02:00
settings.define("lns_server.log_level", {
description = "Log Level",
type = "string",
default = "info"
})
log_level = settings.get("lns_server.log_level")
2023-10-08 08:18:41 +02:00
rednet.host("lns", settings.get("lns_server.rednet_hostname"))
2023-10-08 04:57:23 +02:00
rednet.open(modem_location)
local data = {}
2023-10-08 07:39:10 +02:00
function log(str)
io.write("[" .. os.time() .. "] " .. str .. "\n")
end
2023-10-08 08:36:02 +02:00
function log_debug(str)
if log_level == "debug" then
log(str)
end
end
2023-10-08 04:57:23 +02:00
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
2023-10-08 11:43:24 +02:00
function check_user_in_group(username, group)
2023-10-08 11:44:23 +02:00
local package = {
2023-10-08 11:43:24 +02:00
["action"] = "check_group",
["username"] = username,
["group"] = group
}
local authID = data[auth_server]
2023-10-08 11:44:23 +02:00
rednet.send(authID, package, "auth")
2023-10-08 11:43:24 +02:00
while true do
id, msg = rednet.receive("auth")
if id == authID then
if msg == "invalid request" then
io.write("Invalid request\n")
return false
elseif msg == true then
return true
else
return false
end
end
end
end
2023-10-08 04:57:23 +02:00
load_data()
while true do
2023-10-08 08:44:20 +02:00
client_id, msg = rednet.receive("lns")
2023-10-08 05:40:52 +02:00
request = msg
2023-10-08 04:57:23 +02:00
2023-10-08 05:36:37 +02:00
if request.action == nil or request.hostname == nil then
2023-10-08 08:44:20 +02:00
rednet.send(client_id, "invalid request", "lns")
2023-10-08 05:36:37 +02:00
end
2023-10-08 04:57:23 +02:00
2023-10-08 05:36:37 +02:00
if request.action == "lookup" then
2023-10-08 04:57:23 +02:00
-- check if hostname in data
2023-10-08 05:36:37 +02:00
if request.hostname == nil then
2023-10-08 08:44:20 +02:00
rednet.send(client_id, nil, "lns")
2023-10-08 07:39:10 +02:00
log("Sent nil to " .. client_id)
2023-10-08 04:57:23 +02:00
else
2023-10-08 08:44:20 +02:00
rednet.send(client_id, data[request.hostname], "lns")
2023-10-08 07:39:10 +02:00
log("Sent " .. request.hostname .. " to " .. client_id)
2023-10-08 04:57:23 +02:00
end
end
2023-10-08 05:36:37 +02:00
if request.action == "reload" then
2023-10-08 04:57:23 +02:00
load_data()
2023-10-08 08:44:20 +02:00
rednet.send(client_id, "ok", "lns")
2023-10-08 07:39:10 +02:00
log("Reloaded data")
2023-10-08 04:57:23 +02:00
end
2023-10-08 05:36:37 +02:00
if request.action == "register" then
2023-10-08 08:18:41 +02:00
local auth_passed = false
if require_auth then
local authID = data[auth_server]
2023-10-08 08:48:03 +02:00
if request.token == nil then
rednet.send(client_id, "invalid auth", "lns")
elseif authID ~= nil then
2023-10-08 08:18:41 +02:00
rednet.send(authID, {
2023-10-08 08:51:25 +02:00
["action"] = "token",
["token"] = request.token
2023-10-08 08:44:20 +02:00
}, "auth")
2023-10-08 08:18:41 +02:00
2023-10-08 09:24:17 +02:00
local responseServerID = nil
while responseServerID ~= authID do
responseServerID, auth_response = rednet.receive("auth")
end
2023-10-08 08:18:41 +02:00
local errorPos, errorEnd = string.find(auth_response, "invalid")
if errorPos then
log("Error: " .. auth_response)
else
2023-10-08 11:43:24 +02:00
if check_user_in_group(auth_response, auth_group) then
2023-10-08 11:39:17 +02:00
auth_passed = true
else
rednet.send(client_id, "invalid auth", "lns")
end
2023-10-08 08:18:41 +02:00
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()
2023-10-08 08:44:20 +02:00
rednet.send(client_id, "ok", "lns")
2023-10-08 08:18:41 +02:00
else
2023-10-08 08:44:20 +02:00
rednet.send(client_id, "auth required", "lns")
2023-10-08 08:18:41 +02:00
end
2023-10-08 04:57:23 +02:00
end
end