fix: doh concurrent race issue

This commit is contained in:
wwqgtxx 2024-07-21 22:52:16 +08:00
parent 28794c62c4
commit fd5b537ab1

View File

@ -204,24 +204,24 @@ func (doh *dnsOverHTTPS) exchangeHTTPSClient(
method = http3.MethodGet0RTT method = http3.MethodGet0RTT
} }
url := doh.url requestUrl := *doh.url // don't modify origin url
url.RawQuery = fmt.Sprintf("dns=%s", base64.RawURLEncoding.EncodeToString(buf)) requestUrl.RawQuery = fmt.Sprintf("dns=%s", base64.RawURLEncoding.EncodeToString(buf))
httpReq, err := http.NewRequestWithContext(ctx, method, url.String(), nil) httpReq, err := http.NewRequestWithContext(ctx, method, requestUrl.String(), nil)
if err != nil { if err != nil {
return nil, fmt.Errorf("creating http request to %s: %w", url, err) return nil, fmt.Errorf("creating http request to %s: %w", doh.url, err)
} }
httpReq.Header.Set("Accept", "application/dns-message") httpReq.Header.Set("Accept", "application/dns-message")
httpReq.Header.Set("User-Agent", "") httpReq.Header.Set("User-Agent", "")
httpResp, err := client.Do(httpReq) httpResp, err := client.Do(httpReq)
if err != nil { if err != nil {
return nil, fmt.Errorf("requesting %s: %w", url, err) return nil, fmt.Errorf("requesting %s: %w", doh.url, err)
} }
defer httpResp.Body.Close() defer httpResp.Body.Close()
body, err := io.ReadAll(httpResp.Body) body, err := io.ReadAll(httpResp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("reading %s: %w", url, err) return nil, fmt.Errorf("reading %s: %w", doh.url, err)
} }
if httpResp.StatusCode != http.StatusOK { if httpResp.StatusCode != http.StatusOK {
@ -230,7 +230,7 @@ func (doh *dnsOverHTTPS) exchangeHTTPSClient(
"expected status %d, got %d from %s", "expected status %d, got %d from %s",
http.StatusOK, http.StatusOK,
httpResp.StatusCode, httpResp.StatusCode,
url, doh.url,
) )
} }
@ -239,7 +239,7 @@ func (doh *dnsOverHTTPS) exchangeHTTPSClient(
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"unpacking response from %s: body is %s: %w", "unpacking response from %s: body is %s: %w",
url, doh.url,
body, body,
err, err,
) )