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

util.lib.lua 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. local http_server = require "net.http.server";
  2. local jid = require "util.jid";
  3. local st = require 'util.stanza';
  4. local timer = require "util.timer";
  5. local http = require "net.http";
  6. local cache = require "util.cache";
  7. local array = require "util.array";
  8. local is_set = require 'util.set'.is_set;
  9. local http_timeout = 30;
  10. local have_async, async = pcall(require, "util.async");
  11. local http_headers = {
  12. ["User-Agent"] = "Prosody ("..prosody.version.."; "..prosody.platform..")"
  13. };
  14. local muc_domain_prefix = module:get_option_string("muc_mapper_domain_prefix", "conference");
  15. -- defaults to module.host, the module that uses the utility
  16. local muc_domain_base = module:get_option_string("muc_mapper_domain_base", module.host);
  17. -- The "real" MUC domain that we are proxying to
  18. local muc_domain = module:get_option_string("muc_mapper_domain", muc_domain_prefix.."."..muc_domain_base);
  19. local escaped_muc_domain_base = muc_domain_base:gsub("%p", "%%%1");
  20. local escaped_muc_domain_prefix = muc_domain_prefix:gsub("%p", "%%%1");
  21. -- The pattern used to extract the target subdomain
  22. -- (e.g. extract 'foo' from 'conference.foo.example.com')
  23. local target_subdomain_pattern = "^"..escaped_muc_domain_prefix..".([^%.]+)%."..escaped_muc_domain_base;
  24. -- table to store all incoming iqs without roomname in it, like discoinfo to the muc component
  25. local roomless_iqs = {};
  26. local OUTBOUND_SIP_JIBRI_PREFIXES = { 'outbound-sip-jibri@', 'sipjibriouta@', 'sipjibrioutb@' };
  27. local INBOUND_SIP_JIBRI_PREFIXES = { 'inbound-sip-jibri@', 'sipjibriina@', 'sipjibriina@' };
  28. local RECORDER_PREFIXES = module:get_option_inherited_set('recorder_prefixes', { 'recorder@recorder.', 'jibria@recorder.', 'jibrib@recorder.' });
  29. local split_subdomain_cache = cache.new(1000);
  30. local extract_subdomain_cache = cache.new(1000);
  31. local internal_room_jid_cache = cache.new(1000);
  32. local moderated_subdomains = module:get_option_set("allowners_moderated_subdomains", {})
  33. local moderated_rooms = module:get_option_set("allowners_moderated_rooms", {})
  34. -- Utility function to split room JID to include room name and subdomain
  35. -- (e.g. from room1@conference.foo.example.com/res returns (room1, example.com, res, foo))
  36. local function room_jid_split_subdomain(room_jid)
  37. local ret = split_subdomain_cache:get(room_jid);
  38. if ret then
  39. return ret.node, ret.host, ret.resource, ret.subdomain;
  40. end
  41. local node, host, resource = jid.split(room_jid);
  42. local target_subdomain = host and host:match(target_subdomain_pattern);
  43. local cache_value = {node=node, host=host, resource=resource, subdomain=target_subdomain};
  44. split_subdomain_cache:set(room_jid, cache_value);
  45. return node, host, resource, target_subdomain;
  46. end
  47. --- Utility function to check and convert a room JID from
  48. --- virtual room1@conference.foo.example.com to real [foo]room1@conference.example.com
  49. -- @param room_jid the room jid to match and rewrite if needed
  50. -- @param stanza the stanza
  51. -- @return returns room jid [foo]room1@conference.example.com when it has subdomain
  52. -- otherwise room1@conference.example.com(the room_jid value untouched)
  53. local function room_jid_match_rewrite(room_jid, stanza)
  54. local node, _, resource, target_subdomain = room_jid_split_subdomain(room_jid);
  55. if not target_subdomain then
  56. -- module:log("debug", "No need to rewrite out 'to' %s", room_jid);
  57. return room_jid;
  58. end
  59. -- Ok, rewrite room_jid address to new format
  60. local new_node, new_host, new_resource;
  61. if node then
  62. new_node, new_host, new_resource = "["..target_subdomain.."]"..node, muc_domain, resource;
  63. else
  64. -- module:log("debug", "No room name provided so rewriting only host 'to' %s", room_jid);
  65. new_host, new_resource = muc_domain, resource;
  66. if (stanza and stanza.attr and stanza.attr.id) then
  67. roomless_iqs[stanza.attr.id] = stanza.attr.to;
  68. end
  69. end
  70. return jid.join(new_node, new_host, new_resource);
  71. end
  72. -- Utility function to check and convert a room JID from real [foo]room1@muc.example.com to virtual room1@muc.foo.example.com
  73. local function internal_room_jid_match_rewrite(room_jid, stanza)
  74. -- first check for roomless_iqs
  75. if (stanza and stanza.attr and stanza.attr.id and roomless_iqs[stanza.attr.id]) then
  76. local result = roomless_iqs[stanza.attr.id];
  77. roomless_iqs[stanza.attr.id] = nil;
  78. return result;
  79. end
  80. local ret = internal_room_jid_cache:get(room_jid);
  81. if ret then
  82. return ret;
  83. end
  84. local node, host, resource = jid.split(room_jid);
  85. if host ~= muc_domain or not node then
  86. -- module:log("debug", "No need to rewrite %s (not from the MUC host)", room_jid);
  87. internal_room_jid_cache:set(room_jid, room_jid);
  88. return room_jid;
  89. end
  90. local target_subdomain, target_node = extract_subdomain(node);
  91. if not (target_node and target_subdomain) then
  92. -- module:log("debug", "Not rewriting... unexpected node format: %s", node);
  93. internal_room_jid_cache:set(room_jid, room_jid);
  94. return room_jid;
  95. end
  96. -- Ok, rewrite room_jid address to pretty format
  97. ret = jid.join(target_node, muc_domain_prefix..".".. target_subdomain.."."..muc_domain_base, resource);
  98. internal_room_jid_cache:set(room_jid, ret);
  99. return ret;
  100. end
  101. --- Finds and returns room by its jid
  102. -- @param room_jid the room jid to search in the muc component
  103. -- @return returns room if found or nil
  104. function get_room_from_jid(room_jid)
  105. local _, host = jid.split(room_jid);
  106. local component = hosts[host];
  107. if component then
  108. local muc = component.modules.muc
  109. if muc and rawget(muc,"rooms") then
  110. -- We're running 0.9.x or 0.10 (old MUC API)
  111. return muc.rooms[room_jid];
  112. elseif muc and rawget(muc,"get_room_from_jid") then
  113. -- We're running >0.10 (new MUC API)
  114. return muc.get_room_from_jid(room_jid);
  115. else
  116. return
  117. end
  118. end
  119. end
  120. -- Returns the room if available, work and in multidomain mode
  121. -- @param room_name the name of the room
  122. -- @param group name of the group (optional)
  123. -- @return returns room if found or nil
  124. function get_room_by_name_and_subdomain(room_name, subdomain)
  125. local room_address;
  126. -- if there is a subdomain we are in multidomain mode and that subdomain is not our main host
  127. if subdomain and subdomain ~= "" and subdomain ~= muc_domain_base then
  128. room_address = jid.join("["..subdomain.."]"..room_name, muc_domain);
  129. else
  130. room_address = jid.join(room_name, muc_domain);
  131. end
  132. return get_room_from_jid(room_address);
  133. end
  134. function async_handler_wrapper(event, handler)
  135. if not have_async then
  136. module:log("error", "requires a version of Prosody with util.async");
  137. return nil;
  138. end
  139. local runner = async.runner;
  140. -- Grab a local response so that we can send the http response when
  141. -- the handler is done.
  142. local response = event.response;
  143. local async_func = runner(
  144. function (event)
  145. local result = handler(event)
  146. -- If there is a status code in the result from the
  147. -- wrapped handler then add it to the response.
  148. if tonumber(result.status_code) ~= nil then
  149. response.status_code = result.status_code
  150. end
  151. -- If there are headers in the result from the
  152. -- wrapped handler then add them to the response.
  153. if result.headers ~= nil then
  154. response.headers = result.headers
  155. end
  156. -- Send the response to the waiting http client with
  157. -- or without the body from the wrapped handler.
  158. if result.body ~= nil then
  159. response:send(result.body)
  160. else
  161. response:send();
  162. end
  163. end
  164. )
  165. async_func:run(event)
  166. -- return true to keep the client http connection open.
  167. return true;
  168. end
  169. --- Updates presence stanza, by adding identity node
  170. -- @param stanza the presence stanza
  171. -- @param user the user to which presence we are updating identity
  172. -- @param group the group of the user to which presence we are updating identity
  173. -- @param creator_user the user who created the user which presence we
  174. -- are updating (this is the poltergeist case, where a user creates
  175. -- a poltergeist), optional.
  176. -- @param creator_group the group of the user who created the user which
  177. -- presence we are updating (this is the poltergeist case, where a user creates
  178. -- a poltergeist), optional.
  179. function update_presence_identity(
  180. stanza, user, group, creator_user, creator_group)
  181. -- First remove any 'identity' element if it already
  182. -- exists, so it cannot be spoofed by a client
  183. stanza:maptags(
  184. function(tag)
  185. for k, v in pairs(tag) do
  186. if k == "name" and v == "identity" then
  187. return nil
  188. end
  189. end
  190. return tag
  191. end
  192. )
  193. stanza:tag("identity"):tag("user");
  194. for k, v in pairs(user) do
  195. v = tostring(v)
  196. stanza:tag(k):text(v):up();
  197. end
  198. stanza:up();
  199. -- Add the group information if it is present
  200. if group then
  201. stanza:tag("group"):text(group):up();
  202. end
  203. -- Add the creator user information if it is present
  204. if creator_user then
  205. stanza:tag("creator_user");
  206. for k, v in pairs(creator_user) do
  207. stanza:tag(k):text(v):up();
  208. end
  209. stanza:up();
  210. -- Add the creator group information if it is present
  211. if creator_group then
  212. stanza:tag("creator_group"):text(creator_group):up();
  213. end
  214. end
  215. stanza:up(); -- Close identity tag
  216. end
  217. -- Utility function to check whether feature is present and enabled. Allow
  218. -- a feature if there are features present in the session(coming from
  219. -- the token) and the value of the feature is true.
  220. -- If features are missing but we have granted_features check that
  221. -- if features are missing from the token we check whether it is moderator
  222. function is_feature_allowed(ft, features, granted_features, is_moderator)
  223. if features then
  224. return features[ft] == "true" or features[ft] == true;
  225. elseif granted_features then
  226. return granted_features[ft] == "true" or granted_features[ft] == true;
  227. else
  228. return is_moderator;
  229. end
  230. end
  231. --- Extracts the subdomain and room name from internal jid node [foo]room1
  232. -- @return subdomain(optional, if extracted or nil), the room name
  233. function extract_subdomain(room_node)
  234. local ret = extract_subdomain_cache:get(room_node);
  235. if ret then
  236. return ret.subdomain, ret.room;
  237. end
  238. local subdomain, room_name = room_node:match("^%[([^%]]+)%](.+)$");
  239. local cache_value = {subdomain=subdomain, room=room_name};
  240. extract_subdomain_cache:set(room_node, cache_value);
  241. return subdomain, room_name;
  242. end
  243. function starts_with(str, start)
  244. if not str then
  245. return false;
  246. end
  247. return str:sub(1, #start) == start
  248. end
  249. function starts_with_one_of(str, prefixes)
  250. if not str or not prefixes then
  251. return false;
  252. end
  253. if is_set(prefixes) then
  254. -- set is a table with keys and value of true
  255. for k, _ in prefixes:items() do
  256. if starts_with(str, k) then
  257. return k;
  258. end
  259. end
  260. else
  261. for _, v in pairs(prefixes) do
  262. if starts_with(str, v) then
  263. return v;
  264. end
  265. end
  266. end
  267. return false
  268. end
  269. function ends_with(str, ending)
  270. return ending == "" or str:sub(-#ending) == ending
  271. end
  272. -- healthcheck rooms in jicofo starts with a string '__jicofo-health-check'
  273. function is_healthcheck_room(room_jid)
  274. return starts_with(room_jid, "__jicofo-health-check");
  275. end
  276. --- Utility function to make an http get request and
  277. --- retry @param retry number of times
  278. -- @param url endpoint to be called
  279. -- @param retry nr of retries, if retry is
  280. -- @param auth_token value to be passed as auth Bearer
  281. -- nil there will be no retries
  282. -- @returns result of the http call or nil if
  283. -- the external call failed after the last retry
  284. function http_get_with_retry(url, retry, auth_token)
  285. local content, code, cache_for;
  286. local timeout_occurred;
  287. local wait, done = async.waiter();
  288. local request_headers = http_headers or {}
  289. if auth_token ~= nil then
  290. request_headers['Authorization'] = 'Bearer ' .. auth_token
  291. end
  292. local function cb(content_, code_, response_, request_)
  293. if timeout_occurred == nil then
  294. code = code_;
  295. if code == 200 or code == 204 then
  296. -- module:log("debug", "External call was successful, content %s", content_);
  297. content = content_;
  298. -- if there is cache-control header, let's return the max-age value
  299. if response_ and response_.headers and response_.headers['cache-control'] then
  300. local vals = {};
  301. for k, v in response_.headers['cache-control']:gmatch('(%w+)=(%w+)') do
  302. vals[k] = v;
  303. end
  304. -- max-age=123 will be parsed by the regex ^ to age=123
  305. cache_for = vals.age;
  306. end
  307. else
  308. module:log("warn", "Error on GET request: Code %s, Content %s",
  309. code_, content_);
  310. end
  311. done();
  312. else
  313. module:log("warn", "External call reply delivered after timeout from: %s", url);
  314. end
  315. end
  316. local function call_http()
  317. return http.request(url, {
  318. headers = request_headers,
  319. method = "GET"
  320. }, cb);
  321. end
  322. local request = call_http();
  323. local function cancel()
  324. -- TODO: This check is racey. Not likely to be a problem, but we should
  325. -- still stick a mutex on content / code at some point.
  326. if code == nil then
  327. timeout_occurred = true;
  328. module:log("warn", "Timeout %s seconds making the external call to: %s", http_timeout, url);
  329. -- no longer present in prosody 0.11, so check before calling
  330. if http.destroy_request ~= nil then
  331. http.destroy_request(request);
  332. end
  333. if retry == nil then
  334. module:log("debug", "External call failed and retry policy is not set");
  335. done();
  336. elseif retry ~= nil and retry < 1 then
  337. module:log("debug", "External call failed after retry")
  338. done();
  339. else
  340. module:log("debug", "External call failed, retry nr %s", retry)
  341. retry = retry - 1;
  342. request = call_http()
  343. return http_timeout;
  344. end
  345. end
  346. end
  347. timer.add_task(http_timeout, cancel);
  348. wait();
  349. return content, code, cache_for;
  350. end
  351. -- Checks whether there is status in the <x node
  352. -- @param muc_x the <x element from presence
  353. -- @param status checks for this status
  354. -- @returns true if the status is found, false otherwise or if no muc_x is provided.
  355. function presence_check_status(muc_x, status)
  356. if not muc_x then
  357. return false;
  358. end
  359. for statusNode in muc_x:childtags('status') do
  360. if statusNode.attr.code == status then
  361. return true;
  362. end
  363. end
  364. return false;
  365. end
  366. -- Retrieves the focus from the room and cache it in the room object
  367. -- @param room The room name for which to find the occupant
  368. local function get_focus_occupant(room)
  369. return room:get_occupant_by_nick(room.jid..'/focus');
  370. end
  371. -- Checks whether the jid is moderated, the room name is in moderated_rooms
  372. -- or if the subdomain is in the moderated_subdomains
  373. -- @return returns on of the:
  374. -- -> false
  375. -- -> true, room_name, subdomain
  376. -- -> true, room_name, nil (if no subdomain is used for the room)
  377. function is_moderated(room_jid)
  378. if moderated_subdomains:empty() and moderated_rooms:empty() then
  379. return false;
  380. end
  381. local room_node = jid.node(room_jid);
  382. -- parses bare room address, for multidomain expected format is:
  383. -- [subdomain]roomName@conference.domain
  384. local target_subdomain, target_room_name = extract_subdomain(room_node);
  385. if target_subdomain then
  386. if moderated_subdomains:contains(target_subdomain) then
  387. return true, target_room_name, target_subdomain;
  388. end
  389. elseif moderated_rooms:contains(room_node) then
  390. return true, room_node, nil;
  391. end
  392. return false;
  393. end
  394. -- check if the room tenant starts with vpaas-magic-cookie-
  395. -- @param room the room to check
  396. function is_vpaas(room)
  397. if not room then
  398. return false;
  399. end
  400. -- stored check in room object if it exist
  401. if room.is_vpaas ~= nil then
  402. return room.is_vpaas;
  403. end
  404. room.is_vpaas = false;
  405. local node, host = jid.split(room.jid);
  406. if host ~= muc_domain or not node then
  407. return false;
  408. end
  409. local tenant, conference_name = node:match('^%[([^%]]+)%](.+)$');
  410. if not (tenant and conference_name) then
  411. return false;
  412. end
  413. if not starts_with(tenant, 'vpaas-magic-cookie-') then
  414. return false;
  415. end
  416. room.is_vpaas = true;
  417. return true;
  418. end
  419. -- Returns the initiator extension if the stanza is coming from a sip jigasi
  420. function is_sip_jigasi(stanza)
  421. if not stanza then
  422. return false;
  423. end
  424. return stanza:get_child('initiator', 'http://jitsi.org/protocol/jigasi');
  425. end
  426. function is_transcriber_jigasi(stanza)
  427. if not stanza then
  428. return false;
  429. end
  430. local features = stanza:get_child('features');
  431. if not features then
  432. return false;
  433. end
  434. for i = 1, #features do
  435. local feature = features[i];
  436. if feature.attr and feature.attr.var and feature.attr.var == 'http://jitsi.org/protocol/transcriber' then
  437. return true;
  438. end
  439. end
  440. return false;
  441. end
  442. function get_sip_jibri_email_prefix(email)
  443. if not email then
  444. return nil;
  445. elseif starts_with_one_of(email, INBOUND_SIP_JIBRI_PREFIXES) then
  446. return starts_with_one_of(email, INBOUND_SIP_JIBRI_PREFIXES);
  447. elseif starts_with_one_of(email, OUTBOUND_SIP_JIBRI_PREFIXES) then
  448. return starts_with_one_of(email, OUTBOUND_SIP_JIBRI_PREFIXES);
  449. else
  450. return nil;
  451. end
  452. end
  453. function is_sip_jibri_join(stanza)
  454. if not stanza then
  455. return false;
  456. end
  457. local features = stanza:get_child('features');
  458. local email = stanza:get_child_text('email');
  459. if not features or not email then
  460. return false;
  461. end
  462. for i = 1, #features do
  463. local feature = features[i];
  464. if feature.attr and feature.attr.var and feature.attr.var == "http://jitsi.org/protocol/jibri" then
  465. if get_sip_jibri_email_prefix(email) then
  466. module:log("debug", "Occupant with email %s is a sip jibri ", email);
  467. return true;
  468. end
  469. end
  470. end
  471. return false
  472. end
  473. function is_jibri(occupant)
  474. return starts_with_one_of(type(occupant) == "string" and occupant or occupant.jid, RECORDER_PREFIXES)
  475. end
  476. -- process a host module directly if loaded or hooks to wait for its load
  477. function process_host_module(name, callback)
  478. local function process_host(host)
  479. if host == name then
  480. callback(module:context(host), host);
  481. end
  482. end
  483. if prosody.hosts[name] == nil then
  484. module:log('info', 'No host/component found, will wait for it: %s', name)
  485. -- when a host or component is added
  486. prosody.events.add_handler('host-activated', process_host);
  487. else
  488. process_host(name);
  489. end
  490. end
  491. function table_shallow_copy(t)
  492. local t2 = {}
  493. for k, v in pairs(t) do
  494. t2[k] = v
  495. end
  496. return t2
  497. end
  498. -- Splits a string using delimiter
  499. function split_string(str, delimiter)
  500. str = str .. delimiter;
  501. local result = array();
  502. for w in str:gmatch("(.-)" .. delimiter) do
  503. result:push(w);
  504. end
  505. return result;
  506. end
  507. -- send iq result that the iq was received and will be processed
  508. function respond_iq_result(origin, stanza)
  509. -- respond with successful receiving the iq
  510. origin.send(st.iq({
  511. type = 'result';
  512. from = stanza.attr.to;
  513. to = stanza.attr.from;
  514. id = stanza.attr.id
  515. }));
  516. end
  517. -- Note: http_server.get_request_from_conn() was added in Prosody 0.12.3,
  518. -- this code provides backwards compatibility with older versions
  519. local get_request_from_conn = http_server.get_request_from_conn or function (conn)
  520. local response = conn and conn._http_open_response;
  521. return response and response.request or nil;
  522. end;
  523. -- Discover real remote IP of a session
  524. function get_ip(session)
  525. local request = get_request_from_conn(session.conn);
  526. return request and request.ip or session.ip;
  527. end
  528. return {
  529. OUTBOUND_SIP_JIBRI_PREFIXES = OUTBOUND_SIP_JIBRI_PREFIXES;
  530. INBOUND_SIP_JIBRI_PREFIXES = INBOUND_SIP_JIBRI_PREFIXES;
  531. RECORDER_PREFIXES = RECORDER_PREFIXES;
  532. extract_subdomain = extract_subdomain;
  533. is_feature_allowed = is_feature_allowed;
  534. is_jibri = is_jibri;
  535. is_healthcheck_room = is_healthcheck_room;
  536. is_moderated = is_moderated;
  537. is_sip_jibri_join = is_sip_jibri_join;
  538. is_sip_jigasi = is_sip_jigasi;
  539. is_transcriber_jigasi = is_transcriber_jigasi;
  540. is_vpaas = is_vpaas;
  541. get_focus_occupant = get_focus_occupant;
  542. get_ip = get_ip;
  543. get_room_from_jid = get_room_from_jid;
  544. get_room_by_name_and_subdomain = get_room_by_name_and_subdomain;
  545. get_sip_jibri_email_prefix = get_sip_jibri_email_prefix;
  546. async_handler_wrapper = async_handler_wrapper;
  547. presence_check_status = presence_check_status;
  548. process_host_module = process_host_module;
  549. respond_iq_result = respond_iq_result;
  550. room_jid_match_rewrite = room_jid_match_rewrite;
  551. room_jid_split_subdomain = room_jid_split_subdomain;
  552. internal_room_jid_match_rewrite = internal_room_jid_match_rewrite;
  553. update_presence_identity = update_presence_identity;
  554. http_get_with_retry = http_get_with_retry;
  555. ends_with = ends_with;
  556. split_string = split_string;
  557. starts_with = starts_with;
  558. starts_with_one_of = starts_with_one_of;
  559. table_shallow_copy = table_shallow_copy;
  560. };