GithubHelp home page GithubHelp logo

Comments (6)

herono2010 avatar herono2010 commented on July 17, 2024

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

from lua-resty-dns.

Viste avatar Viste commented on July 17, 2024
                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

from lua-resty-dns.

rulatir avatar rulatir commented on July 17, 2024
                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 {}?

from lua-resty-dns.

Viste avatar Viste commented on July 17, 2024

@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

from lua-resty-dns.

rulatir avatar rulatir commented on July 17, 2024

Thanks!

from lua-resty-dns.

Viste avatar Viste commented on July 17, 2024

@rulatir you're welcome :)

from lua-resty-dns.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.