File

mod_http_status/mod_http_status.lua @ 6299:611380cfad75

mod_csi_grace_period: Readme Fix Tab at beginning of line diff --git a/mod_csi_grace_period/README.md b/mod_csi_grace_period/README.md --- a/mod_csi_grace_period/README.md +++ b/mod_csi_grace_period/README.md @@ -16,9 +16,9 @@ pocket is not the best use of radio time Works with [mod_csi_simple][doc:modules:mod_csi_simple] which is included with Prosody. - ------- ------- - trunk* Works - 13 Works - 0.12 Works + ------- ------- + trunk* Works + 13 Works + 0.12 Works - *as of 2025-06-13 + *as of 2025-06-13
author Menel <menel@snikket.de>
date Fri, 13 Jun 2025 10:08:21 +0200
parent 5702:e274431bf4ce
line wrap: on
line source

module:set_global();

local json = require "util.json";
local datetime = require "util.datetime".datetime;
local ip = require "util.ip";

local modulemanager = require "core.modulemanager";

local permitted_ips = module:get_option_set("http_status_allow_ips", { "::1", "127.0.0.1" });
local permitted_cidr = module:get_option_string("http_status_allow_cidr");

local function is_permitted(request)
	local ip_raw = request.ip;
	if permitted_ips:contains(ip_raw) or
	   (permitted_cidr and ip.match(ip.new_ip(ip_raw), ip.parse_cidr(permitted_cidr))) then
		return true;
	end
	return false;
end

module:provides("http", {
	route = {
		GET = function(event)
			local request, response = event.request, event.response;
			if not is_permitted(request) then
				return 403; -- Forbidden
			end
			response.headers.content_type = "application/json";

			local resp = { ["*"] = true };

			for host in pairs(prosody.hosts) do
				resp[host] = true;
			end

			for host in pairs(resp) do
				local hostmods = {};
				local mods = modulemanager.get_modules(host);
				for mod_name, mod in pairs(mods) do
					hostmods[mod_name] = {
						type = mod.module.status_type;
						message = mod.module.status_message;
						time = datetime(math.floor(mod.module.status_time));
					};
				end
				resp[host] = hostmods;
			end

			return json.encode(resp);
		end;
	};
});