You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.lib.lua 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. local jid = require "util.jid";
  2. local timer = require "util.timer";
  3. local http = require "net.http";
  4. local http_timeout = 30;
  5. local have_async, async = pcall(require, "util.async");
  6. local http_headers = {
  7. ["User-Agent"] = "Prosody ("..prosody.version.."; "..prosody.platform..")"
  8. };
  9. local muc_domain_prefix = module:get_option_string("muc_mapper_domain_prefix", "conference");
  10. -- defaults to module.host, the module that uses the utility
  11. local muc_domain_base = module:get_option_string("muc_mapper_domain_base", module.host);
  12. -- The "real" MUC domain that we are proxying to
  13. local muc_domain = module:get_option_string("muc_mapper_domain", muc_domain_prefix.."."..muc_domain_base);
  14. local escaped_muc_domain_base = muc_domain_base:gsub("%p", "%%%1");
  15. local escaped_muc_domain_prefix = muc_domain_prefix:gsub("%p", "%%%1");
  16. -- The pattern used to extract the target subdomain
  17. -- (e.g. extract 'foo' from 'conference.foo.example.com')
  18. local target_subdomain_pattern = "^"..escaped_muc_domain_prefix..".([^%.]+)%."..escaped_muc_domain_base;
  19. -- table to store all incoming iqs without roomname in it, like discoinfo to the muc compoent
  20. local roomless_iqs = {};
  21. -- Utility function to split room JID to include room name and subdomain
  22. -- (e.g. from room1@conference.foo.example.com/res returns (room1, example.com, res, foo))
  23. local function room_jid_split_subdomain(room_jid)
  24. local node, host, resource = jid.split(room_jid);
  25. -- optimization, skip matching if there is no subdomain or it is not the muc component address at all
  26. if host == muc_domain or not starts_with(host, muc_domain_prefix) then
  27. return node, host, resource;
  28. end
  29. local target_subdomain = host and host:match(target_subdomain_pattern);
  30. return node, host, resource, target_subdomain
  31. end
  32. --- Utility function to check and convert a room JID from
  33. --- virtual room1@conference.foo.example.com to real [foo]room1@conference.example.com
  34. -- @param room_jid the room jid to match and rewrite if needed
  35. -- @param stanza the stanza
  36. -- @return returns room jid [foo]room1@conference.example.com when it has subdomain
  37. -- otherwise room1@conference.example.com(the room_jid value untouched)
  38. local function room_jid_match_rewrite(room_jid, stanza)
  39. local node, _, resource, target_subdomain = room_jid_split_subdomain(room_jid);
  40. if not target_subdomain then
  41. -- module:log("debug", "No need to rewrite out 'to' %s", room_jid);
  42. return room_jid;
  43. end
  44. -- Ok, rewrite room_jid address to new format
  45. local new_node, new_host, new_resource;
  46. if node then
  47. new_node, new_host, new_resource = "["..target_subdomain.."]"..node, muc_domain, resource;
  48. else
  49. -- module:log("debug", "No room name provided so rewriting only host 'to' %s", room_jid);
  50. new_host, new_resource = muc_domain, resource;
  51. if (stanza and stanza.attr and stanza.attr.id) then
  52. roomless_iqs[stanza.attr.id] = stanza.attr.to;
  53. end
  54. end
  55. return jid.join(new_node, new_host, new_resource);
  56. end
  57. -- Utility function to check and convert a room JID from real [foo]room1@muc.example.com to virtual room1@muc.foo.example.com
  58. local function internal_room_jid_match_rewrite(room_jid, stanza)
  59. local node, host, resource = jid.split(room_jid);
  60. if host ~= muc_domain or not node then
  61. -- module:log("debug", "No need to rewrite %s (not from the MUC host)", room_jid);
  62. if (stanza and stanza.attr and stanza.attr.id and roomless_iqs[stanza.attr.id]) then
  63. local result = roomless_iqs[stanza.attr.id];
  64. roomless_iqs[stanza.attr.id] = nil;
  65. return result;
  66. end
  67. return room_jid;
  68. end
  69. local target_subdomain, target_node = extract_subdomain(node);
  70. if not (target_node and target_subdomain) then
  71. -- module:log("debug", "Not rewriting... unexpected node format: %s", node);
  72. return room_jid;
  73. end
  74. -- Ok, rewrite room_jid address to pretty format
  75. return jid.join(target_node, muc_domain_prefix..".".. target_subdomain.."."..muc_domain_base, resource);
  76. end
  77. --- Finds and returns room by its jid
  78. -- @param room_jid the room jid to search in the muc component
  79. -- @return returns room if found or nil
  80. function get_room_from_jid(room_jid)
  81. local _, host = jid.split(room_jid);
  82. local component = hosts[host];
  83. if component then
  84. local muc = component.modules.muc
  85. if muc and rawget(muc,"rooms") then
  86. -- We're running 0.9.x or 0.10 (old MUC API)
  87. return muc.rooms[room_jid];
  88. elseif muc and rawget(muc,"get_room_from_jid") then
  89. -- We're running >0.10 (new MUC API)
  90. return muc.get_room_from_jid(room_jid);
  91. else
  92. return
  93. end
  94. end
  95. end
  96. -- Returns the room if available, work and in multidomain mode
  97. -- @param room_name the name of the room
  98. -- @param group name of the group (optional)
  99. -- @return returns room if found or nil
  100. function get_room_by_name_and_subdomain(room_name, subdomain)
  101. local room_address;
  102. -- if there is a subdomain we are in multidomain mode and that subdomain is not our main host
  103. if subdomain and subdomain ~= "" and subdomain ~= muc_domain_base then
  104. room_address = jid.join("["..subdomain.."]"..room_name, muc_domain);
  105. else
  106. room_address = jid.join(room_name, muc_domain);
  107. end
  108. return get_room_from_jid(room_address);
  109. end
  110. function async_handler_wrapper(event, handler)
  111. if not have_async then
  112. module:log("error", "requires a version of Prosody with util.async");
  113. return nil;
  114. end
  115. local runner = async.runner;
  116. -- Grab a local response so that we can send the http response when
  117. -- the handler is done.
  118. local response = event.response;
  119. local async_func = runner(
  120. function (event)
  121. local result = handler(event)
  122. -- If there is a status code in the result from the
  123. -- wrapped handler then add it to the response.
  124. if tonumber(result.status_code) ~= nil then
  125. response.status_code = result.status_code
  126. end
  127. -- If there are headers in the result from the
  128. -- wrapped handler then add them to the response.
  129. if result.headers ~= nil then
  130. response.headers = result.headers
  131. end
  132. -- Send the response to the waiting http client with
  133. -- or without the body from the wrapped handler.
  134. if result.body ~= nil then
  135. response:send(result.body)
  136. else
  137. response:send();
  138. end
  139. end
  140. )
  141. async_func:run(event)
  142. -- return true to keep the client http connection open.
  143. return true;
  144. end
  145. --- Updates presence stanza, by adding identity node
  146. -- @param stanza the presence stanza
  147. -- @param user the user to which presence we are updating identity
  148. -- @param group the group of the user to which presence we are updating identity
  149. -- @param creator_user the user who created the user which presence we
  150. -- are updating (this is the poltergeist case, where a user creates
  151. -- a poltergeist), optional.
  152. -- @param creator_group the group of the user who created the user which
  153. -- presence we are updating (this is the poltergeist case, where a user creates
  154. -- a poltergeist), optional.
  155. function update_presence_identity(
  156. stanza, user, group, creator_user, creator_group)
  157. -- First remove any 'identity' element if it already
  158. -- exists, so it cannot be spoofed by a client
  159. stanza:maptags(
  160. function(tag)
  161. for k, v in pairs(tag) do
  162. if k == "name" and v == "identity" then
  163. return nil
  164. end
  165. end
  166. return tag
  167. end
  168. )
  169. stanza:tag("identity"):tag("user");
  170. for k, v in pairs(user) do
  171. v = tostring(v)
  172. stanza:tag(k):text(v):up();
  173. end
  174. stanza:up();
  175. -- Add the group information if it is present
  176. if group then
  177. stanza:tag("group"):text(group):up();
  178. end
  179. -- Add the creator user information if it is present
  180. if creator_user then
  181. stanza:tag("creator_user");
  182. for k, v in pairs(creator_user) do
  183. stanza:tag(k):text(v):up();
  184. end
  185. stanza:up();
  186. -- Add the creator group information if it is present
  187. if creator_group then
  188. stanza:tag("creator_group"):text(creator_group):up();
  189. end
  190. stanza:up();
  191. end
  192. end
  193. -- Utility function to check whether feature is present and enabled. Allow
  194. -- a feature if there are features present in the session(coming from
  195. -- the token) and the value of the feature is true.
  196. -- If features is not present in the token we skip feature detection and allow
  197. -- everything.
  198. function is_feature_allowed(session, feature)
  199. if (session.jitsi_meet_context_features == nil
  200. or session.jitsi_meet_context_features[feature] == "true" or session.jitsi_meet_context_features[feature] == true) then
  201. return true;
  202. else
  203. return false;
  204. end
  205. end
  206. --- Extracts the subdomain and room name from internal jid node [foo]room1
  207. -- @return subdomain(optional, if extracted or nil), the room name
  208. function extract_subdomain(room_node)
  209. -- optimization, skip matching if there is no subdomain, no [subdomain] part in the beginning of the node
  210. if not starts_with(room_node, '[') then
  211. return nil,room_node;
  212. end
  213. return room_node:match("^%[([^%]]+)%](.+)$");
  214. end
  215. function starts_with(str, start)
  216. return str:sub(1, #start) == start
  217. end
  218. -- healthcheck rooms in jicofo starts with a string '__jicofo-health-check'
  219. function is_healthcheck_room(room_jid)
  220. if starts_with(room_jid, "__jicofo-health-check") then
  221. return true;
  222. end
  223. return false;
  224. end
  225. --- Utility function to make an http get request and
  226. --- retry @param retry number of times
  227. -- @param url endpoint to be called
  228. -- @param retry nr of retries, if retry is
  229. -- nil there will be no retries
  230. -- @returns result of the http call or nil if
  231. -- the external call failed after the last retry
  232. function http_get_with_retry(url, retry)
  233. local content, code;
  234. local timeout_occurred;
  235. local wait, done = async.waiter();
  236. local function cb(content_, code_, response_, request_)
  237. if timeout_occurred == nil then
  238. code = code_;
  239. if code == 200 or code == 204 then
  240. module:log("debug", "External call was successful, content %s", content_);
  241. content = content_
  242. else
  243. module:log("warn", "Error on public key request: Code %s, Content %s",
  244. code_, content_);
  245. end
  246. done();
  247. else
  248. module:log("warn", "External call reply delivered after timeout from: %s", url);
  249. end
  250. end
  251. local function call_http()
  252. return http.request(url, {
  253. headers = http_headers or {},
  254. method = "GET"
  255. }, cb);
  256. end
  257. local request = call_http();
  258. local function cancel()
  259. -- TODO: This check is racey. Not likely to be a problem, but we should
  260. -- still stick a mutex on content / code at some point.
  261. if code == nil then
  262. timeout_occurred = true;
  263. module:log("warn", "Timeout %s seconds making the external call to: %s", http_timeout, url);
  264. -- no longer present in prosody 0.11, so check before calling
  265. if http.destroy_request ~= nil then
  266. http.destroy_request(request);
  267. end
  268. if retry == nil then
  269. module:log("debug", "External call failed and retry policy is not set");
  270. done();
  271. elseif retry ~= nil and retry < 1 then
  272. module:log("debug", "External call failed after retry")
  273. done();
  274. else
  275. module:log("debug", "External call failed, retry nr %s", retry)
  276. retry = retry - 1;
  277. request = call_http()
  278. return http_timeout;
  279. end
  280. end
  281. end
  282. timer.add_task(http_timeout, cancel);
  283. wait();
  284. return content;
  285. end
  286. -- Checks whether there is status in the <x node
  287. -- @param muc_x the <x element from presence
  288. -- @param status checks for this status
  289. -- @returns true if the status is found, false otherwise or if no muc_x is provided.
  290. function presence_check_status(muc_x, status)
  291. if not muc_x then
  292. return false;
  293. end
  294. for statusNode in muc_x:childtags('status') do
  295. if statusNode.attr.code == status then
  296. return true;
  297. end
  298. end
  299. return false;
  300. end
  301. return {
  302. extract_subdomain = extract_subdomain;
  303. is_feature_allowed = is_feature_allowed;
  304. is_healthcheck_room = is_healthcheck_room;
  305. get_room_from_jid = get_room_from_jid;
  306. get_room_by_name_and_subdomain = get_room_by_name_and_subdomain;
  307. async_handler_wrapper = async_handler_wrapper;
  308. presence_check_status = presence_check_status;
  309. room_jid_match_rewrite = room_jid_match_rewrite;
  310. room_jid_split_subdomain = room_jid_split_subdomain;
  311. internal_room_jid_match_rewrite = internal_room_jid_match_rewrite;
  312. update_presence_identity = update_presence_identity;
  313. http_get_with_retry = http_get_with_retry;
  314. };