From 54185ee6ddecec368f16188f822dd8f50fe51c1c Mon Sep 17 00:00:00 2001 From: EkkoG Date: Wed, 9 Oct 2024 15:45:18 +0000 Subject: [PATCH] fix(doh): problems with not recreating a new one when the client is not valid --- control/dns.go | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/control/dns.go b/control/dns.go index f0cc384be..b83cc73d7 100644 --- a/control/dns.go +++ b/control/dns.go @@ -79,18 +79,32 @@ type DoH struct { func (d *DoH) ForwardDNS(ctx context.Context, data []byte) (*dnsmessage.Msg, error) { if d.client == nil { - var roundTripper http.RoundTripper - if d.http3 { - roundTripper = d.getHttp3RoundTripper() - } else { - roundTripper = d.getHttpRoundTripper() + d.client = d.getClient() + } + msg, err := sendHttpDNS(d.client, d.dialArgument.bestTarget.String(), &d.Upstream, data) + if err != nil { + // If failed to send DNS request, we should try to create a new client. + d.client = d.getClient() + msg, err = sendHttpDNS(d.client, d.dialArgument.bestTarget.String(), &d.Upstream, data) + if err != nil { + return nil, err } + return msg, nil + } + return msg, nil +} - d.client = &http.Client{ - Transport: roundTripper, - } +func (d *DoH) getClient() *http.Client { + var roundTripper http.RoundTripper + if d.http3 { + roundTripper = d.getHttp3RoundTripper() + } else { + roundTripper = d.getHttpRoundTripper() + } + + return &http.Client{ + Transport: roundTripper, } - return sendHttpDNS(d.client, d.dialArgument.bestTarget.String(), &d.Upstream, data) } func (d *DoH) getHttpRoundTripper() *http.Transport {