mirror of
https://github.com/m13253/dns-over-https.git
synced 2026-08-27 21:59:49 +00:00
Register a new HTTP client whenever an HTTP connection error happens
This commit is contained in:
+41
-19
@@ -31,6 +31,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"../json-dns"
|
"../json-dns"
|
||||||
@@ -39,12 +40,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
conf *config
|
conf *config
|
||||||
bootstrap []string
|
bootstrap []string
|
||||||
udpServer *dns.Server
|
udpServer *dns.Server
|
||||||
tcpServer *dns.Server
|
tcpServer *dns.Server
|
||||||
httpTransport *http.Transport
|
bootstrapResolver *net.Resolver
|
||||||
httpClient *http.Client
|
cookieJar *cookiejar.Jar
|
||||||
|
httpClientMux *sync.RWMutex
|
||||||
|
httpTransport *http.Transport
|
||||||
|
httpClient *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
type DNSRequest struct {
|
type DNSRequest struct {
|
||||||
@@ -71,7 +75,7 @@ func NewClient(conf *config) (c *Client, err error) {
|
|||||||
Net: "tcp",
|
Net: "tcp",
|
||||||
Handler: dns.HandlerFunc(c.tcpHandlerFunc),
|
Handler: dns.HandlerFunc(c.tcpHandlerFunc),
|
||||||
}
|
}
|
||||||
bootResolver := net.DefaultResolver
|
c.bootstrapResolver = net.DefaultResolver
|
||||||
if len(conf.Bootstrap) != 0 {
|
if len(conf.Bootstrap) != 0 {
|
||||||
c.bootstrap = make([]string, len(conf.Bootstrap))
|
c.bootstrap = make([]string, len(conf.Bootstrap))
|
||||||
for i, bootstrap := range conf.Bootstrap {
|
for i, bootstrap := range conf.Bootstrap {
|
||||||
@@ -84,7 +88,7 @@ func NewClient(conf *config) (c *Client, err error) {
|
|||||||
}
|
}
|
||||||
c.bootstrap[i] = bootstrapAddr.String()
|
c.bootstrap[i] = bootstrapAddr.String()
|
||||||
}
|
}
|
||||||
bootResolver = &net.Resolver{
|
c.bootstrapResolver = &net.Resolver{
|
||||||
PreferGo: true,
|
PreferGo: true,
|
||||||
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
|
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||||
var d net.Dialer
|
var d net.Dialer
|
||||||
@@ -95,33 +99,51 @@ func NewClient(conf *config) (c *Client, err error) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.httpTransport = new(http.Transport)
|
// Most CDNs require Cookie support to prevent DDoS attack.
|
||||||
|
// Disabling Cookie does not effectively prevent tracking,
|
||||||
|
// so I will leave it on to make anti-DDoS services happy.
|
||||||
|
c.cookieJar, err = cookiejar.New(nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
c.httpClientMux = new(sync.RWMutex)
|
||||||
|
err = c.newHTTPClient()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) newHTTPClient() error {
|
||||||
|
c.httpClientMux.Lock()
|
||||||
|
defer c.httpClientMux.Unlock()
|
||||||
|
if c.httpTransport != nil {
|
||||||
|
c.httpTransport.CloseIdleConnections()
|
||||||
|
}
|
||||||
c.httpTransport = &http.Transport{
|
c.httpTransport = &http.Transport{
|
||||||
DialContext: (&net.Dialer{
|
DialContext: (&net.Dialer{
|
||||||
Timeout: time.Duration(conf.Timeout) * time.Second,
|
Timeout: time.Duration(c.conf.Timeout) * time.Second,
|
||||||
KeepAlive: 30 * time.Second,
|
KeepAlive: 30 * time.Second,
|
||||||
DualStack: true,
|
DualStack: true,
|
||||||
Resolver: bootResolver,
|
Resolver: c.bootstrapResolver,
|
||||||
}).DialContext,
|
}).DialContext,
|
||||||
ExpectContinueTimeout: 1 * time.Second,
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
IdleConnTimeout: 90 * time.Second,
|
IdleConnTimeout: 90 * time.Second,
|
||||||
MaxIdleConns: 100,
|
MaxIdleConns: 100,
|
||||||
MaxIdleConnsPerHost: 10,
|
MaxIdleConnsPerHost: 10,
|
||||||
Proxy: http.ProxyFromEnvironment,
|
Proxy: http.ProxyFromEnvironment,
|
||||||
ResponseHeaderTimeout: time.Duration(conf.Timeout) * time.Second,
|
ResponseHeaderTimeout: time.Duration(c.conf.Timeout) * time.Second,
|
||||||
TLSHandshakeTimeout: time.Duration(conf.Timeout) * time.Second,
|
TLSHandshakeTimeout: time.Duration(c.conf.Timeout) * time.Second,
|
||||||
}
|
}
|
||||||
http2.ConfigureTransport(c.httpTransport)
|
err := http2.ConfigureTransport(c.httpTransport)
|
||||||
// Most CDNs require Cookie support to prevent DDoS attack
|
|
||||||
cookieJar, err := cookiejar.New(nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
c.httpClient = &http.Client{
|
c.httpClient = &http.Client{
|
||||||
Transport: c.httpTransport,
|
Transport: c.httpTransport,
|
||||||
Jar: cookieJar,
|
Jar: c.cookieJar,
|
||||||
}
|
}
|
||||||
return c, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Start() error {
|
func (c *Client) Start() error {
|
||||||
|
|||||||
@@ -93,12 +93,17 @@ func (c *Client) generateRequestGoogle(w dns.ResponseWriter, r *dns.Msg, isTCP b
|
|||||||
}
|
}
|
||||||
req.Header.Set("Accept", "application/json, application/dns-udpwireformat")
|
req.Header.Set("Accept", "application/json, application/dns-udpwireformat")
|
||||||
req.Header.Set("User-Agent", "DNS-over-HTTPS/1.1 (+https://github.com/m13253/dns-over-https)")
|
req.Header.Set("User-Agent", "DNS-over-HTTPS/1.1 (+https://github.com/m13253/dns-over-https)")
|
||||||
|
c.httpClientMux.RLock()
|
||||||
resp, err := c.httpClient.Do(req)
|
resp, err := c.httpClient.Do(req)
|
||||||
|
c.httpClientMux.RUnlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
reply.Rcode = dns.RcodeServerFailure
|
reply.Rcode = dns.RcodeServerFailure
|
||||||
w.WriteMsg(reply)
|
w.WriteMsg(reply)
|
||||||
c.httpTransport.CloseIdleConnections()
|
err1 := c.newHTTPClient()
|
||||||
|
if err1 != nil {
|
||||||
|
log.Fatalln(err1)
|
||||||
|
}
|
||||||
return &DNSRequest{
|
return &DNSRequest{
|
||||||
err: err,
|
err: err,
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-1
@@ -154,12 +154,17 @@ func (c *Client) generateRequestIETF(w dns.ResponseWriter, r *dns.Msg, isTCP boo
|
|||||||
}
|
}
|
||||||
req.Header.Set("Accept", "application/dns-udpwireformat, application/json")
|
req.Header.Set("Accept", "application/dns-udpwireformat, application/json")
|
||||||
req.Header.Set("User-Agent", "DNS-over-HTTPS/1.1 (+https://github.com/m13253/dns-over-https)")
|
req.Header.Set("User-Agent", "DNS-over-HTTPS/1.1 (+https://github.com/m13253/dns-over-https)")
|
||||||
|
c.httpClientMux.RLock()
|
||||||
resp, err := c.httpClient.Do(req)
|
resp, err := c.httpClient.Do(req)
|
||||||
|
c.httpClientMux.RUnlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
reply.Rcode = dns.RcodeServerFailure
|
reply.Rcode = dns.RcodeServerFailure
|
||||||
w.WriteMsg(reply)
|
w.WriteMsg(reply)
|
||||||
c.httpTransport.CloseIdleConnections()
|
err1 := c.newHTTPClient()
|
||||||
|
if err1 != nil {
|
||||||
|
log.Fatalln(err1)
|
||||||
|
}
|
||||||
return &DNSRequest{
|
return &DNSRequest{
|
||||||
err: err,
|
err: err,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user