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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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
  10. = module:get_option_string("muc_mapper_domain_prefix", "conference");
  11. -- defaults to module.host, the module that uses the utility
  12. local muc_domain_base
  13. = module:get_option_string("muc_mapper_domain_base", module.host);
  14. -- The "real" MUC domain that we are proxying to
  15. local muc_domain = module:get_option_string(
  16. "muc_mapper_domain", muc_domain_prefix.."."..muc_domain_base);
  17. local escaped_muc_domain_base = muc_domain_base:gsub("%p", "%%%1");
  18. local escaped_muc_domain_prefix = muc_domain_prefix:gsub("%p", "%%%1");
  19. -- The pattern used to extract the target subdomain
  20. -- (e.g. extract 'foo' from 'conference.foo.example.com')
  21. local target_subdomain_pattern
  22. = "^"..escaped_muc_domain_prefix..".([^%.]+)%."..escaped_muc_domain_base;
  23. -- table to store all incoming iqs without roomname in it, like discoinfo to the muc compoent
  24. local roomless_iqs = {};
  25. -- Utility function to split room JID to include room name and subdomain
  26. -- (e.g. from room1@conference.foo.example.com/res returns (room1, example.com, res, foo))
  27. local function room_jid_split_subdomain(room_jid)
  28. local node, host, resource = jid.split(room_jid);
  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 = node:match("^%[([^%]]+)%](.+)$");
  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. function async_handler_wrapper(event, handler)
  102. if not have_async then
  103. module:log("error", "requires a version of Prosody with util.async");
  104. return nil;
  105. end
  106. local runner = async.runner;
  107. -- Grab a local response so that we can send the http response when
  108. -- the handler is done.
  109. local response = event.response;
  110. local async_func = runner(
  111. function (event)
  112. local result = handler(event)
  113. -- If there is a status code in the result from the
  114. -- wrapped handler then add it to the response.
  115. if tonumber(result.status_code) ~= nil then
  116. response.status_code = result.status_code
  117. end
  118. -- If there are headers in the result from the
  119. -- wrapped handler then add them to the response.
  120. if result.headers ~= nil then
  121. response.headers = result.headers
  122. end
  123. -- Send the response to the waiting http client with
  124. -- or without the body from the wrapped handler.
  125. if result.body ~= nil then
  126. response:send(result.body)
  127. else
  128. response:send();
  129. end
  130. end
  131. )
  132. async_func:run(event)
  133. -- return true to keep the client http connection open.
  134. return true;
  135. end
  136. --- Updates presence stanza, by adding identity node
  137. -- @param stanza the presence stanza
  138. -- @param user the user to which presence we are updating identity
  139. -- @param group the group of the user to which presence we are updating identity
  140. -- @param creator_user the user who created the user which presence we
  141. -- are updating (this is the poltergeist case, where a user creates
  142. -- a poltergeist), optional.
  143. -- @param creator_group the group of the user who created the user which
  144. -- presence we are updating (this is the poltergeist case, where a user creates
  145. -- a poltergeist), optional.
  146. function update_presence_identity(
  147. stanza, user, group, creator_user, creator_group)
  148. -- First remove any 'identity' element if it already
  149. -- exists, so it cannot be spoofed by a client
  150. stanza:maptags(
  151. function(tag)
  152. for k, v in pairs(tag) do
  153. if k == "name" and v == "identity" then
  154. return nil
  155. end
  156. end
  157. return tag
  158. end
  159. )
  160. module:log("debug",
  161. "Presence after previous identity stripped: %s", tostring(stanza));
  162. stanza:tag("identity"):tag("user");
  163. for k, v in pairs(user) do
  164. stanza:tag(k):text(v):up();
  165. end
  166. stanza:up();
  167. -- Add the group information if it is present
  168. if group then
  169. stanza:tag("group"):text(group):up();
  170. end
  171. -- Add the creator user information if it is present
  172. if creator_user then
  173. stanza:tag("creator_user");
  174. for k, v in pairs(creator_user) do
  175. stanza:tag(k):text(v):up();
  176. end
  177. stanza:up();
  178. -- Add the creator group information if it is present
  179. if creator_group then
  180. stanza:tag("creator_group"):text(creator_group):up();
  181. end
  182. stanza:up();
  183. end
  184. module:log("debug",
  185. "Presence with identity inserted %s", tostring(stanza))
  186. end
  187. -- Utility function to check whether feature is present and enabled. Allow
  188. -- a feature if there are features present in the session(coming from
  189. -- the token) and the value of the feature is true.
  190. -- If features is not present in the token we skip feature detection and allow
  191. -- everything.
  192. function is_feature_allowed(session, feature)
  193. if (session.jitsi_meet_context_features == nil
  194. or session.jitsi_meet_context_features[feature] == "true") then
  195. return true;
  196. else
  197. return false;
  198. end
  199. end
  200. function starts_with(str, start)
  201. return str:sub(1, #start) == start
  202. end
  203. -- healthcheck rooms in jicofo starts with a string '__jicofo-health-check'
  204. function is_healthcheck_room(room_jid)
  205. if starts_with(room_jid, "__jicofo-health-check") then
  206. return true;
  207. end
  208. return false;
  209. end
  210. --- Utility function to make an http get request and
  211. --- retry @param retry number of times
  212. -- @param url endpoint to be called
  213. -- @param retry nr of retries, if retry is
  214. -- nil there will be no retries
  215. -- @returns result of the http call or nil if
  216. -- the external call failed after the last retry
  217. function http_get_with_retry(url, retry)
  218. local content, code;
  219. local timeout_occurred;
  220. local wait, done = async.waiter();
  221. local function cb(content_, code_, response_, request_)
  222. if timeout_occurred == nil then
  223. code = code_;
  224. if code == 200 or code == 204 then
  225. module:log("debug", "External call was successful, content %s", content_);
  226. content = content_
  227. else
  228. module:log("warn", "Error on public key request: Code %s, Content %s",
  229. code_, content_);
  230. end
  231. done();
  232. else
  233. module:log("warn", "External call reply delivered after timeout from: %s", url);
  234. end
  235. end
  236. local function call_http()
  237. return http.request(url, {
  238. headers = http_headers or {},
  239. method = "GET"
  240. }, cb);
  241. end
  242. local request = call_http();
  243. local function cancel()
  244. -- TODO: This check is racey. Not likely to be a problem, but we should
  245. -- still stick a mutex on content / code at some point.
  246. if code == nil then
  247. timeout_occurred = true;
  248. module:log("warn", "Timeout %s seconds making the external call to: %s", http_timeout, url);
  249. -- no longer present in prosody 0.11, so check before calling
  250. if http.destroy_request ~= nil then
  251. http.destroy_request(request);
  252. end
  253. if retry == nil then
  254. module:log("debug", "External call failed and retry policy is not set");
  255. done();
  256. elseif retry ~= nil and retry < 1 then
  257. module:log("debug", "External call failed after retry")
  258. done();
  259. else
  260. module:log("debug", "External call failed, retry nr %s", retry)
  261. retry = retry - 1;
  262. request = call_http()
  263. return http_timeout;
  264. end
  265. end
  266. end
  267. timer.add_task(http_timeout, cancel);
  268. wait();
  269. return content;
  270. end
  271. return {
  272. is_feature_allowed = is_feature_allowed;
  273. is_healthcheck_room = is_healthcheck_room;
  274. get_room_from_jid = get_room_from_jid;
  275. async_handler_wrapper = async_handler_wrapper;
  276. room_jid_match_rewrite = room_jid_match_rewrite;
  277. room_jid_split_subdomain = room_jid_split_subdomain;
  278. internal_room_jid_match_rewrite = internal_room_jid_match_rewrite;
  279. update_presence_identity = update_presence_identity;
  280. http_get_with_retry = http_get_with_retry;
  281. };