mirror of
https://github.com/m13253/dns-over-https.git
synced 2026-03-30 14:15:40 +00:00
Try to use context.WithTimeout to detect HTTP timeout. Hopefully it might work.
This commit is contained in:
@@ -172,7 +172,6 @@ func (c *Client) newHTTPClient() error {
|
||||
MaxIdleConns: 100,
|
||||
MaxIdleConnsPerHost: 10,
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
ResponseHeaderTimeout: time.Duration(c.conf.Timeout) * time.Second,
|
||||
TLSHandshakeTimeout: time.Duration(c.conf.Timeout) * time.Second,
|
||||
}
|
||||
if c.conf.NoIPv6 {
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
@@ -33,6 +34,7 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/m13253/dns-over-https/json-dns"
|
||||
"github.com/miekg/dns"
|
||||
@@ -87,6 +89,9 @@ func (c *Client) generateRequestGoogle(w dns.ResponseWriter, r *dns.Msg, isTCP b
|
||||
}
|
||||
req.Header.Set("Accept", "application/json, application/dns-message, application/dns-udpwireformat")
|
||||
req.Header.Set("User-Agent", USER_AGENT)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.conf.Timeout)*time.Second)
|
||||
defer cancel()
|
||||
req = req.WithContext(ctx)
|
||||
c.httpClientMux.RLock()
|
||||
resp, err := c.httpClient.Do(req)
|
||||
c.httpClientMux.RUnlock()
|
||||
@@ -95,10 +100,6 @@ func (c *Client) generateRequestGoogle(w dns.ResponseWriter, r *dns.Msg, isTCP b
|
||||
reply := jsonDNS.PrepareReply(r)
|
||||
reply.Rcode = dns.RcodeServerFailure
|
||||
w.WriteMsg(reply)
|
||||
err1 := c.newHTTPClient()
|
||||
if err1 != nil {
|
||||
log.Fatalln(err1)
|
||||
}
|
||||
return &DNSRequest{
|
||||
err: err,
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
@@ -126,6 +127,9 @@ func (c *Client) generateRequestIETF(w dns.ResponseWriter, r *dns.Msg, isTCP boo
|
||||
}
|
||||
req.Header.Set("Accept", "application/dns-message, application/dns-udpwireformat, application/json")
|
||||
req.Header.Set("User-Agent", USER_AGENT)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.conf.Timeout)*time.Second)
|
||||
defer cancel()
|
||||
req = req.WithContext(ctx)
|
||||
c.httpClientMux.RLock()
|
||||
resp, err := c.httpClient.Do(req)
|
||||
c.httpClientMux.RUnlock()
|
||||
@@ -134,10 +138,6 @@ func (c *Client) generateRequestIETF(w dns.ResponseWriter, r *dns.Msg, isTCP boo
|
||||
reply := jsonDNS.PrepareReply(r)
|
||||
reply.Rcode = dns.RcodeServerFailure
|
||||
w.WriteMsg(reply)
|
||||
err1 := c.newHTTPClient()
|
||||
if err1 != nil {
|
||||
log.Fatalln(err1)
|
||||
}
|
||||
return &DNSRequest{
|
||||
err: err,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user