Skip to content

Commit d499f59

Browse files
committed
feat(dns): add support lookup from /etc/hosts
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
1 parent a914fb1 commit d499f59

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

dns.lua

+39
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,40 @@ local function query(s, id, req, nameserver)
438438
return parse_response(data, id)
439439
end
440440

441+
local function name_from_hosts(qname, opts)
442+
local typ = opts.type or M.TYPE_A
443+
444+
for line in io.lines('/etc/hosts') do
445+
if line:sub(1, 1) ~= '#' and line ~= '' then
446+
local fields = {}
447+
448+
for field in line:gmatch('%S+') do
449+
fields[#fields + 1] = field
450+
end
451+
452+
local address = fields[1]
453+
local address_type
454+
455+
if socket.is_ipv4_address(address) then
456+
address_type = M.TYPE_A
457+
elseif socket.is_ipv6_address(address) then
458+
address_type = M.TYPE_AAAA
459+
end
460+
461+
if address_type == typ and #fields > 1 then
462+
for i = 2, #fields do
463+
if fields[i] == qname then
464+
return {{
465+
type = typ,
466+
address = address
467+
}}
468+
end
469+
end
470+
end
471+
end
472+
end
473+
end
474+
441475
--[[
442476
opts is an optional Table that supports the following fields:
443477
type: The current resource record type, possible values are 1 (TYPE_A), 5 (TYPE_CNAME),
@@ -470,6 +504,11 @@ function M.query(qname, opts)
470504

471505
opts = opts or {}
472506

507+
local res = name_from_hosts(qname, opts)
508+
if res then
509+
return res
510+
end
511+
473512
local nameservers = {}
474513

475514
for _, nameserver in ipairs(opts.nameservers or {}) do

0 commit comments

Comments
 (0)