mod_muc_webchat_url: Fix default url
[prosody-modules.git] / mod_srvinjection / mod_srvinjection.lua
bloba6881d714abd5e4949f3a32cb06f756f021f88b8
2 module:set_global();
4 local adns = require "net.adns";
6 local map_config = module:get_option("srvinjection") or {};
7 local map = module:shared "s2s_map"
9 for host, mapping in pairs(map_config) do
10 if type(mapping) == "table" and type(mapping[1]) == "string" and (type(mapping[2]) == "number") then
11 local connecthost, connectport = mapping[1], mapping[2] or 5269;
12 map[host] = {{
13 srv = {
14 target = connecthost..".";
15 port = connectport;
16 priority = 1;
17 weight = 0;
19 }};
20 else
21 module:log("warn", "Ignoring invalid SRV injection for host '%s'", host);
22 map[host] = nil;
23 end
24 end
26 local original_lookup = adns.lookup;
27 function adns.lookup(handler, qname, qtype, qclass)
28 if qtype == "SRV" then
29 local host = qname:match("^_xmpp%-server%._tcp%.(.*)%.$");
30 local mapping = map[host] or map["*"];
31 if mapping then
32 handler(mapping);
33 return;
34 end
35 elseif qtype == "A" then
36 if (qname == "localhost." or qname == "127.0.0.1.") then
37 handler({{ a = "127.0.0.1" }});
38 return;
39 end
40 local ip = qname:match("^(%d+.%d+.%d+.%d+).$");
41 if ip then
42 handler({{ a = ip }});
43 return;
44 end
45 end
46 return original_lookup(handler, qname, qtype, qclass);
47 end
49 function module.unload()
50 adns.lookup = original_lookup;
51 end