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

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