-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_srvinjection.lua
44 lines (38 loc) · 1.1 KB
/
mod_srvinjection.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module:set_global();
local adns = require "net.adns";
local map_config = module:get_option("srvinjection") or {};
local map = module:shared "s2s_map"
for host, mapping in pairs(map_config) do
if type(mapping) == "table" and type(mapping[1]) == "string" and (type(mapping[2]) == "number") then
local connecthost, connectport = mapping[1], mapping[2] or 5269;
map[host] = {{
srv = {
target = connecthost..".";
port = connectport;
priority = 1;
weight = 0;
};
}};
else
module:log("warn", "Ignoring invalid SRV injection for host '%s'", host);
map[host] = nil;
end
end
local original_lookup = adns.lookup;
function adns.lookup(handler, qname, qtype, qclass)
if qtype == "SRV" then
local host = qname:match("^_xmpp%-server%._tcp%.(.*)%.$");
local mapping = map[host] or map["*"];
if mapping then
handler(mapping);
return;
end
elseif qtype == "A" and (qname == "localhost." or qname == "127.0.0.1.") then
handler({{ a = "127.0.0.1" }});
return;
end
return original_lookup(handler, qname, qtype, qclass);
end
function module.unload()
adns.lookup = original_lookup;
end