Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

util.lib.lua 13KB

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