1 local adns
= require
"net.adns";
2 local basic
= require
"net.resolvers.basic";
3 local unpack
= table.unpack
or unpack
; -- luacheck: ignore 113
6 local resolver_mt
= { __index
= methods
};
8 -- Find the next target to connect to, and
10 function methods
:next(cb
)
12 if #self
.targets
== 0 then
16 local next_target
= table.remove(self
.targets
, 1);
17 self
.resolver
= basic
.new(unpack(next_target
, 1, 4));
18 self
.resolver
:next(function (...)
29 local function ready()
30 self
.targets
= targets
;
34 -- Resolve DNS to target list
35 local dns_resolver
= adns
.resolver();
36 dns_resolver
:lookup(function (answer
, err
)
37 if not answer
and not err
then
38 -- net.adns returns nil if there are zero records or nxdomain
43 if self
.extra
and self
.extra
.default_port
then
44 table.insert(targets
, { self
.hostname
, self
.extra
.default_port
, self
.conn_type
, self
.extra
});
50 if #answer
== 1 and answer
[1].srv
.target
== "." then -- No service here
55 table.sort(answer
, function (a
, b
) return a
.srv
.priority
< b
.srv
.priority
end);
56 for _
, record
in ipairs(answer
) do
57 table.insert(targets
, { record
.srv
.target
, record
.srv
.port
, self
.conn_type
, self
.extra
});
61 end, "_" .. self
.service
.. "._" .. self
.conn_type
.. "." .. self
.hostname
, "SRV", "IN");
64 local function new(hostname
, service
, conn_type
, extra
)
68 conn_type
= conn_type
or "tcp";