mirror of
https://github.com/m13253/dns-over-https.git
synced 2026-08-24 15:59:47 +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,
|
MaxIdleConns: 100,
|
||||||
MaxIdleConnsPerHost: 10,
|
MaxIdleConnsPerHost: 10,
|
||||||
Proxy: http.ProxyFromEnvironment,
|
Proxy: http.ProxyFromEnvironment,
|
||||||
ResponseHeaderTimeout: time.Duration(c.conf.Timeout) * time.Second,
|
|
||||||
TLSHandshakeTimeout: time.Duration(c.conf.Timeout) * time.Second,
|
TLSHandshakeTimeout: time.Duration(c.conf.Timeout) * time.Second,
|
||||||
}
|
}
|
||||||
if c.conf.NoIPv6 {
|
if c.conf.NoIPv6 {
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@@ -33,6 +34,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/m13253/dns-over-https/json-dns"
|
"github.com/m13253/dns-over-https/json-dns"
|
||||||
"github.com/miekg/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("Accept", "application/json, application/dns-message, application/dns-udpwireformat")
|
||||||
req.Header.Set("User-Agent", USER_AGENT)
|
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()
|
c.httpClientMux.RLock()
|
||||||
resp, err := c.httpClient.Do(req)
|
resp, err := c.httpClient.Do(req)
|
||||||
c.httpClientMux.RUnlock()
|
c.httpClientMux.RUnlock()
|
||||||
@@ -95,10 +100,6 @@ func (c *Client) generateRequestGoogle(w dns.ResponseWriter, r *dns.Msg, isTCP b
|
|||||||
reply := jsonDNS.PrepareReply(r)
|
reply := jsonDNS.PrepareReply(r)
|
||||||
reply.Rcode = dns.RcodeServerFailure
|
reply.Rcode = dns.RcodeServerFailure
|
||||||
w.WriteMsg(reply)
|
w.WriteMsg(reply)
|
||||||
err1 := c.newHTTPClient()
|
|
||||||
if err1 != nil {
|
|
||||||
log.Fatalln(err1)
|
|
||||||
}
|
|
||||||
return &DNSRequest{
|
return &DNSRequest{
|
||||||
err: err,
|
err: err,
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-4
@@ -25,6 +25,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"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("Accept", "application/dns-message, application/dns-udpwireformat, application/json")
|
||||||
req.Header.Set("User-Agent", USER_AGENT)
|
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()
|
c.httpClientMux.RLock()
|
||||||
resp, err := c.httpClient.Do(req)
|
resp, err := c.httpClient.Do(req)
|
||||||
c.httpClientMux.RUnlock()
|
c.httpClientMux.RUnlock()
|
||||||
@@ -134,10 +138,6 @@ func (c *Client) generateRequestIETF(w dns.ResponseWriter, r *dns.Msg, isTCP boo
|
|||||||
reply := jsonDNS.PrepareReply(r)
|
reply := jsonDNS.PrepareReply(r)
|
||||||
reply.Rcode = dns.RcodeServerFailure
|
reply.Rcode = dns.RcodeServerFailure
|
||||||
w.WriteMsg(reply)
|
w.WriteMsg(reply)
|
||||||
err1 := c.newHTTPClient()
|
|
||||||
if err1 != nil {
|
|
||||||
log.Fatalln(err1)
|
|
||||||
}
|
|
||||||
return &DNSRequest{
|
return &DNSRequest{
|
||||||
err: err,
|
err: err,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user