annotate mod_srvinjection/mod_srvinjection.lua @ 1324:853a382c9bd6

mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
author Kim Alvefur <zash@zash.se>
date Fri, 28 Feb 2014 15:36:06 +0100
parents 69f7840923f5
children b21236b6b8d8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1324
853a382c9bd6 mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents: 1248
diff changeset
1 local s = require"util.serialization".new"oneline".serialize;
96
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2
662
b3d130e4b3ae mod_srvinjection: Use module:set_global()
Florian Zeitz <florob@babelmonkeys.de>
parents: 337
diff changeset
3 module:set_global();
96
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local adns = require "net.adns";
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6
1248
69f7840923f5 mod_srvinjection: Make the map a shared table so that other plugins can use/modify it
daurnimator <quae@daurnimator.com>
parents: 662
diff changeset
7 local map_config = module:get_option("srvinjection") or {};
69f7840923f5 mod_srvinjection: Make the map a shared table so that other plugins can use/modify it
daurnimator <quae@daurnimator.com>
parents: 662
diff changeset
8 local map = module:shared "s2s_map"
96
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9
1248
69f7840923f5 mod_srvinjection: Make the map a shared table so that other plugins can use/modify it
daurnimator <quae@daurnimator.com>
parents: 662
diff changeset
10 for host, mapping in pairs(map_config) do
96
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 if type(mapping) == "table" and type(mapping[1]) == "string" and (type(mapping[2]) == "number") then
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local connecthost, connectport = mapping[1], mapping[2] or 5269;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 map[host] = {{
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 srv = {
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 target = connecthost..".";
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 port = connectport;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 priority = 1;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 weight = 0;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 };
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 }};
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 else
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 module:log("warn", "Ignoring invalid SRV injection for host '%s'", host);
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 map[host] = nil;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 end
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 end
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 local original_lookup = adns.lookup;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 function adns.lookup(handler, qname, qtype, qclass)
1324
853a382c9bd6 mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents: 1248
diff changeset
29 module:log("debug", "adns.lookup(%s, %s, %s)", s(qname), s(qtype), s(qclass));
96
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 if qtype == "SRV" then
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 local host = qname:match("^_xmpp%-server%._tcp%.(.*)%.$");
1324
853a382c9bd6 mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents: 1248
diff changeset
32 module:log("debug", "qname:match(...) → %s", s(host));
853a382c9bd6 mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents: 1248
diff changeset
33 local mapping = map[host] or map["*"];
853a382c9bd6 mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
Kim Alvefur <zash@zash.se>
parents: 1248
diff changeset
34 module:log("debug", "map[%s] → %s", s(host), s(mapping));
337
beb5073b866a mod_srvinjection: Fix type in variable name.
Waqas Hussain <waqas20@gmail.com>
parents: 336
diff changeset
35 local mapping = map[host] or map["*"];
96
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 if mapping then
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 handler(mapping);
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 return;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 end
99
2d03350613c4 mod_srvinjection: Added support for resolving "localhost" and "127.0.0.1".
Waqas Hussain <waqas20@gmail.com>
parents: 96
diff changeset
40 elseif qtype == "A" and (qname == "localhost." or qname == "127.0.0.1.") then
2d03350613c4 mod_srvinjection: Added support for resolving "localhost" and "127.0.0.1".
Waqas Hussain <waqas20@gmail.com>
parents: 96
diff changeset
41 handler({{ a = "127.0.0.1" }});
2d03350613c4 mod_srvinjection: Added support for resolving "localhost" and "127.0.0.1".
Waqas Hussain <waqas20@gmail.com>
parents: 96
diff changeset
42 return;
96
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 end
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 return original_lookup(handler, qname, qtype, qclass);
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 end
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 function module.unload()
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 adns.lookup = original_lookup;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 end