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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 'foo.muc.example.com')
  21. local target_subdomain_pattern
  22. = "^"..escaped_muc_domain_prefix..".([^%.]+)%."..escaped_muc_domain_base;
  23. -- Utility function to split room JID to include room name and subdomain
  24. local function room_jid_split_subdomain(room_jid)
  25. local node, host, resource = jid.split(room_jid);
  26. local target_subdomain = host and host:match(target_subdomain_pattern);
  27. return node, host, resource, target_subdomain
  28. end
  29. --- Utility function to check and convert a room JID from
  30. -- virtual room1@muc.foo.example.com to real [foo]room1@muc.example.com
  31. -- @param room_jid the room jid to match and rewrite if needed
  32. -- @return returns room jid [foo]room1@muc.example.com when it has subdomain
  33. -- otherwise room1@muc.example.com(the room_jid value untouched)
  34. local function room_jid_match_rewrite(room_jid)
  35. local node, host, resource, target_subdomain = room_jid_split_subdomain(room_jid);
  36. if not target_subdomain then
  37. module:log("debug", "No need to rewrite out 'to' %s", room_jid);
  38. return room_jid;
  39. end
  40. -- Ok, rewrite room_jid address to new format
  41. local new_node, new_host, new_resource
  42. = "["..target_subdomain.."]"..node, muc_domain, resource;
  43. room_jid = jid.join(new_node, new_host, new_resource);
  44. module:log("debug", "Rewrote to %s", room_jid);
  45. return room_jid
  46. end
  47. local function internal_room_jid_match_rewrite(room_jid)
  48. local node, host, resource = jid.split(room_jid);
  49. if host ~= muc_domain or not node then
  50. module:log("debug", "No need to rewrite %s (not from the MUC host)", room_jid);
  51. return room_jid;
  52. end
  53. local target_subdomain, target_node = node:match("^%[([^%]]+)%](.+)$");
  54. if not (target_node and target_subdomain) then
  55. module:log("debug", "Not rewriting... unexpected node format: %s", node);
  56. return room_jid;
  57. end
  58. -- Ok, rewrite room_jid address to pretty format
  59. local new_node, new_host, new_resource = target_node, muc_domain_prefix..".".. target_subdomain.."."..muc_domain_base, resource;
  60. room_jid = jid.join(new_node, new_host, new_resource);
  61. module:log("debug", "Rewrote to %s", room_jid);
  62. return room_jid
  63. end
  64. --- Finds and returns room by its jid
  65. -- @param room_jid the room jid to search in the muc component
  66. -- @return returns room if found or nil
  67. function get_room_from_jid(room_jid)
  68. local _, host = jid.split(room_jid);
  69. local component = hosts[host];
  70. if component then
  71. local muc = component.modules.muc
  72. if muc and rawget(muc,"rooms") then
  73. -- We're running 0.9.x or 0.10 (old MUC API)
  74. return muc.rooms[room_jid];
  75. elseif muc and rawget(muc,"get_room_from_jid") then
  76. -- We're running >0.10 (new MUC API)
  77. return muc.get_room_from_jid(room_jid);
  78. else
  79. return
  80. end
  81. end
  82. end
  83. function async_handler_wrapper(event, handler)
  84. if not have_async then
  85. module:log("error", "requires a version of Prosody with util.async");
  86. return nil;
  87. end
  88. local runner = async.runner;
  89. -- Grab a local response so that we can send the http response when
  90. -- the handler is done.
  91. local response = event.response;
  92. local async_func = runner(
  93. function (event)
  94. local result = handler(event)
  95. -- If there is a status code in the result from the
  96. -- wrapped handler then add it to the response.
  97. if tonumber(result.status_code) ~= nil then
  98. response.status_code = result.status_code
  99. end
  100. -- If there are headers in the result from the
  101. -- wrapped handler then add them to the response.
  102. if result.headers ~= nil then
  103. response.headers = result.headers
  104. end
  105. -- Send the response to the waiting http client with
  106. -- or without the body from the wrapped handler.
  107. if result.body ~= nil then
  108. response:send(result.body)
  109. else
  110. response:send();
  111. end
  112. end
  113. )
  114. async_func:run(event)
  115. -- return true to keep the client http connection open.
  116. return true;
  117. end
  118. --- Updates presence stanza, by adding identity node
  119. -- @param stanza the presence stanza
  120. -- @param user the user to which presence we are updating identity
  121. -- @param group the group of the user to which presence we are updating identity
  122. -- @param creator_user the user who created the user which presence we
  123. -- are updating (this is the poltergeist case, where a user creates
  124. -- a poltergeist), optional.
  125. -- @param creator_group the group of the user who created the user which
  126. -- presence we are updating (this is the poltergeist case, where a user creates
  127. -- a poltergeist), optional.
  128. function update_presence_identity(
  129. stanza, user, group, creator_user, creator_group)
  130. -- First remove any 'identity' element if it already
  131. -- exists, so it cannot be spoofed by a client
  132. stanza:maptags(
  133. function(tag)
  134. for k, v in pairs(tag) do
  135. if k == "name" and v == "identity" then
  136. return nil
  137. end
  138. end
  139. return tag
  140. end
  141. )
  142. module:log("debug",
  143. "Presence after previous identity stripped: %s", tostring(stanza));
  144. stanza:tag("identity"):tag("user");
  145. for k, v in pairs(user) do
  146. stanza:tag(k):text(v):up();
  147. end
  148. stanza:up();
  149. -- Add the group information if it is present
  150. if group then
  151. stanza:tag("group"):text(group):up();
  152. end
  153. -- Add the creator user information if it is present
  154. if creator_user then
  155. stanza:tag("creator_user");
  156. for k, v in pairs(creator_user) do
  157. stanza:tag(k):text(v):up();
  158. end
  159. stanza:up();
  160. -- Add the creator group information if it is present
  161. if creator_group then
  162. stanza:tag("creator_group"):text(creator_group):up();
  163. end
  164. stanza:up();
  165. end
  166. module:log("debug",
  167. "Presence with identity inserted %s", tostring(stanza))
  168. end
  169. -- Utility function to check whether feature is present and enabled. Allow
  170. -- a feature if there are features present in the session(coming from
  171. -- the token) and the value of the feature is true.
  172. -- If features is not present in the token we skip feature detection and allow
  173. -- everything.
  174. function is_feature_allowed(session, feature)
  175. if (session.jitsi_meet_context_features == nil
  176. or session.jitsi_meet_context_features[feature] == "true") then
  177. return true;
  178. else
  179. return false;
  180. end
  181. end
  182. function starts_with(str, start)
  183. return str:sub(1, #start) == start
  184. end
  185. -- healthcheck rooms in jicofo starts with a string '__jicofo-health-check'
  186. function is_healthcheck_room(room_jid)
  187. if starts_with(room_jid, "__jicofo-health-check") then
  188. return true;
  189. end
  190. return false;
  191. end
  192. -- Utility function to make an http get request and
  193. -- retry @param retry number of times
  194. -- @param url endpoint to be called
  195. -- @param retry nr of retries, if retry is
  196. -- nil there will be no retries
  197. -- @returns result of the http call or nil if
  198. -- the external call failed after the last retry
  199. function http_get_with_retry(url, retry)
  200. local content, code;
  201. local wait, done = async.waiter();
  202. local function cb(content_, code_, response_, request_)
  203. code = code_;
  204. if code == 200 or code == 204 then
  205. module:log("debug", "External call was successful, content %s", content_);
  206. content = content_
  207. else
  208. module:log("warn", "Error on public key request: Code %s, Content %s",
  209. code_, content_);
  210. end
  211. done();
  212. end
  213. local function call_http()
  214. return http.request(url, {
  215. headers = http_headers or {},
  216. method = "GET"
  217. }, cb);
  218. end
  219. local request = call_http();
  220. local function cancel()
  221. -- TODO: This check is racey. Not likely to be a problem, but we should
  222. -- still stick a mutex on content / code at some point.
  223. if code == nil then
  224. -- no longer present in prosody 0.11, so check before calling
  225. if http.destroy_request ~= nil then
  226. http.destroy_request(request);
  227. end
  228. if retry == nil then
  229. module:log("debug", "External call failed and retry policy is not set");
  230. done();
  231. elseif retry ~= nil and retry < 1 then
  232. module:log("debug", "External call failed after retry")
  233. done();
  234. else
  235. module:log("debug", "External call failed, retry nr %s", retry)
  236. retry = retry - 1;
  237. request = call_http()
  238. return http_timeout;
  239. end
  240. end
  241. end
  242. timer.add_task(http_timeout, cancel);
  243. wait();
  244. if code == 200 or code == 204 then
  245. return content;
  246. end
  247. return nil;
  248. end
  249. return {
  250. is_feature_allowed = is_feature_allowed;
  251. is_healthcheck_room = is_healthcheck_room;
  252. get_room_from_jid = get_room_from_jid;
  253. async_handler_wrapper = async_handler_wrapper;
  254. room_jid_match_rewrite = room_jid_match_rewrite;
  255. room_jid_split_subdomain = room_jid_split_subdomain;
  256. internal_room_jid_match_rewrite = internal_room_jid_match_rewrite;
  257. update_presence_identity = update_presence_identity;
  258. http_get_with_retry = http_get_with_retry;
  259. };