{"id":13635944,"url":"https://github.com/openresty/lua-resty-dns","last_synced_at":"2025-04-19T04:31:47.610Z","repository":{"id":4154526,"uuid":"5268553","full_name":"openresty/lua-resty-dns","owner":"openresty","description":"DNS resolver for the nginx lua module","archived":false,"fork":false,"pushed_at":"2023-11-23T11:37:48.000Z","size":170,"stargazers_count":315,"open_issues_count":17,"forks_count":108,"subscribers_count":28,"default_branch":"master","last_synced_at":"2024-02-13T20:29:17.538Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/openresty.png","metadata":{"files":{"readme":"README.markdown","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2012-08-02T05:15:50.000Z","updated_at":"2024-01-28T17:47:41.000Z","dependencies_parsed_at":"2023-11-23T12:43:37.664Z","dependency_job_id":null,"html_url":"https://github.com/openresty/lua-resty-dns","commit_stats":{"total_commits":154,"total_committers":15,"mean_commits":"10.266666666666667","dds":"0.20779220779220775","last_synced_commit":"ada80c8712090a52e5180ac2b41d707890cf9106"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-dns","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-dns/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-dns/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openresty%2Flua-resty-dns/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/openresty","download_url":"https://codeload.github.com/openresty/lua-resty-dns/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223790525,"owners_count":17203355,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T00:00:54.472Z","updated_at":"2024-11-09T05:31:09.224Z","avatar_url":"https://github.com/openresty.png","language":"Lua","funding_links":[],"categories":["Libraries","Third Modules","Rust Modules","Lua"],"sub_categories":["C Modules","Lua Modules"],"readme":"Name\n====\n\nlua-resty-dns - Lua DNS resolver for the ngx_lua based on the cosocket API\n\nTable of Contents\n=================\n\n* [Name](#name)\n* [Status](#status)\n* [Description](#description)\n* [Synopsis](#synopsis)\n* [Methods](#methods)\n    * [new](#new)\n    * [destroy](#destroy)\n    * [query](#query)\n    * [tcp_query](#tcp_query)\n    * [set_timeout](#set_timeout)\n    * [compress_ipv6_addr](#compress_ipv6_addr)\n    * [expand_ipv6_addr](#expand_ipv6_addr)\n    * [arpa_str](#arpa_str)\n    * [reverse_query](#reverse_query)\n* [Constants](#constants)\n    * [TYPE_A](#type_a)\n    * [TYPE_NS](#type_ns)\n    * [TYPE_CNAME](#type_cname)\n    * [TYPE_SOA](#type_soa)\n    * [TYPE_PTR](#type_ptr)\n    * [TYPE_MX](#type_mx)\n    * [TYPE_TXT](#type_txt)\n    * [TYPE_AAAA](#type_aaaa)\n    * [TYPE_SRV](#type_srv)\n    * [TYPE_SPF](#type_spf)\n    * [CLASS_IN](#class_in)\n    * [SECTION_AN](#section_an)\n    * [SECTION_NS](#section_ns)\n    * [SECTION_AR](#section_ar)\n* [Automatic Error Logging](#automatic-error-logging)\n* [Limitations](#limitations)\n* [TODO](#todo)\n* [Author](#author)\n* [Copyright and License](#copyright-and-license)\n* [See Also](#see-also)\n\nStatus\n======\n\nThis library is considered production ready.\n\nDescription\n===========\n\nThis Lua library provides a DNS resolver for the ngx_lua nginx module:\n\nhttps://github.com/openresty/lua-nginx-module/#readme\n\nThis Lua library takes advantage of ngx_lua's cosocket API, which ensures\n100% nonblocking behavior.\n\nNote that at least [ngx_lua 0.5.12](https://github.com/openresty/lua-nginx-module/tags) or [OpenResty 1.2.1.11](http://openresty.org/#Download) is required.\n\nAlso, the [bit library](http://bitop.luajit.org/) is also required. If you're using LuaJIT 2.0 with ngx_lua, then the `bit` library is already available by default.\n\nNote that, this library is bundled and enabled by default in the [OpenResty bundle](http://openresty.org/).\n\nIMPORTANT: to be able to generate unique ids, the random generator must be properly seeded using `math.randomseed` prior to using this module.\n\nSynopsis\n========\n\n```nginx\nlua_package_path \"/path/to/lua-resty-dns/lib/?.lua;;\";\n\nserver {\n    location = /dns {\n        content_by_lua_block {\n            local resolver = require \"resty.dns.resolver\"\n            local r, err = resolver:new{\n                nameservers = {\"8.8.8.8\", {\"8.8.4.4\", 53} },\n                retrans = 5,  -- 5 retransmissions on receive timeout\n                timeout = 2000,  -- 2 sec\n                no_random = true, -- always start with first nameserver\n            }\n\n            if not r then\n                ngx.say(\"failed to instantiate the resolver: \", err)\n                return\n            end\n\n            local answers, err, tries = r:query(\"www.google.com\", nil, {})\n            if not answers then\n                ngx.say(\"failed to query the DNS server: \", err)\n                ngx.say(\"retry historie:\\n  \", table.concat(tries, \"\\n  \"))\n                return\n            end\n\n            if answers.errcode then\n                ngx.say(\"server returned error code: \", answers.errcode,\n                        \": \", answers.errstr)\n            end\n\n            for i, ans in ipairs(answers) do\n                ngx.say(ans.name, \" \", ans.address or ans.cname,\n                        \" type:\", ans.type, \" class:\", ans.class,\n                        \" ttl:\", ans.ttl)\n            end\n        }\n    }\n}\n```\n\n[Back to TOC](#table-of-contents)\n\nMethods\n=======\n\n[Back to TOC](#table-of-contents)\n\nnew\n---\n`syntax: r, err = class:new(opts)`\n\nCreates a dns.resolver object. Returns `nil` and a message string on error.\n\nIt accepts a `opts` table argument. The following options are supported:\n\n* `nameservers`\n\n\ta list of nameservers to be used. Each nameserver entry can be either a single hostname string or a table holding both the hostname string and the port number. The nameserver is picked up by a simple round-robin algorithm for each `query` method call. This option is required.\n* `retrans`\n\n\tthe total number of times of retransmitting the DNS request when receiving a DNS response times out according to the `timeout` setting. Defaults to `5` times. When trying to retransmit the query, the next nameserver according to the round-robin algorithm will be picked up.\n* `timeout`\n\n\tthe time in milliseconds for waiting for the response for a single attempt of request transmission. note that this is ''not'' the maximal total waiting time before giving up, the maximal total waiting time can be calculated by the expression `timeout x retrans`. The `timeout` setting can also be changed by calling the `set_timeout` method. The default `timeout` setting is 2000 milliseconds, or 2 seconds.\n* `no_recurse`\n\n\ta boolean flag controls whether to disable the \"recursion desired\" (RD) flag in the UDP request. Defaults to `false`.\n* `no_random`\n\n\ta boolean flag controls whether to randomly pick the nameserver to query first, if `true` will always start with the first nameserver listed. Defaults to `false`.\n\n[Back to TOC](#table-of-contents)\n\ndestroy\n-------\n`syntax: r:destroy()`\n\nDestroy the dns.resolver object by releasing all the internal occupied resources.\n\n[Back to TOC](#table-of-contents)\n\nquery\n-----\n`syntax: answers, err, tries? = r:query(name, options?, tries?)`\n\nPerforms a DNS standard query to the nameservers specified by the `new` method,\nand returns all the answer records in an array-like Lua table. In case of errors, it will\nreturn `nil` and a string describing the error instead.\n\nIf the server returns a non-zero error code, the fields `errcode` and `errstr` will be set accordingly in the Lua table returned.\n\nEach entry in the `answers` returned table value is also a hash-like Lua table\nwhich usually takes some of the following fields:\n\n* `name`\n\n\tThe resource record name.\n* `type`\n\n\tThe current resource record type, possible values are `1` (`TYPE_A`), `5` (`TYPE_CNAME`), `28` (`TYPE_AAAA`), and any other values allowed by RFC 1035.\n* `address`\n\n\tThe IPv4 or IPv6 address in their textual representations when the resource record type is either `1` (`TYPE_A`) or `28` (`TYPE_AAAA`), respectively. Successive 16-bit zero groups in IPv6 addresses will not be compressed by default, if you want that, you need to call the `compress_ipv6_addr` static method instead.\n* `section`\n\n\tThe identifier of the section that the current answer record belongs to. Possible values are `1` (`SECTION_AN`), `2` (`SECTION_NS`), and `3` (`SECTION_AR`).\n* `cname`\n\n\tThe (decoded) record data value for `CNAME` resource records. Only present for `CNAME` records.\n* `ttl`\n\n\tThe time-to-live (TTL) value in seconds for the current resource record.\n* `class`\n\n\tThe current resource record class, possible values are `1` (`CLASS_IN`) or any other values allowed by RFC 1035.\n* `preference`\n\n\tThe preference integer number for `MX` resource records. Only present for `MX` type records.\n* `exchange`\n\n\tThe exchange domain name for `MX` resource records. Only present for `MX` type records.\n* `nsdname`\n\n\tA domain-name which specifies a host which should be authoritative for the specified class and domain. Usually present for `NS` type records.\n* `rdata`\n\n\tThe raw resource data (RDATA) for resource records that are not recognized.\n* `txt`\n\n\tThe record value for `TXT` records. When there is only one character string in this record, then this field takes a single Lua string. Otherwise this field takes a Lua table holding all the strings.\n* `ptrdname`\n\n\tThe record value for `PTR` records.\n\nThis method also takes an optional `options` argument table, which takes the following fields:\n\n* `qtype`\n\n\tThe type of the question. Possible values are `1` (`TYPE_A`), `5` (`TYPE_CNAME`), `28` (`TYPE_AAAA`), or any other QTYPE value specified by RFC 1035 and RFC 3596. Default to `1` (`TYPE_A`).\n* `authority_section`\n\n\tWhen set to a true value, the `answers` return value includes the `Authority` section of the DNS response. Default to `false`.\n* `additional_section`\n\n\tWhen set to a true value, the `answers` return value includes the `Additional` section of the DNS response. Default to `false`.\n\nThe optional parameter `tries` can be provided as an empty table, and will be\nreturned as a third result. The table will be an array with the error message\nfor each (if any) failed try.\n\nWhen data truncation happens, the resolver will automatically retry using the TCP transport mode\nto query the current nameserver. All TCP connections are short lived.\n\n[Back to TOC](#table-of-contents)\n\ntcp_query\n---------\n`syntax: answers, err = r:tcp_query(name, options?)`\n\nJust like the `query` method, but enforce the TCP transport mode instead of UDP.\n\nAll TCP connections are short lived.\n\nHere is an example:\n\n```lua\n    local resolver = require \"resty.dns.resolver\"\n\n    local r, err = resolver:new{\n        nameservers = { \"8.8.8.8\" }\n    }\n    if not r then\n        ngx.say(\"failed to instantiate resolver: \", err)\n        return\n    end\n\n    local ans, err = r:tcp_query(\"www.google.com\", { qtype = r.TYPE_A })\n    if not ans then\n        ngx.say(\"failed to query: \", err)\n        return\n    end\n\n    local cjson = require \"cjson\"\n    ngx.say(\"records: \", cjson.encode(ans))\n```\n\n[Back to TOC](#table-of-contents)\n\nset_timeout\n-----------\n`syntax: r:set_timeout(time)`\n\nOverrides the current `timeout` setting by the `time` argument in milliseconds for all the nameserver peers.\n\n[Back to TOC](#table-of-contents)\n\ncompress_ipv6_addr\n------------------\n`syntax: compressed = resty.dns.resolver.compress_ipv6_addr(address)`\n\nCompresses the successive 16-bit zero groups in the textual format of the IPv6 address.\n\nFor example,\n\n```lua\n    local resolver = require \"resty.dns.resolver\"\n    local compress = resolver.compress_ipv6_addr\n    local new_addr = compress(\"FF01:0:0:0:0:0:0:101\")\n```\n\nwill yield `FF01::101` in the `new_addr` return value.\n\n[Back to TOC](#table-of-contents)\n\nexpand_ipv6_addr\n------------------\n`syntax: expanded = resty.dns.resolver.expand_ipv6_addr(address)`\n\nExpands the successive 16-bit zero groups in the textual format of the IPv6 address.\n\nFor example,\n\n```lua\n    local resolver = require \"resty.dns.resolver\"\n    local expand = resolver.expand_ipv6_addr\n    local new_addr = expand(\"FF01::101\")\n```\n\nwill yield `FF01:0:0:0:0:0:0:101` in the `new_addr` return value.\n\n[Back to TOC](#table-of-contents)\n\narpa_str\n------------------\n`syntax: arpa_record = resty.dns.resolver.arpa_str(address)`\n\nGenerates the reverse domain name for PTR lookups for both IPv4 and IPv6 addresses. Compressed IPv6 addresses\nwill be automatically expanded.\n\nFor example,\n\n```lua\n    local resolver = require \"resty.dns.resolver\"\n    local ptr4 = resolver.arpa_str(\"1.2.3.4\")\n    local ptr6 = resolver.arpa_str(\"FF01::101\")\n```\n\nwill yield `4.3.2.1.in-addr.arpa` for `ptr4` and `1.0.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.0.F.F.ip6.arpa` for `ptr6`.\n\n[Back to TOC](#table-of-contents)\n\nreverse_query\n------------------\n`syntax: answers, err = r:reverse_query(address)`\n\nPerforms a PTR lookup for both IPv4 and IPv6 addresses. This function is basically a wrapper for the `query` command\nwhich uses the `arpa_str` command to convert the IP address on the fly.\n\n[Back to TOC](#table-of-contents)\n\nConstants\n=========\n\n[Back to TOC](#table-of-contents)\n\nTYPE_A\n------\n\nThe `A` resource record type, equal to the decimal number `1`.\n\n[Back to TOC](#table-of-contents)\n\nTYPE_NS\n-------\n\nThe `NS` resource record type, equal to the decimal number `2`.\n\n[Back to TOC](#table-of-contents)\n\nTYPE_CNAME\n----------\n\nThe `CNAME` resource record type, equal to the decimal number `5`.\n\n[Back to TOC](#table-of-contents)\n\nTYPE_SOA\n----------\n\nThe `SOA` resource record type, equal to the decimal number `6`.\n\n[Back to TOC](#table-of-contents)\n\nTYPE_PTR\n--------\n\nThe `PTR` resource record type, equal to the decimal number `12`.\n\n[Back to TOC](#table-of-contents)\n\nTYPE_MX\n-------\n\nThe `MX` resource record type, equal to the decimal number `15`.\n\n[Back to TOC](#table-of-contents)\n\nTYPE_TXT\n--------\n\nThe `TXT` resource record type, equal to the decimal number `16`.\n\n[Back to TOC](#table-of-contents)\n\nTYPE_AAAA\n---------\n`syntax: typ = r.TYPE_AAAA`\n\nThe `AAAA` resource record type, equal to the decimal number `28`.\n\n[Back to TOC](#table-of-contents)\n\nTYPE_SRV\n---------\n`syntax: typ = r.TYPE_SRV`\n\nThe `SRV` resource record type, equal to the decimal number `33`.\n\nSee RFC 2782 for details.\n\n[Back to TOC](#table-of-contents)\n\nTYPE_SPF\n---------\n`syntax: typ = r.TYPE_SPF`\n\nThe `SPF` resource record type, equal to the decimal number `99`.\n\nSee RFC 4408 for details.\n\n[Back to TOC](#table-of-contents)\n\nCLASS_IN\n--------\n`syntax: class = r.CLASS_IN`\n\nThe `Internet` resource record type, equal to the decimal number `1`.\n\n[Back to TOC](#table-of-contents)\n\nSECTION_AN\n----------\n`syntax: stype = r.SECTION_AN`\n\nIdentifier of the `Answer` section in the DNS response. Equal to decimal number `1`.\n\n[Back to TOC](#table-of-contents)\n\nSECTION_NS\n----------\n`syntax: stype = r.SECTION_NS`\n\nIdentifier of the `Authority` section in the DNS response. Equal to the decimal number `2`.\n\n[Back to TOC](#table-of-contents)\n\nSECTION_AR\n----------\n`syntax: stype = r.SECTION_AR`\n\nIdentifier of the `Additional` section in the DNS response. Equal to the decimal number `3`.\n\n[Back to TOC](#table-of-contents)\n\nAutomatic Error Logging\n=======================\n\nBy default, the underlying [ngx_lua](https://github.com/openresty/lua-nginx-module/#readme) module\ndoes error logging when socket errors happen. If you are already doing proper error\nhandling in your own Lua code, then you are recommended to disable this automatic error logging by turning off [ngx_lua](https://github.com/openresty/lua-nginx-module/#readme)'s [lua_socket_log_errors](https://github.com/openresty/lua-nginx-module/#lua_socket_log_errors) directive, that is,\n\n```nginx\n    lua_socket_log_errors off;\n```\n\n[Back to TOC](#table-of-contents)\n\nLimitations\n===========\n\n* This library cannot be used in code contexts like `set_by_lua*`, `log_by_lua*`, and\n`header_filter_by_lua*` where the ngx_lua cosocket API is not available.\n* The `resty.dns.resolver` object instance cannot be stored in a Lua variable at the Lua module level,\nbecause it will then be shared by all the concurrent requests handled by the same nginx\n worker process (see\nhttps://github.com/openresty/lua-nginx-module/#data-sharing-within-an-nginx-worker ) and\nresult in bad race conditions when concurrent requests are trying to use the same `resty.dns.resolver` instance.\nYou should always initiate `resty.dns.resolver` objects in function local\nvariables or in the `ngx.ctx` table. These places all have their own data copies for\neach request.\n\n[Back to TOC](#table-of-contents)\n\nTODO\n====\n\n* Concurrent (or parallel) query mode\n* Better support for other resource record types like `TLSA`.\n\n[Back to TOC](#table-of-contents)\n\nAuthor\n======\n\nYichun \"agentzh\" Zhang (章亦春) \u003cagentzh@gmail.com\u003e, OpenResty Inc.\n\n[Back to TOC](#table-of-contents)\n\nCopyright and License\n=====================\n\nThis module is licensed under the BSD license.\n\nCopyright (C) 2012-2019, by Yichun \"agentzh\" Zhang (章亦春) \u003cagentzh@gmail.com\u003e, OpenResty Inc.\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n[Back to TOC](#table-of-contents)\n\nSee Also\n========\n* the ngx_lua module: https://github.com/openresty/lua-nginx-module/#readme\n* the [lua-resty-memcached](https://github.com/agentzh/lua-resty-memcached) library.\n* the [lua-resty-redis](https://github.com/agentzh/lua-resty-redis) library.\n* the [lua-resty-mysql](https://github.com/agentzh/lua-resty-mysql) library.\n\n[Back to TOC](#table-of-contents)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenresty%2Flua-resty-dns","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenresty%2Flua-resty-dns","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenresty%2Flua-resty-dns/lists"}