@@ -2,7 +2,6 @@ package transport
22
33import (
44 "context"
5- "time"
65
76 "github.com/sagernet/sing-box/adapter"
87 "github.com/sagernet/sing-box/common/dialer"
@@ -12,6 +11,7 @@ import (
1211 "github.com/sagernet/sing-box/log"
1312 "github.com/sagernet/sing-box/option"
1413 "github.com/sagernet/sing/common"
14+ "github.com/sagernet/sing/common/bufio/deadline"
1515 E "github.com/sagernet/sing/common/exceptions"
1616 "github.com/sagernet/sing/common/logger"
1717 M "github.com/sagernet/sing/common/metadata"
@@ -38,7 +38,8 @@ type TLSTransport struct {
3838
3939type tlsDNSConn struct {
4040 tls.Conn
41- queryId uint16
41+ queryId uint16
42+ needDeadlineClose bool
4243}
4344
4445func NewTLS (ctx context.Context , logger log.ContextLogger , tag string , options option.RemoteTLSDNSServerOptions ) (adapter.DNSTransport , error ) {
@@ -104,7 +105,10 @@ func (t *TLSTransport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.M
104105 if err != nil {
105106 return nil , E .Cause (err , "dial TLS connection" )
106107 }
107- return & tlsDNSConn {Conn : tlsConn }, nil
108+ return & tlsDNSConn {
109+ Conn : tlsConn ,
110+ needDeadlineClose : deadline .NeedAdditionalReadDeadline (tlsConn .NetConn ()),
111+ }, nil
108112 })
109113 if err != nil {
110114 return nil , err
@@ -125,9 +129,7 @@ func (t *TLSTransport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.M
125129}
126130
127131func (t * TLSTransport ) exchange (ctx context.Context , message * mDNS.Msg , conn * tlsDNSConn ) (* mDNS.Msg , error ) {
128- if deadline , ok := ctx .Deadline (); ok {
129- conn .SetDeadline (deadline )
130- }
132+ defer setConnDeadline (ctx , conn , conn .needDeadlineClose )()
131133 conn .queryId ++
132134 err := WriteMessage (conn , conn .queryId , message )
133135 if err != nil {
@@ -137,6 +139,5 @@ func (t *TLSTransport) exchange(ctx context.Context, message *mDNS.Msg, conn *tl
137139 if err != nil {
138140 return nil , E .Cause (err , "read response" )
139141 }
140- conn .SetDeadline (time.Time {})
141142 return response , nil
142143}
0 commit comments