From 48a5909e840910a644df7aa84a09d23db8439b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Mon, 6 Oct 2025 14:02:48 +0800 Subject: [PATCH] fix(tlsconn): clear deadline after tcp dial --- dns/dns.go | 13 ++++++++++++- http/http.go | 10 ++++++++++ http2/http2.go | 10 ++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) 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 {