Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. -- XEP-0215 implementation for time-limited turn credentials
  2. -- Copyright (C) 2012-2013 Philipp Hancke
  3. -- This file is MIT/X11 licensed.
  4.  
  5. --turncredentials_secret = "keepthissecret";
  6. --turncredentials = {
  7. -- { type = "stun", host = "8.8.8.8" },
  8. -- { type = "turn", host = "8.8.8.8", port = 3478 },
  9. -- { type = "turn", host = "8.8.8.8", port = 80, transport = "tcp" }
  10. --}
  11. -- for stun servers, host is required, port defaults to 3478
  12. -- for turn servers, host is required, port defaults to tcp,
  13. -- transport defaults to udp
  14.  
  15. local st = require "util.stanza";
  16. local hmac_sha1 = require "util.hashes".hmac_sha1;
  17. local base64 = require "util.encodings".base64;
  18. local os_time = os.time;
  19. local secret = module:get_option_string("turncredentials_secret");
  20. local ttl = module:get_option_number("turncredentials_ttl", 86400);
  21. local hosts = module:get_option("turncredentials") or {};
  22. if not (secret) then
  23. module:log("error", "turncredentials not configured");
  24. return;
  25. end
  26.  
  27. module:add_feature("urn:xmpp:extdisco:1");
  28.  
  29. module:hook_global("config-reloaded", function()
  30. module:log("debug", "config-reloaded")
  31. secret = module:get_option_string("turncredentials_secret");
  32. ttl = module:get_option_number("turncredentials_ttl", 86400);
  33. hosts = module:get_option("turncredentials") or {};
  34. end);
  35.  
  36. module:hook("iq-get/host/urn:xmpp:extdisco:1:services", function(event)
  37. local origin, stanza = event.origin, event.stanza;
  38. if origin.type ~= "c2s" then
  39. return;
  40. end
  41. local now = os_time() + ttl;
  42. local userpart = tostring(now);
  43. local nonce = base64.encode(hmac_sha1(secret, tostring(userpart), false));
  44. local reply = st.reply(stanza):tag("services", {xmlns = "urn:xmpp:extdisco:1"})
  45. for idx, item in pairs(hosts) do
  46. if item.type == "stun" or item.type == "stuns" then
  47. -- stun items need host and port (defaults to 3478)
  48. reply:tag("service", item):up();
  49. elseif item.type == "turn" or item.type == "turns" then
  50. -- turn items need host, port (defaults to 3478),
  51. -- transport (defaults to udp)
  52. -- username, password, ttl
  53. item.username = userpart;
  54. item.password = nonce;
  55. item.ttl = ttl;
  56. reply:tag("service", item):up();
  57. end
  58. end
  59. origin.send(reply);
  60. return true;
  61. end);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement