diff --git a/dns/dns.go b/dns/dns.go index dc497a4..03a75f4 100644 --- a/dns/dns.go +++ b/dns/dns.go @@ -207,6 +207,16 @@ func (ds *DNSList) DialContext(ctx context.Context, dialer *net.Dialer, firstFra MinVersion: tls.VersionTLS12, NextProtos: []string{"dns"}, }) + // re-init ctx due to deadline settings in tcp dial + if dialer.Timeout != 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(context.Background(), dialer.Timeout) + defer cancel() + } else if !dialer.Deadline.IsZero() { + var cancel context.CancelFunc + ctx, cancel = context.WithDeadline(context.Background(), dialer.Deadline) + defer cancel() + } if firstFragmentLen > 0 { logrus.Debugln("[terasu.dns] -- hs tls", host, addr, "use first frag len", firstFragmentLen) err = terasu.Use(tlsConn).HandshakeContext(ctx, firstFragmentLen) @@ -218,9 +228,10 @@ func (ds *DNSList) DialContext(ctx context.Context, dialer *net.Dialer, firstFra logrus.Debugln("[terasu.dns] <- hs tls", host, addr, "succeeded") return ErrSuccess } - logrus.Debugln("[terasu.dns] hs tls", host, addr, "err:", err) + logrus.Debugln("[terasu.dns] -- hs tls", host, addr, "err:", err) _ = tlsConn.Close() if !errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, context.Canceled) { + logrus.Debugln("[terasu.dns] == disable", host, addr) addr.disable(time.Hour) // no need to acquire write lock } } diff --git a/http/http.go b/http/http.go index 547f633..123507e 100644 --- a/http/http.go +++ b/http/http.go @@ -62,6 +62,16 @@ var DefaultClient = http.Client{ ServerName: host, MinVersion: tls.VersionTLS12, }) + // re-init ctx due to deadline settings in tcp dial + if defaultDialer.Timeout != 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(context.Background(), defaultDialer.Timeout) + defer cancel() + } else if !defaultDialer.Deadline.IsZero() { + var cancel context.CancelFunc + ctx, cancel = context.WithDeadline(context.Background(), defaultDialer.Deadline) + defer cancel() + } if terasu.DefaultFirstFragmentLen > 0 { err = terasu.Use(tlsConn).HandshakeContext(ctx, terasu.DefaultFirstFragmentLen) } else { diff --git a/http2/http2.go b/http2/http2.go index 1d4b7e0..ec3a155 100644 --- a/http2/http2.go +++ b/http2/http2.go @@ -59,6 +59,16 @@ var DefaultClient = http.Client{ continue } tlsConn = tls.Client(conn, cfg) + // re-init ctx due to deadline settings in tcp dial + if defaultDialer.Timeout != 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(context.Background(), defaultDialer.Timeout) + defer cancel() + } else if !defaultDialer.Deadline.IsZero() { + var cancel context.CancelFunc + ctx, cancel = context.WithDeadline(context.Background(), defaultDialer.Deadline) + defer cancel() + } if terasu.DefaultFirstFragmentLen > 0 { err = terasu.Use(tlsConn).HandshakeContext(ctx, terasu.DefaultFirstFragmentLen) } else {