2017-07-15 03:03:36 +00:00
|
|
|
local jid = require "util.jid";
|
2020-08-14 12:06:14 +00:00
|
|
|
local timer = require "util.timer";
|
|
|
|
local http = require "net.http";
|
2022-11-28 20:18:59 +00:00
|
|
|
local cache = require "util.cache";
|
2020-08-14 12:06:14 +00:00
|
|
|
|
|
|
|
local http_timeout = 30;
|
2019-12-05 10:09:58 +00:00
|
|
|
local have_async, async = pcall(require, "util.async");
|
2020-08-14 12:06:14 +00:00
|
|
|
local http_headers = {
|
|
|
|
["User-Agent"] = "Prosody ("..prosody.version.."; "..prosody.platform..")"
|
|
|
|
};
|
|
|
|
|
2021-05-12 21:36:02 +00:00
|
|
|
local muc_domain_prefix = module:get_option_string("muc_mapper_domain_prefix", "conference");
|
2017-08-22 20:00:40 +00:00
|
|
|
|
|
|
|
-- defaults to module.host, the module that uses the utility
|
2021-05-12 21:36:02 +00:00
|
|
|
local muc_domain_base = module:get_option_string("muc_mapper_domain_base", module.host);
|
2017-08-22 20:00:40 +00:00
|
|
|
|
|
|
|
-- The "real" MUC domain that we are proxying to
|
2021-05-12 21:36:02 +00:00
|
|
|
local muc_domain = module:get_option_string("muc_mapper_domain", muc_domain_prefix.."."..muc_domain_base);
|
2017-08-22 20:00:40 +00:00
|
|
|
|
|
|
|
local escaped_muc_domain_base = muc_domain_base:gsub("%p", "%%%1");
|
|
|
|
local escaped_muc_domain_prefix = muc_domain_prefix:gsub("%p", "%%%1");
|
|
|
|
-- The pattern used to extract the target subdomain
|
2020-11-04 14:25:03 +00:00
|
|
|
-- (e.g. extract 'foo' from 'conference.foo.example.com')
|
2021-05-12 21:36:02 +00:00
|
|
|
local target_subdomain_pattern = "^"..escaped_muc_domain_prefix..".([^%.]+)%."..escaped_muc_domain_base;
|
2017-08-22 20:00:40 +00:00
|
|
|
|
2022-07-14 07:10:08 +00:00
|
|
|
-- table to store all incoming iqs without roomname in it, like discoinfo to the muc component
|
2020-11-04 14:25:03 +00:00
|
|
|
local roomless_iqs = {};
|
|
|
|
|
2022-11-28 20:18:59 +00:00
|
|
|
local split_subdomain_cache = cache.new(1000);
|
|
|
|
local extract_subdomain_cache = cache.new(1000);
|
|
|
|
local internal_room_jid_cache = cache.new(1000);
|
|
|
|
|
2020-07-14 20:49:51 +00:00
|
|
|
-- Utility function to split room JID to include room name and subdomain
|
2020-11-04 14:25:03 +00:00
|
|
|
-- (e.g. from room1@conference.foo.example.com/res returns (room1, example.com, res, foo))
|
2020-07-14 20:49:51 +00:00
|
|
|
local function room_jid_split_subdomain(room_jid)
|
2022-11-28 20:18:59 +00:00
|
|
|
local ret = split_subdomain_cache:get(room_jid);
|
|
|
|
if ret then
|
|
|
|
return ret.node, ret.host, ret.resource, ret.subdomain;
|
2020-11-05 20:30:06 +00:00
|
|
|
end
|
|
|
|
|
2022-11-28 20:18:59 +00:00
|
|
|
local node, host, resource = jid.split(room_jid);
|
|
|
|
|
2020-07-14 20:49:51 +00:00
|
|
|
local target_subdomain = host and host:match(target_subdomain_pattern);
|
2022-11-28 20:18:59 +00:00
|
|
|
local cache_value = {node=node, host=host, resource=resource, subdomain=target_subdomain};
|
|
|
|
split_subdomain_cache:set(room_jid, cache_value);
|
|
|
|
return node, host, resource, target_subdomain;
|
2020-07-14 20:49:51 +00:00
|
|
|
end
|
|
|
|
|
2017-08-22 20:00:40 +00:00
|
|
|
--- Utility function to check and convert a room JID from
|
2020-11-04 14:25:03 +00:00
|
|
|
--- virtual room1@conference.foo.example.com to real [foo]room1@conference.example.com
|
2017-08-22 20:00:40 +00:00
|
|
|
-- @param room_jid the room jid to match and rewrite if needed
|
2020-11-04 14:25:03 +00:00
|
|
|
-- @param stanza the stanza
|
|
|
|
-- @return returns room jid [foo]room1@conference.example.com when it has subdomain
|
|
|
|
-- otherwise room1@conference.example.com(the room_jid value untouched)
|
|
|
|
local function room_jid_match_rewrite(room_jid, stanza)
|
|
|
|
local node, _, resource, target_subdomain = room_jid_split_subdomain(room_jid);
|
2017-08-22 20:00:40 +00:00
|
|
|
if not target_subdomain then
|
2020-11-04 14:25:03 +00:00
|
|
|
-- module:log("debug", "No need to rewrite out 'to' %s", room_jid);
|
2017-08-22 20:00:40 +00:00
|
|
|
return room_jid;
|
|
|
|
end
|
|
|
|
-- Ok, rewrite room_jid address to new format
|
2020-11-04 14:25:03 +00:00
|
|
|
local new_node, new_host, new_resource;
|
|
|
|
if node then
|
|
|
|
new_node, new_host, new_resource = "["..target_subdomain.."]"..node, muc_domain, resource;
|
|
|
|
else
|
|
|
|
-- module:log("debug", "No room name provided so rewriting only host 'to' %s", room_jid);
|
|
|
|
new_host, new_resource = muc_domain, resource;
|
|
|
|
|
|
|
|
if (stanza and stanza.attr and stanza.attr.id) then
|
|
|
|
roomless_iqs[stanza.attr.id] = stanza.attr.to;
|
|
|
|
end
|
|
|
|
end
|
2021-06-21 20:49:06 +00:00
|
|
|
|
|
|
|
return jid.join(new_node, new_host, new_resource);
|
2017-08-22 20:00:40 +00:00
|
|
|
end
|
|
|
|
|
2020-11-04 14:25:03 +00:00
|
|
|
-- Utility function to check and convert a room JID from real [foo]room1@muc.example.com to virtual room1@muc.foo.example.com
|
|
|
|
local function internal_room_jid_match_rewrite(room_jid, stanza)
|
2022-11-28 20:18:59 +00:00
|
|
|
-- first check for roomless_iqs
|
|
|
|
if (stanza and stanza.attr and stanza.attr.id and roomless_iqs[stanza.attr.id]) then
|
|
|
|
local result = roomless_iqs[stanza.attr.id];
|
|
|
|
roomless_iqs[stanza.attr.id] = nil;
|
|
|
|
return result;
|
|
|
|
end
|
|
|
|
|
|
|
|
local ret = internal_room_jid_cache:get(room_jid);
|
|
|
|
if ret then
|
|
|
|
return ret;
|
|
|
|
end
|
|
|
|
|
2020-07-14 20:49:51 +00:00
|
|
|
local node, host, resource = jid.split(room_jid);
|
|
|
|
if host ~= muc_domain or not node then
|
2020-11-04 14:25:03 +00:00
|
|
|
-- module:log("debug", "No need to rewrite %s (not from the MUC host)", room_jid);
|
2022-11-28 20:18:59 +00:00
|
|
|
internal_room_jid_cache:set(room_jid, room_jid);
|
2020-07-14 20:49:51 +00:00
|
|
|
return room_jid;
|
|
|
|
end
|
2020-11-05 20:30:06 +00:00
|
|
|
|
|
|
|
local target_subdomain, target_node = extract_subdomain(node);
|
2020-07-14 20:49:51 +00:00
|
|
|
if not (target_node and target_subdomain) then
|
2020-11-04 14:25:03 +00:00
|
|
|
-- module:log("debug", "Not rewriting... unexpected node format: %s", node);
|
2022-11-28 20:18:59 +00:00
|
|
|
internal_room_jid_cache:set(room_jid, room_jid);
|
2020-07-14 20:49:51 +00:00
|
|
|
return room_jid;
|
|
|
|
end
|
2020-11-05 20:30:06 +00:00
|
|
|
|
2020-07-14 20:49:51 +00:00
|
|
|
-- Ok, rewrite room_jid address to pretty format
|
2022-11-28 20:18:59 +00:00
|
|
|
ret = jid.join(target_node, muc_domain_prefix..".".. target_subdomain.."."..muc_domain_base, resource);
|
|
|
|
internal_room_jid_cache:set(room_jid, ret);
|
|
|
|
return ret;
|
2020-07-14 20:49:51 +00:00
|
|
|
end
|
2017-08-22 20:00:40 +00:00
|
|
|
|
2017-07-15 03:03:36 +00:00
|
|
|
--- Finds and returns room by its jid
|
|
|
|
-- @param room_jid the room jid to search in the muc component
|
|
|
|
-- @return returns room if found or nil
|
|
|
|
function get_room_from_jid(room_jid)
|
|
|
|
local _, host = jid.split(room_jid);
|
|
|
|
local component = hosts[host];
|
|
|
|
if component then
|
|
|
|
local muc = component.modules.muc
|
|
|
|
if muc and rawget(muc,"rooms") then
|
|
|
|
-- We're running 0.9.x or 0.10 (old MUC API)
|
|
|
|
return muc.rooms[room_jid];
|
|
|
|
elseif muc and rawget(muc,"get_room_from_jid") then
|
|
|
|
-- We're running >0.10 (new MUC API)
|
|
|
|
return muc.get_room_from_jid(room_jid);
|
|
|
|
else
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-12 21:36:02 +00:00
|
|
|
-- Returns the room if available, work and in multidomain mode
|
|
|
|
-- @param room_name the name of the room
|
|
|
|
-- @param group name of the group (optional)
|
|
|
|
-- @return returns room if found or nil
|
|
|
|
function get_room_by_name_and_subdomain(room_name, subdomain)
|
|
|
|
local room_address;
|
|
|
|
|
|
|
|
-- if there is a subdomain we are in multidomain mode and that subdomain is not our main host
|
|
|
|
if subdomain and subdomain ~= "" and subdomain ~= muc_domain_base then
|
2021-05-13 18:07:44 +00:00
|
|
|
room_address = jid.join("["..subdomain.."]"..room_name, muc_domain);
|
2021-05-12 21:36:02 +00:00
|
|
|
else
|
|
|
|
room_address = jid.join(room_name, muc_domain);
|
|
|
|
end
|
|
|
|
|
|
|
|
return get_room_from_jid(room_address);
|
|
|
|
end
|
|
|
|
|
2020-01-23 19:31:05 +00:00
|
|
|
function async_handler_wrapper(event, handler)
|
2019-12-05 10:09:58 +00:00
|
|
|
if not have_async then
|
|
|
|
module:log("error", "requires a version of Prosody with util.async");
|
|
|
|
return nil;
|
|
|
|
end
|
|
|
|
|
|
|
|
local runner = async.runner;
|
2020-04-30 21:26:58 +00:00
|
|
|
|
2018-07-16 21:54:08 +00:00
|
|
|
-- Grab a local response so that we can send the http response when
|
|
|
|
-- the handler is done.
|
|
|
|
local response = event.response;
|
|
|
|
local async_func = runner(
|
|
|
|
function (event)
|
|
|
|
local result = handler(event)
|
|
|
|
|
|
|
|
-- If there is a status code in the result from the
|
|
|
|
-- wrapped handler then add it to the response.
|
|
|
|
if tonumber(result.status_code) ~= nil then
|
|
|
|
response.status_code = result.status_code
|
|
|
|
end
|
|
|
|
|
|
|
|
-- If there are headers in the result from the
|
|
|
|
-- wrapped handler then add them to the response.
|
|
|
|
if result.headers ~= nil then
|
|
|
|
response.headers = result.headers
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Send the response to the waiting http client with
|
|
|
|
-- or without the body from the wrapped handler.
|
|
|
|
if result.body ~= nil then
|
|
|
|
response:send(result.body)
|
|
|
|
else
|
|
|
|
response:send();
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
async_func:run(event)
|
|
|
|
-- return true to keep the client http connection open.
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
2018-03-23 20:58:05 +00:00
|
|
|
--- Updates presence stanza, by adding identity node
|
|
|
|
-- @param stanza the presence stanza
|
|
|
|
-- @param user the user to which presence we are updating identity
|
|
|
|
-- @param group the group of the user to which presence we are updating identity
|
|
|
|
-- @param creator_user the user who created the user which presence we
|
|
|
|
-- are updating (this is the poltergeist case, where a user creates
|
|
|
|
-- a poltergeist), optional.
|
|
|
|
-- @param creator_group the group of the user who created the user which
|
|
|
|
-- presence we are updating (this is the poltergeist case, where a user creates
|
|
|
|
-- a poltergeist), optional.
|
|
|
|
function update_presence_identity(
|
|
|
|
stanza, user, group, creator_user, creator_group)
|
|
|
|
|
|
|
|
-- First remove any 'identity' element if it already
|
|
|
|
-- exists, so it cannot be spoofed by a client
|
|
|
|
stanza:maptags(
|
|
|
|
function(tag)
|
|
|
|
for k, v in pairs(tag) do
|
|
|
|
if k == "name" and v == "identity" then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return tag
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
stanza:tag("identity"):tag("user");
|
|
|
|
for k, v in pairs(user) do
|
2020-11-06 12:05:53 +00:00
|
|
|
v = tostring(v)
|
2018-03-23 20:58:05 +00:00
|
|
|
stanza:tag(k):text(v):up();
|
|
|
|
end
|
|
|
|
stanza:up();
|
|
|
|
|
|
|
|
-- Add the group information if it is present
|
|
|
|
if group then
|
|
|
|
stanza:tag("group"):text(group):up();
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Add the creator user information if it is present
|
|
|
|
if creator_user then
|
|
|
|
stanza:tag("creator_user");
|
|
|
|
for k, v in pairs(creator_user) do
|
|
|
|
stanza:tag(k):text(v):up();
|
|
|
|
end
|
|
|
|
stanza:up();
|
|
|
|
|
|
|
|
-- Add the creator group information if it is present
|
|
|
|
if creator_group then
|
|
|
|
stanza:tag("creator_group"):text(creator_group):up();
|
|
|
|
end
|
|
|
|
stanza:up();
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-06-15 18:10:22 +00:00
|
|
|
-- Utility function to check whether feature is present and enabled. Allow
|
|
|
|
-- a feature if there are features present in the session(coming from
|
|
|
|
-- the token) and the value of the feature is true.
|
|
|
|
-- If features is not present in the token we skip feature detection and allow
|
|
|
|
-- everything.
|
|
|
|
function is_feature_allowed(session, feature)
|
|
|
|
if (session.jitsi_meet_context_features == nil
|
2020-11-23 17:13:05 +00:00
|
|
|
or session.jitsi_meet_context_features[feature] == "true" or session.jitsi_meet_context_features[feature] == true) then
|
2018-06-15 18:10:22 +00:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-05 20:30:06 +00:00
|
|
|
--- Extracts the subdomain and room name from internal jid node [foo]room1
|
|
|
|
-- @return subdomain(optional, if extracted or nil), the room name
|
|
|
|
function extract_subdomain(room_node)
|
2022-11-28 20:18:59 +00:00
|
|
|
local ret = extract_subdomain_cache:get(room_node);
|
|
|
|
if ret then
|
|
|
|
return ret.subdomain, ret.room;
|
2020-11-05 20:30:06 +00:00
|
|
|
end
|
|
|
|
|
2022-11-28 20:18:59 +00:00
|
|
|
local subdomain, room_name = room_node:match("^%[([^%]]+)%](.+)$");
|
|
|
|
local cache_value = {subdomain=subdomain, room=room_name};
|
|
|
|
extract_subdomain_cache:set(room_node, cache_value);
|
|
|
|
return subdomain, room_name;
|
2020-11-05 20:30:06 +00:00
|
|
|
end
|
|
|
|
|
2020-04-30 21:26:58 +00:00
|
|
|
function starts_with(str, start)
|
|
|
|
return str:sub(1, #start) == start
|
|
|
|
end
|
2020-05-12 21:40:39 +00:00
|
|
|
|
2022-07-12 06:51:13 +00:00
|
|
|
function ends_with(str, ending)
|
|
|
|
return ending == "" or str:sub(-#ending) == ending
|
|
|
|
end
|
|
|
|
|
2020-04-30 21:26:58 +00:00
|
|
|
-- healthcheck rooms in jicofo starts with a string '__jicofo-health-check'
|
2020-05-12 21:40:39 +00:00
|
|
|
function is_healthcheck_room(room_jid)
|
2022-11-28 20:18:59 +00:00
|
|
|
return starts_with(room_jid, "__jicofo-health-check");
|
2020-04-30 21:26:58 +00:00
|
|
|
end
|
|
|
|
|
2020-11-04 14:25:03 +00:00
|
|
|
--- Utility function to make an http get request and
|
|
|
|
--- retry @param retry number of times
|
2020-08-14 12:06:14 +00:00
|
|
|
-- @param url endpoint to be called
|
|
|
|
-- @param retry nr of retries, if retry is
|
2021-10-04 10:19:41 +00:00
|
|
|
-- @param auth_token value to be passed as auth Bearer
|
2020-08-14 12:06:14 +00:00
|
|
|
-- nil there will be no retries
|
|
|
|
-- @returns result of the http call or nil if
|
|
|
|
-- the external call failed after the last retry
|
2021-10-04 10:19:41 +00:00
|
|
|
function http_get_with_retry(url, retry, auth_token)
|
2020-08-14 12:06:14 +00:00
|
|
|
local content, code;
|
2020-08-19 14:38:40 +00:00
|
|
|
local timeout_occurred;
|
2020-08-14 12:06:14 +00:00
|
|
|
local wait, done = async.waiter();
|
2021-10-04 10:19:41 +00:00
|
|
|
local request_headers = http_headers or {}
|
|
|
|
if auth_token ~= nil then
|
|
|
|
request_headers['Authorization'] = 'Bearer ' .. auth_token
|
|
|
|
end
|
|
|
|
|
2020-08-14 12:06:14 +00:00
|
|
|
local function cb(content_, code_, response_, request_)
|
2020-08-19 14:38:40 +00:00
|
|
|
if timeout_occurred == nil then
|
|
|
|
code = code_;
|
|
|
|
if code == 200 or code == 204 then
|
|
|
|
module:log("debug", "External call was successful, content %s", content_);
|
|
|
|
content = content_
|
|
|
|
else
|
2021-10-04 10:19:41 +00:00
|
|
|
module:log("warn", "Error on GET request: Code %s, Content %s",
|
2020-08-19 14:38:40 +00:00
|
|
|
code_, content_);
|
|
|
|
end
|
|
|
|
done();
|
2020-08-14 12:06:14 +00:00
|
|
|
else
|
2020-08-19 14:38:40 +00:00
|
|
|
module:log("warn", "External call reply delivered after timeout from: %s", url);
|
2020-08-14 12:06:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function call_http()
|
|
|
|
return http.request(url, {
|
2021-10-04 10:19:41 +00:00
|
|
|
headers = request_headers,
|
2020-08-14 12:06:14 +00:00
|
|
|
method = "GET"
|
|
|
|
}, cb);
|
|
|
|
end
|
|
|
|
|
|
|
|
local request = call_http();
|
|
|
|
|
|
|
|
local function cancel()
|
|
|
|
-- TODO: This check is racey. Not likely to be a problem, but we should
|
|
|
|
-- still stick a mutex on content / code at some point.
|
|
|
|
if code == nil then
|
2020-08-19 14:38:40 +00:00
|
|
|
timeout_occurred = true;
|
|
|
|
module:log("warn", "Timeout %s seconds making the external call to: %s", http_timeout, url);
|
2020-08-14 12:06:14 +00:00
|
|
|
-- no longer present in prosody 0.11, so check before calling
|
|
|
|
if http.destroy_request ~= nil then
|
|
|
|
http.destroy_request(request);
|
|
|
|
end
|
|
|
|
if retry == nil then
|
|
|
|
module:log("debug", "External call failed and retry policy is not set");
|
|
|
|
done();
|
|
|
|
elseif retry ~= nil and retry < 1 then
|
|
|
|
module:log("debug", "External call failed after retry")
|
|
|
|
done();
|
|
|
|
else
|
|
|
|
module:log("debug", "External call failed, retry nr %s", retry)
|
|
|
|
retry = retry - 1;
|
|
|
|
request = call_http()
|
|
|
|
return http_timeout;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
timer.add_task(http_timeout, cancel);
|
|
|
|
wait();
|
|
|
|
|
2021-10-04 10:19:41 +00:00
|
|
|
return content, code;
|
2020-08-14 12:06:14 +00:00
|
|
|
end
|
|
|
|
|
2021-03-08 20:10:59 +00:00
|
|
|
-- Checks whether there is status in the <x node
|
|
|
|
-- @param muc_x the <x element from presence
|
|
|
|
-- @param status checks for this status
|
|
|
|
-- @returns true if the status is found, false otherwise or if no muc_x is provided.
|
|
|
|
function presence_check_status(muc_x, status)
|
|
|
|
if not muc_x then
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
|
|
|
|
for statusNode in muc_x:childtags('status') do
|
|
|
|
if statusNode.attr.code == status then
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
|
2017-07-15 03:03:36 +00:00
|
|
|
return {
|
2020-11-05 20:30:06 +00:00
|
|
|
extract_subdomain = extract_subdomain;
|
2018-06-15 18:10:22 +00:00
|
|
|
is_feature_allowed = is_feature_allowed;
|
2020-04-30 21:26:58 +00:00
|
|
|
is_healthcheck_room = is_healthcheck_room;
|
2017-07-15 03:03:36 +00:00
|
|
|
get_room_from_jid = get_room_from_jid;
|
2021-05-12 21:36:02 +00:00
|
|
|
get_room_by_name_and_subdomain = get_room_by_name_and_subdomain;
|
2018-07-16 21:54:08 +00:00
|
|
|
async_handler_wrapper = async_handler_wrapper;
|
2021-03-08 20:10:59 +00:00
|
|
|
presence_check_status = presence_check_status;
|
2018-03-23 20:58:05 +00:00
|
|
|
room_jid_match_rewrite = room_jid_match_rewrite;
|
2020-07-14 20:49:51 +00:00
|
|
|
room_jid_split_subdomain = room_jid_split_subdomain;
|
|
|
|
internal_room_jid_match_rewrite = internal_room_jid_match_rewrite;
|
2018-03-23 20:58:05 +00:00
|
|
|
update_presence_identity = update_presence_identity;
|
2020-08-14 12:06:14 +00:00
|
|
|
http_get_with_retry = http_get_with_retry;
|
2022-07-12 06:51:13 +00:00
|
|
|
ends_with = ends_with;
|
|
|
|
starts_with = starts_with;
|
2018-05-30 16:32:02 +00:00
|
|
|
};
|