Added me endpoint

This commit is contained in:
Filip Znachor 2020-09-01 22:15:27 +02:00
parent 8852b3eb99
commit b3ac58ef29

30
nds.lua
View file

@ -1,7 +1,7 @@
function handle_request(env)
uhttpd.send("Status: 200 OK\r\n")
uhttpd.send("Content-Type: application/json\r\n\r\n")
-- local t = os.execute("ndsctl json")
local ip = env.REMOTE_HOST
local url = env.headers.URL
local prefix = "/nds/"
if url == prefix.."clients" then
@ -9,6 +9,9 @@ function handle_request(env)
resp = resp:gsub("} {", ",")
print(resp)
end
if url == prefix.."me" then
url = prefix.."client/"..ip
end
if string.starts(url, prefix.."client/") then
local id = string.sub(url,string.len(prefix)+8)
if checkipv4(id) or checkmac(id) or checktoken(id) then
@ -64,4 +67,29 @@ function os.capture(cmd, raw)
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
function table_to_string(tbl)
local result = "{"
for k, v in pairs(tbl) do
-- Check the key type (ignore any numerical keys - assume its an array)
if type(k) == "string" then
result = result.."[\""..k.."\"]".."="
end
-- Check the value type
if type(v) == "table" then
result = result..table_to_string(v)
elseif type(v) == "boolean" then
result = result..tostring(v)
else
result = result.."\""..v.."\""
end
result = result..","
end
-- Remove leading commas from the result
if result ~= "{" then
result = result:sub(1, result:len()-1)
end
return result.."}"
end