-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to return ipv4 address only.... #50
Comments
local answers, err, tries = r:query(host_name, { qtype = r.TYPE_A }) |
|
Three questions:
|
@rulatir No, when I wrote it I made a mistake, there must be r.TYPE_A local r, err = resolver:new{
nameservers = something
retrans = 5,
timeout = 5000,
no_recurse = true
}
if not r then
ngx.log(ngx.ERR, "resolver communicate error: ", err)
return
end
local qtype
if checkIfIpv4(ngx.var.remote_addr) then -- check from what version of IPv we get the request
qtype = r.TYPE_A
else
qtype = r.TYPE_AAAA
end
local ans, err = r:tcp_query(host, { qtype = qtype })
if not ans then
ngx.log(ngx.ERR, "got an error on query: ", err)
return ngx.exit(ngx.HTTP_BAD_GATEWAY) --or something else
end
if ans.errcode ~= nil then -- check if we get errcode in ans query
return ngx.exit(ngx.HTTP_BAD_GATEWAY) --or something else
else
if not next(ans) then -- check for empty ans
ngx.exit(ngx.HTTP_BAD_GATEWAY) --or something else
end
for k,answer in pairs(ans) do -- itr ans-query for get raw IP from ans
if not answer.address then
return ngx.exit(ngx.HTTP_BAD_GATEWAY) -- --or something else
else
return answer.address
end
end
end checkIfIpv4 func code local function checkIfIpv4(ip)
if ip == nil or type(ip) ~= "string" then
return false
end
-- check for format 1.11.111.111 for ipv4
local chunks = {ip:match("(%d+)%.(%d+)%.(%d+)%.(%d+)")}
if (#chunks == 4) then
for _,v in pairs(chunks) do
if (tonumber(v) < 0 or tonumber(v) > 255) then
return false
end
end
return true
else
return false
end
end |
Thanks! |
@rulatir you're welcome :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: