mod_http_health.lua 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. module:set_global();
  2. local ip = require "util.ip";
  3. local modulemanager = require "core.modulemanager";
  4. local permitted_ips = module:get_option_set("http_health_allow_ips", { "::1", "127.0.0.1" });
  5. local permitted_cidr = module:get_option_string("http_health_allow_cidr");
  6. local function is_permitted(request)
  7. local ip_raw = request.ip;
  8. if permitted_ips:contains(ip_raw) or
  9. (permitted_cidr and ip.match(ip.new_ip(ip_raw), ip.parse_cidr(permitted_cidr))) then
  10. return true;
  11. end
  12. return false;
  13. end
  14. module:provides("http", {
  15. route = {
  16. GET = function(event)
  17. local request = event.request;
  18. if not is_permitted(request) then
  19. return 403; -- Forbidden
  20. end
  21. for host in pairs(prosody.hosts) do
  22. local mods = modulemanager.get_modules(host);
  23. for _, mod in pairs(mods) do
  24. if mod.module.status_type == "error" then
  25. return { status_code = 500; headers = { content_type = "text/plain" }; body = "HAS ERRORS\n" };
  26. end
  27. end
  28. end
  29. return { status_code = 200; headers = { content_type = "text/plain" }; body = "OK\n" };
  30. end;
  31. };
  32. });