Add support for IPV6 resolvers
This commit is contained in:
parent
080bed8e88
commit
6c1a7f1efd
2 changed files with 42 additions and 19 deletions
|
@ -19,19 +19,19 @@ describe("resolve", function()
|
||||||
|
|
||||||
dns_helper.mock_dns_query(nil, "oops!")
|
dns_helper.mock_dns_query(nil, "oops!")
|
||||||
assert.are.same({ "example.com" }, dns.resolve("example.com"))
|
assert.are.same({ "example.com" }, dns.resolve("example.com"))
|
||||||
assert.spy(s_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server: oops!")
|
assert.spy(s_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server:\noops!\noops!")
|
||||||
|
|
||||||
dns_helper.mock_dns_query({ errcode = 1, errstr = "format error" })
|
dns_helper.mock_dns_query({ errcode = 1, errstr = "format error" })
|
||||||
assert.are.same({ "example.com" }, dns.resolve("example.com"))
|
assert.are.same({ "example.com" }, dns.resolve("example.com"))
|
||||||
assert.spy(s_ngx_log).was_called_with(ngx.ERR, "server returned error code: 1: format error")
|
assert.spy(s_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server:\nserver returned error code: 1: format error\nserver returned error code: 1: format error")
|
||||||
|
|
||||||
dns_helper.mock_dns_query({})
|
dns_helper.mock_dns_query({})
|
||||||
assert.are.same({ "example.com" }, dns.resolve("example.com"))
|
assert.are.same({ "example.com" }, dns.resolve("example.com"))
|
||||||
assert.spy(s_ngx_log).was_called_with(ngx.ERR, "no A record resolved")
|
assert.spy(s_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server:\nno record resolved\nno record resolved")
|
||||||
|
|
||||||
dns_helper.mock_dns_query({ { name = "example.com", cname = "sub.example.com", ttl = 60 } })
|
dns_helper.mock_dns_query({ { name = "example.com", cname = "sub.example.com", ttl = 60 } })
|
||||||
assert.are.same({ "example.com" }, dns.resolve("example.com"))
|
assert.are.same({ "example.com" }, dns.resolve("example.com"))
|
||||||
assert.spy(s_ngx_log).was_called_with(ngx.ERR, "no A record resolved")
|
assert.spy(s_ngx_log).was_called_with(ngx.ERR, "failed to query the DNS server:\nno record resolved\nno record resolved")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("resolves all A records of given host, caches them with minimal ttl and returns from cache next time", function()
|
it("resolves all A records of given host, caches them with minimal ttl and returns from cache next time", function()
|
||||||
|
|
|
@ -28,6 +28,25 @@ local function a_records_and_max_ttl(answers)
|
||||||
return addresses, ttl
|
return addresses, ttl
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function resolve_host(host, r, qtype)
|
||||||
|
local answers
|
||||||
|
answers, err = r:query(host, { qtype = qtype }, {})
|
||||||
|
if not answers then
|
||||||
|
return nil, -1, tostring(err)
|
||||||
|
end
|
||||||
|
|
||||||
|
if answers.errcode then
|
||||||
|
return nil, -1, string.format("server returned error code: %s: %s", answers.errcode, answers.errstr)
|
||||||
|
end
|
||||||
|
|
||||||
|
local addresses, ttl = a_records_and_max_ttl(answers)
|
||||||
|
if #addresses == 0 then
|
||||||
|
return nil, -1, "no record resolved"
|
||||||
|
end
|
||||||
|
|
||||||
|
return addresses, ttl, nil
|
||||||
|
end
|
||||||
|
|
||||||
function _M.resolve(host)
|
function _M.resolve(host)
|
||||||
local cached_addresses = cache:get(host)
|
local cached_addresses = cache:get(host)
|
||||||
if cached_addresses then
|
if cached_addresses then
|
||||||
|
@ -50,26 +69,30 @@ function _M.resolve(host)
|
||||||
return { host }
|
return { host }
|
||||||
end
|
end
|
||||||
|
|
||||||
local answers
|
local dns_errors = {}
|
||||||
answers, err = r:query(host, { qtype = r.TYPE_A }, {})
|
|
||||||
if not answers then
|
|
||||||
ngx.log(ngx.ERR, "failed to query the DNS server: " .. tostring(err))
|
|
||||||
return { host }
|
|
||||||
end
|
|
||||||
|
|
||||||
if answers.errcode then
|
|
||||||
ngx.log(ngx.ERR, string.format("server returned error code: %s: %s", answers.errcode, answers.errstr))
|
|
||||||
return { host }
|
|
||||||
end
|
|
||||||
|
|
||||||
local addresses, ttl = a_records_and_max_ttl(answers)
|
|
||||||
if #addresses == 0 then
|
|
||||||
ngx.log(ngx.ERR, "no A record resolved")
|
|
||||||
return { host }
|
|
||||||
end
|
|
||||||
|
|
||||||
|
local addresses, ttl
|
||||||
|
addresses, ttl, err = resolve_host(host, r, r.TYPE_A)
|
||||||
|
if not addresses then
|
||||||
|
table.insert(dns_errors, tostring(err))
|
||||||
|
elseif #addresses > 0 then
|
||||||
cache:set(host, addresses, ttl)
|
cache:set(host, addresses, ttl)
|
||||||
return addresses
|
return addresses
|
||||||
|
end
|
||||||
|
|
||||||
|
addresses, ttl, err = resolve_host(host, r, r.TYPE_AAAA)
|
||||||
|
if not addresses then
|
||||||
|
table.insert(dns_errors, tostring(err))
|
||||||
|
elseif #addresses > 0 then
|
||||||
|
cache:set(host, addresses, ttl)
|
||||||
|
return addresses
|
||||||
|
end
|
||||||
|
|
||||||
|
if #dns_errors > 0 then
|
||||||
|
ngx.log(ngx.ERR, "failed to query the DNS server:\n" .. table.concat(dns_errors, "\n"))
|
||||||
|
end
|
||||||
|
|
||||||
|
return { host }
|
||||||
end
|
end
|
||||||
|
|
||||||
return _M
|
return _M
|
||||||
|
|
Loading…
Reference in a new issue