Annotate

mod_srvinjection/mod_srvinjection.lua @ 263:41f1cac40560

mod_smacks: Fixed to use the correct events API.
author Waqas Hussain <waqas20@gmail.com>
date Sat, 16 Oct 2010 07:49:30 +0500
parent 99:2d03350613c4
child 336:e4d5a1d3ff44
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
96
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 module.host = "*";
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 local adns = require "net.adns";
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 local map = module:get_option("srvinjection") or {};
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 for host, mapping in pairs(map) do
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 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
10 local connecthost, connectport = mapping[1], mapping[2] or 5269;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 map[host] = {{
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 srv = {
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 target = connecthost..".";
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 port = connectport;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 priority = 1;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 weight = 0;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 };
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 }};
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 else
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 module:log("warn", "Ignoring invalid SRV injection for host '%s'", host);
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 map[host] = nil;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 end
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 end
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local original_lookup = adns.lookup;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 function adns.lookup(handler, qname, qtype, qclass)
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 if qtype == "SRV" then
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 local host = qname:match("^_xmpp%-server%._tcp%.(.*)%.$");
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 local mapping = map[host];
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 if mapping then
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 handler(mapping);
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 return;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 end
99
2d03350613c4 mod_srvinjection: Added support for resolving "localhost" and "127.0.0.1".
Waqas Hussain <waqas20@gmail.com>
parents: 96
diff changeset
34 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
35 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
36 return;
96
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 end
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 return original_lookup(handler, qname, qtype, qclass);
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 end
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 function module.unload()
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 adns.lookup = original_lookup;
c1f4edf3bea7 mod_srvinjection: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 end