function handle_request(env) uhttpd.send("Status: 200 OK\r\n") uhttpd.send("Content-Type: application/json\r\n\r\n") local ip = env.REMOTE_HOST 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 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 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 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