当前位置:网站首页>Openresty Lua resty DNS domain name resolution
Openresty Lua resty DNS domain name resolution
2022-07-27 20:50:00 【o_ Guatian Lixia_ o】
openresty lua-resty-dns Domain name resolution
Official website :https://github.com/openresty/lua-resty-dns
lua-resty-dns Use
This library is considered production ready
* lua-resty-dns Already in production
This Lua library provides a DNS resolver for the ngx_lua nginx module
This Lua library takes advantage of ngx_lua's cosocket API, which
ensures 100% nonblocking behavior.
* ngx_lua use lua-resty-dns Domain name resolution
* lua-resty-dns utilize ngx_lua Of cosocket api, Non blocking execution
Note that at least ngx_lua 0.5.12 or OpenResty 1.2.1.11 is required.
Also, the bit library is also required. If you're using LuaJIT 2.0
with ngx_lua, then the bit library is already available by default.
* lua-resty-dns Adaptation version :ngx_lua 0.5.12 And above 、OpenResty 1.2.1.11 And above
Note that, this library is bundled and enabled by default in the OpenResty bundle.
* lua-resty-dns stay openresty Default binding in
IMPORTANT: to be able to generate unique ids, the random generator must
be properly seeded using math.randomseed prior to using this module
* In order to generate unique id, Random generator ( Use math.randomseed) Prior to the lua-resty-dns perform
new: Create a domain name resolution object
Grammar format :r, err = class:new(opts)
Creates a dns.resolver object. Returns nil and a message string on error.
* Create a domain name resolution object
* Create failure , return nil、 Error description information
It accepts a opts table argument. The following options are supported:
* receive table Table parameters
* nameservers: List of domain name resolution servers
a 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.
* retrans: The total number of domain name resolution retries
the 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.
* timeout: Timeout time , Unit millisecond
the 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.
* no_recurse: Whether to disable recursion desired, Default false
a boolean flag controls whether to disable the "recursion desired"
(RD) flag in the UDP request. Defaults to false.
* no_random: Whether to always pick the first domain name parser in the list , Default false( Random selection )
a 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
query: Domain name resolution
Grammar format :answers, err, tries? = r:query(name, options?, tries?)
Performs a DNS standard query to the nameservers specified by the new
method, and returns all the answer records in an array-like Lua table.
In case of errors, it will return nil and a string describing the
error instead.
* Send a query to the domain name parser , Returns an array of types table data , Represents all parsing results
* If you fail , return nil、 Error description information
If the server returns a non-zero error code, the fields errcode and
errstr will be set accordingly in the Lua table returned.
* If the return is not 0 Error code ,errcode、errstr Will also be in table Set in
Each entry in the answers returned table value is also a hash-like Lua
table which usually takes some of the following fields:
* The returned result fields are as follows
* name: Resolved resource name
The resource record name.
* type: Resolved resource result type
The current resource record type, possible values are 1 (TYPE_A), 5
(TYPE_CNAME), 28 (TYPE_AAAA), and any other values allowed by RFC 1035.
* address:ip Address (ipv4、ipv6)
The 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.
* section:section identifier
The identifier of the section that the current answer record belongs
to. Possible values are 1 (SECTION_AN), 2 (SECTION_NS), and 3 (SECTION_AR).
* cname: Alias
The (decoded) record data value for CNAME resource records. Only
present for CNAME records.
* ttl: Resource results remaining lifetime
The time-to-live (TTL) value in seconds for the current resource record.
*class: Resource result class
The current resource record class, possible values are 1 (CLASS_IN) or
any other values allowed by RFC 1035.
* preference:MX type The maximum number of results
The preference integer number for MX resource records. Only present for
MX type records.
* exchange:MX resource Exchange domain name
The exchange domain name for MX resource records. Only present for MX
type records.
* nsdname:NS type domain name
A domain-name which specifies a host which should be authoritative for
the specified class and domain. Usually present for NS type records.
* rdata: Unrecognized domain name
The raw resource data (RDATA) for resource records that are not recognized.
* txt:TXT records
The 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.
* ptrdname:PTR records
The record value for PTR records
This method also takes an optional options argument table, which takes the following fields:
* Optional parameters
* qtype: Query type
The 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).
* authority_section: authentication section, Default false
When set to a true value, the answers return value includes the Authority
section of the DNS response. Default to false.
* additional_section: additional section, Default false
When set to a true value, the answers return value includes the Additional
section of the DNS response. Default to false.
The optional parameter tries can be provided as an empty table, and will be
returned as a third result. The table will be an array with the error message
for each (if any) failed try.
* tries The parameter can be an empty table, And it will be returned as the third result parameter
* If an error occurs , An error message will be recorded
When data truncation happens, the resolver will automatically retry using the
TCP transport mode to query the current nameserver. All TCP connections are
short lived
* If data truncation occurs , Will try again
* be-all tcp All connections are short connections
tcp_query:tcp Link query
Grammar format :answers, err = r:tcp_query(name, options?)
Just like the query method, but enforce the TCP transport mode instead of UDP.
All TCP connections are short lived
* and query The function of the method is similar to , Compulsory use tcp transport
* be-all tcp All connections are short connections
set_timeout: Set timeout
Grammar format :r:set_timeout(time)
Overrides the current timeout setting by the time argument in
milliseconds for all the nameserver peers
* Set timeout , Unit millisecond
compress_ipv6_addr: Compress ipv6 Address
Grammar format :compressed = resty.dns.resolver.compress_ipv6_addr(address)
Compresses the successive 16-bit zero groups in the textual format
of the IPv6 address
* Compress ipv6 Address
# Example
local resolver = require "resty.dns.resolver"
local compress = resolver.compress_ipv6_addr
local new_addr = compress("FF01:0:0:0:0:0:0:101")
==> FF01::101
expand_ipv6_addr: Restore compressed ipv6 Address
Grammar format :expanded = resty.dns.resolver.expand_ipv6_addr(address)
Expands the successive 16-bit zero groups in the textual format
of the IPv6 address
* Restore compressed ipv6 Address
# Example
local resolver = require "resty.dns.resolver"
local expand = resolver.expand_ipv6_addr
local new_addr = expand("FF01::101")
==> FF01:0:0:0:0:0:0:101
arpa_str: reverse ip Address
Grammar format :rpa_record = resty.dns.resolver.arpa_str(address)
Generates the reverse domain name for PTR lookups for both IPv4 and
IPv6 addresses. Compressed IPv6 addresses will be automatically expanded
* reverse ipv4、ipv6 Address
# Example
local resolver = require "resty.dns.resolver"
local ptr4 = resolver.arpa_str("1.2.3.4")
local ptr6 = resolver.arpa_str("FF01::101")
==> 4.3.2.1.in-addr.arpa
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
reverse_query:ptr Reverse DNS
Grammar format :answers, err = r:reverse_query(address)
Performs a PTR lookup for both IPv4 and IPv6 addresses. This function
is basically a wrapper for the query command which uses the arpa_str
command to convert the IP address on the fly
* ptr Reverse DNS
Example
lua_package_path "/path/to/lua-resty-dns/lib/?.lua;;";
server {
location = /dns {
content_by_lua_block {
local resolver = require "resty.dns.resolver"
local r, err = resolver:new{
nameservers = {"8.8.8.8", {"8.8.4.4", 53} },
retrans = 5, -- 5 retransmissions on receive timeout
timeout = 2000, -- 2 sec
no_random = true, -- always start with first nameserver
}
if not r then
ngx.say("failed to instantiate the resolver: ", err)
return
end
local answers, err, tries = r:query("www.google.com", nil, {})
if not answers then
ngx.say("failed to query the DNS server: ", err)
ngx.say("retry historie:\n ", table.concat(tries, "\n "))
return
end
if answers.errcode then
ngx.say("server returned error code: ", answers.errcode,
": ", answers.errstr)
end
for i, ans in ipairs(answers) do
ngx.say(ans.name, " ", ans.address or ans.cname,
" type:", ans.type, " class:", ans.class,
" ttl:", ans.ttl)
end
}
}
}
lua-resty-dns Constant
TYPE_A ==> The A resource record type, equal to the decimal number 1
TYPE_NS ==> The NS resource record type, equal to the decimal number 2
TYPE_CNAME ==> The CNAME resource record type, equal to the decimal number 5
TYPE_SOA ==> The SOA resource record type, equal to the decimal number 6
TYPE_PTR ==> The PTR resource record type, equal to the decimal number 12
TYPE_MX ==> The MX resource record type, equal to the decimal number 15
TYPE_TXT ==> The TXT resource record type, equal to the decimal number 16
TYPE_AAAA ==> The AAAA resource record type, equal to the decimal number 28
TYPE_SRV ==> The SRV resource record type, equal to the decimal number 33
TYPE_SPF ==> The SPF resource record type, equal to the decimal number 99
CLASS_IN ==> The Internet resource record type, equal to the decimal number 1
SECTION_AN ==> Identifier of the Answer section in the DNS response.
Equal to decimal number 1
SECTION_NS ==> Identifier of the Authority section in the DNS response.
Equal to the decimal number 2
SECTION_AR ==> Identifier of the Additional section in the DNS response.
Equal to the decimal number 3
Examples of use
default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
}
location /test {
content_by_lua_block {
local resolver = require "resty.dns.resolver"
local cjson = require 'cjson';
local r, err = resolver:new({
nameservers = {"8.8.8.8", {"8.8.4.4", 53} },
retrans = 5, -- 5 retransmissions on receive timeout
timeout = 2000, -- 2 sec
});
if not r then
ngx.say(" Failed to create resolution instance ==> ", err)
return
end
local answers, err, tries = r:query("www.google.com", nil, {})
if not answers then
ngx.say(" Domain name resolution www.google.com Failure ==> ", err)
ngx.say(" Retry parsing records ==> ", table.concat(tries, "\n "))
return
end
if answers.errcode then
ngx.say(" Domain name server resolution error ==> ", answers.errcode, " ==> ", answers.errstr)
end
ngx.say(" Analysis results ==> ", cjson.encode(answers));
for i, ans in ipairs(answers) do
ngx.say(ans.name, " ", ans.address or ans.cname,
" type:", ans.type, " class:", ans.class,
" ttl:", ans.ttl)
end
answers, err = r:query("www.baidu.com")
if not answers then
ngx.say(" Domain name resolution www.baidu.com Failure ==> ", err)
return
end
if answers.errcode then
ngx.say(" Domain name server resolution error ==> ", answers.errcode, " ==> ", answers.errstr)
end
ngx.say("\n Analysis results ==> ", cjson.encode(answers));
for i, ans in ipairs(answers) do
ngx.say(ans.name, " ", ans.address or ans.cname,
" type:", ans.type, " class:", ans.class,
" ttl:", ans.ttl)
end
answers, err = r:reverse_query("www.taobao.com")
if not answers then
ngx.say(" Domain name resolution www.taobao.com Failure ==> ", err)
return
end
if answers.errcode then
ngx.say(" Domain name server resolution error ==> ", answers.errcode, " ==> ", answers.errstr)
end
ngx.say("\n Analysis results ==> ", cjson.encode(answers));
for i, ans in ipairs(answers) do
ngx.say(ans.name, " ", ans.address or ans.cname,
" type:", ans.type, " class:", ans.class,
" ttl:", ans.ttl)
end
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
}
establish openresty Containers
docker run -it -d --net fixed --ip 172.18.0.2 -p 9000:80 \
-v /Users/huli/lua/openresty/dns/default.conf:/etc/nginx/conf.d/default.conf \
--name open-dns lihu12344/openresty
Use tests
[email protected] dns % curl localhost:9000/test
Analysis results ==> [{"ttl":204,"type":1,"section":1,"address":"104.244.46.85","name":"www.google.com","class":1}]
www.google.com 104.244.46.85 type:1 class:1 ttl:204
Analysis results ==> [{"ttl":752,"type":5,"section":1,"cname":"www.a.shifen.com","name":"www.baidu.com","class":1},{"ttl":11,"type":5,"section":1,"cname":"www.wshifen.com","name":"www.a.shifen.com","class":1},{"ttl":200,"type":1,"section":1,"address":"103.235.46.40","name":"www.wshifen.com","class":1}]
www.baidu.com www.a.shifen.com type:5 class:1 ttl:752
www.a.shifen.com www.wshifen.com type:5 class:1 ttl:11
www.wshifen.com 103.235.46.40 type:1 class:1 ttl:200
Analysis results ==> [{"ttl":600,"type":5,"section":1,"cname":"www.taobao.com.danuoyi.tbcache.com","name":"www.taobao.com","class":1}]
www.taobao.com www.taobao.com.danuoyi.tbcache.com type:5 class:1 ttl:600
边栏推荐
- Knowledge dry goods: basic storage service novice Experience Camp
- 软件测试面试题:已知一个队列,如: [1, 3, 5, 7], 如何把第一个数字,放到第三个位置,得到:[3, 5, 1, 7]
- 一周活动速递|深入浅出第8期;Meetup成都站报名进行中
- When adding RTSP devices to easycvr platform, what is the reason for the phenomenon that they are all connected by TCP?
- Session attack
- 2022-07-19 网工进阶(二十)BGP-路由优选、路由优选逐条分析
- 金仓数据库 Oracle 至 KingbaseES 迁移最佳实践 (4. Oracle数据库移植实战)
- Leetcode:1498. Number of subsequences that meet the conditions [sort + bisection + power hash table]
- Best practices for Oracle kingbasees migration of Jincang database (4. Oracle database migration practice)
- 【程序人生】“阶段总结“-不甘平凡
猜你喜欢

EasyCVR平台添加RTSP设备时,出现均以TCP方式连接的现象是什么原因?

How to improve the picture transmission speed and success rate in the development of IM instant messaging under the mobile network

IM即时通讯开发如何提升移动网络下图片传输速度和成功率

Kingbasees heterogeneous database migration guide (4. Application migration process)

Lennix Lai, OKx financial market director: Web3 is a revolution

浅析即时通讯移动端 IM 开发中登录请求的优化

Introduction to JVs Foundation

When adding RTSP devices to easycvr platform, what is the reason for the phenomenon that they are all connected by TCP?

人家这才叫软件测试工程师,你那只是混口饭吃(附HR面试宝典)

面了个腾讯拿38K跳槽出来的,见识到了真正的测试天花板
随机推荐
Innovative cases | the growth strategy of digitalization of local life services and upgrading of Gaode brand
[efficiency] abandon notepad++, this open source substitute is more awesome!
IM即时通讯开发如何提升移动网络下图片传输速度和成功率
关于栈迁移的那些事儿
JVS基础介绍
Ten year test old bird talk about mobile terminal compatibility test
软件测试面试题:已知一个字符串为“hello_world_yoyo”, 如何得到一个队列 [“hello“,“world“,“yoyo“]
MySQL 日志查询日志
Can tonghuashun open an account on weekends? Is it safe to open an account
Download of MySQL driver jar package -- nanny tutorial
Flask Mdict builds online MDICT Dictionary Service
Software test interview question: if a string is known as "hello_world_yoyo", how to get a queue ["hello", "world", "yoyo"]
用户和权限创建普通用户
软件测试面试题:字符串 “axbyczdj“,如果得到结果“abcd
How to optimize the open source community experience through developer metrics
未定义变量 “Lattice“ 或类 “Lattice.latticeEasy“(Matlab)
用户和权限修改用户密码
js中数组与字符串常用方法属性总结
力扣解法汇总592-分数加减运算
Lennix Lai, OKx financial market director: Web3 is a revolution