Skip to content

Commit

Permalink
fix(dns): do not panic when parse /etc/resolv.conf fail
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Jan 8, 2024
1 parent a642061 commit 6fc97a3
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions dns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

-- Referenced from https://github.com/openresty/lua-resty-dns/blob/master/lib/resty/dns/resolver.lua

local file = require 'eco.core.file'
local socket = require 'eco.socket'

local M = {
Expand Down Expand Up @@ -38,6 +39,10 @@ local function parse_resolvconf()
local nameservers = {}
local conf = {}

if not file.access('/etc/resolv.conf') then
return nil, 'not found "/etc/resolv.conf"'
end

for line in io.lines('/etc/resolv.conf') do
if line:match('search') then
local search = line:match('search%s+(.+)')
Expand Down Expand Up @@ -480,14 +485,17 @@ function M.query(qname, opts)
nameservers[#nameservers + 1] = { host, port, socket.is_ipv6_address(host) }
end

local resolvconf = parse_resolvconf()
local resolvconf, err = parse_resolvconf()
if not resolvconf then
return nil, err
end

for _, nameserver in ipairs(resolvconf.nameservers) do
nameservers[#nameservers + 1] = nameserver
end

if #nameservers < 1 then
error('not found valid nameservers')
return nil, 'not found valid nameservers'
end

if not qname:match('%.') and resolvconf.search then
Expand Down

0 comments on commit 6fc97a3

Please sign in to comment.