Skip to content
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

Open
herono2010 opened this issue Jan 15, 2020 · 6 comments
Open

how to return ipv4 address only.... #50

herono2010 opened this issue Jan 15, 2020 · 6 comments

Comments

@herono2010
Copy link

No description provided.

@herono2010
Copy link
Author

local answers, err, tries = r:query(host_name, { qtype = r.TYPE_A })
the answers also have some cname (type:5) value....

@Viste
Copy link

Viste commented Jan 28, 2020

                local ans, err = r:tcp_query(host, { qtype = TYPE_A })
                if not ans then
                    ngx.log(ngx.ERR, "got an error on query: ", err)
                    return ngx.exit(ngx.HTTP_GATEWAY_TIMEOUT)
                end

                for k,answer in pairs(ans) do
                    return answer.address
                end

@rulatir
Copy link

rulatir commented Oct 19, 2020

                local ans, err = r:tcp_query(host, { qtype = TYPE_A })
                if not ans then
                    ngx.log(ngx.ERR, "got an error on query: ", err)
                    return ngx.exit(ngx.HTTP_GATEWAY_TIMEOUT)
                end

                for k,answer in pairs(ans) do
                    return answer.address
                end

Three questions:

  1. Should it be TYPE_A or r.TYPE_A? Which one is correct?
  2. Is changing r.TYPE_A to TYPE_A exactly the change that turns this piece of code from non-working to working?
  3. If 2. is true, does it mean that TYPE_A is a global constant and r.TYPE_A is nil, which is why { qtype = r.TYPE_A } is effectively {}?

@Viste
Copy link

Viste commented Oct 20, 2020

@rulatir No, when I wrote it I made a mistake, there must be r.TYPE_A
this is 100% working code with checks for return ipv4 or ipv6 depends on which version the connection was made from

                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

@rulatir
Copy link

rulatir commented Oct 20, 2020

Thanks!

@Viste
Copy link

Viste commented Oct 20, 2020

@rulatir you're welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants