in kong/kong/resty/dns/client.lua [463:650]
_M.init = function(options)
log(DEBUG, PREFIX, "(re)configuring dns client")
local resolv, hosts, err
options = options or {}
staleTtl = options.staleTtl or 4
log(DEBUG, PREFIX, "staleTtl = ", staleTtl)
cacheSize = options.cacheSize or 10000
noSynchronisation = options.noSynchronisation
log(DEBUG, PREFIX, "noSynchronisation = ", tostring(noSynchronisation))
dnscache = lrucache.new(cacheSize)
defined_hosts = {}
local order = options.order or orderValids
typeOrder = {}
local ip_preference
for i,v in ipairs(order) do
local t = v:upper()
if not ip_preference and (t == "A" or t == "AAAA") then
ip_preference = t
end
assert(orderValids[t], "Invalid dns record type in order array; "..tostring(v))
typeOrder[i] = _M["TYPE_"..t]
end
assert(#typeOrder > 0, "Invalid order list; cannot be empty")
log(DEBUG, PREFIX, "query order = ", table_concat(order,", "))
local hostsfile = options.hosts or utils.DEFAULT_HOSTS
if ((type(hostsfile) == "string") and (fileexists(hostsfile)) or
(type(hostsfile) == "table")) then
hosts, err = utils.parseHosts(hostsfile)
if not hosts then return hosts, err end
else
log(WARN, PREFIX, "Hosts file not found: "..tostring(hostsfile))
hosts = {}
end
if not hosts.localhost then
hosts.localhost = {
ipv4 = "127.0.0.1",
ipv6 = "[::1]",
}
end
local ttl = 10*365*24*60*60
for name, address in pairs(hosts) do
name = string_lower(name)
if address.ipv4 then
cacheinsert({{
name = name,
address = address.ipv4,
type = _M.TYPE_A,
class = 1,
ttl = ttl,
}})
defined_hosts[name..":".._M.TYPE_A] = true
cachesetsuccess(name, _M.TYPE_A)
log(DEBUG, PREFIX, "adding A-record from 'hosts' file: ",name, " = ", address.ipv4)
end
if address.ipv6 then
cacheinsert({{
name = name,
address = address.ipv6,
type = _M.TYPE_AAAA,
class = 1,
ttl = ttl,
}})
defined_hosts[name..":".._M.TYPE_AAAA] = true
if ip_preference == "AAAA" or not cachegetsuccess(name) then
cachesetsuccess(name, _M.TYPE_AAAA)
end
log(DEBUG, PREFIX, "adding AAAA-record from 'hosts' file: ",name, " = ", address.ipv6)
end
end
validTtl = options.validTtl
log(DEBUG, PREFIX, "validTtl = ", tostring(validTtl))
local resolvconffile = options.resolvConf or utils.DEFAULT_RESOLV_CONF
if ((type(resolvconffile) == "string") and (fileexists(resolvconffile)) or
(type(resolvconffile) == "table")) then
resolv, err = utils.applyEnv(utils.parseResolvConf(resolvconffile))
if not resolv then return resolv, err end
else
log(WARN, PREFIX, "Resolv.conf file not found: "..tostring(resolvconffile))
resolv = {}
end
if not resolv.options then resolv.options = {} end
if #(options.nameservers or {}) == 0 and resolv.nameserver then
options.nameservers = {}
for _, address in ipairs(resolv.nameserver) do
local ip, port, t = utils.parseHostname(address)
if t == "ipv6" and not options.enable_ipv6 then
log(DEBUG, PREFIX, "skipping IPv6 nameserver ", port and (ip..":"..port) or ip)
elseif t == "ipv6" and ip:find([[%]], nil, true) then
log(DEBUG, PREFIX, "skipping IPv6 nameserver (scope not supported) ", port and (ip..":"..port) or ip)
else
if port then
options.nameservers[#options.nameservers + 1] = { ip, port }
else
options.nameservers[#options.nameservers + 1] = ip
end
end
end
end
options.nameservers = options.nameservers or {}
if #options.nameservers == 0 then
log(WARN, PREFIX, "Invalid configuration, no valid nameservers found")
else
for _, r in ipairs(options.nameservers) do
log(DEBUG, PREFIX, "nameserver ", type(r) == "table" and (r[1]..":"..r[2]) or r)
end
end
options.retrans = options.retrans or resolv.options.attempts or 5
log(DEBUG, PREFIX, "attempts = ", options.retrans)
if options.no_random == nil then
options.no_random = not resolv.options.rotate
else
options.no_random = not not options.no_random
end
log(DEBUG, PREFIX, "no_random = ", options.no_random)
if not options.timeout then
if resolv.options.timeout then
options.timeout = resolv.options.timeout * 1000
else
options.timeout = 2000
end
end
log(DEBUG, PREFIX, "timeout = ", options.timeout, " ms")
options.ndots = options.ndots or resolv.options.ndots or 1
log(DEBUG, PREFIX, "ndots = ", options.ndots)
options.search = options.search or resolv.search or { resolv.domain }
log(DEBUG, PREFIX, "search = ", table_concat(options.search,", "))
for i = #options.search, 1, -1 do
if options.search[i] == "." then
table_remove(options.search, i)
end
end
badTtl = options.badTtl or 1
log(DEBUG, PREFIX, "badTtl = ", badTtl, " s")
emptyTtl = options.emptyTtl or 30
log(DEBUG, PREFIX, "emptyTtl = ", emptyTtl, " s")
config = options
poolMaxRetry = 1
poolMaxWait = options.timeout / 1000 * options.retrans
return true
end