comparison mod_turncredentials/mod_turncredentials.lua @ 1059:95ab35ef52ba

mod_turncredentials: XEP-0215 implementation for time-limited turn credentials
author Philipp Hancke <fippo@goodadvice.pages.de>
date Mon, 10 Jun 2013 15:07:00 +0100
parents
children 2da546139cb5
comparison
equal deleted inserted replaced
1058:1255de347dd4 1059:95ab35ef52ba
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 local st = require "util.stanza";
6 local hmac_sha1 = require "util.hmac".sha1;
7 local base64 = require "util.encodings".base64;
8 local os_time = os.time;
9 local secret = module:get_option("turncredentials_secret") or false;
10 local host = module:get_option("turncredentials_host") or false -- use ip addresses here to avoid further dns lookup latency
11 local port = module:get_option("turncredentials_port") or 3478
12 if not (secret and host) then
13 module:log("error", "turncredentials not configured");
14 return;
15 end
16
17 module:hook("iq/host/urn:xmpp:extdisco:1:services", function(event)
18 local origin, stanza = event.origin, event.stanza;
19 if stanza.attr.type ~= "get" or stanza.tags[1].name ~= "services" or origin.type ~= "c2s" then
20 return;
21 end
22 local now = os_time();
23 local userpart = tostring(now);
24 local nonce = base64.encode(hmac_sha1(secret, tostring(userpart), false));
25 origin.send(st.reply(stanza):tag("services", {xmlns = "urn:xmpp:extdisco:1"})
26 :tag("service", { type = "stun", host = host, port = port }):up()
27 :tag("service", { type = "turn", host = host, port = port, username = userpart, password = nonce }):up()
28 );
29 return true;
30 end);