1 local adns
= require
"net.adns";
2 local inet_pton
= require
"util.net".pton
;
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 cb(unpack(next_target
, 1, 4));
23 local function ready()
25 if n
> 0 then return; end
26 self
.targets
= targets
;
30 local is_ip
= inet_pton(self
.hostname
);
33 cb(self
.conn_type
.."6", self
.hostname
, self
.port
, self
.extra
);
34 elseif #is_ip
== 4 then
35 cb(self
.conn_type
.."4", self
.hostname
, self
.port
, self
.extra
);
40 -- Resolve DNS to target list
41 local dns_resolver
= adns
.resolver();
42 dns_resolver
:lookup(function (answer
)
44 for _
, record
in ipairs(answer
) do
45 table.insert(targets
, { self
.conn_type
.."4", record
.a
, self
.port
, self
.extra
});
49 end, self
.hostname
, "A", "IN");
51 dns_resolver
:lookup(function (answer
)
53 for _
, record
in ipairs(answer
) do
54 table.insert(targets
, { self
.conn_type
.."6", record
.aaaa
, self
.port
, self
.extra
});
58 end, self
.hostname
, "AAAA", "IN");
61 local function new(hostname
, port
, conn_type
, extra
)
65 conn_type
= conn_type
or "tcp";