Skip to content

Commit

Permalink
fix: doh concurrent race issue
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Jul 21, 2024
1 parent 28794c6 commit fd5b537
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions dns/doh.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,24 +204,24 @@ func (doh *dnsOverHTTPS) exchangeHTTPSClient(
method = http3.MethodGet0RTT
}

url := doh.url
url.RawQuery = fmt.Sprintf("dns=%s", base64.RawURLEncoding.EncodeToString(buf))
httpReq, err := http.NewRequestWithContext(ctx, method, url.String(), nil)
requestUrl := *doh.url // don't modify origin url
requestUrl.RawQuery = fmt.Sprintf("dns=%s", base64.RawURLEncoding.EncodeToString(buf))
httpReq, err := http.NewRequestWithContext(ctx, method, requestUrl.String(), 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("User-Agent", "")
httpResp, err := client.Do(httpReq)
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()

body, err := io.ReadAll(httpResp.Body)
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 {
Expand All @@ -230,7 +230,7 @@ func (doh *dnsOverHTTPS) exchangeHTTPSClient(
"expected status %d, got %d from %s",
http.StatusOK,
httpResp.StatusCode,
url,
doh.url,
)
}

Expand All @@ -239,7 +239,7 @@ func (doh *dnsOverHTTPS) exchangeHTTPSClient(
if err != nil {
return nil, fmt.Errorf(
"unpacking response from %s: body is %s: %w",
url,
doh.url,
body,
err,
)
Expand Down

1 comment on commit fd5b537

@lux5am
Copy link

@lux5am lux5am commented on fd5b537 Jul 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wwqgtxx sometimes I got HTTPS response with A request. Does this commit fixed it?
I can't really test it because it's random, but at least once a day I got DNS probe issue with chrome browser.

>>kdig superuser.com @8.8.8.8 +https
;; TLS session (TLS1.3)-(ECDHE-X25519)-(RSA-PSS-RSAE-SHA256)-(AES-256-GCM)
;; HTTP session (HTTP/2-POST)-(8.8.8.8/dns-query)-(status: 200)
;; ->>HEADER<<- opcode: QUERY; status: NOERROR; id: 0
;; Flags: qr rd ra; QUERY: 1; ANSWER: 2; AUTHORITY: 0; ADDITIONAL: 1

;; EDNS PSEUDOSECTION:
;; Version: 0; flags: ; UDP size: 512 B; ext-rcode: NOERROR
;; PADDING: 390 B

;; QUESTION SECTION:
;; superuser.com.               IN      A

;; ANSWER SECTION:
superuser.com.          300     IN      A       104.18.43.79
superuser.com.          300     IN      A       172.64.144.177

;; Received 468 B
;; Time 2024-03-05 06:23:24 WITA
;; From 8.8.8.8@443(TCP) in 1073.3 ms

>>kdig superuser.com @192.168.1.1 -p 5354
;; ->>HEADER<<- opcode: QUERY; status: NOERROR; id: 22795
;; Flags: qr aa rd ra; QUERY: 1; ANSWER: 1; AUTHORITY: 0; ADDITIONAL: 0

;; QUESTION SECTION:
;; superuser.com.               IN      A

;; ANSWER SECTION:
superuser.com.          81      IN      HTTPS   1 . alpn=h2 ipv4hint=104.18.43.79,172.64.144.177

;; Received 65 B
;; Time 2024-03-05 06:23:41 WITA
;; From 192.168.1.1@7874(UDP) in 3.0 ms

Please sign in to comment.