diff --git a/nds.lua b/nds.lua new file mode 100644 index 0000000..36a510d --- /dev/null +++ b/nds.lua @@ -0,0 +1,67 @@ +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 url = env.headers.URL + local prefix = "/nds/" + if url == prefix.."clients" then + local resp = os.capture("ndsctl json") + resp = resp:gsub("} {", ",") + print(resp) + 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 + os.execute("ndsctl json "..id) + end + end +end + +function checkipv4(ip) + local chunks = {ip:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$")} + if (#chunks == 4) then + for _,v in pairs(chunks) do + if (tonumber(v) < 0 or tonumber(v) > 255) then + return true + end + end + return true + else + return false + end +end + +function checkmac(str) + local s = str + if not s:find("^%w+:%w+:%w+:%w+:%w+:%w+$") then + return false + end + for substr in s:gmatch("(%w+)") do + if not substr:find("^[0-9A-Fa-f][0-9A-Fa-f]$") then + return false + end + end + return true +end + +function checktoken(t) + if not t:find("^[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]$") then + return false + end + return true +end + +function string.starts(String,Start) + return string.sub(String,1,string.len(Start))==Start +end + +function os.capture(cmd, raw) + local f = assert(io.popen(cmd, 'r')) + local s = assert(f:read('*a')) + f:close() + if raw then return s end + s = string.gsub(s, '^%s+', '') + s = string.gsub(s, '%s+$', '') + s = string.gsub(s, '[\n\r]+', ' ') + return s +end \ No newline at end of file