{"id":291466,"date":"2018-10-27T20:40:02","date_gmt":"2018-10-27T16:40:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=291466"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=291466","title":{"rendered":"DNS over TLS \u2014 \u0428\u0438\u0444\u0440\u0443\u0435\u043c \u043d\u0430\u0448\u0438 DNS \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Stunnel \u0438 Lua"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<p><a href=\"https:\/\/habr.com\/post\/427957\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/lx\/tv\/k-\/lxtvk-ug3ul_vknfupcpp-uw0ge.jpeg\"><\/a><br \/>  <em><a href=\"https:\/\/commons.wikimedia.org\/wiki\/File:%D0%94%D0%9E%D0%A2_%E2%84%96401_(%D0%BD%D0%B0%D0%B4%D0%B7%D0%B5%D0%BC%D0%BD%D0%B0_%D1%87%D0%B0%D1%81%D1%82%D0%B8%D0%BD%D0%B0).jpg\">\u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f<\/a><br \/>  <\/em><br \/>  \u041f\u043e\u0441\u043b\u0435 \u043d\u043e\u0432\u043e\u0441\u0442\u0438 \u043e \u0442\u043e\u043c \u0447\u0442\u043e <a href=\"https:\/\/habr.com\/post\/427639\/\">&#171;Google Public DNS \u0442\u0438\u0445\u043e \u0432\u043a\u043b\u044e\u0447\u0438\u043b\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 DNS over TLS&#187;<\/a> \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e. \u0423 \u043c\u0435\u043d\u044f \u0443\u0436\u0435 \u0435\u0441\u0442\u044c <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Stunnel\">Stunnel<\/a> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Transmission_Control_Protocol\">TCP<\/a> \u0442\u0443\u043d\u043d\u0435\u043b\u044c \u0434\u043e \u0433\u0443\u0433\u043b\u0430. \u041d\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u0431\u0449\u0430\u044e\u0442\u0441\u044f \u0441 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/DNS\">DNS<\/a> \u043f\u043e <a href=\"https:\/\/ru.wikipedia.org\/wiki\/UDP\">UDP<\/a> \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u043f\u0440\u043e\u043a\u0441\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u0430\u0442\u044c UDP \u043f\u0430\u043a\u0435\u0442\u044b \u0432 TCP \u043f\u043e\u0442\u043e\u043a \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e. \u041c\u044b \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0435\u0433\u043e \u043d\u0430 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Lua\">Lua<\/a>.<\/p>\n<p>  <\/p>\n<p>\u0412\u0441\u044f \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u043c\u0435\u0436\u0434\u0443 TCP \u0438 UDP DNS \u043f\u0430\u043a\u0435\u0442\u0430\u043c\u0438:<\/p>\n<p>  <\/p>\n<blockquote><p>4.2.2. TCP usage<br \/>  Messages sent over TCP connections use server port 53 (decimal). The message is prefixed with a two byte length field which gives the message length, excluding the two byte length field. This length field allows the low-level processing to assemble a complete message before beginning to parse it.<\/p><\/blockquote>\n<p><a href=\"https:\/\/www.ietf.org\/rfc\/rfc1035\">RFC1035: DOMAIN NAMES \u2014 IMPLEMENTATION AND SPECIFICATION<\/a><\/p>\n<p>  <\/p>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c \u0434\u0435\u043b\u0430\u0435\u043c \u0442\u0443\u0434\u0430:<\/p>\n<p>  <\/p>\n<ol>\n<li>\u0431\u0435\u0440\u0451\u043c \u043f\u0430\u043a\u0435\u0442 \u0438\u0437 UDP <\/li>\n<li>\u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043a \u043d\u0435\u043c\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043f\u0430\u0440\u0443 \u0431\u0430\u0439\u0442 \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0443\u043a\u0430\u0437\u0430\u043d \u0440\u0430\u0437\u043c\u0435\u0440 \u044d\u0442\u043e\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u0430<\/li>\n<li>\u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0432 TCP \u043a\u0430\u043d\u0430\u043b<\/li>\n<\/ol>\n<p>  <\/p>\n<p>\u0418 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443:<\/p>\n<p>  <\/p>\n<ol>\n<li>\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0437 TCP \u043f\u0430\u0440\u0443 \u0431\u0430\u0439\u0442 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u0430\u043a\u0435\u0442\u0430<\/li>\n<li>\u0447\u0438\u0442\u0430\u0435\u043c \u043f\u0430\u043a\u0435\u0442 \u0438\u0437 TCP <\/li>\n<li>\u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u0435\u043b\u044e \u043f\u043e UDP<\/li>\n<\/ol>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h3 id=\"nastraivaem-stunnel\">\u041d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c Stunnel<\/h3>\n<p>  <\/p>\n<p>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u044f. \u041f\u0438\u0448\u0435\u043c \u0432 stunnel.conf:<\/p>\n<p>  <\/p>\n<pre><code>[dns] client = yes accept  = 127.0.0.1:53 connect = 8.8.8.8:853<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c Stunnel:<\/p>\n<p>  <\/p>\n<ol>\n<li>\u043f\u0440\u0438\u043c\u0435\u0442 \u043d\u0435 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 TCP \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 127.0.0.1:53<\/li>\n<li>\u043e\u0442\u043a\u0440\u043e\u0435\u0442 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 TLS \u0442\u0443\u043d\u0435\u043b\u044c \u0434\u043e \u0430\u0434\u0440\u0435\u0441\u0430 8.8.8.8:853 (Google DNS) <\/li>\n<li>\u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0442\u0443\u0434\u0430 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e<\/li>\n<\/ol>\n<p>  <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c Stunnel<\/p>\n<p>  <\/p>\n<p>\u0420\u0430\u0431\u043e\u0442\u0443 \u0442\u0443\u043d\u0435\u043b\u044f \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439:<\/p>\n<p>  <\/p>\n<pre><code>nslookup -vc ya.ru 127.0.0.1<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u043f\u0446\u0438\u044f vc \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 nslookup \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c TCP \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043a DNS \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0432\u043c\u0435\u0441\u0442\u043e UDP.<\/p>\n<p>  <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p>  <\/p>\n<pre><code>*** Can't find server name for address 127.0.0.1: Non-existent domain Server:  UnKnown Address:  127.0.0.1  Non-authoritative answer: Name:    ya.ru Address:  (\u0437\u0434\u0435\u0441\u044c IP \u044f\u043d\u0434\u0435\u043a\u0441\u0430)<\/code><\/pre>\n<p>  <\/p>\n<h3 id=\"pishem-skript\">\u041f\u0438\u0448\u0435\u043c \u0441\u043a\u0440\u0438\u043f\u0442<\/h3>\n<p>  <\/p>\n<p>\u042f \u043f\u0438\u0448\u0443 \u043d\u0430 <a href=\"https:\/\/www.lua.org\/\">Lua 5.3<\/a>. \u0412 \u043d\u0451\u043c \u0443\u0436\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u0447\u0438\u0441\u043b\u0430\u043c\u0438. \u041d\u0443 \u0438 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u044c <a href=\"http:\/\/w3.impa.br\/~diego\/software\/luasocket\/\">Lua Socket<\/a>.<\/p>\n<p>  <\/p>\n<p>\u0418\u043c\u044f \u0444\u0430\u0439\u043b\u0430: <a href=\"https:\/\/github.com\/ivan386\/lua-simple-udp-to-tcp-dns-proxy\/blob\/master\/simple-udp-to-tcp-dns-proxy.lua\">simple-udp-to-tcp-dns-proxy.lua<\/a><\/p>\n<p>  <\/p>\n<pre><code class=\"lua\">local socket = require \"socket\" -- \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c lua socket<\/code><\/pre>\n<p>  <\/p>\n<p><code>--[[--<\/code><\/p>\n<p>  <\/p>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0434\u0430\u043c\u043f \u043f\u0430\u043a\u0435\u0442\u0430 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c. \u0425\u043e\u0447\u0435\u0442\u0441\u044f \u0432\u0438\u0434\u0435\u0442\u044c \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u043f\u0440\u043e\u043a\u0441\u0438.<\/p>\n<p>  <\/p>\n<p><code>--]]--<\/code><\/p>\n<p>  <\/p>\n<pre><code class=\"lua\">function serialize(data)     -- \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u043c \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u043d\u0435 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u044b a-z \u0438 0-9 \u0438 \u0442\u0438\u0440\u0435 \u0432 HEX \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 '\\xFF'     return \"'\"..data:gsub(\"[^a-z0-9-]\", function(chr) return (\"\\\\x%02X\"):format(chr:byte()) end)..\"'\" end<\/code><\/pre>\n<p>  <\/p>\n<p><code>--[[--<\/code><\/p>\n<p>  <\/p>\n<h4 id=\"udp-v-tcp-i-obratno\">UDP \u0432 TCP \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e<\/h4>\n<p>  <\/p>\n<p>\u041f\u0438\u0448\u0435\u043c \u0434\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043e\u043f\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u043c\u044f \u043a\u0430\u043d\u0430\u043b\u0430\u043c\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>  <\/p>\n<p><code>--]]--<\/code><\/p>\n<p>  <\/p>\n<pre><code class=\"lua\">-- \u0437\u0434\u0435\u0441\u044c \u043f\u0430\u043a\u0435\u0442\u044b \u0438\u0437 UDP \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u0430\u044e\u0442\u0441\u044f \u0432 TCP \u043f\u043e\u0442\u043e\u043a function udp_to_tcp_coroutine_function(udp_in, tcp_out, clients)     repeat         coroutine.yield() -- \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u043d\u043e\u043c\u0443 \u0446\u0438\u043a\u043b\u0443         packet, err_ip, port = udp_in:receivefrom() -- \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c UDP \u043f\u0430\u043a\u0435\u0442         if packet then             -- &gt; - big endian             -- I - unsigned integer             -- 2 - 2 bytes size             tcp_out:send(((\"&gt;I2\"):pack(#packet))..packet) -- \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u0430\u043a\u0435\u0442\u0430 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0432 TCP             local id = (\"&gt;I2\"):unpack(packet:sub(1,2))    -- \u0447\u0438\u0442\u0430\u0435\u043c ID \u043f\u0430\u043a\u0435\u0442\u0430             clients[id] = {ip=err_ip, port=port}          -- \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0430\u0434\u0440\u0435\u0441 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u0435\u043b\u044f             print(os.date(\"%c\", os.time()) ,err_ip, port, \"&gt;\", serialize(packet)) -- \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u043c \u043f\u0430\u043a\u0435\u0442 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c         end     until false end  -- \u0437\u0434\u0435\u0441\u044c \u043f\u0430\u043a\u0435\u0442\u044b \u0438\u0437 TCP \u043f\u043e\u0442\u043e\u043a\u0430 \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u0430\u044e\u0442\u0441\u044f \u043a \u0430\u0434\u0440\u0435\u0441\u0430\u0442\u0443 \u043f\u043e UDP function tcp_to_udp_coroutine_function(tcp_in, udp_out, clients)     repeat         coroutine.yield() -- \u0432\u043e\u0437\u0432\u0440\u0430\u0448\u044f\u0435\u043c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u043d\u043e\u043c\u0443 \u0446\u0438\u043a\u043b\u0443         -- &gt; - big endian         -- I - unsigned integer         -- 2 - 2 bytes size         local packet = tcp_in:receive((\"&gt;I2\"):unpack(tcp_in:receive(2)), nil) -- \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c TCP \u043f\u0430\u043a\u0435\u0442         local id = (\"&gt;I2\"):unpack(packet:sub(1,2))                            -- \u0447\u0438\u0442\u0430\u0435\u043c ID \u043f\u0430\u043a\u0435\u0442\u0430         local client = clients[id]                                            -- \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u0435\u043b\u044f         if client then             udp_out:sendto(packet, client.ip, client.port) -- \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u0430\u043a\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u0435\u043b\u044e \u043f\u043e UDP             clients[id] = nil                              -- \u043e\u0447\u0438\u0449\u0430\u0435\u043c \u044f\u0447\u0435\u0439\u043a\u0443             print(os.date(\"%c\", os.time()) ,client.ip, client.port, \"&lt;\", serialize(packet)) -- \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u043c \u043f\u0430\u043a\u0435\u0442 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c         end     until false end<\/code><\/pre>\n<p>  <\/p>\n<p><code>--[[--<\/code><\/p>\n<p>  <\/p>\n<p>\u041e\u0431\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442 coroutine.yield(). \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0435\u0440\u0432\u044b\u043c \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0430 \u0434\u0430\u043b\u044c\u0448\u0435 \u0434\u0435\u043b\u0430\u0442\u044c coroutine.resume(co) \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432.<\/p>\n<p>  <\/p>\n<h4 id=\"main\">main<\/h4>\n<p>  <\/p>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c main \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0443 \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u0446\u0438\u043a\u043b.<\/p>\n<p>  <\/p>\n<p><code>--]]--<\/code><\/p>\n<p>  <\/p>\n<pre><code class=\"lua\">function main()     local tcp_dns_socket = socket.tcp() -- \u043f\u043e\u0434\u0433\u043e\u0442\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c TCP \u0441\u043e\u043a\u0435\u0442     local udp_dns_socket = socket.udp() -- \u043f\u043e\u0434\u0433\u043e\u0442\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c UDP \u0441\u043e\u043a\u0435\u0442      local tcp_connected, err = tcp_dns_socket:connect(\"127.0.0.1\", 53) -- \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0435\u043c\u0441\u044f \u0441 TCP \u0442\u0443\u043d\u0435\u043b\u0435\u043c     assert(tcp_connected, err) -- \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0447\u0442\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u043b\u0438\u0441\u044c     print(\"tcp dns connected\") -- \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u043c \u0447\u0442\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u043b\u0438\u0441\u044c \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c      local udp_open, err = udp_dns_socket:setsockname(\"127.0.0.1\", 53) -- \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c UDP \u043f\u043e\u0440\u0442     assert(udp_open, err)      -- \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0447\u0442\u043e \u043e\u0442\u043a\u0440\u044b\u043b\u0438     print(\"udp dns port open\") -- \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u043c \u0447\u0442\u043e UDP \u043f\u043e\u0440\u0442 \u043e\u0442\u043a\u0440\u044b\u0442      -- \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0442\u0435\u043c \u0447\u0442\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u044b Lua \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043a\u043b\u044e\u0447 \u0447\u0442\u043e \u0443\u0433\u043e\u0434\u043d\u043e \u043a\u0440\u043e\u043c\u0435 nil     -- \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043a\u0430\u043a \u043a\u043b\u044e\u0447 \u0441\u043e\u043a\u0435\u0442 \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u043d\u0451\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443     local coroutines = {         [tcp_dns_socket] = coroutine.create(tcp_to_udp_coroutine_function), -- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 TCP to UDP         [udp_dns_socket] = coroutine.create(udp_to_tcp_coroutine_function)  -- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 UDP to TCP     }      local clients = {} -- \u0437\u0434\u0435\u0441\u044c \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u0435\u043b\u0438 \u043f\u0430\u043a\u0435\u0442\u043e\u0432      -- \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u0441\u043e\u043a\u0435\u0442\u044b \u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u0435\u043b\u0435\u0439     coroutine.resume(coroutines[tcp_dns_socket], tcp_dns_socket, udp_dns_socket, clients)      coroutine.resume(coroutines[udp_dns_socket], udp_dns_socket, tcp_dns_socket, clients)      -- \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 socket.select \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0441\u043e\u043a\u0435\u0442 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043a \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445     local socket_list = {tcp_dns_socket, udp_dns_socket}       repeat -- \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u0446\u0438\u043a\u043b         -- socket.select \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442 \u0438\u0437 socket_list \u0441\u043e\u043a\u0435\u0442\u044b \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0435\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0432 \u0431\u0443\u0444\u0435\u0440\u0435         -- \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u043e\u0432\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0441 \u043d\u0438\u043c\u0438. \u0426\u0438\u043a\u043b for \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043d\u043e\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b           for _, in_socket in ipairs(socket.select(socket_list)) do             -- \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0430\u0441\u0441\u043e\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u0441 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u043c \u0441\u043e\u043a\u0435\u0442\u043e\u043c \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443             local ok, err = coroutine.resume(coroutines[in_socket])             if not ok then                 -- \u0435\u0441\u043b\u0438 \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0430\u0441\u044c \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439 \u0442\u043e                 print(err) -- \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443                 return     -- \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u043c \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u0446\u0438\u043a\u043b             end         end     until false end<\/code><\/pre>\n<p>  <\/p>\n<p><code>--[[--<\/code><\/p>\n<p>  <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e. \u0415\u0441\u043b\u0438 \u0432\u0434\u0440\u0443\u0433 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043a\u0440\u044b\u0442\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043c\u044b \u0447\u0435\u0440\u0435\u0437 \u0441\u0435\u043a\u0443\u043d\u0434\u0443 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c \u0435\u0433\u043e \u0437\u0430\u043d\u043e\u0432\u043e \u0432\u044b\u0437\u0432\u0430\u0432 main.<\/p>\n<p>  <\/p>\n<p><code>--]]--<\/code><\/p>\n<p>  <\/p>\n<pre><code>repeat     coroutine.resume(coroutine.create(main)) -- \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c main     socket.sleep(1) -- \u043f\u0435\u0440\u0435\u0434 \u0440\u0435\u0441\u0442\u0430\u0440\u0442\u043e\u043c \u0436\u0434\u0451\u043c \u043e\u0434\u043d\u0443 \u0441\u0435\u043a\u0443\u043d\u0434\u0443 until false<\/code><\/pre>\n<p>  <\/p>\n<h3 id=\"proveryaem\">\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c<\/h3>\n<p>  <\/p>\n<ol>\n<li>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c stunnel<\/p>\n<p>  <\/li>\n<li>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043d\u0430\u0448 \u0441\u043a\u0440\u0438\u043f\u0442<\/p>\n<p>  <\/p>\n<pre><code>lua5.3 simple-udp-to-tcp-dns-proxy.lua<\/code><\/pre>\n<p>  <\/li>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439<\/p>\n<p>  <\/p>\n<pre><code>nslookup ya.ru 127.0.0.1<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0431\u0435\u0437 &#8216;-vc&#8217; \u0442\u0430\u043a \u0442\u0430\u043a \u043c\u044b \u0443\u0436\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b\u0438 \u043f\u0440\u043e\u043a\u0441\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442 UDP DNS \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 TCP \u0442\u0443\u043d\u0435\u043b\u044c.<\/p>\n<p>  <\/li>\n<\/ol>\n<p>  <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p>  <\/p>\n<pre><code>*** Can't find server name for address 127.0.0.1: Non-existent domain Server:  UnKnown Address:  127.0.0.1  Non-authoritative answer: Name:    ya.ru Address:  (\u0437\u0434\u0435\u0441\u044c IP \u044f\u043d\u0434\u0435\u043a\u0441\u0430)<\/code><\/pre>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0441\u0451 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0432 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445 \u0441\u043e\u0435\u0434\u0438\u0434\u0435\u043d\u0438\u044f \u043a\u0430\u043a DNS \u0441\u0435\u0440\u0432\u0435\u0440 &#171;127.0.0.1&#187;<\/p>\n<p>  <\/p>\n<h3 id=\"zaklyuchenie\">\u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0448\u0438 DNS \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u043e\u0434 \u0437\u0430\u0448\u0438\u0442\u043e\u0439 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/TLS\">TLS<\/a>.<\/p>\n<p>  <\/p>\n<h3 id=\"ssylki\">\u0441\u0441\u044b\u043b\u043a\u0438<\/h3>\n<p>  <\/p>\n<ol>\n<li><a href=\"https:\/\/www.ietf.org\/rfc\/rfc1035\">RFC1035: DOMAIN NAMES \u2014 IMPLEMENTATION AND SPECIFICATION<\/a><\/li>\n<li><a href=\"https:\/\/ru.wikipedia.org\/wiki\/DNS_%D0%BF%D0%BE%D0%B2%D0%B5%D1%80%D1%85_TLS\">DNS \u043f\u043e\u0432\u0435\u0440\u0445 TLS<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/ivan386\/lua-simple-udp-to-tcp-dns-proxy\/blob\/master\/simple-udp-to-tcp-dns-proxy.lua\">simple-udp-to-tcp-dns-proxy.lua<\/a><\/li>\n<\/ol>\n<\/div>\n<p>        <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d()}}}t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>     <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/post\/427957\/\"> https:\/\/habr.com\/post\/427957\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<p><a href=\"https:\/\/habr.com\/post\/427957\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/lx\/tv\/k-\/lxtvk-ug3ul_vknfupcpp-uw0ge.jpeg\"><\/a><br \/>  <em><a href=\"https:\/\/commons.wikimedia.org\/wiki\/File:%D0%94%D0%9E%D0%A2_%E2%84%96401_(%D0%BD%D0%B0%D0%B4%D0%B7%D0%B5%D0%BC%D0%BD%D0%B0_%D1%87%D0%B0%D1%81%D1%82%D0%B8%D0%BD%D0%B0).jpg\">\u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f<\/a><br \/>  <\/em><br \/>  \u041f\u043e\u0441\u043b\u0435 \u043d\u043e\u0432\u043e\u0441\u0442\u0438 \u043e \u0442\u043e\u043c \u0447\u0442\u043e <a href=\"https:\/\/habr.com\/post\/427639\/\">&#171;Google Public DNS \u0442\u0438\u0445\u043e \u0432\u043a\u043b\u044e\u0447\u0438\u043b\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 DNS over TLS&#187;<\/a> \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e. \u0423 \u043c\u0435\u043d\u044f \u0443\u0436\u0435 \u0435\u0441\u0442\u044c <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Stunnel\">Stunnel<\/a> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Transmission_Control_Protocol\">TCP<\/a> \u0442\u0443\u043d\u043d\u0435\u043b\u044c \u0434\u043e \u0433\u0443\u0433\u043b\u0430. \u041d\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u0431\u0449\u0430\u044e\u0442\u0441\u044f \u0441 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/DNS\">DNS<\/a> \u043f\u043e <a href=\"https:\/\/ru.wikipedia.org\/wiki\/UDP\">UDP<\/a> \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u043f\u0440\u043e\u043a\u0441\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u0430\u0442\u044c UDP \u043f\u0430\u043a\u0435\u0442\u044b \u0432 TCP \u043f\u043e\u0442\u043e\u043a \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e. \u041c\u044b \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0435\u0433\u043e \u043d\u0430 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Lua\">Lua<\/a>.<\/p>\n<p>  <\/p>\n<p>\u0412\u0441\u044f \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u043c\u0435\u0436\u0434\u0443 TCP \u0438 UDP DNS \u043f\u0430\u043a\u0435\u0442\u0430\u043c\u0438:<\/p>\n<p>  <\/p>\n<blockquote><p>4.2.2. TCP usage<br \/>  Messages sent over TCP connections use server port 53 (decimal). The message is prefixed with a two byte length field which gives the message length, excluding the two byte length field. This length field allows the low-level processing to assemble a complete message before beginning to parse it.<\/p><\/blockquote>\n<p><a href=\"https:\/\/www.ietf.org\/rfc\/rfc1035\">RFC1035: DOMAIN NAMES \u2014 IMPLEMENTATION AND SPECIFICATION<\/a><\/p>\n<p>  <\/p>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c \u0434\u0435\u043b\u0430\u0435\u043c \u0442\u0443\u0434\u0430:<\/p>\n<p>  <\/p>\n<ol>\n<li>\u0431\u0435\u0440\u0451\u043c \u043f\u0430\u043a\u0435\u0442 \u0438\u0437 UDP <\/li>\n<li>\u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043a \u043d\u0435\u043c\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043f\u0430\u0440\u0443 \u0431\u0430\u0439\u0442 \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0443\u043a\u0430\u0437\u0430\u043d \u0440\u0430\u0437\u043c\u0435\u0440 \u044d\u0442\u043e\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u0430<\/li>\n<li>\u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0432 TCP \u043a\u0430\u043d\u0430\u043b<\/li>\n<\/ol>\n<p>  <\/p>\n<p>\u0418 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443:<\/p>\n<p>  <\/p>\n<ol>\n<li>\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0437 TCP \u043f\u0430\u0440\u0443 \u0431\u0430\u0439\u0442 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u0430\u043a\u0435\u0442\u0430<\/li>\n<li>\u0447\u0438\u0442\u0430\u0435\u043c \u043f\u0430\u043a\u0435\u0442 \u0438\u0437 TCP <\/li>\n<li>\u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u0435\u043b\u044e \u043f\u043e UDP<\/li>\n<\/ol>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-291466","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/291466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=291466"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/291466\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=291466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=291466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=291466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}