comparison mod_turncredentials/mod_turncredentials.lua @ 3642:2bbf655431be

mod_turncredentials: Add parallel implementation of XEP-0215 v0.7
author Kim Alvefur <zash@zash.se>
date Fri, 09 Aug 2019 18:59:35 +0200
parents deb5ece56c49
children 915c7bd5f754
comparison
equal deleted inserted replaced
3641:58b49d883f0c 3642:2bbf655431be
4 4
5 local st = require "util.stanza"; 5 local st = require "util.stanza";
6 local hmac_sha1 = require "util.hashes".hmac_sha1; 6 local hmac_sha1 = require "util.hashes".hmac_sha1;
7 local base64 = require "util.encodings".base64; 7 local base64 = require "util.encodings".base64;
8 local os_time = os.time; 8 local os_time = os.time;
9 local datetime = require "util.datetime".datetime;
9 local secret = module:get_option_string("turncredentials_secret"); 10 local secret = module:get_option_string("turncredentials_secret");
10 local host = module:get_option_string("turncredentials_host"); -- use ip addresses here to avoid further dns lookup latency 11 local host = module:get_option_string("turncredentials_host"); -- use ip addresses here to avoid further dns lookup latency
11 local port = module:get_option_number("turncredentials_port", 3478); 12 local port = module:get_option_number("turncredentials_port", 3478);
12 local ttl = module:get_option_number("turncredentials_ttl", 86400); 13 local ttl = module:get_option_number("turncredentials_ttl", 86400);
13 if not (secret and host) then 14 if not (secret and host) then
29 :tag("service", { type = "stun", host = host, port = ("%d"):format(port) }):up() 30 :tag("service", { type = "stun", host = host, port = ("%d"):format(port) }):up()
30 :tag("service", { type = "turn", host = host, port = ("%d"):format(port), username = userpart, password = nonce, ttl = ("%d"):format(ttl) }):up() 31 :tag("service", { type = "turn", host = host, port = ("%d"):format(port), username = userpart, password = nonce, ttl = ("%d"):format(ttl) }):up()
31 ); 32 );
32 return true; 33 return true;
33 end); 34 end);
35
36 module:add_feature("urn:xmpp:extdisco:2");
37
38 module:hook("iq-get/host/urn:xmpp:extdisco:2:services", function(event)
39 local origin, stanza = event.origin, event.stanza;
40 if origin.type ~= "c2s" then
41 return;
42 end
43 local now = os_time() + ttl;
44 local userpart = tostring(now);
45 local nonce = base64.encode(hmac_sha1(secret, tostring(userpart), false));
46 origin.send(st.reply(stanza):tag("services", {xmlns = "urn:xmpp:extdisco:2"})
47 :tag("service", { type = "stun", host = host, port = ("%d"):format(port) }):up()
48 :tag("service", { type = "turn", host = host, port = ("%d"):format(port), username = userpart, password = nonce, expires = datetime(ttl), restricted = "1" }):up()
49 );
50 return true;
51 end);