diff --git a/apps/dns_proxy/src/dns_handler.erl b/apps/dns_proxy/src/dns_handler.erl index fd63def..f82a4f9 100644 --- a/apps/dns_proxy/src/dns_handler.erl +++ b/apps/dns_proxy/src/dns_handler.erl @@ -167,12 +167,9 @@ build_response(Query, #dns_cache{expire_at = ExpireAt, answers = Answers, author Now = os:system_time(second), RemainingTTL = ExpireAt - Now, - Adjust = fun(RR) -> RR#dns_rr{ttl = max(0, RemainingTTL)} end, - Answers2 = [Adjust(RR) || RR <- Answers], - Authority2 = [Adjust(RR) || RR <- Authority], - - Additional0 = [Adjust(RR) || RR <- Additional], - Additional2 = add_opt_if_needed(Query, Additional0), + Answers2 = [adjust_ttl(RR, RemainingTTL) || RR <- Answers], + Authority2 = [adjust_ttl(RR, RemainingTTL) || RR <- Authority], + Additional2 = [adjust_ttl(RR, RemainingTTL) || RR <- Additional], Query#dns_message{ qr = true, @@ -182,20 +179,7 @@ build_response(Query, #dns_cache{expire_at = ExpireAt, answers = Answers, author additional = Additional2 }. -add_opt_if_needed(Query, Additional) -> - case dns_opt:find(Query#dns_message.additional) of - false -> - %% 客户端未使用 EDNS → 不在响应中加 OPT - Additional; - {ok, OptReq} -> - %% 客户端使用 EDNS → 构造自身的 OPT RR - UdpSize = dns_opt:udp_payload(OptReq), - DoBit = dns_opt:do_bit(OptReq), - - OptResp = dns_opt:make(UdpSize, DoBit), - - %% 去除上游响应里的旧 OPT(如果有) - Additional2 = [RR || RR <- Additional, RR#dns_rr.type =/= opt], - %% 新的 OPT 一定放在 Additional 最后 - Additional2 ++ [OptResp] - end. \ No newline at end of file +adjust_ttl(RR = #dns_rr{}, RemainingTTL) -> + RR#dns_rr{ttl = max(0, RemainingTTL)}; +adjust_ttl(RR, _RemainingTTL) -> + RR. \ No newline at end of file diff --git a/apps/dns_proxy/src/dns_opt.erl b/apps/dns_proxy/src/dns_opt.erl deleted file mode 100644 index 725fb8a..0000000 --- a/apps/dns_proxy/src/dns_opt.erl +++ /dev/null @@ -1,46 +0,0 @@ -%%-------------------------------------------------------------------- -%% EDNS (OPT RR) Utility for dns_erlang -%%-------------------------------------------------------------------- --module(dns_opt). - --export([find/1, make/2, udp_payload/1, do_bit/1]). - --include_lib("dns_erlang/include/dns.hrl"). - -%%-------------------------------------------------------------------- -%% 查找 OPT RR(RR type = opt) -%%-------------------------------------------------------------------- -find(RRs) -> - case lists:dropwhile(fun(RR) -> RR#dns_rr.type =/= opt end, RRs) of - [] -> - false; - [RR|_] -> - {ok, RR} - end. - -%%-------------------------------------------------------------------- -%% 获取 DO bit(TTL 的最高 bit) -%%-------------------------------------------------------------------- -do_bit(RR) -> - (RR#dns_rr.ttl band 16#8000) =/= 0. - -%%-------------------------------------------------------------------- -%% 获取 UDP payload size(来自 class 字段) -%%-------------------------------------------------------------------- -udp_payload(RR) -> - RR#dns_rr.class. - -%%-------------------------------------------------------------------- -%% 构造新的 OPT RR(符合 RFC6891) -%% make(UdpPayloadSize, DoBit) -%%-------------------------------------------------------------------- -make(UdpPayloadSize, DoBit) -> - TTL = if DoBit -> 16#8000; true -> 0 end, - #dns_rr{ - name = <<>>, % Root label - type = opt, - class = UdpPayloadSize, - ttl = TTL, - % No EDNS options by default - data = [] - }.